Raspberry 2B && Ubuntu mate 16.04 && *** 完美透明代理

关键词:Raspberry 2B, Ubuntu mate 16.04 for raspberry, ***, Dnsmasq, ChinaDns, Iptables

这两天利用手头的树莓派,搭建了一个透明代理,只要连上树莓派的热点,不需要任何配置,自动FQ。效果挺不错的,效果图:

总体的步骤呢,可以说包括:

在树莓派上利用无线网卡,建立热点;

利用ShadowSocks,结合Iptables, ChinaDns, Dnsmasq搭建透明代理。

搭建热点

安装了Ubuntu 16.04 mate for raspberry后,ifconfig发现interface的名字很奇怪,查了下udev使用了Predictable interfaces names来命各种interface,我就把它改回去了。过程参考了王晔的博客(wangye.org),我用的是EDUP产RTL8188CUS芯片的无线网卡。首先就是安装hostapd,根据 man pages的说明:

"hostapd - IEEE 802.11 AP, IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator。" 参考的博客里说的通过apt-get install hostapd安装的hostapd并不兼容RTL8188CUS芯片的网卡,不过博客是在2013年写的,现在是否支持并不知道,不过我也是按照博客里给的链接进行的安装。

 sudo apt-get install hostapd
 wget http://www.daveconroy.com/wp3/wp-content/uploads/2013/07/hostapd.zip
 unzip hostapd.zip
 sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.bak
 sudo mv hostapd /usr/sbin/hostapd.edimax
 sudo ln -sf /usr/sbin/hostapd.edimax /usr/sbin/hostapd
 sudo chown root.root /usr/sbin/hostapd
  /usr/sbin/hostapd

安装好hostapd后,接着就是配置,配置文件在/etc/hostapd/hostapd.conf,可配置SSID, 密码等基本信息。

 interface=wlan0
 driver=rtl871xdrv
 ssid=Ck-Raspberry
 hw_mode=g
 channel=
 macaddr_acl=
 auth_algs=
 ignore_broadcast_ssid=
 wpa=
 wpa_passphrase=MYPASSWORD
 wpa_key_mgmt=WPA-PSK
 wpa_pairwise=TKIP
 rsn_pairwise=CCMP

安装Dnsmasq,Dnsmasq是一个轻量级的DHCP和带有缓存的DNS服务器。安装它能够让连接到树莓派热点的设备自动获取到IP和DNS配置。使用Sudo apt-get install dnsmasq安装dnsmasq,dnsmasq的配置文件在/etc/dnsmasq.conf,进行如下配置:

 # If you want dnsmasq to listen for DHCP and DNS requests only on
 # specified interfaces (and the loopback) give the name of the
 # interface (eg eth0) here.
 # Repeat the line for more than one interface.
 interface=wlan0
 # Uncomment this to enable the integrated DHCP server, you need
 # to supply the range of addresses available for lease and optionally
 # a lease time. If you have more than one network, you will need to
 # repeat this for each network on which you want to supply DHCP
 # service.
 dhcp-range=192.168.11.20,192.168.11.150,48h
 # Override the default route supplied by dnsmasq, which assumes the
 # router is the same machine as the one running dnsmasq.
 dhcp-option=,192.168.11.1
 dhcp-option=,192.168.11.1,114.114.114.114

在dnsmasq的配置文件中,我们指定了DHCP分发给连接到的设备的网关,DNS等配置,因此,我们还需要对wlan0接口进行配置,将wlan0的地址指定为网关,DNS服务器的地址。

Vi /etc/network/interfaces,修改wlan0接口:

 allow-hotplug wlan0
 iface wlan0 inet static
 address 192.168.11.1
 netmask 255.255.255.0

到此,我们已经基本配置好了让树莓派作为网关的基本内容,最后一步就是开启树莓派的路由转发功能,将wlan0的流量转发到eth0上。

sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

编辑/etc/sysctl.conf,设置net.ipv4.ip_forward=1

 sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
 sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
 sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

将iptables的设置保存下来,在每次重新启动机器的时候,重新进行设置。

sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

编辑/etc/network/interfaces,将up iptables-restore < /etc/iptables.ipv4.nat添加到最后,至此,搭建热点的步骤已经全部完成,重启机器,连接热点,成功上网!!

搭建透明代理

GFW为了防止FQ,一方面通过DNS劫持来防止解析到一些域名的地址;另一方面就是阻止向一些IP地址进行连接。当然还有其他手段。

利用***,能够实现穿越GFW。我们可以将wlan0接口的流量全部转发到***监听的本地端口,但是这样有缺点,就是有些不必要走***的国内流量也走了***。需要安装的程序有shadowsocks-libev,dnsmasq,chinadns。

为了防止DNS劫持,我利用dnsmasq和ChinaDns以及ss-tunnel来保证DNS得到正确解析。参考:https://sourceforge.net/p/openwrt-dist/wiki/DNS/ 我利用的是其中的方案5。Dnsmasq将作为本地的DNS服务器,当Dnsmasq接收到DNS请求时,会将DNS请求转发到其上游的DNS服务器,我们将ChinaDns作为其上游的DNS服务器,而ChinaDns会正确地解析出域名的IP地址。ChinaDns需要配置多个DNS服务器,必须至少有一个国内的DNS 和一个国外的DNS,解析的时候会同时向国内的DNS和国外的DNS发送请求进行解析,并根据策略判断采用哪个结果。我利用ss-tunnel的端口转发功能,在本地监听一个端口,转发DNS请求,这个就相当于一个国外的DNS服务器。大概原理说清楚了,讲下具体操作。

 ss-tunnel -c /etc/shadowsocks.json -l  -L  –u

/etc/shadowssocks里根据shadowsocks服务器端信息进行设置,包括ip,端口,加密方式,本地监听端口等。

在/etc/init.d/chinadns中,配置监听端口15353以及国内和国外DNS服务器。127.0.0.1:5300会被认为是国外DNS服务器。

 $DAEMON \
 -m \
 -c /usr/local/share/chnroute.txt \
 -p  \
 -s
 > /var/log/$NAME.log > /var/log/$NAME.err.log &

在/etc/dnsmasq.conf中,配置Dnsmasq的上游DNS服务器,就是Chinadns。

 # If you don't want dnsmasq to read /etc/resolv.conf or any other
 # file, getting its servers from this file instead (see below), then
 # uncomment this.
 no-resolv
 server=

基本步骤就是这些,下面就是要保证ss-tunnel能够开机自动启动就可以了。

为了防止GFW阻止我们向某些IP进行连接,我利用ss-redir进行设置socks5代理,然后通过socks5代理来连接那些被GFW所blocked的IP。讲下具体操作。

首先要利用iptables进行设置,将wlan0接口的流量根据目的IP进行转发,如果目的主机的IP在国外,就转发到socks5代理处;否则,则是直接进行通信。

 iptables -t nat -N SHADOWSOCKS
 # Ignore your shadowsocks server's addresses
 # It's very IMPORTANT, just be careful.
 iptables -t nat -A SHADOWSOCKS -d 138.128.204.223 -j RETURN
 # Ignore LANs IP address
 iptables -t nat -A SHADOWSOCKS -d  -j RETURN
 iptables -t nat -A SHADOWSOCKS -d  -j RETURN
 iptables -t nat -A SHADOWSOCKS -d  -j RETURN
 iptables -t nat -A SHADOWSOCKS -d  -j RETURN
 iptables -t nat -A SHADOWSOCKS -d  -j RETURN
 iptables -t nat -A SHADOWSOCKS -d  -j RETURN
 iptables -t nat -A SHADOWSOCKS -d  -j RETURN
 iptables -t nat -A SHADOWSOCKS -d  -j RETURN
 # Ignore Asia IP address
 iptables -t nat -A SHADOWSOCKS -d  -j RETURN
 iptables -t nat -A SHADOWSOCKS -d  -j RETURN
 iptables -t nat -A SHADOWSOCKS -d  -j RETURN
 iptables -t nat -A SHADOWSOCKS -d  -j RETURN
 iptables -t nat -A SHADOWSOCKS -d  -j RETURN
 .......
 .......
 # Anything else should be redirected to shadowsocks's local port
 iptables -t nat -A SHADOWSOCKS -p tcp -j REDIRECT --to-ports
 # Apply the rules to nat client
 iptables -t nat -A PREROUTING -p tcp -j SHADOWSOCKS
 iptables -t nat -A OUTPUT -p tcp -j SHADOWSOCKS

socks5代理则通过ss-redir -vc /etc/shadowsocks.json运行。最后保证iptables的设置和ss-redir能够保持开机运行就可以。完全完工,手机连上wifi自动FQ,挺好用的。

在整个过程中,自己还学会了怎么调试iptables,感觉挺不错的。

参考

http://wangye.org/blog/archives/845/

https://zzz.buzz/zh/gfw/2016/02/16/deploy-shadowsocks-on-routers/

http://hbprotoss.github.io/posts/da-jian-zhi-neng-fan-qiang-lu-you-qi.html

http://liberize.me/tech/play-with-raspberry-pi.html

https://sourceforge.net/p/openwrt-dist/wiki/DNS/

https://cokebar.info/archives/1410

https://velaciela.ms/openwrt-shadowsocks-chinadns

http://www.faqs.org/docs/iptables/traversingoftables.html

http://adminberlin.de/iptables-debugging/

http://backreference.org/2010/06/11/iptables-debugging/

http://serverfault.com/questions/385937/how-to-enable-iptables-trace-target-on-debian-squeeze-6

Raspberry 2B && Ubuntu mate 16.04 && *** 完美透明代理的更多相关文章

  1. 在 Ubuntu Mate 16.04 上通过 PPA 升级 Mate 1.14

    导读 Mate 桌面环境 1.14 现在可以在 Ubuntu Mate 16.04 ("Xenial Xerus") 上使用了.根据这个版本的描述,为了全面测试 Mate 1.14 ...

  2. 树莓派 Ubuntu mate 16.04 下开启vncserver(自动启动+改分辨率)

    树莓派 Ubuntu mate 16.04 下开启vncserver(自动启动+改分辨率) 参考博文:https://blog.csdn.net/Mr_dhy/article/details/8282 ...

  3. 树莓派Ubuntu Mate 16.04 修改为国内更新源

    收藏:https://blog.csdn.net/wang_shuai_ww/article/details/80386708 更换步骤以root身份打开 /etc/apt/sources.list ...

  4. 树莓派 ubuntu mate 16.04 系统默认软件源

    deb http://ports.ubuntu.com/ xenial main restricted universe multiverse deb-src http://ports.ubuntu. ...

  5. win10系统搭建虚拟机:VMware Workstation Player 12环境+Ubuntu Kylin 16.04 LTS系统

    笔者小白一枚,其实连虚拟机是个啥都不知道...实属惭愧,介于此所以今天倒腾了一下花了一上午就已经搭建好一个VMware Workstation Player 12免费版的,很哈皮,于是赶紧分享一下. ...

  6. ubuntu kylin 16.04系统的基本安装

    系统版本:ubuntu kylin 16.04 硬件状况:500G HDD+120G SSD 已安装操作系统:WIN 10专业版(craked) ——WIN 10系统是装在SSD的第一个盘符内的.以前 ...

  7. Ubuntu Server 16.04下ASP.NET Core Web Api + MySql + Dapper在 Jexus、nginx 下的简单测试

    一.环境及工具 1.服务器 VirtualBox5.1.4 安装 Ubuntu Server 16.04 amd64 MySql Ver 14.14 Distrib 5.6.21 Jexus 5.8. ...

  8. Ubuntu Server 14.04升级Ubuntu Server 16.04

    Ubuntu Server 14.04升级Ubuntu Server 16.04 :转 http://blog.csdn.net/chszs 1.终端下执行命令 $ sudo apt-get upda ...

  9. [原创]在HP DL380 G7服务器上部署基于Ubuntu Server 16.04 和 VirtualBox的云平台

    对于一线开发人员来说,一提到虚拟机平台,往往会让人联想到在价格昂贵的服务器上部署VMware vSphere之类软件来实现. 笔者作为一个资深码农,也是一直梦寐着在自己家中打造一个真正的家庭私有云,秒 ...

随机推荐

  1. VBA 字符串操作

    Trim(string) 去掉string左右两端空白 Ltrim(string) 去掉string左端空白 Rtrim(string) 去掉string右端空白 Len(string) 计算stri ...

  2. WPF命令绑定 自定义命令

    WPF的命令系统是wpf中新增加的内容,在以往的winfom中并没有.为什么要增加命令这一块内容.在winform里面的没有命令只使用事件的话也可以实现程序员希望实现的功能.这个问题在很多文章中都提到 ...

  3. linux 下 Shell编程(三)

    if语句应用实例 if语句可以在程序中实现各种逻辑判断. 用if语句判断并显示文件的信息 可以用test命令和相关的参数来判断文件的属性,然后根据判断结果输出文件的信息. #!/bin/bash #4 ...

  4. linux 下shell程序(二)

    输入和输出 输入指的是Shell程序读入数据.有从文件读取.从用户输入读取等方式读入数据.输出指的是Shell程序的运行 结果的处理,可以显示到屏幕或保存到文件. 用ceho命令输出结果 echo $ ...

  5. 《从零开始学Swift》学习笔记(Day 34)——静态属性是怎么回事?

    原创文章,欢迎转载.转载请注明:关东升的博客 我先来设计一个类:有一个Account(银行账户)类,假设它有3个属性:amount(账户金额).interestRate(利率)和owner(账户名). ...

  6. 清空javascript数组数据

    var arrayObj = new Array(); arrayObj.splice(0, arrayObj.length);//清空数组数据

  7. Jquery来对form表单提交(mvc方案)

    来自:http://www.cnblogs.com/lmfeng/archive/2011/06/18/2084325.html 我先说明一下,这是asp.net mvc 里面的用法, Jquery来 ...

  8. <2013 07 31> 没有必然的理由

    <2013 07 31> 没有必然的理由 没有必然的理由 人类从野蛮走向文明 也可能,从野蛮走向更野蛮 没有必然的理由 人群从疯狂走向理智 也可能,从疯狂走向更疯狂 没有必然的理由 你我从 ...

  9. 批处理 ECHO命令输出空行

    众所周知,如果echo后面跟一个环境变量,但是该变量却为空时,相当于不加任何参数的echo,即输出当前echo是on还是off.很多文章或者教程给出的解决方案都是在echo后面加一个点号echo.,这 ...

  10. Python __setitem__()、__getitem__()、__delitem__()

    转载:http://blog.csdn.net/xhw88398569/article/details/48690163 __xxxitem__:使用 [''] 的方式操作属性时被调用 __setit ...