-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Implement shadowsocks relay according to the wiki #66
Open
vmlinz
wants to merge
1
commit into
ftao:master
Choose a base branch
from
vmlinz:ss-relay-nat
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
ss_relay_src_port: 8839 | ||
ss_relay_dst_ip: 127.0.0.1 | ||
ss_relay_dst_port: 8839 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/bin/sh | ||
#set -e | ||
|
||
IPTABLES="/sbin/iptables" | ||
|
||
SS_RELAY_SRC_IP=$1 | ||
SS_RELAY_SRC_PORT=$2 | ||
SS_RELAY_DST_IP=$3 | ||
SS_RELAY_DST_PORT=$4 | ||
|
||
$IPTABLES -t nat -A PREROUTING -p tcp --dport $SS_RELAY_SRC_PORT -j DNAT --to-destination $SS_RELAY_DST_IP:$SS_RELAY_DST_PORT | ||
$IPTABLES -t nat -A PREROUTING -p udp --dport $SS_RELAY_SRC_PORT -j DNAT --to-destination $SS_RELAY_DST_IP:$SS_RELAY_DST_PORT | ||
$IPTABLES -t nat -A POSTROUTING -p tcp -d $SS_RELAY_DST_IP --dport $SS_RELAY_DST_PORT -j SNAT --to-source $SS_RELAY_SRC_IP | ||
$IPTABLES -t nat -A POSTROUTING -p udp -d $SS_RELAY_DST_IP --dport $SS_RELAY_DST_PORT -j SNAT --to-source $SS_RELAY_SRC_IP | ||
|
||
echo "Shadowsocks relay rules are set up" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
- name: setup ss relay | ||
command: "/opt/ss-relay/setup_ss_relay {{ ansible_default_ipv4.address }} {{ ss_relay_src_port }} {{ ss_relay_dst_ip }} {{ ss_relay_dst_port }}" | ||
tags: | ||
- ss-relay |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
# shadowsocks relay server | ||
|
||
- name: ensure working dir exists | ||
action: file path=/opt/ss-relay/ state=directory | ||
tags: | ||
- ss-relay | ||
|
||
- include: setup_ss_relay.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Shadowsocks relay https://github.com/shadowsocks/shadowsocks/wiki/Setup-a-Shadowsocks-relay with both udp and tcp | ||
# setup nat rules | ||
- name: upload setup_ss_relay script | ||
copy: src=setup_ss_relay | ||
dest=/opt/ss-relay/setup_ss_relay | ||
mode=755 | ||
notify: | ||
- setup ss relay | ||
tags: ss-relay | ||
|
||
# ensure rules are loaded when booting up | ||
- name: make sure setup_ss_relay is in rc.local | ||
lineinfile: dest=/etc/rc.local | ||
insertafter="^#" | ||
regexp="/opt/ss-relay/setup_ss_relay" | ||
line="/opt/ss-relay/setup_ss_relay {{ ansible_default_ipv4.address }} {{ ss_relay_src_port }} {{ ss_relay_dst_ip }} {{ ss_relay_dst_port }}" | ||
state=present | ||
tags: ss-relay | ||
|
||
# see https://github.com/clowwindy/shadowsocks/wiki/Optimizing-Shadowsocks | ||
- name: update sysctl for performance | ||
sysctl: name="{{ item.name }}" value="{{ item.value }}" state=present reload=yes | ||
with_items: | ||
- {"name" : "fs.file-max", "value" : "51200"} | ||
- {"name" : "net.core.rmem_max", "value" : "67108864 "} | ||
- {"name" : "net.core.wmem_max", "value" : "67108864 "} | ||
- {"name" : "net.core.netdev_max_backlog", "value" : "250000"} | ||
- {"name" : "net.core.somaxconn", "value" : "3240000"} | ||
- {"name" : "net.ipv4.tcp_syncookies", "value" : "1"} | ||
- {"name" : "net.ipv4.tcp_tw_reuse", "value" : "1"} | ||
- {"name" : "net.ipv4.tcp_tw_recycle", "value" : "0"} | ||
- {"name" : "net.ipv4.tcp_fin_timeout", "value" : "30"} | ||
- {"name" : "net.ipv4.tcp_keepalive_time", "value" : "1200"} | ||
- {"name" : "net.ipv4.ip_local_port_range", "value" : "10000 65000"} | ||
- {"name" : "net.ipv4.tcp_max_syn_backlog", "value" : "8192"} | ||
- {"name" : "net.ipv4.tcp_max_tw_buckets", "value" : "5000"} | ||
- {"name" : "net.ipv4.tcp_fastopen", "value" : "3"} | ||
- {"name" : "net.ipv4.tcp_rmem", "value" : "4096 87380 67108864"} | ||
- {"name" : "net.ipv4.tcp_wmem", "value" : "4096 65536 67108864"} | ||
- {"name" : "net.ipv4.tcp_mtu_probing", "value" : "1"} | ||
tags: | ||
- ss-relay | ||
|
||
- name: enable ip forwarding | ||
sysctl: name="{{ item.name }}" value="{{ item.value }}" state=present reload=yes | ||
with_items: | ||
- {"name" : "net.ipv4.ip_forward", "value" : "1"} | ||
tags: | ||
- ss-relay | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
# shadowsocks relay | ||
# https://github.com/shadowsocks/shadowsocks/wiki/Setup-a-Shadowsocks-relay | ||
|
||
- hosts: ss-relay | ||
|
||
roles: | ||
- ss-relay |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
唯一担心的是事情时这个脚本不是 idempotent 的, 多次执行, iptable 里面会多出一些垃圾, 或者是报错。
然后如果修改了端口, 再次执行的话也会在iptable 中遗留之前的指令。
建议你参考 https://github.com/ftao/vpn-deploy-playbook/blob/master/roles/nat/templates/opt/easynat/setup_nat 中的做法, 自定义 chain 的方法。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ftao 谢谢,我今天测试的时候也发现,我要改端口,那nat规则里面还是会遗留之前的设置。如果上来就清空iptables好像也不对。我去看看你的easynat。