2020/01/17

更新 Let's Encrypt 失敗

將 hass.io 做 snapshot 還原時,針對 Let's Encrypt addon 啟動確認是否可以正常更新憑證。
發現卻無法更新,出現以下錯誤。
File “/root/.local/share/letsencrypt/local/lib/python2.7/site-packages/letsencrypt/storage.py”, line 273, in _check_symlinks
“expected {0} to be a symlink”.format(link))
CertStorageError: expected /etc/letsencrypt/live/example.org/cert.pem to be a symlink
這原因是當 Let's Encrypt 更新憑證時,會將 live/example.org/cert.pem 做 symbolic link 到 archive/example.org 裡面 certX.pem (x 表示數字,越大越新)。
如果檢查到不是 symbolic link 就會判斷有問題而不執行更新。
而做 snapshot 還原時會修改到這部分,此時只要移除 live/example.org/ 裡面的憑證重新做 symbolic link 即可。
以下 X 表示最大的數字。
rm -f live/example.org/*.pem
ln -sf archive/example.org/certX.pem live/example.org/cert.pem
ln -sf archive/example.org/chainX.pem live/example.org/chain.pem
ln -sf archive/example.org/fullchainX.pem live/example.org/fullchain.pem
ln -sf archive/example.org/privkeyX.pem live/example.org/privkey.pem

2020/01/16

Using nmap to do presence-detection

Based on these two posts.
- https://www.home-assistant.io/integrations/nmap_tracker/
- https://www.home-assistant.io/integrations/person/

1. add these lines to configuration.yaml
device_tracker:
  - platform: nmap_tracker
    # global config
    interval_seconds: 15
    consider_home: 180
    new_device_defaults:
      track_new_devices: false
      hide_if_away: false
    # config for nmap_tracker
    hosts: # network address to scan
      - 192.168.1.1/24
    home_interval: 10 # if it is home, not scan this device at least 10 mins
    exclude: # not to scan this device
      - 192.168.1.1
2. restart hass.io
3. check known_devices.yaml in Configurator, this file will be auto generated.
original:
11_22_33_44_55_66:
  hide_if_away: false
  icon:
  mac: 11:22:33:44:55:66
  name: 11 22 33 44 55 66
  picture:
  track: false

22_22_33_44_55_66:
  hide_if_away: false
  icon:
  mac: 22:22:33:44:55:66
  name: 22 22 33 44 55 66
  picture:
  track: false

updated:
me:
  hide_if_away: false
  icon:
  mac: 11:22:33:44:55:66
  name: me
  picture:
  track: true

her:
  hide_if_away: false
  icon:
  mac: 22:22:33:44:55:66
  name: her
  picture:
  track: true
4. go Configuration -> Persons, Click on the account, and select Track Device, in this case, choose device_tracker.me
5. If someone doesn't have the account in hass.io, go to Configurator and edit configuration.yaml.
Add these lines and then restart hass.io
person:
  - name: Her
    id: her1224 # this must be unique.
    device_trackers:
      - device_tracker.her
6. go Configuration -> Persons to check if add successfully.

2020/01/15

Maintain ssh tunnel

Based on this: https://github.com/daradib/sidedoor

1. installation
sudo apt install sidedoor
2. create ssh public key and add to remote user ~/.ssh/authorized_keys
sudo ssh-keygen -t rsa -N '' -f /home/myaccount/id_rsa
cat /home/myaccount/id_rsa.pub | ssh myaccount@my.remote.server 'cat >> .ssh/authorized_keys'
3. modify /etc/sidedoor/config
Host my.remote.server
  # SSH private key to access the remote server.
  IdentityFile /home/myname/id_rsa

  # because I am using ddns, my IP might change, so I set it to no
  StrictHostKeyChecking no

  # SSH host key of the remote server.
  UserKnownHostsFile /dev/null

  # If tunneled traffic is encrypted (e.g., SSH),
  # it probably does not compress well.
  Compression no

  # Disable password authentication.
  BatchMode yes

  # Terminate if unable to set up port forwarding.
  ExitOnForwardFailure yes

  # Enable SSH keepalives.
  ServerAliveInterval 30

  # Disconnect after unresponsive SSH keepalives.
  ServerAliveCountMax 3

4. modify /etc/default/sidedoor
# Configuration for sidedoor service

# Arguments to pass to sidedoor, which are in turn passed to ssh.
# See man ssh for possible options.
# For example, uncomment the line below to create a remote port forward
# to the local SSH server (running on port 22, exposed on port 8022).
OPTIONS='-R 12345:localhost:9999'

# Remote SSH server to connect to, i.e., [user@]hostname.
REMOTE_SERVER=myaccount@my.remote.server

5. start up sidedoor service and check sidedoor status
sudo systemctl enable sidedoor.service
sudo systemctl restart sidedoor.service
systemctl status sidedoor.service

How to clean reinstall hassio ?

1. stop services
systemctl stop hassio-supervisor.service
systemctl stop hassio-apparmor.service

2. stop docker container
docker ps #list containers
docker container stop @containerid # remove @containerid with image name start with homeassistant
docker container prune # remove all stopped containers

3. remove docker images
docker images # list all images
docker image remove @imagename # remove @imangename start with homeassistant

4. remove data when install hassio.
# if you install like this 'hassio_install.sh -m raspberrypi3 -d /share/ha_data', remove all data in /share/ha_data
rm -rf /share/ha_data
rm -f /etc/systemd/system/hassio-*

Raspberry PI wifi mac 重啟後一直改變

想把 raspberry pi wifi 連線成固定某個 IP,
當設定好後發現每次重啟 raspberry pi wifi 的 mac 一直改變。

要修正這部份,可以新增一個檔案 "100-disable-wifi-mac-randomization.conf" 至 /etc/NetworkManager/conf.d
內容如下
[connection]
wifi.mac-address-randomization=1
 
[device]
wifi.scan-rand-mac-address=no
可透過以下指令確認 wifi mac 是否還有一直變動
sudo systemctl restart NetworkManager

2020/01/14

網站資料庫 v133

目前所有支援網站: https://goo.gl/jXLsJu

更新紀錄:
版本: 133 - 20200114
+ 新增 '起點' 登入支援
+ '輕小說文庫 [Wenku8]' 新增強制分頁功能
+ 新增 '鯉魚鄉 [腐書網]' 2020y 分類
- 調整 '新筆趣閣', '下書網', '黃金屋中文', '卡提諾小說網 [101novel]', '飛速中文網', '八零電子書' 使用網址
- 調整 '木子書屋' 朗讀問題
- 修正針對 android 4.4 機型載入問題
- 修正'豆豆小說閱讀網'簡介問題
- 再度修正 '全本小說網 [Quanben5]' 內文部份載入問題
- 修正 '4K 文學' 內文載入問題
- 修正 '中網文學' 搜尋問題
- 修正 '卡提諾小說網 [101novel]', '好看小說網' 載入問題
- 修正 'ESJZone' 部份內文載入問題
- 修正 'FC2' 解析以及部份內文載入問題
- 修正 '飛速中文網' 內文載入問題
- 修正 '繁體小說網', '耽美中文網' 搜尋問題

更新方式:
1. 手動更新
a) 設定的網站資料庫
b) 點擊下載
c) 下載檔案 "網站資料庫更新包x_v133.zip"
d) 設定的網站資料庫
e) 匯入

2. 自動更新
a) 設定的網站資料庫
b) 點擊版本

2020/01/12

Ssh port forwarding

操作之前記得先修改 sshd_config 加上
PermitTunnel yes # 允許做 ssh tunnel
GatewayPorts yes # 允許 remote port forwarding

Local Port forwarding (Local to Remote)
ssh -L 1234:192.168.1.1:80 user@example.com 
當使用帳號 user ssh 到 example.com 後,本地端的 port 1234 會對應到遠端 192.168.1.1 的 port 80。
當這個連線保持開啟時,開啟瀏覽器連線至 127.00.1:1234 則會變成連線到 192.168.1.1:80。

Remote Port Forwarding (Remote to Local)
ssh -R 1234:192.168.1.1:80 user@example.com 
當使用帳號 user ssh 到 example.com 後,遠端的 port 1234 會對應到 192.168.1.1 的 port 80。
(192.168.1.1 為本地端的 IP) 這表示當此連線保持開啟時,連線至 example.com:1234 都會直接導至本地端的 port 80。