2025年5月30日 星期五

FreeBSD伺服器會莫名斷線的解決方法

原因

IP (163.26.79.1) 在 gateway/NAT router 中的狀態「卡住」或 session 失效

  • 外網無法連進,區網正常 → 顯示封包無法從 gateway 對你主機做 port forwarding 或送達。

  • 重開機會讓 gateway 重新認識你的主機(重新發送 ARP 請求、重建 NAT 表、MAC/IP 對應表),所以就恢復正常。

解決辦法

建立一個腳本 /root/netfix.sh

#!/bin/sh

logger "[netfix] 啟動網路堆疊重建..."

service netif stop

service routing stop

sleep 2

service netif start

service routing start

logger "[netfix] 完成"

儲存後設定執行權限:

chmod +x /root/netfix.sh

執行:
sh /root/netfix.sh

設定排程自動重啟網路

crontab -e

加上一行,每天凌晨 2 點自動修復:

0 2 * * * /root/netfix.sh 

查看記錄檔
grep netfix /var/log/cron

設定自動通知


Step 1. 安裝 msmtp 

pkg install msmtp

Step 2. 申請 Gmail 「應用程式密碼」

https://myaccount.google.com/apppasswords

Step 3. 建立 msmtp 設定檔

# /usr/local/etc/msmtprc
defaults
auth           on
tls            on
tls_trust_file /usr/local/etc/ssl/cert.pem
logfile        /var/log/msmtp.log

account        gmail
host           smtp.gmail.com
port           587
from           your_email@gmail.com
user           your_email@gmail.com
password       your_app_password

account default : gmail

Step 4. 設定權限保護

chmod 600 /usr/local/etc/msmtprc

Step 5. 測試寄信
echo "測試內容" | msmtp -a gmail your_target_email@example.com






2025年5月21日 星期三

架設Scratch-gui伺服器

 連結
https://github.com/LLK/scratch-gui.git

1. 下載 scratch-gui-develop.zip 並放到適合的資料夾

2. 解壓縮 unzip scratch-gui-develop.zip

3. 開啟資料夾 

npm install

npm start

成功的話你就可以打開瀏覽器訪問:

👉 http://localhost:8601


※ 必須安裝node.js

2025年5月16日 星期五

用 nuitka 將 python 程式打包成可執行檔

指令

python -m nuitka link.py --standalone --enable-plugin=tk-inter --output-dir=output --windows-disable-console --windows-icon=your_icon.ico


2025年5月1日 星期四

自架node.js伺服器

#到自己的遊戲資料夾,安裝依存
npm init -y
npm install express socket.io 

#安裝必要元件
npm install mysql2


建立 rc.d 啟動腳本

1. 建立 rc.d 腳本

ee /usr/local/etc/rc.d/node_game

貼上以下資料

# PROVIDE: node_game
# REQUIRE: LOGIN
# KEYWORD: shutdown

. /etc/rc.subr

name="node_game"
rcvar=node_game_enable
pidfile="/var/run/${name}.pid"
logfile="/var/log/${name}.log"
command="/usr/sbin/daemon"
command_args="-f -P ${pidfile} -o ${logfile} /usr/local/bin/node /home/chocho/game/server.js"

load_rc_config $name
run_rc_command "$1"

2. 設為可執行檔

chmod +x /usr/local/etc/rc.d/node_game

3. 啟用自動啟動

sysrc node_game_enable=YES

4. 手動啟動測試

service node_game start

可以用 tail -f /var/log/node_game.log 來確認 log 是否正確輸出。

可以用 ps aux | grep node 檢查 node有沒有在運行

5.設定 node_game.log 的備份大小

ee /etc/newsyslog.conf

在最後一行加上這段:

/var/log/node_game.log  root:wheel  644  7  1000  *  J

欄位說明
/var/log/node_game.log要輪替的 log 檔案
root:wheellog 檔的擁有者與群組(根據實際擁有者可調整)
644log 檔的權限
7最多保留 7 份舊檔案(log.0.gz ~ log.6.gz)
1000超過 1000KB(1MB)時輪替
*每天都可以檢查,不限制特定時間
J壓縮輪替的 log(變成 .gz

測試輪替

newsyslog -nv

強制輪替

newsyslog -vF



讓 Apache 反向代理 WebSocket 或遊戲伺服器請求

ee /usr/local/etc/apache24/httpd.conf

加入

    # --- 開始新增反向代理設定 ---

    # Proxy for HTTP requests to game server
    ProxyPass "/game/" "http://localhost:3000/"
    ProxyPassReverse "/game/" "http://localhost:3000/"

    # Proxy for WebSocket connections (Socket.IO)
    ProxyPass "/socket.io/" "http://localhost:3000/socket.io/"
    ProxyPassReverse "/socket.io/" "http://localhost:3000/socket.io/"

並將以下三列取消註解

LoadModule proxy_module libexec/apache24/mod_proxy.so
LoadModule proxy_http_module libexec/apache24/mod_proxy_http.so
LoadModule proxy_wstunnel_module libexec/apache24/mod_proxy_wstunnel.so

重新啟動

service apache24 restart

可以達成目標

只有使用者瀏覽 http://yourdomain.com/game/ 時,請求才會被轉發到你的遊戲伺服器(例如 Node.js)。

其他請求(例如首頁 http://yourdomain.com/ 或 /about.html)仍由 Apache 原本處理的靜態網頁提供。



2025年4月25日 星期五

FreeBSD 14.1架站 - 設定安全性憑證 SSL紀錄

預計使用 Let's Encrypt 免費憑證

1.先搜尋可安裝的軟體

# pkg search certbot

py311-certbot-3.1.0,1          Let's Encrypt client
py311-certbot-apache-3.1.0     Apache plugin for Certbot
py311-certbot-dns-cloudflare-3.1.0 Cloudflare DNS plugin for Certbot
py311-certbot-dns-cpanel-0.4.0_1 CPanel DNS Authenticator plugin for Certbot
py311-certbot-dns-digitalocean-3.1.0 DigitalOcean DNS Authenticator plugin for Certbot
py311-certbot-dns-dnsimple-3.1.0 DNSimple DNS Authenticator plugin for Certbot
py311-certbot-dns-dnsmadeeasy-3.1.0 DNS Made Easy DNS Authenticator plugin for Certbot
py311-certbot-dns-gandi-1.5.0_1 Gandi LiveDNS plugin for Certbot
py311-certbot-dns-gehirn-3.1.0 Gehirn Infrastructure Service DNS Authenticator plugin for Certbot
py311-certbot-dns-google-3.1.0 Google Cloud DNS Authenticator plugin for Certbot
py311-certbot-dns-linode-3.1.0 Linode DNS Authenticator plugin for Certbot
py311-certbot-dns-luadns-3.1.0 LuaDNS Authenticator plugin for Certbot
py311-certbot-dns-nsone-3.1.0  NS1 DNS Authenticator plugin for Certbot
py311-certbot-dns-ovh-3.1.0    OVH DNS Authenticator plugin for Certbot
py311-certbot-dns-powerdns-0.2.1_2 PowerDNS DNS Authenticator plugin for Certbot
py311-certbot-dns-rfc2136-3.1.0 RFC 2136 DNS Authenticator plugin for Certbot
py311-certbot-dns-route53-3.1.0 Route53 DNS Authenticator plugin for Certbot
py311-certbot-dns-sakuracloud-3.1.0 Sakura Cloud DNS Authenticator plugin for Certbot
py311-certbot-dns-standalone-1.1_1 Standalone DNS Authenticator plugin for Certbot
py311-certbot-nginx-3.1.0      NGINX plugin for Certbot

2.安裝必要軟體
pkg install py311-certbot py311-certbot-apache

3.搜尋虛擬主機設定
# grep -r "VirtualHost \*:80" /usr/local/etc/apache24/

/usr/local/etc/apache24/extra/httpd-vhosts.conf.sample:<VirtualHost *:80>
/usr/local/etc/apache24/extra/httpd-vhosts.conf.sample:<VirtualHost *:80>
/usr/local/etc/apache24/extra/httpd-vhosts.conf:<VirtualHost *:80>
/usr/local/etc/apache24/extra/httpd-vhosts.conf:<VirtualHost *:80>

4. 修改虛擬主機設定
# ee /usr/local/etc/apache24/extra/httpd-vhosts.conf

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/www/web/"
    ServerName gen-slps.slps.tn.edu.tw
    ServerAlias gen-slps.slps.tn.edu.tw
    ErrorLog "/usr/local/www/apache24/logs/dummy-host.example.com-error_log"
    CustomLog "/usr/local/www/apache24/logs/dummy-host.example.com-access_log" common
</VirtualHost>

5. 修改 httpd.conf
# ee /usr/local/etc/apache24/httpd.conf
1. Include /usr/local/etc/apache24/extra/httpd-vhosts.conf
2. #LoadModule rewrite_module libexec/apache24/mod_rewrite.so
3. #LoadModule ssl_module libexec/apache24/mod_ssl.so

6. 安裝並設定憑證,安裝完重開 apache
certbot --apache -d gen-slps.slps.tn.edu.tw
service apache24 restart

7. 增加虛擬主機設定,強制輸入ip的使用者進入網域
<VirtualHost *:80>
    ServerName 163.26.79.1
    Redirect permanent / https://gen-slps.slps.tn.edu.tw/
</VirtualHost>

後記:

※ 憑證位置 /usr/local/etc/letsencrypt/live/gen-slps.slps.tn.edu.tw/cert.pem

※ 查詢憑證到期日 
openssl x509 -in /usr/local/etc/letsencrypt/live/gen-slps.slps.tn.edu.tw/cert.pem -noout -dates

2025年2月7日 星期五

VScode安裝及中文化

1. 安裝VSCodeSetup-x64-1.52.1.exe

2. 安裝ndp48-x86-x64-allos-enu.exe

3. 畫面的左方功能列表中,可以找到 Extensions,點選後輸入 “Chinese” 即可搜尋到中文的相關語系,選擇中文(繁體)後頁面的右方即會出現相關介紹,此時按下 “Install” 就會安裝中文語系套件。

如果要玩Pyhton

4. 安裝最新版本 https://www.python.org/downloads/

5.