2023年9月8日 星期五

Fix Ubuntu NO_PUBKEY B53DC80D13EDEF05

Google Cloud package update failed

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -

2023年8月10日 星期四

HAProxy Reverse Proxy with SSL

憑證掛在 Reverse Proxy, 內部走 HTTP

frontend http-in
mode http
bind *:80
bind *:443 ssl crt /etc/haproxy/pank.org-crt-chain-key.pem
use_backend %[req.hdr(Host),lower]

backend test1.pank.org
balance first
mode http
option forwardfor except 127.0.0.0/8
server web-1 192.168.0.27:80

backend test2.pank.org
balance first
mode http
option forwardfor except 127.0.0.0/8
server web-2 192.168.0.42:80

test1.pank.org 及 test2.pank.org 使用同一個 wildcard 憑證,
crt 是 crt+chain+key 放同一個檔
若兩個站的憑證不同,可以用 crt-list 指定多個憑證
例: bind *:443 ssl crt-list /etc/haproxy/crt-list.txt
crt-list.txt 的內容
/etc/haproxy/domain1.com-crt-chain-key.pem
/etc/haproxy/domain2.com-crt-chain-key.pem
有設定 option forwardfor 會帶 X-Forwarded-For 到 backend server

2023年8月9日 星期三

NGINX Reverse Proxy with SSL

憑證掛在 Reverse Proxy, 內部走 HTTP

/etc/nginx/conf.d/test1.conf

server {
listen 443 ssl;
server_name test1.pank.org;
ssl_certificate /etc/nginx/conf.d/pank.org-crt-chain.pem;
ssl_certificate_key /etc/nginx/conf.d/pank.org-key.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
add_header X-Proxy-Cache $upstream_cache_status;
proxy_pass http://192.168.0.27;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

若後端有多台,建立多個 conf 即可
有設定 proxy_set_header X-Forwarded-For 會帶 X-Forwarded-For 到 backend server

Apache Reverse Proxy with SSL

憑證掛在 Reverse Proxy, 內部走 HTTP

<VirtualHost *:443>
ServerName test1.pank.org
ServerAdmin pank@pank.org
ErrorLog "/var/log/httpd/test1_error_log"
TransferLog "/var/log/httpd/test1_access_log"
SSLEngine on
ProxyPass / http://192.168.0.27/
ProxyPassReverse / http://192.168.0.27/
SSLProtocol all -SSLv2
SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
SSLCertificateFile "/etc/httpd/conf.d/pank.org-crt.pem"
SSLCertificateKeyFile "/etc/httpd/conf.d/pank.org-key.pem"
SSLCertificateChainFile /etc/httpd/conf.d/chain.pem
</VirtualHost>

若後端有多台,建立多個 VirtualHost 區塊即可
Apache 預設就會帶 X-Forwarded-For 到 backend server,不用特別設定

2023年8月3日 星期四

yt-dlp - youtube-dl replacement

youtube-dl 之前是一個好用的視頻下載工具,但在 2020 年 10 月,被 RIAA 要求下架,
Github 也關閉了它及其他 fork 的專案
但在很多使用者抱怨下,2020 年 11 月 Github 恢復了 youtube-dl 的專案,
但是近來都沒更新了,最後更新日期是 2021 年 12 月
yt-dlp 是 youtube-dl 的 fork,仍常常更新,可視為 youtube-dl 的替代品

2023年7月3日 星期一

Simple traffic shapping with iptables

使用 iptables 限速

範例: Linux Gateway, 區網的網段是 192.168.0.0/24
要限制每個 IP 都有上傳下載 16M/16M bps

iptables -A FORWARD -d 192.168.0.0/24 -m hashlimit --hashlimit-name ratelimitin --hashlimit-mode dstip --hashlimit-above 2048kb/s -j DROP
iptables -A FORWARD -s 192.168.0.0/24 -m hashlimit --hashlimit-name ratelimitin --hashlimit-mode srcip --hashlimit-above 2048kb/s -j DROP
Rule 要分兩個方向來下,兩這條分別是下載及上傳
16M bps = 16384 k bps = 2048 kb/s

實測結果還算準

2023年6月27日 星期二

Get Public IP Address from OpenDNS

一般取得所在 Public IP 都是用 Web 服務取得,例如:
curl ip4.tw
curl ifconfig.co

OpenDNS 有提供一個服務,對他們的 name server 查 myip.opendns.com 會回應來源 IP
dig +short myip.opendns.com @resolver1.opendns.com
host myip.opendns.com resolver1.opendns.com

發現若環境同時有 IPv4+IPv6 可能會不正常,因為兩個 IP 都有的話,通常會走 IPv6 的路由,用 IPv6 路由去查 A 記錄導致無資料

保險一點,指令要區分 IPv4、IPv6
dig -4 +short A myip.opendns.com @resolver1.opendns.com
dig -6 +short AAAA myip.opendns.com @resolver1.opendns.com

2023年6月18日 星期日

Import Rocky Linux 9 to WSL

需先安裝 XZ Utils for Windows 把 xz.exe 放在 PATH 有指到的地方

curl -LOs https://github.com/rocky-linux/sig-cloud-instance-images/raw/Rocky-9.2.20230513-Base-x86_64/layer.tar.xz
上面的 layer.tar.xz 是官方的網址,其他版本網址可到 sig-cloud-instance-images 選擇 Branch
xz -d layer.tar.xz
wsl --import RockyLinux9 D:\WSL\RockyLinux9 layer.tar
Distro 名稱 RockyLinux9 及資料目錄名稱 D:\WSL\RockyLinux9 請依自身環境調整
wsl -d RockyLinux9
即完成進入 RockyLinux9 WSL

ref. Import any Linux distribution to use with WSL

2023年5月20日 星期六

Migrate Google Compute Engine VM image to local

想要把 Google Compute Engine VM 備份,或者 clone 一份到 local 做測試
先關機建立映像檔,再把映像檔匯出到 Bucket (可以選擇匯出格式 vmdk、vhdx 等),然後就可以下載
但會發現在自己的環境無法開機,看起來是找不到 root



解決方式必需重建 initramfs
但因為找不到 root,在這個 emergency mode 無法做這個動作 (dracut -f)
需要找對應的版本的 ISO 來開機,由於系統內可能沒有跟 ISO 內一樣版號的 /lib/modules
最簡單的方式就是裝 ISO 內的 Kernel,流程上一樣會建立 initramfs

以下以 CentOS 7.9 為例:
以光碟開機進入 Rescue 模式後,選 1
chroot /mnt/sysimage
mount /dev/cdrom /mnt
cd /mnt/Packages
rpm -ivh kernel-3.10.0-1160.el7.x86_64.rpm
exit
reboot
應該就可以正常開機了

備註:
一開始畫面出現這個
Probing EDD (edd=off to disable)... ok
其實跟開不了機無關,不影響是否可以正常開機


Fix Ubuntu NO_PUBKEY B53DC80D13EDEF05

Google Cloud package update failed curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyring...