这个是逗比根据地的备份镜像站(非官方)。牢记域名: [doubibackup.com]。搜索:标签云
投稿文章 | 广告合作 | Telegram 群组 / 公告频道 / 使用教程
广告

『原创』Shadowsocks HaProxy 中继(中转/端口转发) 便捷管理脚本

Shadowsocks Toyo 34评论
广告
本文最后更新于 2018年9月20日 20:10 可能会因为没有更新而失效。如已失效或需要修正,请留言!

最近没什么教程好写的,那就闲的没事写个脚本吧!

Shadowsocks 中继(中转)能用的工具很多,我的网站介绍过 HaProxy、iptables、Socat。

昨天我写了Socat的脚本,今天我就写个HaProxy的脚本。当然,iptables的端口转发一键脚本我暂时是不会写的,iptables我也并不是很熟练 - -。


HaProxy手动教程:Shadowsocks利用HaProxy实现中继(中转)加速

其他 Shadowsocks中继(中转)教程:传送门

Socat:

优点:支持 TCP/UDP 转发。缺点:不支持端口段(多个端口需要开启多个转发)

HaProxy:

优点:支持 TCP 转发,支持 端口段 转发。缺点:不支持 UDP 转发。

iptables:

优点:支持 TCP/UDP 转发,支持 端口段 转发。缺点:配置麻烦,容易出错。

系统要求

Centos / Debian / Ubuntu 14.04 +

推荐 Debian / Ubuntu,这个是我一直使用的系统,我的脚本在这个系统上面出错率最低。

脚本版本

Ver: 1.0.7

安装步骤

wget -N --no-check-certificate https://raw.githubusercontent.com/ToyoDAdoubiBackup/doubi/master/haproxy.sh && chmod +x haproxy.sh && bash haproxy.sh

下载并运行脚本后,会自动检测并开始安装。安装完成后,会提示你输入监听的端口(转发的端口,支持端口段 2333-6666),欲转发到的IP:

请输入 HaProxy 的 本地监听端口(转发端口) [1-65535](支持端口段: 2333-6666): 10000-20000
请输入 HaProxy 欲转发的 IP:2.2.2.2

——————————————————————————————
请检查 HaProxy 配置是否有误 !

本地监听端口 : 10000
欲转发 IP : 2.2.2.2
——————————————————————————————

请按任意键继续,如有配置错误请使用 Ctrl+C 退出。

安装完成之后会提示:

——————————————————————————————
	HaProxy 已启动 !
——————————————————————————————
	HaProxy 配置信息: 

	本地 IP :  1.1.1.1
	本地监听端口 :  10000-2000

	欲转发 IP :  2.2.2.2
	欲转发端口 :  10000-20000
——————————————————————————————

使用方法

# 查看haproxy配置
bash haproxy.sh view

# 设置haproxy
bash haproxy.sh set

# 启动haproxy
bash haproxy.sh start

# 停止haproxy
bash haproxy.sh stop

# 重启haproxy
bash haproxy.sh restart

# 查看haproxy状态
bash haproxy.sh status

# 卸载haproxy
bash haproxy.sh uninstall

# 对了,haproxy安装的时候自动设置了系统服务,所以你还可以用以下命令来管理
# 启动haproxy
service haproxy start

# 停止haproxy
service haproxy stop

# 重启haproxy
service haproxy restart

# 查看haproxy状态
service haproxy status

# 重载haproxy配置
service haproxy reload

Shadowsocks客户端说明

假设你的 海外服务器(被中转) 中搭建的Shadowsocks服务端的IP是 2.2.2.2  ,SS端口是 10000

假设你的 中转服务器 的IP是 1.1.1.1 ,本地监听端口和SS端口一致,都是 10000

那么,你的Shadowsocks客户端,添加Shadowsocks服务器,IP填写 1.1.1.1 ,端口填写 10000 其他的 密码/加密方式/协议/混淆等等 全部和原Shadowsocks账号一样!

安装目录

配置文件:/etc/haproxy/haproxy.cfg

其他说明

防火墙设置

脚本 v1.0.1版本 已经自动设置了防火墙规则,如果发现没有或者其他情况,可以自己开放端口。

如果你设置后无法链接,那么多半是防火墙 阻拦了,只要开放端口 就行了。以上面的 示例的中继端口 23333 为例。

iptables -I INPUT -p tcp --dport 23333 -j ACCEPT
# 端口段
iptables -I INPUT -p tcp --dport 2333:6666 -j ACCEPT

Centos系统执行(debian/ubuntu 不需要):

/etc/init.d/iptables save
# 保存防火墙
/etc/init.d/iptables restart
# 重启防火墙

提示wget: unknown host “raw.githubusercontent.com” 之类的错误

这是无法解析我的域名,多半是DNS的问题,请更换DNS为谷歌DNS。

echo "nameserver 8.8.8.8" > /etc/resolv.conf
echo "nameserver 8.8.4.4" >> /etc/resolv.conf

提示 wget: command not found 的错误

这是你的系统精简的太干净了,wget都没有安装,所以需要安装wget。

Centos系统:

yum install -y wget

Debian/Ubuntu系统:

apt-get install -y wget

更换 apt源,解决安装错误:[错误] Socat安装失败 等等

一些VPS的 apt源太老旧,导致无法安装socat,所以我这里写上如何更换 apt源。所以我只针对这两个系统,Centos的自己去谷歌yum镜像源。

依次输入就可以更换apt源了,下面的代码是以 us美国 为例,你可以自己去这里选一个近一些合适的,然后替换下面代码中 us.sources.list 的 us 。

wget -N --no-check-certificate -P /etc/apt https://raw.githubusercontent.com/ToyoDAdoubiBackup/doubi/master/sources/us.sources.list
rm -rf /etc/apt/sources.list
cp /etc/apt/us.sources.list /etc/apt/sources.list

更新日志

2017年08月12日,版本 v1.0.7

1.修复 部分系统 停止HaProxy异常的问题。

2017年08月12日,版本 v1.0.6

1.修复 检测系统版本不正确的问题。

2017年08月12日,版本 v1.0.5

1.修复 CentOS7 可能停止失败的问题。

2017年08月12日,版本 v1.0.4

1.修复 CentOS7 默认系统服务为systemctl 导致启动失败的问题。

2017年08月11日,版本 v1.0.3

1.新增 保存防火墙规则功能(原来的时候重启服务器防火墙规则就没了)。

2.修复 Debian系统 卸载失败问题。

3.优化 开机启动问题(改了开机启动方式)。

—— 这个脚本好老了,里面的代码也挺乱的,简单更新一下好了。

2017年03月11日,版本 v1.0.2

1.修复 CentOS系统 启动失败问题

2016年12月24日,版本 v1.0.1

1.增加 自动设置开机启动功能。

2.增加 自动设置开放防火墙端口功能。

3.修复 启动/停止 成功,但是提示失败的BUG。

2016年12月22日,版本 v1.0.0

1.推出 正式版本。


HaProxy手动教程:Shadowsocks利用HaProxy实现中继(中转)加速

其他 Shadowsocks中继(中转)教程:传送门

转载请超链接注明:逗比根据地 » 『原创』Shadowsocks HaProxy 中继(中转/端口转发) 便捷管理脚本
责任声明:本站一切资源仅用作交流学习,请勿用作商业或违法行为!如造成任何后果,本站概不负责!

赞 (25)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(34)个小伙伴在吐槽
  1. haproxy 启动失败
    ● haproxy.service - HAProxy Load Balancer
       Loaded: loaded (/lib/systemd/system/haproxy.service; enabled)
       Active: failed (Result: start-limit) since Fri 2018-10-12 07:10:06 CST; 29s ago
         Docs: man:haproxy(1)
               file:/usr/share/doc/haproxy/configuration.txt.gz
      Process: 4231 ExecStart=/usr/sbin/haproxy-systemd-wrapper -f ${CONFIG} -p /run/haproxy.pid $EXTRAOPTS (code=exited, status=0/SUCCESS)
      Process: 4230 ExecStartPre=/usr/sbin/haproxy -f ${CONFIG} -c -q (code=exited, status=0/SUCCESS)
     Main PID: 4231 (code=exited, status=0/SUCCESS)
    
    Oct 12 07:10:06 debian systemd[1]: haproxy.service holdoff time over, scheduling restart.
    Oct 12 07:10:06 debian systemd[1]: Stopping HAProxy Load Balancer...
    Oct 12 07:10:06 debian systemd[1]: Starting HAProxy Load Balancer...
    Oct 12 07:10:06 debian systemd[1]: haproxy.service start request repeated too quickly, refusing to start.
    Oct 12 07:10:06 debian systemd[1]: Failed to start HAProxy Load Balancer.
    Oct 12 07:10:06 debian systemd[1]: Unit haproxy.service entered failed state.
    
    chuanshuo2018-10-12 14:23 回复
    • 请问怎么解决的 最近更新脚本以后 经常爆出这个问题
      kk2018-10-29 21:23 回复
  2. Ubuntu 17 安装后报 Haproxy 启动失败,查看 status 只是报 Haproxy 没有运行
    wayne2018-02-25 21:32 回复
  3. 套路云, debian 7x64位系统,安装失败,提示如下,不知道什么原因?安全组端口已放开了0-30000. [email protected]:~# wget -N --no-check-certificate https://softs.host/Bash/haproxy.sh && chmod +x haproxy.sh && bash haproxy.sh --2017-11-27 14:45:43-- https://softs.host/Bash/haproxy.sh Resolving softs.host (softs.host)... 69.12.67.44 Connecting to softs.host (softs.host)|69.12.67.44|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 8785 (8.6K) [application/octet-stream] Saving to: `haproxy.sh' 100%[======================================>] 8,785 --.-K/s in 0s 2017-11-27 14:45:55 (202 MB/s) - `haproxy.sh' saved [8785/8785] haproxy.sh: line 62: haproxy: command not found Get:1 http://mirrors.cloud.aliyuncs.com wheezy Release.gpg [2,373 B] Ign http://mirrors.cloud.aliyuncs.com wheezy/updates Release.gpg Get:2 http://mirrors.cloud.aliyuncs.com wheezy-updates Release.gpg [1,601 B] Get:3 http://mirrors.cloud.aliyuncs.com wheezy Release [191 kB] Ign http://mirrors.cloud.aliyuncs.com wheezy/updates Release Get:4 http://mirrors.cloud.aliyuncs.com wheezy-updates Release [155 kB] Get:5 http://mirrors.cloud.aliyuncs.com wheezy/main Sources [5,993 kB] Get:6 http://mirrors.cloud.aliyuncs.com wheezy/contrib Sources [47.7 kB] Get:7 http://mirrors.cloud.aliyuncs.com wheezy/non-free Sources [93.4 kB] Get:8 http://mirrors.cloud.aliyuncs.com wheezy/main amd64 Packages [5,839 kB] Get:9 http://mirrors.cloud.aliyuncs.com wheezy/contrib amd64 Packages [42.0 kB] Get:10 http://mirrors.cloud.aliyuncs.com wheezy/non-free amd64 Packages [80.8 kB] Get:11 http://mirrors.cloud.aliyuncs.com wheezy/contrib Translation-en [34.8 kB] Get:12 http://mirrors.cloud.aliyuncs.com wheezy/main Translation-en [3,846 kB] Get:13 http://mirrors.cloud.aliyuncs.com wheezy/non-free Translation-en [66.1 kB] Get:14 http://mirrors.cloud.aliyuncs.com wheezy-updates/main Sources [5,505 B] Get:15 http://mirrors.cloud.aliyuncs.com wheezy-updates/contrib Sources [14 B] Get:16 http://mirrors.cloud.aliyuncs.com wheezy-updates/non-free Sources [929 B] Get:17 http://mirrors.cloud.aliyuncs.com wheezy-updates/main amd64 Packages [7,047 B] Get:18 http://mirrors.cloud.aliyuncs.com wheezy-updates/contrib amd64 Packages [14 B] Get:19 http://mirrors.cloud.aliyuncs.com wheezy-updates/non-free amd64 Packages [488 B] Get:20 http://mirrors.cloud.aliyuncs.com wheezy-updates/contrib Translation-en [14 B] Get:21 http://mirrors.cloud.aliyuncs.com wheezy-updates/main Translation-en [4,879 B] Get:22 http://mirrors.cloud.aliyuncs.com wheezy-updates/non-free Translation-en [496 B] Err http://mirrors.cloud.aliyuncs.com wheezy/updates/main Sources 404 Not Found Err http://mirrors.cloud.aliyuncs.com wheezy/updates/contrib Sources 404 Not Found Err http://mirrors.cloud.aliyuncs.com wheezy/updates/non-free Sources 404 Not Found Err http://mirrors.cloud.aliyuncs.com wheezy/updates/main amd64 Packages 404 Not Found Err http://mirrors.cloud.aliyuncs.com wheezy/updates/contrib amd64 Packages 404 Not Found Err http://mirrors.cloud.aliyuncs.com wheezy/updates/non-free amd64 Packages 404 Not Found Ign http://mirrors.cloud.aliyuncs.com wheezy/updates/contrib Translation-en_US Ign http://mirrors.cloud.aliyuncs.com wheezy/updates/contrib Translation-en Ign http://mirrors.cloud.aliyuncs.com wheezy/updates/main Translation-en_US Ign http://mirrors.cloud.aliyuncs.com wheezy/updates/main Translation-en Ign http://mirrors.cloud.aliyuncs.com wheezy/updates/non-free Translation-en_US Ign http://mirrors.cloud.aliyuncs.com wheezy/updates/non-free Translation-en Fetched 16.4 MB in 4s (3,290 kB/s) W: Failed to fetch http://mirrors.cloud.aliyuncs.com/debian/dists/wheezy/updates/main/source/Sources 404 Not Found W: Failed to fetch http://mirrors.cloud.aliyuncs.com/debian/dists/wheezy/updates/contrib/source/Sources 404 Not Found W: Failed to fetch http://mirrors.cloud.aliyuncs.com/debian/dists/wheezy/updates/non-free/source/Sources 404 Not Found W: Failed to fetch http://mirrors.cloud.aliyuncs.com/debian/dists/wheezy/updates/main/binary-amd64/Packages 404 Not Found W: Failed to fetch http://mirrors.cloud.aliyuncs.com/debian/dists/wheezy/updates/contrib/binary-amd64/Packages 404 Not Found W: Failed to fetch http://mirrors.cloud.aliyuncs.com/debian/dists/wheezy/updates/non-free/binary-amd64/Packages 404 Not Found E: Some index files failed to download. They have been ignored, or old ones used instead. haproxy.sh: line 72: haproxy: command not found [错误] 安装HaProxy失败,请检查 !
    墙出新境界2017-11-27 14:47 回复
    • 不要用阿里云自带的 软件包源,更换源文件即可。https://softs.host/?dir=Bash/sources ,看这里,注意根据地区修改下载的文件名。
      Toyo2017-11-27 16:23 回复
      • 搞定了,谢谢逗比大佬。用国内套路云100M宽带的中转套路云新加坡,试了tinyport和haproxy,中转后速度都基本没提升,不知道为什么,这情况的话中转就没什么意义了。
        墙出新境界2017-11-28 19:05 回复
  4. 想问一下,这个支持代理多个服务器么?就是假设我有2个美国的ssr服务器,想用这个中转服务器的不同端口做中转,怎么添加多个ip呢?
    机智的苏先锋2017-08-20 18:55 回复
    • HaProxy是支持多个端口中转不同IP,不过我这个脚本嫌麻烦没加上多IP中转,你可以参考手动教程修改配置文件:https://doub.io/ss-jc29/
      Toyo2017-08-20 19:24 回复
  5. 补充:haproxy.sh: line 176: /etc/init.d/haproxy: No such file or directory
    狐狸2017-08-11 23:17 回复
    • 什么系统?安装的haproxy什么版本?是否安装正常?/etc/init.d/haproxy是haproxy安装完整后自带的服务脚本。
      Toyo2017-08-12 09:26 回复
  6. 请问:总是HaProxy 启动失败 !怎么办?谢谢!
    狐狸2017-08-11 22:27 回复
  7. haproxy.sh: line 239: sudo: command not found haproxy.sh: line 240: sudo: command not found [错误] HaProxy卸载失败,请检查 !
    X2017-08-11 19:18 回复
    • 脚本已更新,请重新下载覆盖。
      Toyo2017-08-11 19:34 回复
  8. 小白,折腾了好久 终于搞定了. CentOS 如果启动不成功的话 尝试 关闭SELinux. 感谢大神的一键脚本, 虽然我没有一键安装成功吧-.-
    OldKing2017-07-23 17:17 回复
  9. 大神,ssr可以设置转发吗求教程,一键
    子夜2017-03-07 17:36 回复
    • 这个教程不就是一键脚本?
      Toyo2017-03-07 19:04 回复
  10. 请问这个有办法解决嚒: 请按任意键继续,如有配置错误请使用 Ctrl+C 退出。 iptables v1.4.21: invalid port/service `ve' specified Try `iptables -h' or 'iptables --help' for more information. haproxy.sh: line 99: /etc/haproxy/haproxy.cfg: Permission denied haproxy.sh: line 160: /etc/init.d/haproxy: No such file or directory [错误] HaProxy 启动失败 ! 而且输入 service haproxy status 会提示: Redirecting to /bin/systemctl status haproxy.service ● haproxy.service - HAProxy Load Balancer Loaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled; vendor preset: disabled) Active: failed (Result: exit-code) since Tue 2017-02-21 08:10:13 CST; 23h ago Main PID: 17508 (code=exited, status=1/FAILURE) (reverse-i-search)`': ^C
    jmyz_04552017-02-22 08:13 回复
  11. 请问直接用 yum install -y haproxy 安装的跟你的脚本有什么不一样? 用上面的命令安装后用 service haproxy start 提示 Redirecting to /bin/systemctl start haproxy.service
    jmyz_04552017-02-21 16:56 回复
    • 上句话有歧义,是用 yum install -y haproxy 会出现后面句话。后来我用 /bin/systemctl start haproxy.service 回车后一点反应都没有,在 netstat -apn | grep haproxy 里什么都看不到,为什么源的软件反而不行呢?
      jmyz_04552017-02-21 19:41 回复
      • centos很迷,我用过的次数屈指可数,所以不清楚。
        Toyo2017-02-21 19:57 回复
        • 哈哈,我看见好多人都是用 Centos 就装了,没办法
          jmyz_04552017-02-21 22:15 回复
  12. 安装后提示unknown keyword 'ulimit' in 'global' section,我把那一行#掉后可以运行,什么原因呢,谢谢,Aliyun转发Vultr,谢谢,都是Debian 系统
    Lion2017-01-30 19:19 回复
    • 这个注释掉不影响haproxy运行,至于报错,可能跟haproxy版本有关系,因为是通过包管理器安装,根据源不同,安装的haproxy版本也可能不一样,会有差别。
      Toyo2017-01-30 20:47 回复
      • 谢谢
        Lion2017-01-30 20:52 回复
  13. 请问一下 中转后 流量是扣中转服务器还是被中转的?
    chp2017-01-08 14:28 回复
    • 都扣,你SS下载100MB文件,你的SS服务器(被中转服务器)首先下载这个文件到VPS,然后通过上行上传给中转服务器,然后中转服务器,通过下行接收,下载到中转服务器本地,然后通过上行上传给你PC SS客户端。
      所以说,你挂SS下载100M文件,SS服务器(被中转)消耗了200MB流量,然后中转服务器消耗了200MB流量。
      Shadowsocks本身就是一个流量中转软件。
      Toyo2017-01-08 16:14 回复
      • 想再问问 如果中转服务器带宽200m SS服务器5m 那中转后是不是200m?
        chp2017-01-11 10:30 回复
        • 还是5M,你下载一个文件,Shadowsocks服务器下载完你要的文件后,会把数据通过上行上传给中转服务器,然后中转服务器通过下行下载到VPS本地,然后中转服务器再把数据通过上行上传给你的电脑SS客户端。
          而服务器说的5M一般都是指的 上行5M.
          Toyo2017-01-11 12:35 回复
  14. Reading package lists... W: There is no public key available for the following key IDs: 9D6D8F6BC857C906 W: There is no public key available for the following key IDs: 7638D0442B90D010 Reading package lists... Building dependency tree... Reading state information... E: Unable to locate package haproxy haproxy install failed. 手动./haproxy.shh.1 可以填写端口和转发IP,但是结尾也是haproxy install failed. 我用的是板瓦工的DEBIAN 7
    7892016-12-30 04:21 回复
  15. Debian 7 一键安装包提示 Reading package lists... Done Building dependency tree Reading state information... Done E: Unable to locate package haproxy haproxy.sh: line 62: haproxy: command not found [错误] 安装HaProxy失败,请检查 ! 手动安装提示无法找到haproxy 我从官网找的1.7安装后找到了在/usr/local/haproxy 然后就不知道怎么弄了。。 最近刚学习这些东西。。 感谢你
    7892016-12-30 04:13 回复
    • 一些比较老的apt源不包括HaProxy,你只要换一个新的源就行了。
      Toyo2016-12-30 14:38 回复
  16. 我用Vultr家 日本的vps os:ub14 x64 ,一键脚本执行完后,提示请检查Haproxy配置是否有误。 在配置中提示的支持端口是不是意思,需要SS服务器端也是在这个端口端内。 然后错误后也一直无法启动是否是上述原因?
    Johnson.zha02016-12-23 15:16 回复
    • 比如你搭建的Shadowsocks服务端 设置的SS账号端口是 2333,那你这里就写2333,IP填写你SS账号的IP,然后你的Shadowsocks客户端连接中转服务器,就是IP填写 中转服务器的IP,端口依然是2333,其他的密码,加密方式什么的都一样。
      错误是提示什么错误?
      Toyo2016-12-23 17:12 回复