2024年4月4日 星期四

Chrome for Testing and Chromedriver

Chrome for Testing 這一頁,可以取得特定版本的 Chrome 來測試
本文主拿它在 Linux 上跑 Chromedriver,當作爬蟲用途,
它是真的執行瀏覽器來跑結果,所以只要是瀏覽器可以看得到的,都爬得到
環境是 Rocky Linux 9,使用預設 GDM

# 建立環境
cd /tmp
wget https://storage.googleapis.com/chrome-for-testing-public/123.0.6312.86/linux64/chrome-linux64.zip
wget https://storage.googleapis.com/chrome-for-testing-public/123.0.6312.86/linux64/chromedriver-linux64.zip
cd /usr/local
unzip /tmp/chrome-linux64.zip
unzip /tmp/chromedriver-linux64.zip
cd /usr/bin
ln -s /usr/local/chromedriver-linux64/chromedriver .
ln -s /usr/local/chrome-linux64/chrome .

# 建立 chromdriver 啟動腳本
echo '#!/bin/bash' > /usr/local/bin/chromedriver.sh
echo 'chromedriver --log-path=/dev/shm/chromedriver.log --verbose --append-log --readable-timestamp --whitelisted-ips >> /dev/shm/chromedriver.output 2>&1 &' >> /usr/local/bin/chromedriver.sh
chmod 755 /usr/local/bin/chromedriver.sh

# 設定 GDM 登入後啟動上面的腳本,這邊的 username 請代換成自己的登入名稱
echo '#!/bin/bash' > /etc/gdm/PostLogin/Default
echo 'export DISPLAY=:0' >> /etc/gdm/PostLogin/Default
echo 'su pank /usr/local/bin/chromedriver.sh > /dev/null &' >> /etc/gdm/PostLogin/Default
chmod 755 /etc/gdm/PostLogin/Default

# 設定 iptables 可以導到 Local Port,for 外部 call API 用
echo net.ipv4.conf.all.route_localnet=1 >> /etc/sysctl.conf
sysctl -p
iptables -t nat -A PREROUTING -p tcp --dport 9515 -j DNAT --to 127.0.0.1:9515

到這邊建立完成,已可以另一台電腦調用上面建立的 Chrome 資源,
這邊使用 php-webdriver 測試,抓取 Bing 的網頁

composer require php-webdriver/webdriver

<?php
require __DIR__."/vendor/autoload.php";
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\WebDriverExpectedCondition;
$serverUrl = 'http://192.168.1.10:9515';
$driver = RemoteWebDriver::create($serverUrl, DesiredCapabilities::chrome());
$driver->get('https://bing.com');
$driver->wait(10,200)->until(WebDriverExpectedCondition::titleIs('Bing'));
echo $driver->getPageSource();
$driver->quit();
?>

2024年3月29日 星期五

Linux ping report no answer

Linux ping 預設不會印出無回應的狀況,只要加上 -O 就可以了,就跟 Windows ping 差不多
有掉包的話比較清楚

# ping -O 192.168.1.8
PING 192.168.1.8 (192.168.1.8) 56(84) bytes of data.
64 bytes from 192.168.1.8: icmp_seq=1 ttl=64 time=0.347 ms
64 bytes from 192.168.1.8: icmp_seq=2 ttl=64 time=0.237 ms
no answer yet for icmp_seq=3
no answer yet for icmp_seq=4
64 bytes from 192.168.1.8: icmp_seq=5 ttl=64 time=0.224 ms
64 bytes from 192.168.1.8: icmp_seq=6 ttl=64 time=0.213 ms

再加 -D 參數可以在前面印出時間,可惜是 UNIX timestamp 格式,不方便直接讀取

# ping -DO 192.168.1.8
PING 192.168.1.8 (192.168.1.8) 56(84) bytes of data.
[1711724576.274247] 64 bytes from 192.168.1.8: icmp_seq=1 ttl=64 time=0.254 ms
[1711724577.288744] 64 bytes from 192.168.1.8: icmp_seq=2 ttl=64 time=0.225 ms
[1711724578.312747] 64 bytes from 192.168.1.8: icmp_seq=3 ttl=64 time=0.220 ms

若要在前面加入時間,推薦用 ts,在 moreutils 套件內,它是個 Perl 腳本,可搭配任何程式使用

# ping -O 192.168.1.8 | ts
Mar 29 23:08:25 PING 192.168.1.8 (192.168.1.8) 56(84) bytes of data.
Mar 29 23:08:25 64 bytes from 192.168.1.8: icmp_seq=1 ttl=64 time=0.257 ms
Mar 29 23:08:26 64 bytes from 192.168.1.8: icmp_seq=2 ttl=64 time=0.239 ms
Mar 29 23:08:27 64 bytes from 192.168.1.8: icmp_seq=3 ttl=64 time=0.240 ms








iptables hashlimit

把 Linux 充當 NAT 的情況下,可以用 iptables hashlimit module 來達到簡單的限速

iptables -A FORWARD -d 192.168.0.0/16 -m hashlimit --hashlimit-name download --hashlimit-mode dstip --hashlimit-above 2048kb/s -j DROP
iptables -A FORWARD -s 192.168.0.0/16 -m hashlimit --hashlimit-name upload --hashlimit-mode srcip --hashlimit-above 1024kb/s -j DROP

第1行,下載規則,當目的是內網時, 以 dstip 分類,限速 2048kbytes/s = 16Mbps
第2行,上傳規則,當來源是內網時, 以 srcip 分類,限速 1048kbytes/s = 8Mbps

效果: 內網每個 IP 都享有 16Mbps/8Mbps 頻寬。測速會超過一些,誤差在 10% 內,還算滿準的。

ref. man iptables-extensions

2024年3月21日 星期四

RHEL 9 install xrdp

xrdp 是 Linux 版的 RDP server (遠端桌面)
它運作時需要 Xvnc 的 libvnc.so, 所以安裝時需要連同 VNC 一起安裝(只需 minimal)

dnf -y install xrdp tigervnc-server-minimal
systemctl enable xrdp --now

在 Rocky Linux 9 的環境,有遇到登入後立即斷線的狀況,似乎是 gdm 的問題,換成 lightdm 就正常了

dnf -y install lightdm
systemctl disable gdm --now
systemctl enable lightdm --now

2024年3月14日 星期四

DNS negative cache

在查詢一個域名時,若當資料不存在,會出現 NXDOMAIN
DNS 也會 cache 這個查不到的結果,其預設的 TTL 跟 DNS 及 Zone 的 SOA TTL 有關
例如 BIND 的 max-ncache-ttl 預設是 10800 秒
當 SOA 中的 TTL 小於 DNS 的 ncache-ttl,會看 SOA 中的 TTL

某些情境,需要比較快速查到最新資料,需要縮短這個值,BIND 的 max-ncache-ttl 可以設 0
也就是 disable negative cache,遇到 NXDOMAIN 都不 cache
這樣設定的話,本來沒設定的域名,設了解析後就可以馬上查到,公用服務的 DNS 不建議這樣設定

2024年2月22日 星期四

Install w3m in Rocky Linux

Rocky Linux 中安裝 w3m 需要啟用 powertools repo
未啟用前安裝會出現此錯誤
- nothing provides perl(NKF) needed by w3m-0.5.3-60.git20230121.el8.x86_64 from epel

啟用 powertools repo 指令
dnf config-manager --set-enabled powertools
dnf -y install w3m

2024年1月20日 星期六

Han River Running Place 2024 (潭子旱溪大操場)

由於 旱溪西路拓寬 (2023-08-31 開放通車),有部份自行車道被犧牲了,破壞了原本一圈 5.7K 的 旱溪大操場
現在旱溪西路從松竹路口往北,過南興北二路一點點,自行車道就沒了,只有 1.2K。
附圖潭興路以北區塊,就是小一點的潭子旱溪大操場,一圈 3.3K。
地圖的右下角綠色起點右側是聚興福德爺廟,有飲水機可以喝水,也有厠所,需過馬路。

根據台中市政府的公告,會將犧牲掉的自行車道再補回來,期待旱溪大操場早日回歸
「市府觀旅局已將自行車道改道至旱溪東路,目前也積極爭取中央補助計畫,新設自行車道併同人行道整體規劃,
預計建置於旱溪西路堤頂上,透過分階段調整的方式,將原有旱溪西路的道路環境做更完善的規劃。」

2023年12月31日 星期日

cstream - stream limit tool

cstream 通用串流處理工具
此工具可以將 output 做限流處理,本來在找降低 mysqldump loading 的方案,
看到 這篇 回文,裡面提到將 mysqldump pipe 給 cstream 限速。
實測的確有效,大幅降低 mysqldump 期間對服務效能造成的影響

另一個應用範例: 下載限速 1MB/s
curl https://osk-jp-ping.vultr.com/vultr.com.100MB.bin | cstream -t 1048576 > output
當然,curl 本身就有限速參數,上面經由 cstream 限速的語法效果跟下面這句相同
curl --limit-rate 1048576 https://osk-jp-ping.vultr.com/vultr.com.100MB.bin > output

Get IP addresses by ASN

whois -h whois.radb.net -i origin AS15169 | grep ^route

result
route: IPv4 network segment
route6: IPv6 network segment

2023年11月17日 星期五

Fix Unable to detect release version (use '--releasever' to specify release version)

rpm -ivh https://mirror01.idc.hinet.net/CentOS/8-stream/BaseOS/x86_64/os/Packages/centos-gpg-keys-8-6.el8.noarch.rpm
rpm -ivh https://mirror01.idc.hinet.net/CentOS/8-stream/BaseOS/x86_64/os/Packages/centos-stream-repos-8-6.el8.noarch.rpm
rpm -ivh https://mirror01.idc.hinet.net/CentOS/8-stream/BaseOS/x86_64/os/Packages/centos-stream-release-8.6-1.el8.noarch.rpm

會遇到這個問題是在 migrate to Rocky Linux 時中斷造成
再次執行 migrate2rocky.sh 腳本會出現錯誤

Found a full or partial RockyLinux install already in place. Aborting
because continuing with the migration could cause further damage to system.

修改 migrate2rocky.sh 把出現上面訊息的 if 那一段拿掉,上面的 rpm 補完,再執行一次就可以了

Windows 11 bypass TPM and create local account

跳過 TPM 檢查及建立本機帳號,適用 Windows 11 25H2 Aug 2026 版本 看到選擇語言時,Shift-F10 叫出 CMD,執行 regedit HKEY_LOCAL_MACHINE\SYSTEM\Setup 下面新增 機碼(Key)LabConfig ...