Centos8下编译安装Shadowsocks-rust 作者: Hogwarts 发布于: 2021-01-02 更新于: 2024-02-25 分类: 红杏出墙 Rust语言很火,不懂火的原因。盖因为比Go省内存,或Go的体积大等等,不是很懂。Libev版除重大Bug外已经不再更新。所以弄个ss-rust尝尝鲜。 # 一、搭建前的准备 ## 1.1 安装必要的软件及编辑器 sudo yum -y install vim wget curl git gcc make openssl-devel.x86_64 ## 1.2 安装Rust语言环境 **安装命令:**`curl https://sh.rustup.rs -sSf | sh` 1) Proceed with installation (default) 2) Customize installation 3) Cancel installation 选1即可 **安装结束后运行:**`. ~/.profile`使生效。 **查看安装成功与否** **运行:**`which cargo` 显示:/root/.cargo/bin/cargo **运行:**`which rustc` 显示:/root/.cargo/bin/rustc **运行:**`cargo --version` 显示:cargo 1.51.0 (43b129a20 2021-03-16) **运行:**`rustc -V` 显示:rustc 1.51.0 (2fd73fabe 2021-03-23) **更新命令:**rustup update ## 1.3 编译安装libsodium 依次运行以下命令 wget https://download.libsodium.org/libsodium/releases/LATEST.tar.gz tar -zxvf LATEST.tar.gz cd libsodium-stable ./configure make make install ldconfig cd ###Debian10下wget下载报错 GnuTLS: A TLS fatal alert has been received. GnuTLS: received alert [70]: Error in protocol version Unable to establish SSL connection. 则可用axel下载 # 二、编译安装shadowsocks-rust ## 2.1 下载shadowsocks-rust源码 git clone https://github.com/shadowsocks/shadowsocks-rust.git ## 2.2 编译源码 cd shadowsocks-rust export SODIUM_USE_PKG_CONFIG=1 cargo check #检查一下 cargo build --release #开始编译 等待编译完成 strip ./target/release/ssserver #去除调试符号表以减少文件体积 输入:./target/release/ssserver -V #查看版本号 显示:shadowsocks 1.10.2 #显示版本号 ## 2.3 安装到默认路径 make install TARGET=release 显示如下信息: install -d /usr/local/bin #注意路径 install -m 755 target/release/sslocal /usr/local/bin/sslocal install -m 755 target/release/ssserver /usr/local/bin/ssserver install -m 755 target/release/ssurl /usr/local/bin/ssurl install -m 755 target/release/ssmanager /usr/local/bin/ssmanager **恭喜你,安装成功了。** ## 2.4 多说一句 如果不想自行编译的话可到GitHub的官网仓库中下载安装 参考下文 4.2.2.2 # 三、运行shadowsocks-rust ## 3.1 建立运行文件 mkdir -p /usr/local/etc/shadowsocks-rust vim /usr/local/etc/shadowsocks-rust/config.json 输入以下内容 { "server":"0.0.0.0", "server_port":443, #自设端口 "timeout":300, "method":"none", #自设加密格式,可选aes-128-gcm "password":"passwd", #自设密码 "fast_open":false, "nameserver":"8.8.4.4", "mode":"tcp_only" } Xray文档中有这样的描述: > [shadowsocks配置中,有这么一句说明:“none” 不加密方式下,服务器端不会验证 “password” 中的密码。为确保安全性, 一般需要加上 TLS 并在传输层使用安全配置,例如 WebSocket 配置较长的 path。 我(引用,不是本人)觉得这句说明是有误导性的,给人一种“使用未加密的shadowsocks套上TLS是安全的”这种错觉。但这种配置下shadowsocks的UDP数据是危险的,可能会坑了一些勇于尝试的萌新。因此希望文档能够对此加以说明。另外socks5协议也是同理。我(引用,不是本人)觉得这句说明是有误导性的,给人一种“使用未加密的shadowsocks套上TLS是安全的”这种错觉。但这种配置下shadowsocks的UDP数据是危险的,可能会坑了一些勇于尝试的萌新。因此希望文档能够对此加以说明。另外socks5协议也是同理。 参考:`https://github.com/XTLS/Xray-core/discussions/237`] ## 3.2 试运行shadowsocks-rust /usr/local/bin/ssserver -c /usr/local/etc/shadowsocks-rust/config.json 能够正常运行后,按ctrl+c中止。 ## 3.3 加入开机自动运行 vim /etc/systemd/system/shadowsocks-rust.service 输入以下内容 [Unit] Description=Shadowsocks-Rust Service After=network.target [Service] Type=simple User=nobody Group=nobody StandardOutput=null CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE NoNewPrivileges=true ExecStart=/usr/local/bin/ssserver -c /usr/local/etc/shadowsocks-rust/config.json Restart=on-failure RestartPreventExitStatus=10 [Install] WantedBy=multi-user.target 输入后给予必要权限 chown -R nobody:root /usr/local/bin chown -R nobody:root /usr/local/etc/shadowsocks-rust systemctl daemon-reload systemctl start shadowsocks-rust #启动 systemctl status shadowsocks-rust #查看启动状态 systemctl enable shadowsocks-rust #加入开机自启 ##3.4 可能存在的问题 ###3.4.1 可能nobody权限启动失败 将 /etc/systemd/system/shadowsocks-rust.service 中的 User=nobody改为User=root,并将Group=nobody删除。 此处涉及到nobody用户和用户组的问题 可运行`id nobody`查看nobody属于哪个组 Debian下nobody用户默认组为nogroup,则将Group=nobody改为Group=nogroup ###3.4.2 非root用户监听1024以下端口的问题 报错:`failed to start server: app/proxyman/inbound: failed to listen TCP on 443 > transport/internet: failed to listen on address: [::]:443 > transport/internet/websocket: failed to listen TCP(for WS) on [::]:443 > listen tcp 0.0.0.0: 443: bind: permission denied` 解决: setcap 'cap_net_bind_service=+ep' /usr/local/bin/ssserver ##3.5 多用户 官方给出了服务端多用户配置,未尝试。 https://github.com/shadowsocks/shadowsocks-rust # 四、加入plugin插件 ## 4.1 方式一 caddy监听443端口 经测试,无论是caddy还是nginx进行ws协议的转发,速度和plugin插件直接监听有很大差别,当然通还是能通的,或许是本人技术问题。所以不推荐此种方式。 上面搭建的ss-rust单独用起来很容易被墙,配合v2ray-plugin和caddy防止主动探测,减少被墙几率。caddy监听443端口,不影响建站等其它需求。 具体配置和搭建教程参考此篇文章: https://shiping.date/35.html 可参照v2ray-plugin的教程,安装秋水逸冰新编译的xray-plugin,或将xray-plugin下载后改名为v2ray-plugin。 https://github.com/teddysun/xray-plugin/releases ## 4.2 方式二 shadowsocks-rust监听443端口,配合xray-plugin插件 ### 4.2.1 申请域名证书 此方式就是传说的HTTPS模式,相当稳。前提是需要域名和证书,如何申请前文有讲。参考:`https://winamp.top/93.html` ### 4.2.2 shadowsocks-rust安装运行 #### 4.2.2.1 自行编译 见上文。 #### 4.2.2.2 GitHub下载解压 wget https://github.com/shadowsocks/shadowsocks-rust/releases/download/v1.11.0/shadowsocks-v1.11.0.x86_64-unknown-linux-gnu.tar.xz #下载 tar -xf shadowsocks-v1.11.0.x86_64-unknown-linux-gnu.tar.xz ssserver #解压 mv ssserver /usr/local/bin/ #移动到运行目录 chmod +x /usr/local/bin/ssserver #防翻车给予运行权限 ### 4.2.3 下载xray-plugin插件 wget https://github.com/teddysun/xray-plugin/releases/download/v1.4.2/xray-plugin-linux-amd64-v1.4.2.tar.gz #下载 tar -zxvf xray-plugin-linux-amd64-v1.4.2.tar.gz -C /usr/local/bin/xray-plugin #解压到运行目录并重命名为xray-plugin chmod +x /usr/local/bin/xray-plugin #给予运行权限 ### 4.2.4 修改shadowsocks-rust配置文件 vim /usr/local/etc/shadowsocks-rust/config.json #输入以下内容 { "server":"0.0.0.0", "server_port":443, "timeout":300, "method":"aes-128-gcm", "password":"你的密码", "fast_open":false, "nameserver":"8.8.4.4", "mode":"tcp_only", "plugin":"xray-plugin", #有的机器需绝对路径,比如甲骨文。 "plugin_opts":"server;tls;host=你的域名;cert=/证书路径/fullchain.cer;key=/密钥路径/private.key" } ### 4.2.5 试运行 /usr/local/bin/ssserver -c /etc/shadowsocks-rust/config.json #显示以下内容 INFO shadowsocks 1.11.0 INFO shadowsocks tcp server listening on 127.0.0.1:35801, inbound address 0.0.0.0:443 Xray 1.4.2 (Xray, Penetrates Everything.) Custom (go1.16.2 linux/amd64) A unified platform for anti-censorship. [Warning] core: Xray 1.4.2 started ### 4.2.6 运行命令 前文有加入开机启动的方法,直接启动即可。 systemctl start shadowsocks-rust systemctl status shadowsocks-rust #PS #五、一些配置 ##5.1 直接监听443端口 { "server":"0.0.0.0", "server_port":443, "password":"password", #修改密码 "timeout":300, "method":"aes-256-gcm", "fast_open":false, "nameserver":"8.8.8.8", "mode":"tcp_only", "plugin":"xray-plugin", "plugin_opts":"server;tls;host=domain.com;cert=/etc/shadowocks-rust/fullchain.cer;key=/etc/shadowsocks-rust/private.key" #需修改域名,证书,密钥 } ##5.2 报错 Dec 06 21:15:38 ptr ssserver[15836]: 2021/12/06 21:15:38 failed to start server: app/proxyman/inbound: failed to listen TCP on 443 > transport/internet: failed to listen on address: 0.0.0.0:443 > transport/internet/websocket: failed to listen TCP(for WS) on 0.0.0.0:443 > listen tcp 0.0.0.0:443:bind: permission denied 以为是运行用户的问题,无数次尝试。以root用户没有问题,nobody用户运行失败,经搜索,可能是5.3的问题,果真是。 ##5.3 问题的解决 在 Linux 中,只有 root 可以绑定到 1024 以下的端口 setcap 'cap_net_bind_service=+ep' /usr/local/bin/ssserver setcap 'cap_net_bind_service=+ep' /usr/local/bin/xray-plugin 标签: shadowsocks, shadowsocks-rust, rust