机械境

这里只是我的后花园,随性而写

准备工作

  • 注册 chatGPT 账户
  • 注册 Telegram Bot,通过 @BotFather 创建,并获取 token
  • 获取 Telegram Chat ID, 通过 @userinfobot 查询
  • 获取 Access Token
  • 准备反代服务,可以选择别人提供的,不是一定要自己部署

运行

  • 克隆 chatgpt-telegram-bot

  • 准备配置文件 local.json

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    {
    "debug": 1,
    "bot": {
    "token": "telegram bot token",
    "groupIds": [],
    "userIds": [],
    "chatCmd": "/chat"
    },
    "api": {
    "type": "unofficial",
    "unofficial": {
    "accessToken": "chatGPT access token",
    "apiReverseProxyUrl": "https://chat.duti.tech/api/conversation",
    "model": ""
    }
    }
    }
    • userIds 为数字,比如 114797892,可指定多个,如果不指定,就所有人都可用,groupIds 同理,建议配置上
    • accessToken 会有过期时间,暂时没特别好的自动更新的办法,主要是有 cloudflare 验证
  • 修改 docker-compose.yml,并构建镜像

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    version: '3'

    services:
    chatgpt:
    image: chatgpt-telegram-bot
    container_name: chatgpt
    build: .
    restart: unless-stopped
    volumes:
    - ./local.json:/app/config/local.json
    networks:
    - nginx-proxy

    networks:
    nginx-proxy:
    external: true

    docker-compose build 构建镜像

  • 运行
    docker-compose up -d 运行

  • 查看 log
    docker-compose logs -f --tail 100 chatgpt

    如无意外的话,应该会输出类似的日志

    1
    2
    3
    4
    5
    6
    > node --experimental-loader=extensionless dist/index.js

    (node:42) ExperimentalWarning: Custom ESM Loaders is an experimental feature. This feature could change at any time
    (Use `node --trace-warnings ...` to show where the warning was created)
    2/23/2023, 3:06:13 AM 🔮 ChatGPT API has started...
    2/23/2023, 3:06:14 AM 🤖 Bot @xxx_bot has started...
  • 在 Telegram 中添加刚才创建的机器人,通过 /chat 聊天内容 开始,/help 会显示支持的指令

---EOF---

之前介绍了使用 antigensheldon 管理 Zsh 配置,由于 antigen 已经停止维护了,后面就有了 antibody,但是这个也停止维护了,最终就有了继任者 antidote,这几个使用上都大同小异。

  • brew install antidote 安装

  • $HOME 目录添加 .zsh_plugins.txt 来定义需要使用的插件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    # .zsh_plugins.txt

    # comments are supported like this
    github.com/rupa/z
    github.com/zsh-users/zsh-completions

    # empty lines are skipped

    # annotations are also allowed:
    github.com/romkatv/zsh-bench kind:path
    github.com/olets/zsh-abbr kind:defer

    # frameworks like oh-my-zsh are supported
    github.com/ohmyzsh/ohmyzsh path:lib
    github.com/ohmyzsh/ohmyzsh path:plugins/command-not-found
    github.com/ohmyzsh/ohmyzsh path:plugins/common-aliases
    github.com/ohmyzsh/ohmyzsh path:plugins/docker
    github.com/ohmyzsh/ohmyzsh path:plugins/gem
    github.com/ohmyzsh/ohmyzsh path:plugins/git
    github.com/ohmyzsh/ohmyzsh path:plugins/npm
    github.com/ohmyzsh/ohmyzsh path:plugins/tmux
    github.com/ohmyzsh/ohmyzsh path:plugins/yarn
    github.com/ohmyzsh/ohmyzsh path:plugins/fzf

    # prompts:
    # with prompt plugins, remember to add this to your .zshrc:
    # `autoload -Uz promptinit && promptinit && prompt pure`
    # github.com/sindresorhus/pure kind:fpath
    # github.com/romkatv/powerlevel10k kind:fpath

    # popular fish-like plugins
    github.com/mattmc3/zfunctions
    github.com/zsh-users/zsh-autosuggestions
    github.com/zdharma-continuum/fast-syntax-highlighting kind:defer
    # github.com/zsh-users/zsh-history-substring-search
  • .zshrc 中添加下面的内容,以后修改 .zsh_plugin.txt 中的内容后会自动更新

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    # ~/.zshrc
    # Set the name of the static .zsh plugins file antidote will generate.

    zsh_plugins=${ZDOTDIR:-~}/.zsh_plugins.zsh

    # Ensure you have a .zsh_plugins.txt file where you can add plugins.
    [[ -f ${zsh_plugins:r}.txt ]] || touch ${zsh_plugins:r}.txt

    # Lazy-load antidote.
    fpath+=(${ZDOTDIR:-~}/.antidote)
    autoload -Uz $fpath[-1]/antidote

    # Generate static file in a subshell when .zsh_plugins.txt is updated.
    if [[ ! $zsh_plugins -nt ${zsh_plugins:r}.txt ]]; then
    (antidote bundle <${zsh_plugins:r}.txt >|$zsh_plugins)
    fi

    # Source your static plugins file.
    source $zsh_plugins

    eval "$(starship init zsh)"

---EOF---

  • 安装 gpgpinentry
    brew install gpg pinentry-mac

  • 修改 gpg-agent 配置
    echo pinentry-program $(whitch pinentry-mac) >> ~/.gnupg/gpg-agent.conf

  • 通过 gpgconf 检查配置

    1
    2
    3
    4
    5
    6
    7
    gpg:OpenPGP:/opt/homebrew/Cellar/gnupg/2.4.0/bin/gpg
    gpgsm:S/MIME:/opt/homebrew/Cellar/gnupg/2.4.0/bin/gpgsm
    keyboxd:Public Keys:/opt/homebrew/Cellar/gnupg/2.4.0/libexec/keyboxd
    gpg-agent:Private Keys:/opt/homebrew/Cellar/gnupg/2.4.0/bin/gpg-agent
    scdaemon:Smartcards:/opt/homebrew/Cellar/gnupg/2.4.0/libexec/scdaemon
    dirmngr:Network:/opt/homebrew/Cellar/gnupg/2.4.0/bin/dirmngr
    pinentry:Passphrase Entry:/opt/homebrew/opt/pinentry/bin/pinentry
  • 检查 pinentry-mac 是否能正确弹出密码界面
    echo GETPIN | pinentry-mac

  • 修改 git 签名配置

    1
    2
    3
    git config --global user.signingkey F0406D8A
    git config --global commit.gpgsign true
    git config --global gpg.program $(which gpg)
  • 测试签名是否正确
    echo "test" | gpg --clearsign

    输出结果:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    gpg: using "F0406D8A525890198021BB243D8D256084332679" as default secret key for signing
    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA512

    test
    -----BEGIN PGP SIGNATURE-----

    iQEzBAEBCgAdFiEE8EBtilJYkBmAIbskPY0lYIQzJnkFAmPsoRUACgkQPY0lYIQz
    JnkqTQf8DSIYBfaB6ijhO3U7K4FYvCjeMACV3UbuRJI6WxEyDkn4Iglaw/Y4C/s5
    U12ba4PIGLmzqRSkISFbIj+5eKH97CQoB/kVlfQWL+wkYCpTWNAIPEBa7FGU4BYN
    9dSW/00XEdrWz9Lvzb0QAP2t9a8DIPIhyDGpQoxfq+0enZuFJKMRtKPzDKkpaylG
    MTEYEwV5VnyYNsIu8K37h27oPWKPSadT3SGM+m4vFP68V9Thw0/qZFJS8NW7OW8b
    RwB5FVSIsORypAtN0AtzzTd8cxEq6Rf9rPkYgtfeGGrCzZdyfKsPe/AoWEP5/ZuY
    FzQGzjxomBPHLDt7k89Aq5mwK2NAKw==
    =73Fx
    -----END PGP SIGNATURE-----
  • 诊断问题

    1
    2
    3
    4
    5
    # Kill gpg-agent
    killall gpg-agent

    # Run gpg-agent in daemon mode
    gpg-agent --daemon

---EOF---

通过 WATCHTOWER_NOTIFICATION_URL 定义 Telegram 的通知信息,当然了也可以定义其他的通知方式。底层是通过 shoutrrr 实现的,只要 shoutrrr 支持的方式这里都可以用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
version: '3.5'
services:
watchtower:
image: containrrr/watchtower
container_name: watchtower
restart: unless-stopped
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
- TZ=Asia/Shanghai
- WATCHTOWER_LIFECYCLE_HOOKS=True
- WATCHTOWER_NOTIFICATIONS=shoutrrr
- WATCHTOWER_NOTIFICATION_URL=telegram://token@telegram?chats=@channel-1,chat-id-1
- WATCHTOWER_DEBUG=true
- WATCHTOWER_CLEANUP=true
# - WATCHTOWER_SCHEDULE=0 0 20 * * 0
- WATCHTOWER_POLL_INTERVAL=43200
command: reference filebrowser
  • 通过 @BotFather 创建自己的通知机器人,并获取 token
  • 通过 @RawDataBot 获取 Chat ID

---EOF---

原始问题,一言以蔽之就是在 daemon.json 增加 Host 之后,服务会启动失败。

解决方案:

  • /etc/systemd/system/docker.service.d/simple_dockerd.conf 中增加
    1
    2
    3
    [Service]
    ExecStart=
    ExecStart=/usr/bin/dockerd
  • /etc/docker/daemon.json 中增加
    1
    {"hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]}
  • 重启 docker 服务
    1
    2
    systemctl daemon-reload && 
    systemctl restart docker.service
  • 检查 docker 服务状态
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    ❯ systemctl status docker.service
    ● docker.service - Docker Application Container Engine
    Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/docker.service.d
    └─simple_dockerd.conf
    Active: active (running) since Wed 2023-02-15 11:49:01 CST; 1h 18min ago
    TriggeredBy: ● docker.socket
    Docs: https://docs.docker.com
    Main PID: 1193 (dockerd)
    Tasks: 350
    Memory: 198.1M
    CPU: 11.520s
    CGroup: /system.slice/docker.service
    ├─1193 /usr/bin/dockerd
  • 检查 TCP 连接
    1
    curl -X GET http://localhost:2375/containers/json?all=1

---EOF---

介绍

Fish 是一种用户友好的 Unix shell,它支持自动补全、语法高亮和其他功能,使得命令行操作变得更加容易。Fish 具有简单的语法,并且可以自动识别文件和目录名称,这使得它特别适合新手使用。Fish 还有一个强大的脚本语言,可以用来创建复杂的脚本和工具。Fish 也支持多种不同的主题和样式,可以根据用户的喜好进行自定义。

Fish 和 Bash 和 Zsh 对比,一言以蔽之就是开箱即用的高亮和自动不全,缺点就是和 POSIX 不兼容。Fish 有个骚操作就是执行 fish_config 后就可以在浏览器中配置。

Bash 中的特殊变量也挺难记的,Fish 中也都改成了单词:

1
2
3
4
5
6
7
$*, $@, $1 ...: $argv  # 函数或者脚本的参数
$0: status filename # 函数或者脚本的名字
$#: 使用 $argv 的长度
$?: $status # 上一个命令的返回值
$$: $fish_pid # shell 的 pid
$!: $last_pid # 上一个命令的 pid
$-: 大多数使用是 status is-interactive 和 status is-login

更多的差异,查看官方文档

安装

通过 brew install fish fisher starship 一键安装。

  • fisher: 作为 Fish 的包管理工具,就不需要每次都去修改 config.fish
  • Starship: Rust 编写的轻量、迅速、可无限定制的高颜值终端,类似 - powerlevel10k

配置

我安装的插件如下,可通过 fisher install meaningful-ooo/sponge 安装, fisher update 来进行更新。

1
2
3
4
5
6
7
8
9
10
❯ fisher list
jorgebucaran/fisher
patrickf1/fzf.fish
ankitsumitg/docker-fish-completions
jethrokuan/z
meaningful-ooo/sponge
jhillyerd/plugin-git
rstacruz/fish-asdf
jorgebucaran/replay.fish
nickeb96/puffer-fish

更多可参考 awsm.fish 进行安装。

其他基本上没什么需要配置的,如果需要的话,修改 ~/.config/fish/config.fish 文件即可。

主题

可通过 starship preset nerd-font-symbols > ~/.config/starship.toml 设置 symbols,更多设置,可参考官方文档,大部分时候都不需要。

最终只需要在 ~/.config/fish/config.fish 中添加一行 starship init fish | source 即可。

成果

之后只要在 iTerm2 中设置 fish 为启动 shell,显示效果如下:

从试用上来看,比 iTerm2+Zsh 的组合启动速度肉眼可见的迅速。但是由于不兼容 POSIX shell,暂时通过 replay.fish临时执行 Bash 脚本,后续还要看看是否有其他问题。

---EOF---

介绍

之前一直使用使用 antigen 管理 Zsh 配置,由于 antigen 已经很就不更新了,最后一次代码提交为 2019,就切换到了 sheldon

sheldon 是 Rust 编写的 Zsh 包管理器,特性如下:

  • Plugins from Git repositories.
    • Branch / tag / commit support.
    • Submodule support.
    • First class support for GitHub repositories.
    • First class support for Gists.
  • Arbitrary remote scripts or binary plugins.
  • Local plugins.
  • Inline plugins.
  • Highly configurable install methods using templates.
  • Shell agnostic, with sensible defaults for Zsh.
  • Super-fast plugin loading and parallel installation. See benchmarks.
  • Config file using TOML syntax.
  • Clean ~/.zshrc or ~/.bashrc (just add 1 line).

安装

通过 brew install sheldon 一键安装,也有其他安装方式可选。

配置

执行 sheldon init 会生成默认配置 plugins.toml,文件存放在 $XDG_CONFIG_HOME/sheldon,一般这个路径就是 ~/.config/sheldon/plugins.toml。可以直接编辑这个配置,也可以通过 sheldon CLI 来操作。

CLI

sheldon 有三种不同类型的命令:

  • init 初始化一个新的配置文件。
  • locksource 处理插件下载、安装和 shell 源代码的生成。
  • add,edit,remove 会自动更新配置文件

详细使用,可以参考官方说明,常用的就是 sheldon lock --update 更新所有插件。

配置结构

1
2
3
4
5
6
7
8
9
# ~/.config/sheldon/plugins.toml

# ┌─ Unique name for the plugin
# ┌──┴─┐
[plugins.base16]
github = "chriskempson/base16-shell"
# └─────┬────┘ └─────┬────┘
# │ └─ GitHub repository name
# └─ GitHub user or organization

Read more »

缘起

偶尔出差的时候,需要访问家里的网络,办公室已经有一台服务器使用了 Zerotier网络,索性就把家里的路由也加到网络里面,这样的话,就可以同时访问公司和家里的网络了。因为办公室那台是我控制的,所以不会出现其他同事会进入到家庭网络的情况。

ZeroTier 是一款软件定义网络(SDN)解决方案,可以帮助企业快速部署和管理全球虚拟网络。它使用一个分布式的虚拟交换机来连接所有的节点,无需物理交换机或配置复杂的 VPN。ZeroTier 可以在任何地方部署,包括云、数据中心、家庭和办公室。它还可以与其他 SDN 解决方案集成,如 OpenFlow 和 Cisco ACI。ZeroTier 还可以通过多个平台使用,包括 Windows、macOS、Linux、Android 和 iOS。

安装 Zerotier-one-moon

由于 Zerotier 的中转服务器都在国外,访问速度有可能会慢,所以增加一台中转服务器。 使用 zerotier-one-moon 可以快速一键部署自己的中转服务器。我这里就找了一台闲置的阿里云服务器,使用下来速度还可以,基本感受不到卡顿。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
version: "3.5"

services:
zerotier-moon:
image: ghcr.io/gythialy/zerotier-one-moon:latest
container_name: zerotier-moon
cap_add:
- NET_ADMIN
- SYS_ADMIN
devices:
- /dev/net/tun
ports:
- 9993:9993/udp
command:
- -4 xxx.xxx.xxx.xxx
- -p 9993
volumes:
- ./zerotier-one:/var/lib/zerotier-one

服务正常启动后,会得到 moon id,类似 61941d46d1,已经服务器的公网 IP 和端口,记得在防火墙把响应的端口打开。

配置 Zerotier 账号

账号本身没什么,就是注册登陆后建立一个 Zerotier 网络,这里建议设置成 private network,这样别人就不能随便加入了。

授权自己的接入点

因为设置成了 private network,所以默认是无法加入这个网络的,可以在 Zerotier 的网络管理界面看到哪些点,然后在 auth 上勾选一下,就授权了这个点接入了。

设置分配 IP 的网段

其实不设置根本没问题,你只要设置好路由表保证分配的网段范围都划分到同一个 LAN 就行。我这里设置成了 10.147.17.1~10.147.17.254,保证这个 IP 段不和你两端任意一段路由器的 LAN IP 段冲突就行。

路由设置

在 菜单 VPN/Zerotier 中填入刚才在网页上生成的 Network ID,保存并应用即可,正常情况下 Zerotier 的服务应该就启动起来了。

启动完成后,在 Zerotier 的网络管理界面授权通过,至此路由就应该连接到了 Zerotier 网络。

设置 Zerotier Moon

通过命令行 sudo zerotier-cli orbit 61941d46d1 61941d46d1 添加 Zerotier moon。可以通过 SSH 登录到路由器,或者通过 OpenWRT 提供的 TTYD 终端都可以,看个人习惯。

可以通过 sudo zerotier-cli peers 查看节点的连接情况

1
2
3
4
5
6
7
8
9
200 peers
<ztaddr> <ver> <role> <lat> <link> <lastTX> <lastRX> <path>
61941d46d1 1.6.5 MOON 10 DIRECT 307 306 139.224.186.142/9993
61d294b8cb - PLANET 168 DIRECT 2593 2431 50.8.73.34/9993
62f845ae71 - PLANET 321 DIRECT 2593 7327 50.8.252.138/9993
748cde7190 - PLANET 232 DIRECT 7640 2361 103.195.108.66/9993
952fcf1db7 - PLANET 247 DIRECT 1628481820060 2346 195.181.174.159/9993
a05acf0233 1.6.4 LEAF 205 DIRECT 2773 2773 34.135.35.67/46781
c8a68837ae - LEAF -1 RELAY

如果出现 <role> 为 MOON 的节点信息,则表示通过 moon 提速完成,上表中的 IP 和 Zerotier Id 都是随机的生成的。

设置 Zerotier 的路由表

在 Zerotier 的网络管理界面中添加路由表,我的路由表如下:

1
2
10.147.17.0/24 (LAN)
192.168.5.0/24 via 10.147.17.151

第一个表示 10.147.17.0/24 这是同一个局域网,分配出来的 IP 在这个范围的直连就行了,不用过别的网关了。

第二条表示,家庭的路由器后网段是 192.168.5.0/24,而 OpenWRT 在 Zerotier 网络得到的 IP 是 10.147.17.151(这个可以在 zerotier 网站查,也可以通过 ifconfig 命令在路由器查),那么 zerotier 局域网内的其他 IP 要访问这个 192.168.5.0/24 网段的话,自然要通过 10.147.17.151 作为网关。

如果有多个网络的话,增加多条路由表即可,不过 Zerotier 免费的版本,最多可以添加 25 个设备,如果超过这个设备的话,可以通过之间 PLANT 来解决。

后记

在 OpenWRT 上配置好 Zerotier 后,路由后的所有设备都可以直接访问 10.147.17.x 段的服务,无需做其他配置。在出差的时候,在本地启动 Zerotier 服务后,也可以直接访问家中的 192.168.5.x 的服务,比如 NAS 之类的。

---EOF---

0%