服务器私有化

Mar 22, 2025

服务器不需要数据库,仅转发数据或处理节点间的协调工作,所以部署起来还是比较简单的。

仅需要配置认证部分, NAT 遍历部分和其他一些参数。

一行命令运行服务器

pgmap -c /etc/pgmap.yml

关于配置文件配置项含义及如何配置,请往下看。

认证

所有的网络节点需要与服务器建立 WebSocket 连接,用于传输控制数据和转发私有网路数据包,只有通过认证的节点才可以正常建立连接。

服务器接收一个 JSONSecret 对象(一个 JSON 文件)用于认证,所以客户端需要携带这个 JSONSecret 才可以正常加入网络。

对于 JSONSecret 的生成目前有 2 种方式:

相关配置项: pgmap.yml

secret_key: fb58d680-4af2-4a18-8e22-639609d114d4
secret_rotation_period: 12h
secret_validity_period: 48h
oidc_providers:
  - name: google
    issuer: https://accounts.google.com
    client_id: client_id_placeholder
    client_secret: client_secret_placeholder
    redirect_url: https://openpg.in/oidc/authorize/google
    scopes:
      - profile
      - email
  - name: linuxdo
    auth_url: https://connect.linux.do/oauth2/authorize
    token_url: https://connect.linux.do/oauth2/token
    user_info_url: https://connect.linux.do/api/user
    client_id: client_id_placeholder
    client_secret: client_secret_placeholder
    redirect_url: https://openpg.in/oidc/authorize/linuxdo
    scopes: []

NAT 部分

客户端会从服务器拿到 STUN 服务器列表用于诊断公有网络的类型。

相关配置项: pgmap.yml

stuns:
  - stun.miwifi.com:3478
  - stunserver2024.stunprotocol.org:3478
  - 106.13.249.54:3478
  - 106.12.251.193:3478
  - 106.12.251.31:3478
  - 106.12.251.52:3478

服务器配置越多,越可靠,但也不能太多,影响性能。一般是 3-10 个。

其他配置项

pgmap.yml

listen: 127.0.0.1:9987
rate_limiter:
  limit: 200000
  burst: 512000

使用一个 7 层代理包装 https/wss

pgmap 本身监听一个 tcp 端口用于提供 http 和 ws 服务,没有 tls 安全层,所以最佳实践是前端使用支持 tls 的 7 层代理器(如 caddy, nginx)

容器化部署

dockerhub 已经存在一个 linux/amd64 架构的镜像了 https://hub.docker.com/r/rkonfj/openpg

可以这样运行 pgmap :

docker run -d --net host -v $PWD/pgmap.yml:/etc/pgmap.yml rkonfj/openpg pgmap -c /etc/pgmap.yml

最后

感谢你耐心看完,有过有任何不清楚的地方,请评论区告诉我。 也可以加入

Telegram 群组:https://t.me/+-S5L6ZCBxlxkMTRl
QQ 群组:1039776116