(redsocks注意事项:
使用iptables时,redirect到的端口,对应的ip是和来源ip同一个网段的。
例如:路由器上192.168.1.0/24网络,那redsocks就必须要监听192.168.1.1,只监听127.0.0.1是不够的。)
base { log_debug = on; log_info = on; //日志文件,调试时可指定为标准错误"stderr" log = "file:/home/hedaors/redsocks/socks.log"; // 是否以后台模式运行 daemon = on redirector = iptables; } redsocks { //local_ip设置为0.0.0.0则可共享,设备为127.0.0.1则只能在本机使用 local_ip = 0.0.0.0 local_port = 12345; // 本来有的代理的IP和端口,可能是由ssh -D指定的 ip = 127.0.0.1; port = 1080; // known types: socks4, socks5, http-connect, http-relay type = socks5; }
建立个 iptables 规则脚本, tpsocks.sh
#!/bin/sh # iptables路径 IPTABLES="/usr/sbin/iptables" # redsocks路径 REDSOCKS_DIR="/home/hedaors/redsocks" REDSOCKS="$REDSOCKS_DIR/redsocks" # 配置文件中指定的端口 REDSOCKS_PORT="12345" # socks代理IP和端口 SOCKS_HOST="127.0.0.1" SOCKS_PORT="1080" # 运行redsocks if [ "$USER" != "root" ]; then echo -n 'Restarting redsocks... ' pkill -U $USER redsocks 2>/dev/null sleep 1 cd $REDSOCKS_DIR && $REDSOCKS if [ $? -eq 0 ]; then echo Done else echo Error fi exit 0; elif [ "$1" != "iptables" ]; then exit 0 fi $IPTABLES -t nat -D PREROUTING -p tcp -j REDSOCKS_FILTER 2>/dev/null $IPTABLES -t nat -D OUTPUT -p tcp -j REDSOCKS_FILTER 2>/dev/null $IPTABLES -t nat -F REDSOCKS_FILTER 2>/dev/null $IPTABLES -t nat -X REDSOCKS_FILTER 2>/dev/null $IPTABLES -t nat -F REDSOCKS 2>/dev/null $IPTABLES -t nat -X REDSOCKS 2>/dev/null # Create our own chain $IPTABLES -t nat -N REDSOCKS $IPTABLES -t nat -N REDSOCKS_FILTER # Do not try to redirect local traffic $IPTABLES -t nat -I REDSOCKS_FILTER -o lo -j RETURN ### 以下是iptables策略配置,包括白名单和黑名单,默认开启白名单 ### 你应该至少修改一行配置,也很简单 ## Do not redirect LAN traffic and some other reserved addresses. (blacklist option) # 黑名单选项:指定的分组通过默认路由转发,其它的都转向SOCKS代理 $IPTABLES -t nat -A REDSOCKS_FILTER -d 0.0.0.0/8 -j RETURN $IPTABLES -t nat -A REDSOCKS_FILTER -d 127.0.0.0/8 -j RETURN $IPTABLES -t nat -A REDSOCKS_FILTER -d 169.254.0.0/16 -j RETURN $IPTABLES -t nat -A REDSOCKS_FILTER -d 172.16.0.0/12 -j RETURN $IPTABLES -t nat -A REDSOCKS_FILTER -d 192.168.0.0/16 -j RETURN $IPTABLES -t nat -A REDSOCKS_FILTER -j REDSOCKS # Redirect all traffic that gets to the end of our chain # 将未指定的转发到SOCKS代理,实际上不会用到 $IPTABLES -t nat -A REDSOCKS -p tcp -j REDIRECT --to-port $REDSOCKS_PORT ## Filter all traffic from the own host # 将分组转到REDSOCKS_FILTER,以保证对分组的区分处理,作用于本机 ## BE CAREFULL HERE IF THE SOCKS-SERVER RUNS ON THIS MACHINE $IPTABLES -t nat -A OUTPUT -p tcp -j REDSOCKS_FILTER # Filter all traffic that is routed over this host # 将分组转到REDSOCKS_FILTER,以保证对分组的区分处理,作用于子网 $IPTABLES -t nat -A PREROUTING -p tcp -j REDSOCKS_FILTER echo IPtables reconfigured.
打开redsocks
./tp-socks.sh加载iptables策略
sudo ./tpsocks.sh iptables好了,现在就可以傲视学校的网络中心了。
PS: 因为redsocks 不能把 dns 在远端解析,所以还要在 主机上做个 dns 转发,要安装 bind9 软件包。在 /etc/bind/named.conf 里的 options 字段里加入:
重启 named 服务就行了.forwarders { 8.8.8.8; }
forward only;
(
- iptables -t nat -N REDSOCKS
- iptables -t nat -A PREROUTING -i br-lan -p tcp -j REDSOCKS
- iptables -t nat -A PREROUTING -i br-lan -p udp -j REDSOCKS
- # Do not redirect traffic to the followign address ranges
- iptables -t nat -A REDSOCKS -d 127.0.0.0/8 -j RETURN
- iptables -t nat -A REDSOCKS -d 192.168.0.0/16 -j RETURN
- iptables -t nat -A REDSOCKS -d 10.8.0.0/16 -j RETURN
- iptables -t nat -A REDSOCKS -d 224.0.0.0/4 -j RETURN
- iptables -t nat -A REDSOCKS -d 240.0.0.0/4 -j RETURN
- # Redirect all kinds of traffic
- iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-ports 1081
- iptables -t nat -A REDSOCKS -p udp -j REDIRECT --to-ports 1081)
(https://gist.github.com/afriza/1097210/796886ba50978ca2426d7bedafd61076858b2c64)
--------------------------------------------------------------利用 redsocks 做强制的系统级全局代理
本文介绍利用 redsocks 做强制的系统级全局代理,在Ubuntu 以及Android 系统上实现SSH翻墙。Ubuntu 上可以设置系统的网络代理,但是这只是一个系统配置,是否使用这些配置还是由应用层决定(如Firefox可以选择 “直接连接” 或者”使用系统代理设置”)。强制80、443端口走代理,可以省去应用层设置,这种方案可以用于 android 系统翻墙。本文主要介绍的PC上的安装方法,但是这种方案对于普通PC用户而言,对比简单ssh socks代理并无优势;本文的目的主要是为Android 系统翻墙提供一个可行的方案。
可以认为本文是 @bjin 的 redsocks on android指南(墙 外) 的补充(我建议大家先看一遍原文再回来),针对原文中提到的 redsocks 方案中 dns解析问题,提供一个解决方法,并能够在PC上进行演示。我认为redsocks 方案中,不论何种代理类型,dns解析问题都会存在,因为是透明代理,dns解析都会在浏览器上直接进行。另外,“国内干净无污染的dns”很难找,不会 有公共的,只能自己搭建一个。此前我特地考察了下 v2ex dns,是放在linode 上的,也不行(dns污染的原理,大概是监控所有出国的dns请求,如果发现“非法”域名的dns,gfw会抢先返回一个错误的结果,所以仅仅换用国外的 dns 不能解决问题。详情参考:深入理解GFW:DNS污染, 墙外)
安装 redsocks
首先说下 redsocks 以及 iptables 安装、配置。编译 redsocks 过程很无痛的,仅仅依赖于 libevent:
mkdir /opt/src cd /opt/src git clone https://github.com/bjin/redsocks cd redsocks sudo apt-get install libevent make控制脚本(参考原文做了些改动)redsocks.sh:
#! /bin/sh case "$1" in start|"") cd /opt/src/redsocks if [ -e redsocks.log ] ; then rm redsocks.log fi ./redsocks -p /opt/src/redsocks/redsocks.pid #set daemon = on in config file # start redirection iptables -t nat -A OUTPUT -p tcp --dport 80 -j REDIRECT --to 12345 iptables -t nat -A OUTPUT -p tcp --dport 443 -j REDIRECT --to 12345 ;; stop) cd /opt/src/redsocks if [ -e redsocks.pid ]; then kill `cat redsocks.pid` rm redsocks.pid else echo already killed, anyway, I will try killall killall -9 redsocks fi # stop redirection iptables -t nat -F OUTPUT ;; start_ssh) ssh -NfD 1234 user@example.cc #TODO: change it!!! ;; stop_ssh) ps aux|grep "ssh -NfD 1234"|awk '{print $2}'|xargs kill ;; clean_dns) iptables -A INPUT -p udp --sport 53 -m state --state ESTABLISHED -m gfw -j DROP -m comment --comment "drop gfw dns hijacks" *) echo "Usage: redsocks start|stop|start_ssh|stop_ssh|clean_dns" >&2 exit 3 ;; esac配置文件 redsocks.conf:
base { // debug: connection progress & client list on SIGUSR1 log_debug = on; // info: start and end of client session log_info = on; /* possible `log' values are: * stderr * file:/path/to/file * syslog:FACILITY facility is any of "daemon", "local0"..."local7" */ log = stderr; // detach from console daemon = on; /* Change uid, gid and root directory, these options require root * privilegies on startup. * Note, your chroot may requre /etc/localtime if you write log to syslog. * Log is opened before chroot & uid changing. */ // user = nobody; // group = nobody; // chroot = "/var/chroot"; /* possible `redirector' values are: * iptables - for Linux * ipf - for FreeBSD * pf - for OpenBSD * generic - some generic redirector that MAY work */ redirector = iptables; } redsocks { /* `local_ip' defaults to 127.0.0.1 for security reasons, * use 0.0.0.0 if you want to listen on every interface. * `local_*' are used as port to redirect to. */ local_ip = 127.0.0.1; local_port = 12345; // `ip' and `port' are IP and tcp-port of proxy-server ip = 127.0.0.1; port = 1234; // known types: socks4, socks5, http-connect, http-relay type = socks5; }启动:
./redsocks.sh start_ssh ./redsocks.sh start关闭:
./redsocks.sh stop ./redsocks.sh stop_sshstart_ssh 和 stop_ssh 两个动作是相对独立的,用于开关代理。只需要保证 redsocks 运行时,代理正常工作即可。android 手机上,ssh 隧道应该可以直接用 connnectbot 建立。
安装西厢的反DNS污染模块
redsocks 启动之后上国内网站基本没问题了。但是 twitter 可能还上不去。原因在于浏览器不知道你设置了代理,于是自己去解析dns,而dns解析用的53端口没有通过代理。因此我们还需要把dns 的问题解决掉。
目前来看,西厢计划中的反dns污染模块还是可以工作的。我们装上这个模块给装上,就不需要自己架无污染的dns服务器,绿色环保。
PC安装西厢,我今天自己做了一次,参考我的tumblr:记录一下Ubuntu 10.04 上西厢的安装过程。
我把开启的命令也写在上面的控制脚本里了:./redsocks.sh clean_dns 。这一步建议做成开机自启动,因为一直开着也没坏处。
scholarzhang 项目的android 移植,参考 @tewilove 的 liujinyuan 项目,以及 @tewilove 这篇总结(为避免一些可能的麻烦,我把内容复制出来了;tumblr 上有原文链接)。另外,我希望能有人把西厢中的反dns污染代码单独抽离出来,放在github上单独维护,或许能简化安装过程,有志于参与开发的同学们也更容易理解。
from http://liruqi.wordpress.com/2011/04/07/redsocks-for-global-proxy/
相关帖子: http://briteming.blogspot.co.uk/2012/02/redirect-all-tcp-traffic-through.html
---------------------------------------
Linux桌面系统里的局部代理转换为全局代理教程
只要你有本地的socks代理(比如ssh tunnel,shadowsocks),人人都可以拥有VPN!
现在教程开始:
1. 安装编译dnsforwarder:
https://github.com/holmium/
下载解压以后,把dnsforwarder放到home主目录:
cd dnsforwarder-5
apt-get install libcurl4-nss-dev libssl-dev
./configure && make &&make install
cp default.config ../config
cd ..
gedit config
做如下修改:
PrimaryServer TCP
即TCP解析优先.
2. 将本机DNS彻底指向127.0.0.1
为什么说是彻底指向127.0.0.1,因为仅仅修改/etc/
方法如下:
gedit /etc/resolvconf/resolv.conf.d/
添加 :nameserver 127.0.0.1
sudo /etc/init.d/resolvconf reload
gedit /etc/NetworkManager/
把dnsmasq注释掉改成:#dnsmasq
gedit /etc/network/interfaces
添加 :nameserver 127.0.0.1
gedit /etc/dhcp/dhclient.conf
把这一行的注释去掉:#prepend domain-name-servers 127.0.0.1;
改成prepend domain-name-servers 127.0.0.1;
sudo service network-manager restart
至此,本机DNS彻底指向了127.0.0.1了
3. 安装配置redsocks:
apt-get install redsocks
gedit /etc/redsocks.conf
把里面的内容删光,把下面的内容粘贴进去:
base {
log_debug = off;
log_info = on;
log = "file:/tmp/redsocks.log";
daemon = on;
redirector = iptables;
}
redsocks {
local_ip = 127.0.0.1;
local_port = 12345;
ip = 127.0.0.1;
port = 1080;
type = socks5;
}
存盘退出。
运行redsocks .
:~$ ps aux|grep redsocks
root 736 1 0 19:43 ? 00:00:10 /usr/sbin/redsocks -c /etc/redsocks.conf
gary 4067 3997 0 20:38 pts/19 00:00:00 grep --color=auto redsocks
后台进程已经运行
4.创建iptables规则:
在主目录下新建一个iptables.sh文档:
gedit iptables.sh
把下面的内容粘贴进去:
sudo iptables -t nat -A OUTPUT -d 0.0.0.0/8 -j RETURN
sudo iptables -t nat -A OUTPUT -d 10.0.0.0/8 -j RETURN
sudo iptables -t nat -A OUTPUT -d 127.0.0.0/8 -j RETURN
sudo iptables -t nat -A OUTPUT -d 169.254.0.0/16 -j RETURN
sudo iptables -t nat -A OUTPUT -d 172.16.0.0/12 -j RETURN
sudo iptables -t nat -A OUTPUT -d 172.17.0.0/12 -j RETURN
sudo iptables -t nat -A OUTPUT -d 192.168.0.0/16 -j RETURN
sudo iptables -t nat -A OUTPUT -d 224.0.0.0/4 -j RETURN
sudo iptables -t nat -A OUTPUT -d 240.0.0.0/4 -j RETURN
sudo iptables -t nat -A OUTPUT -d VPS_IP -j RETURN
sudo iptables -t nat -A OUTPUT -p tcp -j REDIRECT --to-ports 12345
把里面的vps_ip替换成你自己的VPS 的IP地址(
chmod +x iptables.sh
5. 验证redsocks+iptables+
我总结的口诀就是SDI, 即启动顺序是:SS=>dnsforwarder=>
为什么没有redsocks?
开始验证:
依次运行:
sslocal -c ss配置文件名.json -d start (以dameon后台运行方式)
dnsforwarder -d -f config
./iptables.sh
验证:
curl google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.co.jp/
</BODY></HTML>
打开chrome,采用直连方式访问google,
然后在终端直接运行tor,发现tor瞬间就能boot strap到100%! 连前置都不用!
验证成功!
为了让操作简单,建立一个批处理:
gedit vpn.sh
内容如下:
sslocal -c SS配置文件名.json -d start
dnsforwarder -d -f config
./iptables.sh
chmod +x vpn.sh
每次开机只要运行./vpn.sh,全局代理就开启了:
/gary# ./vpn.sh
INFO: loading config from vultr.json
2016-02-20 19:44:07 INFO loading libsodium from libsodium.so.18
started
DNSforwarder mainly by holmium. Version 5.0.29 . License : GPL v3.
Time of compilation : Feb 20 2016 12:59:24.
Please run `dnsforwarder -p' if something goes wrong.
Configure File : config
deamon process pid : 1849
root@gary-GA-MA770T-UD3P:/
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.co.jp/
</BODY></HTML>
另一种方法是用dnscrypt-proxy :
下载 https://download.dnscrypt.org/
解压以后,用./configure && make && make install 安装
使用方法:dnscrypt-proxy -d -R dnsserver-name
在/usr/local/share/dnscrypt-
dnsserver-name的列表:
同样可以建立一个批处理:
gedit vpn2.sh
内容如下:
sslocal -c SS配置文件名.json -d start
dnscrypt-proxy -d -R dnsserver-name
./iptables.sh
chmod +x vpn2.sh
每次开机只要运行./vpn2.sh,全局代理就开启了:
为什么抗DNS污染是本教程的关键:
因为:在redsocks+
原因也在于redsocks不能支持UDP协议的转发。
1. 采用TCP协议解析DNS
2. DNS加密
第三种方法有人说它是UDP转发,我没试过,至少前端代理:
这种方案对于软件的要求太高,可靠性值得怀疑,不推荐。
最后多说一句,这种方法并不是真的VPN,因为没有tun-
好处:可以方便无法调用代理的程序使用: 比如docker,bash终端等,如今搭建蓝灯编译环境,
另外更新ubuntu的软件源也变得非常容易了,
其他像git clone 都非常快,再也不用proxychains了
至此教程完成。
update:
UDP转发的全局代理方法: 经过反复测试,SS原生支持UDP转发,因此不需要第三方抗DNS污染软件也能通过SS的UDP转发功能实现正确解析DNS。
方法如下:
redsocks.conf:
base {
log_debug = off;
log_info = on;
log = "file:/tmp/reddi.log";
daemon = on;
redirector = iptables;
}
redsocks {
local_ip = 127.0.0.1;
local_port = 12345;
ip = 127.0.0.1;
port = 1080;
type = socks5;}
redudp {
local_ip = 127.0.0.1;
local_port = 58;
ip = 127.0.0.1;
port = 1080;
dest_ip = 8.8.8.8;
dest_port = 53;
udp_timeout = 30;
udp_timeout_stream = 180;
}
iptables:
iptables -t nat -A OUTPUT -d 127.0.0.1 -j RETURN
iptables -t nat -A OUTPUT -d 10.0.0.0/8 -j RETURN
iptables -t nat -A OUTPUT -d 172.16.0.0/16 -j RETURN
iptables -t nat -A OUTPUT -d 192.168.0.0/16 -j RETURN
iptables -t nat -A OUTPUT -d 127.0.0.0/8 -j RETURN
iptables -t nat -A OUTPUT -d VPSIP -j RETURN
iptables -t nat -A OUTPUT -p tcp -j REDIRECT --to-ports 12345
iptables -t nat -A OUTPUT -p udp --dport 53 -j DNAT --to 127.0.0.1:58
/etc/resolv.conf 改为 nameserver 8.8.8.8
重启redsocks. service redsocks restart
启动 ss: sslocal -c SS JSON配置文件 -d start
启动新iptables: ./iptables.sh
经过测试,除了dnsforwarder, dnsncrypt也可以使用:
dnscrypt-proxy --d -R cisco
gary # dnscrypt-proxy -R cisco
[INFO] - [cisco] does not support DNS Security Extensions
[WARNING] - [cisco] logs your activity - a different provider might be better a choice if privacy is a concern
[NOTICE] Starting dnscrypt-proxy 1.6.1
[INFO] Generating a new session key pair
[INFO] Done
[INFO] Server certificate #1435874751 received
[INFO] This certificate is valid
[INFO] Chosen certificate #1435874751 is valid from [2015-07-03] to [2016-07-02]
[INFO] Server key fingerprint is ED19:BFBA:FAFC:9257:DFDC:68C7:69BF:AC24:94CD:743F:3C1D:4966:134D:FE2C:4BDC:F315
[NOTICE] Proxying from 127.0.0.1:53 to 208.67.220.220:443
在使用了dnscrypt以后,全局代理的DNS也能正确解析。
所以,如果是这样,理论上:支持dns over TCP或者dns加密的工具的其他软件比如: unbound,bind,pcap_dnsproxy等也应该可以使用。
-------------------------------------------------
Linux桌面系统的「真」全局 HTTP 代理方案
看到 ArchWiki 上 GoAgent 条目的亚全局代理方案,只是设置了代理相关环境变量。我就想,为什么不实现一个真正的全局 HTTP 代理呢?
最终,答案是:Linux 太灵活了,以至于想写一个脚本来搞定很麻烦。不过方案如下,有兴趣的可以折腾折腾。
首先,需要用到的工具:dnsmasq、iptables、redsocks,以及 HTTP 代理工具。dnsmasq 是用来缓存 DNS 请求的,iptables 把 TCP 流转接到 redsocks,而 redsocks 将 TCP 流转接到代理上。
最小 dnsmasq 配置如下:
1
2
3
4
| listen-address=127.0.0.1 cache-size=500 server=127.0.0.1#5353 bogus-nxdomain=127.0.0.1 |
这里使用了本地的 dnscrypt 服务(假设其在 5353 端口上提供服务)。也可以使用国外服务器,只是需要更细致的配置来迫使其走 TCP。
iptables 命令如下:
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
| # 创建一个叫 REDSOCKS 的链,查看和删除的时候方便 iptables -t nat -N REDSOCKS # 所有输出的数据都使用此链 iptables -t nat -A OUTPUT -j REDSOCKS # 代理自己不要再被重定向,按自己的需求调整/添加。一定不要弄错,否则会造成死循环的 iptables -t nat -I REDSOCKS -m owner --uid-owner redsocks -j RETURN iptables -t nat -I REDSOCKS -m owner --uid-owner goagent -j RETURN iptables -t nat -I REDSOCKS -m owner --uid-owner dnscrypt -j RETURN # 局域网不要代理 iptables -t nat -A REDSOCKS -d 0.0.0.0 /8 -j RETURN iptables -t nat -A REDSOCKS -d 10.0.0.0 /8 -j RETURN iptables -t nat -A REDSOCKS -d 169.254.0.0 /16 -j RETURN iptables -t nat -A REDSOCKS -d 172.16.0.0 /12 -j RETURN iptables -t nat -A REDSOCKS -d 192.168.0.0 /16 -j RETURN iptables -t nat -A REDSOCKS -d 224.0.0.0 /4 -j RETURN iptables -t nat -A REDSOCKS -d 240.0.0.0 /4 -j RETURN # HTTP 和 HTTPS 转到 redsocks iptables -t nat -A REDSOCKS -p tcp --dport 80 -j REDIRECT --to-ports $HTTP_PORT iptables -t nat -A REDSOCKS -p tcp --dport 443 -j REDIRECT --to-ports $HTTPS_PORT # 如果使用国外代理的话,走 UDP 的 DNS 请求转到 redsocks,redsocks 会让其使用 TCP 重试 iptables -t nat -A REDSOCKS -p udp --dport 53 -j REDIRECT --to-ports $DNS_PORT # 如果走 TCP 的 DNS 请求也需要代理的话,使用下边这句。一般不需要 iptables -t nat -A REDSOCKS -p tcp --dport 53 -j REDIRECT --to-ports $HTTPS_PORT |
redsocks 的配置:
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
| base { log_debug = off; log_info = off; daemon = on; redirector = iptables; } // 处理 HTTP 请求 redsocks { local_ip = 127.0.0.1; local_port = $HTTP_PORT; ip = $HTTP_PROXY_IP; port = $HTTP_PROXY_PORT; type = http-relay; } // 处理 HTTPS 请求,需要一个支持 HTTP CONNECT 的代理服务器,或者 socks 代理服务器 redsocks { local_ip = 127.0.0.1; local_port = $HTTPS_PORT; ip = $SSL_PROXY_IP; port = $SSL_PROXY_PORT; type = http-connect; // or socks4, socks5 } // 回应 UDP DNS 请求,告诉其需要使用 TCP 协议重试 dnstc { local_ip = 127.0.0.1; local_port = $DNS_PORT; } |
然后以相应的用户和配置文件启动 dnsmasq 以及 redsocks。修改
/etc/resolv.conf
:
1
| nameserver 127.0.0.1 |
至于分流的事情,HTTP 部分可以交给 privoxy,但是 HTTPS 部分不好办。可以再设立一个像 GoAgent 那样的中间人型 HTTPS 代理,或者更简单地,直接根据 IP 地址,国内的直接
RETURN
掉。
以上就是整个方案了。有些麻烦而我又不需要所以没测试。反正就是这个意思。Android 软件 GAEProxy 就是这么干的(不过它没使用 iptables 的 owner 模块,导致我不小心弄出了死循环)。另外,BSD 系统也可以使用类似的方案。
from http://lilydjwg.is-programmer.com/2014/2/7/linux-really-global-http-proxy.42701.html
其评论:
HTTPS 请求试试看 sniproxy。
現在在用修改路由表的方案,把apnic上國內ip都走正常網關,default是vpn。還要一個遞歸解析並緩存的dns client……很需要一個 proxychains 這樣的工具,但能更方便的設置代理。
之前一直不会设置iptables,老是设置好了就无法打开网页,估计是死循环了,iptables -t nat -I REDSOCKS -m owner --uid-owner root -j RETURN加了root之后就好了。
用Android NDK编译了redsocks2,方法和编译redsocks一样,redsocks我自己编译好能用,redsocks2的网址:https://github.com/semigodking/redsocks,应该是一个作者的,你看看呢,大概已经分析出原因了,就是redsocks2编译出来时调用动态库,而redsocks编译后是调用静态库,用file命令就能看出来,不知道怎么把redsocks2也编译成静态库的? answer:这个项目用的是 Makefile,你加 CFLAGS=-static 环境变量试试。 NDK 编译出来的动态链接版本应该一样好用的呀,只要把相应的库都放到系统上即可。 把提示出错的那个库放到手机里,然后就可以用了,正常使用了.因为刚学linux,对很多都不是太懂,只能依靠教程,网上这个软件的教程太少了,不知道redsocks与ProxyDroid有什么区别,貌似ProxyDroid这个也是伪全局代理在android上,查了资料说:redsocks能在安卓上实现全局代理,就试试编译了。
你可以试试 proxytunnel。它会把 HTTP CONNECT proxy 映射为标准输入输出。
可以配合 socat 用。
---------------------------------
用redsocks设置linux桌面系统的全局代理
1. 优点: 设置一次,全局代理。连虚拟机都不要设置代理。
2. 缺点: 会停止工作,我把它放到crontab里。
1. 编译redsocks https://github.com/darkk/redsocks.
2. proxy.sh
iptables -t nat -N REDSOCKS || true
iptables -t nat -F REDSOCKS
iptables -t nat -A REDSOCKS -d 0.0.0.0/8 -j RETURN
iptables -t nat -A REDSOCKS -d 10.0.0.0/8 -j RETURN
iptables -t nat -A REDSOCKS -d 127.0.0.0/8 -j RETURN
iptables -t nat -A REDSOCKS -d 169.254.0.0/16 -j RETURN
iptables -t nat -A REDSOCKS -d 172.16.0.0/12 -j RETURN
iptables -t nat -A REDSOCKS -d 192.168.0.0/16 -j RETURN
iptables -t nat -A REDSOCKS -d 224.0.0.0/4 -j RETURN
iptables -t nat -A REDSOCKS -d 240.0.0.0/4 -j RETURN
iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-ports 6666
iptables -t nat -A REDSOCKS -p udp -j REDIRECT --to-ports 8888
iptables -t nat -A OUTPUT -p tcp -j REDSOCKS
pkill redsocks
/root/bin/redsocks -c /root/bin/redsocks.conf
3. redsocks.conf
base {
log_debug = on;
log_info = on;
daemon = on;
redirector = iptables;
}
redsocks {
local_ip = 127.0.0.1;
local_port = 6666;
ip = 10.102.248.16; <---- proxy server IP.
port = 1080;
type = socks5;
}
redudp {
local_ip = 127.0.0.1;
local_port = 8888;
ip = 10.102.248.16;
port = 1080;
}
dnstc {
local_ip = 127.0.0.1;
local_port = 5300;
}
Ubuntu 下服务开机自启动。
Ubuntu 下iptables 开机自启动
http://www.cnblogs.com/tonybuaa/archive/2013/06/01/3112666.html
----------------------------------------------------------------------
相关帖子:
http://briteming.blogspot.com/2016/02/redsocks.html
http://briteming.blogspot.com/2012/02/redirect-all-tcp-traffic-through.html
-----------------------------------------------------------------------------------------------------
redsocks on android指南
redsocks是一个通用的proxy redirector,能实现系统级别的全局代理(HTTP或者socks),从而可以实现HTTP代理或者SSH翻墙。
大概的原理是用iptables把外向的包重定向到本地redsocks开的端口,然后redsocks可以从tcp/ip头里得到包的目标ip的端口,然后就通过代理建立连接。
由于iptables的语法非常灵活,可以非常有效地控制哪些包走代理(国内国外,端口,进程,甚至是Level7的协议种类),但是这个方案也有一个比较致命的缺点。
由于redsocks并非针对http协议,所以只能得到目标地址的ip而不是域名,这样的话在建立连接之前需要有一次dns询问。 这个问题在ssh的情况下是不存在的,因为ssh采用的socks5提供了dns的代理,同时redsocks也支持udp,在config文件里多加一个udp就可以了。 但是在使用http代理的情况下,这个问题必须通过一个干净无污染的dns解决,而且显然,这个dns服务器必须在国内。否则就必须手工维护一个很大的hosts文件了。
cross compile ARM下的redsocks
先下载codesourcery.com提供的gcc arm toolchain,不嫌麻烦也可以自己做一个
$ wget http://www.codesourcery.com/sgpp/lite/arm/portal/package7851/public/arm-none-linux-gnueabi/arm-2010.09-50-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2 $ md5sum arm-2010.09-50-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2 f9dbd7a2daf20724e013cc4b5b64d62f arm-2010.09-50-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2 $ tar jxf arm-2010.09-50-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2 $ export PATH=$PATH:`pwd`/arm-2010.09/bin
下载并交叉编译libevent2
$ wget http://monkey.org/~provos/libevent-2.0.10-stable.tar.gz $ md5sum libevent-2.0.10-stable.tar.gz a37401d26cbbf28185211d582741a3d4 libevent-2.0.10-stable.tar.gz $ tar zxf libevent-2.0.10-stable.tar.gz $ cd libevent-2.0.10-stable $ ./configure --host=arm-none-linux-gnueabi $ make $ cp .libs/libevent.a .. $ DESTDIR=/tmp make install $ cd ..
用生成的
libevent.a
编译redsocks$ git clone -b libevent2-fix https://github.com/bjin/redsocks $ cd redsocks $ sed -i 's/-levent/..\/libevent.a -lrt/g' Makefile $ CC=arm-none-linux-gnueabi-gcc CFLAGS="-O4 -static -I /tmp/usr/local/include" make $ file redsocks redsocks: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, for GNU/Linux 2.6.16, not stripped
手机端的配置
先说下手机端的要求,必须有iptables,这个在CyanogenMod里是自带的,否则可以从网上下一个。另外显然需要有root权限,最好配合SuperUser这个app。
下面的例子是以比较简单的http代理为例,ssh的情况复杂些,需要额外用openssh建立隧道
$ cat redsocks.conf # redsocks的配置文件 base { log_debug = on; log_info = on; log = "file:/data/redsocks/redsocks.log"; daemon = on; // 在后台运行 redirector = iptables; } redsocks { local_ip = 127.0.0.1; local_port = 12345; ip = <PROXY IP>; port = <PROXY PORT>; type = http-connect; // HTTP CONNECT,一般用于HTTPS login = "<PROXY USER>"; // 可选 password = "<PROXY PASS>"; // 可选 } redsocks { local_ip = 127.0.0.1; local_port = 54321; ip = <PROXY IP>; port = <PROXY PORT>; type = http-relay; // 一般的HTTP 代理 login = "<PROXY USER>"; // 可选 password = "<PROXY PASS>"; // 可选 } $ cat start_re.sh # 启动iptables重定向的脚本 /system/bin/iptables -t nat -A OUTPUT -p tcp --dport 80 -j REDIRECT --to 54321 /system/bin/iptables -t nat -A OUTPUT -p tcp --dport 443 -j REDIRECT --to 12345 /system/bin/iptables -t nat -A OUTPUT -p udp --dport 53 -j DNAT --to-destination x.x.x.x:53 #用iptables重定向dns包,x.x.x.x必须是国内干净的dns服务器 $ cat start.sh # 启动redsocks的脚本 cd /data/redsocks if [ -e redsocks.log ] ; then rm redsocks.log fi ./redsocks -p /data/redsocks/redsocks.pid $ cat stop_re.sh # 关闭iptables重定向的脚本 /system/bin/iptables -t nat -F OUTPUT $ cat stop.sh # 关闭redsocks的脚本 cd /data/redsocks if [ -e redsocks.pid ]; then kill `cat redsocks.pid` rm redsocks.pid else echo already killed, anyway, I will try killall killall -9 redsocks fi $ adb shell mkdir /data/redsocks $ for file in `ls *.sh` redsocks redsocks.conf; do adb push $file /data/redsocks/; done
最后建议采用Scripter运行脚本,直接调用
/data/redsocks/xxx.sh
就可以了,全局建议开着redsocks
,想翻墙就start_re.sh
,想关闭就stop_re.sh
from http://web.archive.org/web/20130311083448/http://bjin.posterous.com/
------------------------------
强制Android应用通过网络代理进行通信
在对Android应用进行安全性测试的时候,因为大多数应用不提供代理设置,所以对其进行抓包分析比较麻烦。一种可行的方法是使用iptables将数据重定向到本地代理的监听端口,由本地代理再转发到我们的分析工具上。如果是HTTP/HTTPS通信的话,可以实现与WEB安全测试相同的效果。
本地代理工具可以使用redsocks, 编译方式详见:http://bjin.posterous.com/redsocks-on-android
由于一些特殊原因,有些童鞋可能看不了链接,已经编译好的程序下载链接如下:http://download.csdn.net/detail/white_eyes/4765277
编写如下脚本文件(假设将程序和文件准备放到/data/redsocks下)
redsocks.conf
start.sh
start_re.sh
stop.sh
stop_re.sh
将redsocks和脚本文件放到手机的data/redsocks目录下,用终端启动即可。
from http://blog.csdn.net/white_eyes/article/details/8181946
-------------------
相关帖子:
http://briteming.blogspot.com/2016/02/redsocks.html---------
将SOCKS代理可路由化
与VPN相比,SSH动态转发只是一个SOCKS代 理,不如VPN那种建立一个点到点连接的方式处理方便。首先,VPN连接可以在路由表中配置,不需要软件支持和显式指定;其次,因为VPN的“连接”性 质,用户可以灵活地配置流量的走向,比如访问Chinternet用原本的连接,访问Internet用VPN连接。当然,SOCKS代理的优势也是显而 易见的,只要有一个SSH账号,即可建立隧道,一般来说,要比VPN的花费要少。在后续的文章中,将介绍在普通的无SSH支持的PHP主机上实现穿透代理。
它的应用场景如下:
1. 透明代理,即可不指定代理而自动使用SOCKS代理转发。换句话说,把代理配置在了应用层以下
2. 共享代理,即把只能本地访问的SOCKS代理(SSH隧道)转换成可共享的。也就是不透露SSH账号且只建立一次隧道,即可供多人使用。
3. 转发规则自定义,即可将分组分类(比如按目的地址分类)并配置转发。
4. 其它。
redsocks
用过squid的读者应该知道,用squid和iptables来实现上面的需求很简单,首先在服务端squid.conf中的port选项后增加“transparent”字段,然后将相应请求用iptables redirect到端口即可。
iptables -t nat -A PREROUTING -p tcp --dport 80 -d xx.xx.xx.xx -j REDIRECT --to-port 3128
但SSH建立的socks代理不具备这样的功能,只能通过第三方的软件来扩展。 本文介绍一款名为redsocks的软件。redsocks是我目前发现在这方面最好的软件。
redsocks可以在http://darkk.net.ru/redsocks/下载.
redsocks的官方说明:
This tool allows you to redirect any TCP connection to SOCKS or HTTPS
proxy using your firewall, so redirection is system-wide.
Why is that useful? I can suggest following reasons:
* you use tor[1] and don't want any TCP connection to leak.
* you use DVB ISP and this ISP provides internet connectivity with some
special daemon that may be also called "Internet accelerator" and this
accelerator acts as proxy. Globax[2] is example of such an accelerator.
Linux/iptables, OpenBSD/pf and FreeBSD/ipfw are supported.
Linux/iptables is well-tested, other implementations may have bugs,
your bugreports are welcome.
Transocks[3] is alike project but it has noticable performance penality.
Transsocks_ev[4] is alike project too, but it has no HTTPS-proxy support
and does not support authentication.
redsocks的安装
1. 安装libevent组件
libevent的官方页面:http://www.monkey.org/~provos/libevent/
值得注意的是,发行版源仓库中的libevent一般的版本较低,在编译redsocks时会失败。Debian/Ubuntu所需的libevent可以从此下载:http://ftp.de.debian.org/debian/pool/main/libe/libevent/
2. 编译redsocks
解压、make
这个应该不需要详细介绍了
3. 编辑配置文件
在redsocks目录下建立配置文件redsocks.conf。内容如下:
base {
log_debug = on;
log_info = on;
//日志文件,调试时可指定为标准错误"stderr"
log = "file:/home/lige/soft/redsocks/socks.log";
// 是否以后台模式运行
daemon = on
redirector = iptables;
}
redsocks {
//local_ip设置为0.0.0.0则可共享,设备为127.0.0.1则只能在本机使用
local_ip = 0.0.0.0
local_port = 12345;
// 本来有的代理的IP和端口,可能是由ssh -D指定的
ip = 127.0.0.1;
port = 1080;
// known types: socks4, socks5, http-connect, http-relay
type = socks5;
}
4. iptables策略
新建一个脚本tp-socks.sh,用来运行redsocks和生成iptables策略.
脚本摘自http://przemoc.net/tips/linux
#!/bin/sh
# iptables路径
IPTABLES="/usr/local/sbin/iptables"
# redsocks路径
REDSOCKS_DIR="/home/lili/soft/redsocks"
REDSOCKS="$REDSOCKS_DIR/redsocks"
# 配置文件中指定的端口
REDSOCKS_PORT="12345"
# socks代理IP和端口
SOCKS_HOST="127.0.0.1"
SOCKS_PORT="1080"
# 运行redsocks
if [ "$USER" != "root" ]; then
echo -n 'Restarting redsocks... '
pkill -U $USER redsocks 2>/dev/null
sleep 1
cd $REDSOCKS_DIR && $REDSOCKS
if [ $? -eq 0 ]; then
echo Done
else
echo Error
fi
exit 0;
elif [ "$1" != "iptables" ]; then
exit 0
fi
$IPTABLES -t nat -D PREROUTING -p tcp -j REDSOCKS_FILTER 2>/dev/null
$IPTABLES -t nat -D OUTPUT -p tcp -j REDSOCKS_FILTER 2>/dev/null
$IPTABLES -t nat -F REDSOCKS_FILTER 2>/dev/null
$IPTABLES -t nat -X REDSOCKS_FILTER 2>/dev/null
$IPTABLES -t nat -F REDSOCKS 2>/dev/null
$IPTABLES -t nat -X REDSOCKS 2>/dev/null
# Create our own chain
$IPTABLES -t nat -N REDSOCKS
$IPTABLES -t nat -N REDSOCKS_FILTER
# Do not try to redirect local traffic
$IPTABLES -t nat -I REDSOCKS_FILTER -o lo -j RETURN
### 以下是iptables策略配置,包括白名单和黑名单,默认开启白名单
### 你应该至少修改一行配置,也很简单
# Redirect only specified addresses and do not try redirect other traffic. (whitelist option)
# 白名单选项:只重定向指定的分组到SOCKS代理,其它的都按默认路由转发
$IPTABLES -t nat -A REDSOCKS_FILTER -m iprange --dst-range 192.168.0.10-192.168.0.30 -j REDSOCKS
$IPTABLES -t nat -A REDSOCKS_FILTER -d 126.0.0.0/8 -j REDSOCKS
$IPTABLES -t nat -A REDSOCKS_FILTER -j RETURN
## Do not redirect LAN traffic and some other reserved addresses. (blacklist option)
# 黑名单选项:指定的分组通过默认路由转发,其它的都转向SOCKS代理
#$IPTABLES -t nat -A REDSOCKS_FILTER -d 0.0.0.0/8 -j RETURN
#$IPTABLES -t nat -A REDSOCKS_FILTER -d 10.0.0.0/8 -j RETURN
#$IPTABLES -t nat -A REDSOCKS_FILTER -d 127.0.0.0/8 -j RETURN
#$IPTABLES -t nat -A REDSOCKS_FILTER -d 169.254.0.0/16 -j RETURN
#$IPTABLES -t nat -A REDSOCKS_FILTER -d 172.16.0.0/12 -j RETURN
#$IPTABLES -t nat -A REDSOCKS_FILTER -d 192.168.0.0/16 -j RETURN
#$IPTABLES -t nat -A REDSOCKS_FILTER -d 224.0.0.0/4 -j RETURN
#$IPTABLES -t nat -A REDSOCKS_FILTER -d 240.0.0.0/4 -j RETURN
#$IPTABLES -t nat -A REDSOCKS_FILTER -j REDSOCKS
## Do not redirect traffic for the SOCKS-Server
## Not needed if server is not on a whitelist or is already blacklisted.
# 不转发。实际没什么意义,不需要SOCKS时只需要清空nat表即可
#$IPTABLES -t nat -I REDSOCKS -p tcp -d $SOCKS_HOST --dport $SOCKS_PORT -j RETURN
# Redirect all traffic that gets to the end of our chain
# 将未指定的转发到SOCKS代理,实际上不会用到
$IPTABLES -t nat -A REDSOCKS -p tcp -j REDIRECT --to-port $REDSOCKS_PORT
## Filter all traffic from the own host
# 将分组转到REDSOCKS_FILTER,以保证对分组的区分处理,作用于本机
## BE CAREFULL HERE IF THE SOCKS-SERVER RUNS ON THIS MACHINE
$IPTABLES -t nat -A OUTPUT -p tcp -j REDSOCKS_FILTER
# Filter all traffic that is routed over this host
# 将分组转到REDSOCKS_FILTER,以保证对分组的区分处理,作用于子网
$IPTABLES -t nat -A PREROUTING -p tcp -j REDSOCKS_FILTER
echo IPtables reconfigured.
一般来说,只需要增加白名单里的条目即可。
5. 运行
打开redsocks
./tp-socks.sh
加载iptables策略
sudo ./tp-socks.sh iptables
实例:自动使用SOCKS代理访问某微博网站
这事儿不能说太细,仅供参考
把这个网站的IP添加到脚本的白名单策略中
$IPTABLES -t nat -A REDSOCKS_FILTER -d xx.xx.xx.xx -j REDSOCKS
但是由于DNS污染,本地解析的IP可能是错误的,而且socks代理的远端DNS解析的方法不适用。解决的办法有2种:
(1) 将DNS解析,即目的端口为53的分组转发到SOCKS代理
(2)在远程计算机上解析IP后,在/etc/hosts中将IP和站点绑定
经过以上步骤,站点成功打开。值得注意的是,这种方法应用于网关时,子网中的主机可以不用更改任何选项访问此站点。这就是透明化的好处之一.
--------
相关帖子:https://briteming.blogspot.com/2016/02/redsocks.html
--------
用 ss-redir 或 redsocks 搭建透明网关
原生的Android刷机后第一次开机系统初始设置要连接谷歌服务器,如果没有透明代理就很麻烦,进不了桌面。这里可以通过一台Linux的机器(我用的是第一代树莓派)来达到透明代理的作用。平常情况下手机,平板和机顶盒也可以通过透明代理上网,简化配置。
环境要求
手机和树莓派在同一个局域网,并且可以相互访问,手机的WiFi连接,树莓派可以是无线网络也可以是网线连接(没有无线网络的第一代树莓派即可)。有可用的SS / SSR或者SOCKS5代理。
用SS-redir的搭建带SS代理的端口
如果是SSR用shadowsocksr-libev里的SS-再导向。注意后者多了一个
r
。我用的是SSR,在树莓派上自己编译的shadowsocksr-libev ,下面以SSR为例,SS类似。
配置ss-redir.json,参数和配置SSR一样,注意:
"local_address":"0.0.0.0"
,这个必须为0.0.0.0。如果同一个机器上也运行SSR,local_port要用不同的端口,如SSR用1081,ss-redir用1088,下面配置里会用到1088这个端口。
运行SS-再导向,
-v
会显示一些日志信息:
调试完成后,实际运行可以用:
启用内核转发
使用iptables的转发需要打开内核的IPv4转发功能,编辑/etc/sysctl.conf中,设置把net.ipv4.ip_forward = 1,让更新实时生效:
设置iptables的,IPSET转发
设置iptables的的目的是将本机特定IP的流量转向β-redir的监听的端口,这个端口是带代理的,这样特定IP的流量就自动代理了。可以配置跳过无需代理的中国IP.iptables执行要根权限,可以切换到根用户,或者用须藤方式运行。
先获取中国IP范围,保存文件是cn.zone。
创建china.ipset脚本,内容如下:
运行脚本创建中国的IPSET,脚本会把cn.zone文件里的IP段都加到中国的IPSET里。
创建的iptables命令脚本
把上面的命令保存成iprules.sh文件,运行设置到系统里。
如果设置错误,清理iptables的设置用下面的命令:
手机端设置
手机端WiFi连接,选择静态IP,网关填写树莓派的IP。如果正常,此时手机不用配置代理即可正常访问Google服务器。
验证国内IP过滤
用redsocks2替代β-redir的
树莓派上本来跑了个SSR,环境太恶劣,经常需要tcping找可用的地址,重启SSR,不想再维护β-再导向的稳定性了,所以切换到redsocks了,redsocks可以直接用SSR提供socks5的代理,只维护SSR稳定可用即可。
没有用原版的redsocks,使用了修改版的redsocks2,下载源代码编译略过。
没有用原版的redsocks,使用了修改版的redsocks2,下载源代码编译略过。
redsocks2的配置config.json如下。如果socks5的代理是本机,
ip = 192.168.1.104;
行改成ip = 0.0.0.0;
。文件配置里log_debug log_info daemon
调试的时候可以根据需要配置上分类中翻译关闭或者,daemon = on
是后台运行。这里redsocks监听的端口也配置成1088。
运行redsocks2
据说redsocks稳定性可能有些问题,配置的cron计划任务,每天凌晨3点重启一下好了。
IPSET设置速度优化
用
sudo bash china.ipset
设置IPSET后,可以把IPSET设置保存起来,后面再恢复速度就快很多。
把下面脚本保存为ipset.restore.sh,每次开机设置一次。
sudo iptables -t nat -F
三条的含义是清空iptables nat配置,不然多次执行ipset会提示正在使用,禁止毁灭。建议:每次应该都是先设置ipset,再设置iptables的。其他说明
- 不支持UDP流量转发,DNS污染用其他方法解决。我用的dnsmasq + overture.dnsmasq做缓存,推动做域名翻墙和国内IP分流.overture国内使用dnspod的DNS服务,国外使用8.8.8.8,这两个DNS服务都支持EDNS。
- iptables的规则可以SH脚本运行,或者iptables-save命令后用的iptables-恢复来加载
参考资料
--------------
使用iptables,ipset的全局智能代理
首先,我这里的全局智能代理实现的功能主要有以下几点:
- 基于iptables的全局代理,在Linux下可以实现只需设置一次,所有的软件(无论是GUI还是CLI)都可以免设置自动使用这个代理。
- 基于ipset,加入所有的国内IP项,对所有tcp流量进行判断,避免对国内的服务进行代理,一方面可以节省服务器流量,另一方面可以享受国内ip的优势,比如国内的版权视频、音乐,最后,避免被国内一些和政府有合作的IT企业发现你使用了代理。
- iptables把需要代理的tcp流量交给shadowsocks-libev中的ss-redir,让它与代理服务器通讯。
让我们开始吧,在最开始,首先要说明的是:这份教程只适用于想要让你的Linux设备使用代理的用户,同时,下文的所有内容假设你运行的是Arch Linux。
设置 iptables
首先,在
/etc/iptables
目录下创建一份新的规则:# Transparent SOCKS proxy
*nat
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
:REDSOCKS - [0:0]
# Redirect all output through redsocks
-A OUTPUT -p tcp -j REDSOCKS
# Whitelist LANs and some other reserved addresses.
# https://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses
-A REDSOCKS -d 0.0.0.0/8 -j RETURN
-A REDSOCKS -d 10.0.0.0/8 -j RETURN
-A REDSOCKS -d 127.0.0.0/8 -j RETURN
-A REDSOCKS -d 169.254.0.0/16 -j RETURN
-A REDSOCKS -d 172.16.0.0/12 -j RETURN
-A REDSOCKS -d 192.168.0.0/16 -j RETURN
-A REDSOCKS -d 224.0.0.0/4 -j RETURN
-A REDSOCKS -d 240.0.0.0/4 -j RETURN
# whitelist China ip.
-A REDSOCKS -p tcp -m set --match-set china dst -j RETURN
# import shadowsocks server ip
-A REDSOCKS -d xxx.xxx.xxx.xxx -j RETURN
# shadowsocks server port
#-A REDSOCKS -p tcp --dport xxxx -j RETURN
# Redirect everything else to redsocks port
-A REDSOCKS -p tcp -j REDIRECT --to-ports 1081
COMMIT
这份配置文件的主要功能是,把所有的tcp流量(不包括一些保留的ip段,包括局域网网段;使用ipset存储的所有中国ip段,关于ipset后文讲;代理服务器自己的ip或端口,这是为了避免代理自己不要再被重定向,端口或ip选择一个就行)
然后把这份规则软链接到
/etc/iptables/iptables.rules
,启动iptables并设置开机启动。sudo systemctl start iptables.service
sudo systemctl enable iptables.service
配置ipset
ipset是 Linux 防火墙iptables的一个伴随工具。它允许你建立规则来轻松愉快地屏蔽一组IP地址。 ipset可以把大量ip或ip段存储在一个set中,实际上这个set是一个高效的查询结构,搜索的逻辑依然在iptables本身,iptables会判断IP地址或者哪个别的什么字段在不在这个set中,判断的过程使用二叉树,哈希等算法因此非常高效。
依次执行一下命令。
# Destroy ipset if it already exists
sudo systemctl stop iptables.service
sudo ipset destroy china
# Create the ipset list
sudo ipset -N china hash:net
# remove any old list that might exist from previous runs of this script
rm cn.zone
# Pull the latest IP set for China
wget -P . http://www.ipdeny.com/ipblocks/data/countries/cn.zone
# Add each IP address from the downloaded list into the ipset 'china'
# for i in $(cat ./cn.zone ); do ipset -A china $i; done
执行完成后,所有中国ip已经通过ipset保存在内存中,可以被iptable调用,参见上文中的iptables规则
-A REDSOCKS -p tcp -m set --match-set china dst -j RETURN
,一旦遇到tcp包,其目标地址为名为china的ipset中的ip,就直接放行。
目前创建的ipse存在于内存中,重启后将会消失。要使ipset持久化,要这样做:
# ipset save > /etc/ipset.conf
同时把ipset设置为开机启动:
sudo systemctl enable ipset.service
配置 ss-redir
在配置文件中设一个未使用的端口,我使用的是1081,在iptables的规则中与之对应
-A REDSOCKS -p tcp -j REDIRECT --to-ports 1081
,然后启动ss-redir:sudo systemctl start shadowsocks-libev-redir@config
sudo systemctl enable shadowsocks-libev-redir@config
这样,就全部完成了。
------------------------------
使用redsocks把shadowsocks转为全局代理
前言
shadowsocks是个非常棒的翻墙代理,它可以在本地提供一个socks5端口供软件使用代理。比如启动chrome的时候加上
--proxy-server="socks5://myproxy:1080"
这条参数就可以让chrome走代理;然而,有些软件不支持设置代理的功能。所以有时候,我们需要一个全局代理的环境,使用VPN是一个通常的选择,不过一般购买的VPN的速度不如shadowsocks,同时,VPN更容易受到GFW的干扰。为什么不把正在使用的shadowsocks利用起来作为一个全局代理呢?
本文介绍了一种利用redsocks与iptables实现全局代理的方法,其基本原理如下:
- iptables的规则将所有tcp包转发到redsocks打开的本地端口
- redsocks接收tcp包并转发给shadowsocks打开的本地socks端口
- shadowsocks将接收的包转发给远端的代理服务器
由于使用了redsocks与iptables,因此本文的方法仅适用于Linux。
安装
由于使用的是Arch Linux,我直接通过AUR安装
$ yaourt redsocks
通过
yaourt
搜索redsocks包,然后选择你要安装的包的序号就可以进行安装了。注意:下文中的配置文件的路径全部基于Arch Linux,如果你用的不是Arch Linux,请根据自己的情况更改。
其他发行版可以通过各自的包管理器或者下载源码安装。
配置
配置redsocks
我在包提供的原有配置的基础上进行了一些修改,主要是把
redsocks -> port
修改为shadowsocks的本地端口(1080),另外,由于只需要转发tcp流量,我把 redudp
和 dnstc
段的配置全部注释了。注意 redsocks -> local_port
配置的端口是用来接收iptables传来的流量用的,设置为一个不会和别的程序冲突的端口,但要保证和iptables规则里的端口对应。$ vim /etc/redsocks.conf
base {
// debug: connection progress & client list on SIGUSR1
log_debug = off;
// info: start and end of client session
log_info = off;
/* possible `log' values are:
* stderr
* "file:/path/to/file"
* syslog:FACILITY facility is any of "daemon", "local0"..."local7"
*/
// log = stderr;
// log = "file:/path/to/file";
log = "syslog:daemon";
// detach from console
daemon = on;
/* Change uid, gid and root directory, these options require root
* privilegies on startup.
* Note, your chroot may requre /etc/localtime if you write log to syslog.
* Log is opened before chroot & uid changing.
*/
user = redsocks;
group = redsocks;
// chroot = "/var/chroot";
/* possible `redirector' values are:
* iptables - for Linux
* ipf - for FreeBSD
* pf - for OpenBSD
* generic - some generic redirector that MAY work
*/
redirector = iptables;
}
redsocks {
/* `local_ip' defaults to 127.0.0.1 for security reasons,
* use 0.0.0.0 if you want to listen on every interface.
* `local_*' are used as port to redirect to.
*/
local_ip = 127.0.0.1;
local_port = 31338;
// listen() queue length. Default value is SOMAXCONN and it should be
// good enough for most of us.
// listenq = 128; // SOMAXCONN equals 128 on my Linux box.
// `max_accept_backoff` is a delay to retry `accept()` after accept
// failure (e.g. due to lack of file descriptors). It's measured in
// milliseconds and maximal value is 65535. `min_accept_backoff` is
// used as initial backoff value and as a damper for `accept() after
// close()` logic.
// min_accept_backoff = 100;
// max_accept_backoff = 60000;
// `ip' and `port' are IP and tcp-port of proxy-server
// You can also use hostname instead of IP, only one (random)
// address of multihomed host will be used.
ip = 127.0.0.1;
// 修改为shadosocks的本地端口
port = 1080;
// known types: socks4, socks5, http-connect, http-relay
type = socks5;
// login = "foobar";
// password = "baz";
}
//redudp {
// `local_ip' should not be 0.0.0.0 as it's also used for outgoing
// packets that are sent as replies - and it should be fixed
// if we want NAT to work properly.
//local_ip = 127.0.0.1;
//local_port = 10053;
// `ip' and `port' of socks5 proxy server.
//ip = 127.0.0.1;
//port = 1080;
// login = username;
// password = pazzw0rd;
// kernel does not give us this information, so we have to duplicate it
// in both iptables rules and configuration file. By the way, you can
// set `local_ip' to 127.45.67.89 if you need more than 65535 ports to
// forward ;-)
// This limitation may be relaxed in future versions using contrack-tools.
//dest_ip = 8.8.8.8;
//dest_port = 53;
//udp_timeout = 30;
//udp_timeout_stream = 180;
//}
//dnstc {
// fake and really dumb DNS server that returns "truncated answer" to
// every query via UDP, RFC-compliant resolver should repeat same query
// via TCP in this case.
// local_ip = 127.0.0.1;
// local_port = 5300;
//}
// you can add more `redsocks' and `redudp' sections if you need.
iptables规则
也是在包提供的原有配置的基础上进行了一些修改,唯一的修改只是增加了这一条
-A REDSOCKS -d proxy_server_ip -j RETURN
,使代理自己不要再被重定向,不然就无限循环了。。$ vim /etc/iptables/redsocks.rules
# Transparent SOCKS proxy
# See: http://darkk.net.ru/redsocks/
*nat
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
:REDSOCKS - [0:0]
# Redirect all output through redsocks
-A OUTPUT -p tcp -j REDSOCKS
# Whitelist LANs and some other reserved addresses.
# https://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses
-A REDSOCKS -d 0.0.0.0/8 -j RETURN
-A REDSOCKS -d 10.0.0.0/8 -j RETURN
-A REDSOCKS -d 127.0.0.0/8 -j RETURN
-A REDSOCKS -d 169.254.0.0/16 -j RETURN
-A REDSOCKS -d 172.16.0.0/12 -j RETURN
-A REDSOCKS -d 192.168.0.0/16 -j RETURN
-A REDSOCKS -d 224.0.0.0/4 -j RETURN
-A REDSOCKS -d 240.0.0.0/4 -j RETURN
# import shadowsocks server ip
#-A REDSOCKS -d xxx.xxx.xxx.xxx -j RETURN
# shadowsocks server port
-A REDSOCKS -p tcp --dport xxxxx -j RETURN
# Redirect everything else to redsocks port
-A REDSOCKS -p tcp -j REDIRECT --to-ports 31338
COMMIT
运行
开启redsocks和iptables服务,并让它们开机自启:
$ sudo systemctl start redsocks.service iptables.service
$ sudo systemctl enable redsocks.service iptables.service
当你需要进入全局代理时,简单地导入
redsocks.rules
就行:$ sudo iptables-restore < /etc/iptables/redsocks.rules
当你需要退出全局代理环境,只需要清空iptables规则,redsocks可以让他一直运行:
$ sudo /usr/lib/systemd/scripts/iptables-flush
一些人可能是自己编译源码进行安装的,这些Arch Linux软件包内的默认配置和运行脚本可能对你有所帮助。
高级配置
由于我只在极少数特殊情况下才会用到全局代理,所以DNS防污染,国内ip白名单等都没有弄,有需要的可以研究下以下几个技术:
-
dnsmasq
-
ChinaDNS
-----------------
利用 ss + redsocks + iptables 实现ubuntu桌面系统翻墙
我们需要一台运行在墙外的服务器,能正常访问被墙的服务,然后将被墙服务器的流量转发到墙外的服务器上去,解决方案就是 ss + redsocks + iptables。ss
shadowsocks,基于 SOCKS5 协议的加密代理软件
安装
两台服务器都需要安装 sssudo apt-get update sudo apt-get install python-pip pip install shadowsocks
启动
墙外服务器后台启动 ss server// $PORT: 设置 ss server 运行的端口号 // $PASSWORD: 设置 ss server 的密码 // -m aes-256-cfb: 加密方式 sudo ssserver -p $PORT -k $PASSWORD -m aes-256-cfb --user nobody -d start
被墙服务器后台启动 ss client// $IP: ss server 的地址 // $PORT: ss server 的密码 // -b 127.0.0.1: 设置 ss client 运行的地址 // -l 1080: 设置 ss client 运行的端口 // -t 600: 设置连接 ss server 超时时间 sslocal -s $IP -p $PORT -b 127.0.0.1 -l 1080 -k $PASSWORD -t 600
redsocks
利用路由表将 TCP 连接转发到 SOCKS 或 HTTP 连接的工具
安装
被墙服务器安装sudo apt-get update sudo apt-get install redsocks
启动
被墙服务器后台启动
sudo service redsocks start
配置
一般使用默认配置文件 /etc/redsocks.conf 即可,如果 ss client 的启动参数不是默认的就需要自己设置// /etc/redsocks.conf base { // debug: connection progress & client list on SIGUSR1 log_debug = off; // info: start and end of client session log_info = on; /* possible `log' values are: * stderr * "file:/path/to/file" * syslog:FACILITY facility is any of "daemon", "local0"..."local7" */ log = "syslog:daemon"; // detach from console daemon = on; /* Change uid, gid and root directory, these options require root * privilegies on startup. * Note, your chroot may requre /etc/localtime if you write log to syslog. * Log is opened before chroot & uid changing. */ user = redsocks; group = redsocks; // chroot = "/var/chroot"; /* possible `redirector' values are: * iptables - for Linux * ipf - for FreeBSD * pf - for OpenBSD * generic - some generic redirector that MAY work */ redirector = iptables; } redsocks { /* `local_ip' defaults to 127.0.0.1 for security reasons, * use 0.0.0.0 if you want to listen on every interface. * `local_*' are used as port to redirect to. */ local_ip = 127.0.0.1; local_port = 12345; // `ip' and `port' are IP and tcp-port of proxy-server // You can also use hostname instead of IP, only one (random) // address of multihomed host will be used. ip = 127.0.0.1; port = 1080; // known types: socks4, socks5, http-connect, http-relay type = socks5; // login = "foobar"; // password = "baz"; }
现在 ss 和 redsocks 都已经启动了,最后要利用 iptables 规划路由,将流量通过 redsocks 转发到 ss 上。iptables
Linux 内核集成的 IP 信息包过滤系统
路由规划
// 新建路由转发表中的一个链 REDSOCKS sudo iptables -t nat -N REDSOCKS // 设置不需要代理转发的网段 // 目的为墙外代理服务器的数据包一定不能转发 sudo iptables -t nat -A REDSOCKS -d $SS_SERVER_IP -j RETURN // 目的为局域网和本地回环地址的数据包不用转发 sudo iptables -t nat -A REDSOCKS -d 172.0.0.0/24 -j RETURN sudo iptables -t nat -A REDSOCKS -d 192.168.0.0/16 -j RETURN // 将数据包转发到 redsocks sudo iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-ports 12345 // 将 REDSOCKS 链的规则应用到经过 eth0 网卡的数据包 sudo iptables -t nat -A OUTPUT -p tcp -o eth0 -j REDSOCKS
以上的路由规则在服务器重启后就失效了,可以写到 shell 脚本中,开机后运行。
---------------------
在 Linux 桌面系统上,使用redsocks把socks5代理转换为全局代理
在中国大陆由于一些众所周知以及网络环境有时候会比较差的原因, 我们可能会需要使用代理服务器来进行转发流量. 但是有些软件不支持代理, 如何让它走代理呢? 那就要介绍下redsocks
了.安装
redsocks 的安装比较方便 从 GitHub 检出代码然后编译就好了git clone https://github.com/darkk/redsocks cd redsocks make
配置
默认我们认为本地socks
代理在127.0.0.1:1080
将配置文件配为下面这样// $ cat redsocks.conf base { log_debug = off; log_info = off; log = "syslog:daemon"; // 改成 on 使用 daemon 运行 daemon = on; user = redsocks; group = redsocks; redirector = iptables; } redsocks { local_ip = 127.0.0.1; local_port = 31338; ip = 127.0.0.1; port = 1080; type = socks5; }
启动
./redsocks
就可以运行了配置 iptables
# Transparent SOCKS proxy # See: http://darkk.net.ru/redsocks/ *nat :PREROUTING ACCEPT [0:0] :INPUT ACCEPT [0:0] :OUTPUT ACCEPT [0:0] :POSTROUTING ACCEPT [0:0] :REDSOCKS - [0:0] # Redirect all output through redsocks -A OUTPUT -p tcp -j REDSOCKS # Whitelist LANs and some other reserved addresses. # https://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses -A REDSOCKS -d 0.0.0.0/8 -j RETURN -A REDSOCKS -d 10.0.0.0/8 -j RETURN -A REDSOCKS -d 127.0.0.0/8 -j RETURN -A REDSOCKS -d 169.254.0.0/16 -j RETURN -A REDSOCKS -d 172.16.0.0/12 -j RETURN -A REDSOCKS -d 192.168.0.0/16 -j RETURN -A REDSOCKS -d 224.0.0.0/4 -j RETURN -A REDSOCKS -d 240.0.0.0/4 -j RETURN #-A REDSOCKS -d { 代理服务端的地址 } -j RETURN # shadowsocks server port -A REDSOCKS -p tcp --dport { 代理服务端的端口 } -j RETURN # Redirect everything else to redsocks port -A REDSOCKS -p tcp -j REDIRECT --to-ports 31338 COMMIT
然后全部的TCP
链接就会走代理了。
---------------------------------
transparent TCP to SOCKS5 redirector.
yellowsocks
yellowsocks类似于redsocks,是依赖iptables把tcp转换为socks5的全局代理工具。
配置代理
在linux或者路由器上启动,指定监听端口、socks5的地址端口。
./yellowsocks -l :4455 -t 127.0.0.1:1080
配置iptables
*nat
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
:REDSOCKS - [0:0]
# Redirect all output through redsocks
-A OUTPUT -p tcp -j REDSOCKS
# Whitelist LANs and some other reserved addresses.
# https://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses
-A REDSOCKS -d 0.0.0.0/8 -j RETURN
-A REDSOCKS -d 10.0.0.0/8 -j RETURN
-A REDSOCKS -d 127.0.0.0/8 -j RETURN
-A REDSOCKS -d 169.254.0.0/16 -j RETURN
-A REDSOCKS -d 172.16.0.0/12 -j RETURN
-A REDSOCKS -d 192.168.0.0/16 -j RETURN
-A REDSOCKS -d 224.0.0.0/4 -j RETURN
-A REDSOCKS -d 240.0.0.0/4 -j RETURN
-A REDSOCKS -d {socks5的ip地址} -j RETURN
-A REDSOCKS -p tcp --dport {socks5的端口} -j RETURN
# Redirect everything else to redsocks port
-A REDSOCKS -p tcp -j REDIRECT --to-ports 4455
COMMIT
openwrt配置
- 编译yellowsocks成路由器对应版本,如GOOS=linux GOARCH=mipsle go build
- 部分CPU需要在kernel里打开FPU,重新编译openwrt固件,编译方法参考官网
- 路由器启动后,在/etc/firewall.user添加转发规则
iptables -t nat -N YELLOWSOCKS
iptables -t nat -A PREROUTING -i br-lan -p tcp -j YELLOWSOCKS
# Do not redirect traffic to the followign address ranges
iptables -t nat -A YELLOWSOCKS -d 0.0.0.0/8 -j RETURN
iptables -t nat -A YELLOWSOCKS -d 10.0.0.0/8 -j RETURN
iptables -t nat -A YELLOWSOCKS -d 127.0.0.0/8 -j RETURN
iptables -t nat -A YELLOWSOCKS -d 169.254.0.0/16 -j RETURN
iptables -t nat -A YELLOWSOCKS -d 172.16.0.0/16 -j RETURN
iptables -t nat -A YELLOWSOCKS -d 192.168.0.0/16 -j RETURN
iptables -t nat -A YELLOWSOCKS -d 224.0.0.0/4 -j RETURN
iptables -t nat -A YELLOWSOCKS -d 240.0.0.0/4 -j RETURN
# Redirect all kinds of traffic
iptables -t nat -A YELLOWSOCKS -p tcp -j REDIRECT --to-ports 4455
- 启动yellowsocks
./yellowsocks -l :4455 -t sock5的ip:sock5的端口 -nolog 1 -noprint 1
- 如果上不去网,说明有dns污染,可以通过pingtunnel解决
- 设置openwrt自带的dnsmasq转发
uci add_list dhcp.@dnsmasq[0].server="127.0.0.1#5353"
uci set dhcp.@dnsmasq[0].noresolv="1"
uci set dhcp.@dnsmasq[0].localuse="1"
uci commit dhcp
/etc/init.d/dnsmasq restart
- 启动pingtunnel,转发dns到远端
./pingtunnel -type client -l :5353 -s yourserver -t 8.8.8.8:53 -key yourkey -nolog 1 -noprint 1
from
https://github.com/esrrhs/yellowsocks
-----------------------
pr0cks
python script to transparently forward all TCP and DNS traffic through a socks (like ssh -D option) or HTTPS (CONNECT) proxy using iptables -j REDIRECT target. Only works on linux for now.Features :
- set up a local transparent proxy compatible with socks4 socks5 and HTTP CONNECT proxies allowing to forward any TCP traffic transparently using iptables
- set up a local transparent DNS proxy translating UDP port 53 requests to TCP allowing DNS traffic to go through a proxy without UDP support (like ssh -D option)
- DNS caching mechanism to speed up the DNS resolutions through pr0cks
Usage example: let's rock
As an example we will use the socks5 proxy of openssh (the option -D)
$ ssh -D 1080 user@sshserver
then you can add some iptables rules :
$ iptables -t nat -A OUTPUT ! -d <my_ssh_server_IP>/32 -o eth0 -p tcp -m tcp -j REDIRECT --to-ports 10080
$ iptables -t nat -A OUTPUT -o eth0 -p udp -m udp --dport 53 -j REDIRECT --to-ports 1053
then start pr0cks :
$ python pr0cks.py --proxy SOCKS5:127.0.0.1:1080
All your TCP traffic and DNS traffic should now pass through the ssh server kinda like if you had setup a tun VPN through ssh but without admin rights on the server ! #help
python pr0cks.py -h
usage: procks [-h] [--proxy PROXY] [-p PORT] [-v] [--username USERNAME]
[--password PASSWORD] [--dns-port DNS_PORT]
[--dns-server DNS_SERVER]
Transparent SOCKS5/SOCKS4/HTTP_CONNECT Proxy
optional arguments:
-h, --help show this help message and exit
--proxy PROXY proxytype:ip:port to forward our connections through.
proxytype can be SOCKS5, SOCKS4 or HTTP
-p PORT, --port PORT port to bind the transparent proxy on the local socket
(default 10080)
-v, --verbose print all the connections requested through the proxy
--username USERNAME Username to authenticate with to the server. The
default is no authentication.
--password PASSWORD Only relevant when a username has been provided
--dns-port DNS_PORT dns port to listen on (default 1053)
--dns-server DNS_SERVER
ip:port of the DNS server to forward all DNS requests
to using TCP through the proxy (default
208.67.222.222:53)
Dependencies
- dnslib (https://pypi.python.org/pypi/dnslib) for DNS UDP to TCP translation support
- tested with Python 2.7
from https://github.com/n1nj4sec/pr0cks
--------
-------------------------------------------
--------
Making SOCKS proxy transparent
UPDATE:
If we have a limited connectivity to the world from current location, but still can connect to a shell account fully open to the world (or open to the other non-public network), than dynamic port forwarding available in
I'm assuming that you already have SOCKS server bound to localhost on standard port 1080 (e.g. you started
transocks_ev
is unstable, therefore using a lot better redsocks
is safer choice. Sorry for this late update.If we have a limited connectivity to the world from current location, but still can connect to a shell account fully open to the world (or open to the other non-public network), than dynamic port forwarding available in
ssh
can save us. This feature (accessible by -D
option) in fact makes ssh acting as SOCKS server. OK, but what can we do if our application doesn't support SOCKS proxy? It's important question, because vast majority of software is unaware of such protocol. In Linux we have a great tsocks
(http://tsocks.sourceforge.net/), shell wrapper which transparently allows an application to use SOCKS proxy. In Windows there are FreeCap
(http://www.freecap.ru/eng/) and Vilongu HTTP SOCKS Tunneler
http://www.connectionresume.com/vilongu/ - they do the same thing, but in a different way. Nice, but what if we have dozens of machines to set up. Teaching all users how to use any of mentioned application can be also really inconvenient. Making SOCKS proxy transparent will solve (almost?) all our problems. Is it feasible? YES, but you must have an access to superuser account on a gateway server (it can be also any other server, but gateway is used here for simplicity).I'm assuming that you already have SOCKS server bound to localhost on standard port 1080 (e.g. you started
ssh
with -D1080
).- Install
libevent
(http://www.monkey.org/~provos/libevent/). It can be already available in your distribution repository. This will be used for compiling in next step, so you must get development package (usuallylibevent-dev
). - Download
redsocks
(http://darkk.net.ru/redsocks/ - either usegit
to clone repository, get latest snapshot or get locally archived revision from 2009.05.21: redsocks-a37aa7a.tar.gz) and build it usingmake
. - Configure
redsocks
by editingredsocks.conf
. I assume that it contains:
base { log_debug = on; log_info = on; log = "file:/home/users/przemoc/redsocks/redsocks.log"; daemon = on; redirector = iptables; } redsocks { local_ip = 0.0.0.0; local_port = 12345; ip = 192.168.0.1; port = 1080; type = socks5; }
- Configure, tune and run below script first time as root with one argument
iptables
and later as normal user.
#!/bin/sh # Where to find iptables IPTABLES="/usr/sbin/iptables" # Where to find redsocks REDSOCKS_DIR="/home/users/przemoc/redsocks" REDSOCKS="$REDSOCKS_DIR/redsocks" # Port that is redsocks listening on REDSOCKS_PORT="12345" # Location of our SOCKS-server SOCKS_HOST="192.168.0.1" SOCKS_PORT="1080" # Start redsocks if [ "$USER" != "root" ]; then echo -n 'Restarting redsocks... ' pkill -U $USER redsocks 2>/dev/null sleep 1 cd $REDSOCKS_DIR && $REDSOCKS if [ $? -eq 0 ]; then echo Done else echo Error fi exit 0; elif [ "$1" != "iptables" ]; then exit 0 fi $IPTABLES -t nat -D PREROUTING -p tcp -j REDSOCKS_FILTER 2>/dev/null $IPTABLES -t nat -D OUTPUT -p tcp -j REDSOCKS_FILTER 2>/dev/null $IPTABLES -t nat -F REDSOCKS_FILTER 2>/dev/null $IPTABLES -t nat -X REDSOCKS_FILTER 2>/dev/null $IPTABLES -t nat -F REDSOCKS 2>/dev/null $IPTABLES -t nat -X REDSOCKS 2>/dev/null # Create our own chain $IPTABLES -t nat -N REDSOCKS $IPTABLES -t nat -N REDSOCKS_FILTER # Do not try to redirect local traffic $IPTABLES -t nat -I REDSOCKS_FILTER -o lo -j RETURN ### Below whitelist and blacklist cannot operate together. ### If you want to change it, refactor the code. It's simple. # Redirect only specified addresses and do not try redirect other traffic. (whitelist option) $IPTABLES -t nat -A REDSOCKS_FILTER -m iprange --dst-range 192.168.0.10-192.168.0.30 -j REDSOCKS $IPTABLES -t nat -A REDSOCKS_FILTER -d 126.0.0.0/8 -j REDSOCKS $IPTABLES -t nat -A REDSOCKS_FILTER -j RETURN ## Do not redirect LAN traffic and some other reserved addresses. (blacklist option) #$IPTABLES -t nat -A REDSOCKS_FILTER -d 0.0.0.0/8 -j RETURN #$IPTABLES -t nat -A REDSOCKS_FILTER -d 10.0.0.0/8 -j RETURN #$IPTABLES -t nat -A REDSOCKS_FILTER -d 127.0.0.0/8 -j RETURN #$IPTABLES -t nat -A REDSOCKS_FILTER -d 169.254.0.0/16 -j RETURN #$IPTABLES -t nat -A REDSOCKS_FILTER -d 172.16.0.0/12 -j RETURN #$IPTABLES -t nat -A REDSOCKS_FILTER -d 192.168.0.0/16 -j RETURN #$IPTABLES -t nat -A REDSOCKS_FILTER -d 224.0.0.0/4 -j RETURN #$IPTABLES -t nat -A REDSOCKS_FILTER -d 240.0.0.0/4 -j RETURN #$IPTABLES -t nat -A REDSOCKS_FILTER -j REDSOCKS ## Do not redirect traffic for the SOCKS-Server ## Not needed if server is not on a whitelist or is already blacklisted. #$IPTABLES -t nat -I REDSOCKS -p tcp -d $SOCKS_HOST --dport $SOCKS_PORT -j RETURN # Redirect all traffic that gets to the end of our chain $IPTABLES -t nat -A REDSOCKS -p tcp -j REDIRECT --to-port $REDSOCKS_PORT ## Filter all traffic from the own host ## BE CAREFULL HERE IF THE SOCKS-SERVER RUNS ON THIS MACHINE $IPTABLES -t nat -A OUTPUT -p tcp -j REDSOCKS_FILTER # Filter all traffic that is routed over this host $IPTABLES -t nat -A PREROUTING -p tcp -j REDSOCKS_FILTER echo IPtables reconfigured.
- Now all hosts with your machine set as a gateway use SOCKS proxy accordingly to
iptables
rules. Transparently! - Open another beer bottle and enjoy.
----------------------------------
A simple Shadowsocks transparent proxy setup script.
archwrt-ss.sh
A simple Shadowsocks transparent proxy setup script.Main features
- GFWList Mode supported
- Chnroute (Bypass Mainland IP) Mode supported
- Customized Blacklist/Whitelist supported
- Game Mode (udp redirection on both OUTPUT chains and PREROUTING chains ) supported
Depends
- shadowsocks-libev
- simple-obfs (optional)
- shadowsocks-v2ray-plugin (optional)
- dnsmasq
- ipset
- iptables
- AdguardTeam/dnsproxy (optional)
Usage
Info:
Contibuted by monlor & edward-p
Usage:
archwrt-ss.sh {Command} {Option} {Config File}
Commands:
start | stop | restart | status | update
Options:
gfwlist | bypass | gamemode | global
Config File:
Specify which config.json to use. by default the script will use the last one used.
Example:
archwrt-ss.sh start bypass Start with bypass mode
archwrt-ss.sh restart gfwlist Restart with gfwlist mode
archwrt-ss.sh restart bypass sfo2 Retart with bypass mode using /etc/shadowsocks/sfo2.json
archwrt-ss.sh restart sfo2 Retart using /etc/shadowsocks/sfo2.json
archwrt-ss.sh start Start with default mode [current:bypass]
archwrt-ss.sh update Update rules
Installation
Install on AUR (Archlinux only)
yay -S archwrt-ss.sh-git
Install manually
$ git clone https://github.com/archwrt/archwrt-ss.sh
$ cd archwrt-ss.sh
$ sudo install -Dm755 archwrt-ss.sh /usr/bin/archwrt-ss.sh
$ sudo install -Dm644 archwrt-ss.conf /etc/archwrt/ss/archwrt-ss.conf
$ sudo install -Dm644 archwrt-ss.service /usr/lib/systemd/system/archwrt-ss.service
$ sudo systemctl daemon-reload
Start
The script use
systemctl start shadowsocks-libev-redir@${ss_conf}
to start ss-redir.
You need set up your config name (without extension
.json
) to ss_conf
in /etc/archwrt/ss/archwrt-ss.conf
You need write your
${ss_conf}.json
in /etc/shadowsocks
Check if there's any error, for example:
$ ss-redir -c /etc/shadowsocks/config.json
Not yet, you need set
puredns_port
in /etc/archwrt/ss/archwrt-ss.conf
as the upstream of the dnsmasq.
Now you can start by:
$ sudo systemctl start archwrt-ss.service
For auto start:
systemctl enable archwrt-ss.service
Customized Blacklist/Whitelist
- blacklist: by default, located at
/etc/archwrt/ss/blacklist.txt
- whitelist: by default, located at
/etc/archwrt/ss/whitelist.txt
comment with
#
is supported
IP/NET/Domains suported, one line each, for example:
...
# This is a comment
127.0.0.1 #ip
127.0.0.0/24 #net
example.com #domain
...
from
https://github.com/archwrt/archwrt-ss.sh
redsocks2 configure and setup quickly; a transparent socks5proxy redirector; redirect any tcp/udp connection to backends socks5 proxy servers.
应用场景
- 把
redsocks2
部署在网关服务器,让用户无感知使用socks5
代理上网,即透明代理; 网关服务器需要同时有内网IP
和外网IP
. redsocks2
的作用是将用户的数据包封装上socks5
头,然后传递给后端socks5
代理服务器(也可以是socks5
代理负载均衡器, 可用nginx
或iptables
实现).TCP
和UDP
均可很好的支持.
安装
- 提前编译好
redsocks2
可执行文件
yum install libevent2-devel openssl-devel -y
wget https://github.com/semigodking/redsocks/archive/release-0.66.zip
unzip release-0.66
cd redsocks-release-0.66
make -j $(grep -c processor /proc/cpuinfo)
- 根据实际情况修改
src/transparent_socks5proxy_redirector_setup.sh
前置的几个变量
prefix_dir=/usr/local/redsocks2 # 安装路径
logdir=$prefix_dir/log # 用户访问日志路径
logsavedays=300 # 日志保留时间
redsocks_server_ip=192.168.1.254 # redsocks2安装在的服务器内网IP, 此处一定不能为127.0.0.1,公网IP不安全也不考虑
redsocks_tcp_port=10001 # redsocks2用于接收TCP包的监听端口
redsocks_udp_port=20001 # redsocks2用于接收UDP包的监听端口
socks_server="101.19.11.60:1080 12.5.11.27:1080" # redsocks2将封装后的数据包转发给后端的socks5代理服务器列表(或socks5代理负载均衡器, 或tcp转发器), 可以是一个, 多个的话使用空格分隔
- 执行安装脚本
bash transparent_socks5proxy_redirector_setup.sh
使用说明
安装后的默认路径:
/usr/local/redsocks2/
, 目录中相关文件说明如下:redsocks2 # redsocks2程序
redsocks2.service # 整个服务的启动、关闭、重启、状态查看脚本
redsocks.conf # redsocks2的配置文件
log/access.log # 用户访问的日志
log/logrotate.sh # 用户日志每日轮转脚本
err.log # redsocks2的debug日志
启用服务:
./redsocks2.service start
-------------------------
transparent TCP to SOCKS5 redirector.
yellowsocks
yellowsocks类似于redsocks,是依赖iptables把tcp转换为socks5的全局代理工具。
配置代理
在linux或者路由器上启动,指定监听端口、socks5的地址端口。
./yellowsocks -l :4455 -t 127.0.0.1:1080
配置iptables
*nat
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
:REDSOCKS - [0:0]
# Redirect all output through redsocks
-A OUTPUT -p tcp -j REDSOCKS
# Whitelist LANs and some other reserved addresses.
# https://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses
-A REDSOCKS -d 0.0.0.0/8 -j RETURN
-A REDSOCKS -d 10.0.0.0/8 -j RETURN
-A REDSOCKS -d 127.0.0.0/8 -j RETURN
-A REDSOCKS -d 169.254.0.0/16 -j RETURN
-A REDSOCKS -d 172.16.0.0/12 -j RETURN
-A REDSOCKS -d 192.168.0.0/16 -j RETURN
-A REDSOCKS -d 224.0.0.0/4 -j RETURN
-A REDSOCKS -d 240.0.0.0/4 -j RETURN
-A REDSOCKS -d {socks5的ip地址} -j RETURN
-A REDSOCKS -p tcp --dport {socks5的端口} -j RETURN
# Redirect everything else to redsocks port
-A REDSOCKS -p tcp -j REDIRECT --to-ports 4455
COMMIT
openwrt配置
- 编译yellowsocks成路由器对应版本,如GOOS=linux GOARCH=mipsle go build
- 部分CPU需要在kernel里打开FPU,重新编译openwrt固件,编译方法参考官网
- 路由器启动后,在/etc/firewall.user添加转发规则
iptables -t nat -N YELLOWSOCKS
iptables -t nat -A PREROUTING -i br-lan -p tcp -j YELLOWSOCKS
# Do not redirect traffic to the followign address ranges
iptables -t nat -A YELLOWSOCKS -d 0.0.0.0/8 -j RETURN
iptables -t nat -A YELLOWSOCKS -d 10.0.0.0/8 -j RETURN
iptables -t nat -A YELLOWSOCKS -d 127.0.0.0/8 -j RETURN
iptables -t nat -A YELLOWSOCKS -d 169.254.0.0/16 -j RETURN
iptables -t nat -A YELLOWSOCKS -d 172.16.0.0/16 -j RETURN
iptables -t nat -A YELLOWSOCKS -d 192.168.0.0/16 -j RETURN
iptables -t nat -A YELLOWSOCKS -d 224.0.0.0/4 -j RETURN
iptables -t nat -A YELLOWSOCKS -d 240.0.0.0/4 -j RETURN
# Redirect all kinds of traffic
iptables -t nat -A YELLOWSOCKS -p tcp -j REDIRECT --to-ports 4455
- 启动yellowsocks
./yellowsocks -l :4455 -t sock5的ip:sock5的端口 -nolog 1 -noprint 1
- 如果上不去网,说明有dns污染,可以通过pingtunnel解决
- 设置openwrt自带的dnsmasq转发
uci add_list dhcp.@dnsmasq[0].server="127.0.0.1#5353"
uci set dhcp.@dnsmasq[0].noresolv="1"
uci set dhcp.@dnsmasq[0].localuse="1"
uci commit dhcp
/etc/init.d/dnsmasq restart
- 启动pingtunnel,转发dns到远端
./pingtunnel -type client -l :5353 -s yourserver -t 8.8.8.8:53 -key yourkey -nolog 1 -noprint 1
- 或者参考yellowdns
from https://github.com/esrrhs/yellowsocks