找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广告投放联系QQ68610888
查看: 103350|回复: 34

redsocks连接socks5代理服务器实现全局代理,只是为了访问外网

  [复制链接]
发表于 2014-3-21 01:56 | 显示全部楼层 |阅读模式
本帖最后由 lostinfever 于 2014-3-22 02:32 编辑

一提到redsocks和代理这些关键词,大家都会联想到这是翻WALL用的。
虽然redsocks是为翻WALL而生,但是我们还可以用它来连接固定的socks5代理,让内网用户通过openwrt访问外网

我的网络环境是这样的,通过pppoe拨号的方式获得内网IP,没有访问外网的权限,很苦逼,但是前些天无意中发现内网里有一台socks5的代理服务器,连接这个代理之后就可以畅通无阻的访问外网了。
在windows下,可以通过pppoe拨号获得内网IP,使用Proxifier这款软件配置socks5全局代理,来获得访问外网的权限。但是手机平板就难以做到这两步。
所以我想用openwrt做路由,先pppoe拨号获得内网IP,再通过redsocks2将内网里所有设备的连接请求全部转发到指定的socks5代理服务器,这样就可以实现终端免设置访问外网了。

估计有这种需求的人不多,但是还是记录下来作为一个例子给大家参考吧(基于openwrt-x86):

1.先使用内网账号pppoe拨号获得内网IP:


2.获取适应你openwrt平台的redsocks2,这里提供一个x86的redsocks2,其他平台请自行搜索或编译:


3.配置redsocks2,配置文件在/etc/config/redsocks.conf:
  1. base {
  2. log_debug = off;
  3. log_info = off;
  4. daemon = on;
  5. redirector= iptables;
  6. }

  7. redsocks {
  8. local_ip = 192.168.1.1;
  9. local_port = 1081;
  10. ip = 你的socks5代理服务器地址,也可以是域名;
  11. port = 1080;
  12. type = socks5;
  13. autoproxy = 0;
  14. }
复制代码
配置文件写好后,将启动redsocks的命令加入rc.local里,实现开机启动:
  1. # Put your custom commands here that should be executed once
  2. # the system init finished. By default this file does nothing.

  3. redsocks2 -c /etc/config/redsocks.conf

  4. exit 0
复制代码
4.配置iptables,网络-防火墙-自定义规则,把内网所有端口的数据全部转发到redsocks:
  1. iptables -t nat -N REDSOCKS
  2. iptables -t nat -A PREROUTING -i br-lan -p tcp -j REDSOCKS
  3. iptables -t nat -A PREROUTING -i br-lan -p udp -j REDSOCKS

  4. # Do not redirect traffic to the followign address ranges
  5. iptables -t nat -A REDSOCKS -d 127.0.0.0/8 -j RETURN
  6. iptables -t nat -A REDSOCKS -d 192.168.0.0/16 -j RETURN
  7. iptables -t nat -A REDSOCKS -d 10.8.0.0/16 -j RETURN
  8. iptables -t nat -A REDSOCKS -d 224.0.0.0/4 -j RETURN
  9. iptables -t nat -A REDSOCKS -d 240.0.0.0/4 -j RETURN

  10. # Redirect all kinds of traffic

  11. iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-ports 1081
  12. iptables -t nat -A REDSOCKS -p udp -j REDIRECT --to-ports 1081
复制代码
通过这样的配置之后,内网所有设备的访问请求都会通过op主路由被转发到socks5代理,获得外网访问权限:


PS:还有一个没解决的问题,这样设置之后,内网其他设备的请求是可以正常转发了,但是op本身访问网络时并没有通过socks5代理转发。

由于本地发起的网络请求不通过防火墙,所以不会走prerouting,因而没有被转发。

不知道要设置一条什么样的转发规则,能让op本地的连接请求也被转发到redsocks上,求精通iptables的大神指教!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×

评分

参与人数 1恩山币 +1 收起 理由
春暖花开 + 1 城会玩

查看全部评分

我的恩山、我的无线 The best wifi forum is right here.
发表于 2014-3-21 08:48 | 显示全部楼层
学习了
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2014-3-21 09:27 | 显示全部楼层
不错,可惜我这里的内网环境比楼主的还复杂
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2014-3-21 09:53 | 显示全部楼层
我也有这个问题, 应该是路由本身不是通过br-lan口或 prerouting之类的, 所以以下两条命令并不能把路由自身的请求转到 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
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| 发表于 2014-3-21 10:24 来自手机 | 显示全部楼层
muziling 发表于 2014-3-21 09:53
我也有这个问题, 应该是路由本身不是通过br-lan口或 prerouting之类的, 所以以下两条命令并不能把路由自 ...

说得对,刚百度查了一下:

本地连接的数据包不会经过网卡转发,是由内核处理后直接交给进程,所以不会走prerouting,需要对127.0.0.1的output数据包配置转发,等下测试一下
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2014-3-21 13:18 | 显示全部楼层
这个方法也可以用到http代理服务器吗?我也是单位内网,跟楼主情况有点像
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| 发表于 2014-3-21 13:20 | 显示全部楼层
guohao1118 发表于 2014-3-21 13:18
这个方法也可以用到http代理服务器吗?我也是单位内网,跟楼主情况有点像

http代理也可以,但是只能代理http请求,只有80端口能通

socks5代理是全部协议都可以穿透,所有端口都可以代理
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2014-3-21 14:03 | 显示全部楼层
路由器本身:

iptables -t nat -A OUTPUT 1 -p tcp  -j REDSOCKS
iptables -t nat -A OUTPUT 1 -p udp  -j REDSOCKS
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2014-3-21 14:07 | 显示全部楼层
SZPUNK 发表于 2014-3-21 14:03
路由器本身:

iptables -t nat -A OUTPUT 1 -p tcp  -j REDSOCKS

# iptables -t nat -A OUTPUT 1 -p tcp  -j REDSOCKS
Bad argument `1'
Try `iptables -h' or 'iptables --help' for more information.


iptables 版本是 v1.4.10
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2014-3-21 14:15 | 显示全部楼层
muziling 发表于 2014-3-21 14:07
# iptables -t nat -A OUTPUT 1 -p tcp  -j REDSOCKS
Bad argument `1'
Try `iptables -h' or 'iptable ...

去掉 1 试试。
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2014-3-21 14:27 | 显示全部楼层

好像不行,加了, 随便wget一个东西,都是connection refused

# wget http://www.baiud.com
Connecting to www.baiud.com (216.218.207.151:80)
wget: can't connect to remote host (216.218.207.151): Connection refused
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2014-3-21 14:42 | 显示全部楼层
muziling 发表于 2014-3-21 14:27
好像不行,加了, 随便wget一个东西,都是connection refused

# wget http://www.baiud.com
  1. root@rt-n16:/tmp/home/root# iptables -t nat -A OUTPUT -p tcp -j SHADOWSOCKS
  2. root@rt-n16:/tmp/home/root# wget <a target="_blank" href="http://www.baiud.com">http://www.baiud.com</a>
  3. Connecting to <a target="_blank" href="http://www.baiud.com">www.baiud.com</a> (216.218.207.151:80)
  4. wget: server returned error: HTTP/1.1 403 Forbidden
  5. root@rt-n16:/tmp/home/root# wget <a target="_blank" href="http://www.baidu.com">http://www.baidu.com</a>
  6. Connecting to <a target="_blank" href="http://www.baidu.com">www.baidu.com</a> (111.13.100.91:80)
  7. Connecting to <a target="_blank" href="http://www.baidu.com">www.baidu.com</a> (111.13.100.92:80)
  8. index.html           100% |*********************************************************************************************************************************| 16428   0:00:00 ETA
  9. root@rt-n16:/tmp/home/root#
复制代码

你那个地址有问题吧。。。。
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2014-3-21 14:53 | 显示全部楼层
本帖最后由 muziling 于 2014-3-21 15:00 编辑
SZPUNK 发表于 2014-3-21 14:42
你那个地址有问题吧。。。。

好奇怪,我的就是不行, 你的iptabes规则列表是什么

[root@wins2012:/tmp]# iptables -t nat -A OUTPUT -p tcp -j REDSOCKS
[root@wins2012:/tmp]# wget http://www.baidu.com
Connecting to www.baidu.com (115.239.210.27:80)
wget: can't connect to remote host (115.239.210.27): Connection refused
[root@wins2012:/tmp]# wget http://www.qq.com
Connecting to www.qq.com (14.17.32.211:80)
wget: can't connect to remote host (14.17.32.211): Connection refused





我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2014-3-21 14:56 | 显示全部楼层
  1. root@rt-n16:/tmp/home/root# wgetpro --no-check-certificate https://twitter.com
  2. --2014-03-21 14:55:26--  https://twitter.com/
  3. Resolving twitter.com... 199.59.149.230, 199.59.150.39, 199.59.149.198
  4. Connecting to twitter.com|199.59.149.230|:443... connected.
  5. WARNING: cannot verify twitter.com's certificate, issued by '/C=US/O=VeriSign, Inc./OU=VeriSign Trust Network/OU=Terms of use at https://www.verisign.com/rpa (c)06/CN=VeriSign Class 3 Extended Validation SSL CA':
  6.   Unable to locally verify the issuer's authority.
  7. HTTP request sent, awaiting response... 200 OK
  8. Length: 42596 (42K) [text/html]
  9. Saving to: 'index.html'

  10. 100%[========================================================================================================================================>] 42,596      67.8KB/s   in 0.6s   

  11. 2014-03-21 14:55:27 (67.8 KB/s) - 'index.html' saved [42596/42596]

  12. root@rt-n16:/tmp/home/root# cat index.html | grep \<title
  13.       <title>Twitter</title>
  14. root@rt-n16:/tmp/home/root#
复制代码

twitter 也没问题。
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2014-3-21 15:01 | 显示全部楼层
SZPUNK 发表于 2014-3-21 14:56
twitter 也没问题。

我配在redsocks里的代理是192.168的网段,会是这个原因?
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

有疑问请添加管理员QQ86788181|手机版|小黑屋|Archiver|恩山无线论坛(常州市恩山计算机开发有限公司版权所有) ( 苏ICP备05084872号 )

GMT+8, 2024-4-24 16:45

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

| 江苏省互联网有害信息举报中心 举报信箱:js12377 | @jischina.com.cn 举报电话:025-88802724 本站不良内容举报信箱:68610888@qq.com 举报电话:0519-86695797

快速回复 返回顶部 返回列表