openwrt路由器_基于openwrt的shadowsocks智能代理配置

更新时间:2019-11-20    来源:php安装    手机版     字体:

【www.bbyears.com--php安装】

配置环境

openwrt12.09稳定版
基于ar71xx

安装shadowsocks

因为shadowsocks的aes-256加密需要高版本的libpolarssl,但是12.09源中的libpolarssl不是最新的,虽说做个软链接也能够“骗过”shadowsocks,但是对于一个重度强迫症患者来说一定要装最新的!!
经测试,trunk源中的libpolarssl可以在12.09中使用,ssh到路由器上并安装:


cd /tmp
wget http://downloads.openwrt.org/snapshots/trunk/ar71xx/packages/libpolarssl_1.3.7-1_ar71xx.ipk
opkg install libpolarssl_1.3.7-1_ar71xx.ipk
wget http://lecterlee.com/myfile/shadowsocks-libev-polarssl_1.4.6_ar71xx.ipk #因为shadowsocks的下载地址被gfw屏蔽,所以我保存了一份在服务器上
opkg install shadowsocks-libev-polarssl_1.4.6_ar71xx.ipk
这样shadowsocks就搭建完成了,shadowsocks默认启动时运行的是ss-local,但是如果要搭建全局代理的话需要使用ss-redir,所以,我们需要修改一下启动脚本:

sed -i "s/ss-local/ss-redir/g" /etc/init.d/shadowsocks

修改配置文件/etc/shadowsocks.json:


{
    "server":"1.1.1.1",
    "server_port":12121,
    "local_port":1081,
    "password":"21212",
    "method": "aes-256-cfb",
    "timeout":600
}

启动并设置开机启动:


/etc/init.d/shadowsocks start
/etc/init.d/shadowsocks enable
使用iptables重定向流量

经过上一步,已经可以进行科学上网了,但是所有的流量都会经过vpn,我们需要使用iptables对流量进行重定向,我使用的是比较无脑的除了亚洲以外全部重定向的规则,在这里https://gist.github.com/reee/fe174cfd8985273bc478,按自己的情况修改运行即可,需要安装iptables-mod-nat-extra:

opkg update
opkg install iptables-mod-nat-extra

这样做的好处是不需要频繁的修改iptables规则,但是gfw也会屏蔽一些台湾的ip段,如果有需要的话可以自行修改,经过一番google,发现北落师门老兄用的是另一种方法,即默认所有流量走本地,需要使用代理的地址通过dig和APNIC WHOIS工具来获得其ip段,并使用iptables重定向流量,这种方法比较智能,但是对于懒人来说是不能忍受的,在这里我也把方法贴出来,供大家选择:


iptables -t nat -N SHADOWSOCKS     #创建SHADOWSOCKS链
iptables -t nat -A SHADOWSOCKS -p tcp -d 74.125.0.0/16 -j REDIRECT --to-ports 1081
iptables -t nat -A SHADOWSOCKS -p tcp -d 173.194.0.0/16 -j REDIRECT --to-ports 1081     #google的一些IP段重定向到1081端口(shadowsocks监听的端口)
iptables -t nat -A SHADOWSOCKS -p tcp -j RETURN     #忽略其他tcp请求
iptables -t nat -A PREROUTING -p tcp -j SHADOWSOCKS     #在路由之前使用SHADOWSOCKS链
上面是一个使用的例子,仅仅列出了使用google的方法,如果以后遇到需要使用代理的地址时,只需在SHADOWSOCKS链添加相应的redirect规则即可,但是需要注意的是,添加时要使用-I而不是-A,因为使用-A会使新添加的规则处于链的最底部,而由于上面有一个return规则会使新规则不生效,所以,要保证SHADOWSOCKS链中的最后一个规则为return规则

使用tcp协议查询dns

按照上面的步骤做完后,会发现google可以访问了,但一些类似于facebook之类的网站还是不能访问,这是因为gfw对53端口使用udp协议的dns进行了污染,你所查询到的是错误的ip,我们使用dnsmasq+pdnsd来避免dns污染
dnsmasq在openwrt属于默认安装软件,只需安装pdnsd即可

opkg update
opkg install pdnsd

安装完成后将/etc/pdnsd.conf文件修改为:


global {
    # debug = on;         
    perm_cache=1024;
    cache_dir="/var/pdnsd";
    run_as="nobody";
    server_port = 1053;    #指定1053端口,避免和dnsmasq端口冲突
    server_ip = 127.0.0.1;
    status_ctl = on;
    query_method=tcp_only;     #只使用tcp查询
    min_ttl=15m;
    max_ttl=1w;
    timeout=10;
}
 
server {
    label= "mydns";
    ip = 8.8.8.8;     #上游dns地址,必须要支持tcp查询
    root_server = on;。
    uptest = none;
}

启动pdnsd并设置为开机启动:

/etc/init.d/pdnsd start
/etc/init.d/pdnsd enable

新建dnsmasq配置文件夹并添加规则:


mkdir /etc/dnsmasq.d
cat >> /etc/dnsmasq.d/fuckgfw.conf << EOF
#Google and Youtube
server=/.google.com/127.0.0.1#1053
server=/.google.com.hk/127.0.0.1#1053
server=/.gstatic.com/127.0.0.1#1053
server=/.ggpht.com/127.0.0.1#1053
server=/.googleusercontent.com/127.0.0.1#1053
server=/.appspot.com/127.0.0.1#1053
server=/.googlecode.com/127.0.0.1#1053
server=/.googleapis.com/127.0.0.1#1053
server=/.gmail.com/127.0.0.1#1053
server=/.google-analytics.com/127.0.0.1#1053
server=/.youtube.com/127.0.0.1#1053
server=/.googlevideo.com/127.0.0.1#1053
server=/.youtube-nocookie.com/127.0.0.1#1053
server=/.ytimg.com/127.0.0.1#1053
server=/.blogspot.com/127.0.0.1#1053
server=/.blogger.com/127.0.0.1#1053
 
#FaceBook
server=/.facebook.com/127.0.0.1#1053
server=/.thefacebook.com/127.0.0.1#1053
server=/.facebook.net/127.0.0.1#1053
server=/.fbcdn.net/127.0.0.1#1053
server=/.akamaihd.net/127.0.0.1#1053
 
#Twitter
server=/.twitter.com/127.0.0.1#1053
server=/.t.co/127.0.0.1#1053
server=/.bitly.com/127.0.0.1#1053
server=/.twimg.com/127.0.0.1#1053
server=/.tinypic.com/127.0.0.1#1053
server=/.yfrog.com/127.0.0.1#1053
EOF

配置文件中的域名转发到pdnsd进行tcp查询,这里直接使用北落师门老兄的配置文件了,列出了常用的被污染域名,可以按照自己需要添加
修改dnsmasq配置文件使其识别我们新建的目录,只需在/etc/dnsmasq.conf最后一行追加:

conf-dir=/etc/dnsmasq.d

这样就实现了基于openwrt的智能代理,所有连接此路由器的设配无需配置即可科学上网

但是,我发现google的8.8.8.8公共dns服务器在我这里丢包率到达75%,萌生了自己搭建dns的念头,但是bind9默认优先使用udp,没有找到设置为强制tcp的方法,经过一番google后,终于在Malash老兄这里找到了解决办法
首先是拥有一台在墙外的服务器(之前用的shadowsocks服务器即可),下载并解压bind9


wget http://www.isc.org/downloads/file/bind-9-10-0b1-2/?version=tar.gz -O bind-9.10.0-P2.tar.gz
tar xf bind-9.10.0-P2.tar.gz
cd bind-9.10.0-P2
修改bind9源码文件lib/dns/resolver.c,大约在1445行

将 query->options = options; 修改为 query->options = options | DNS_FETCHOPT_TCP;
编译并安装bind9


./configure --prefix=/usr/local/named --enable-threads --enable-largefile
make && make install

生成配置文件


/usr/local/named/sbin/rndc-confgen > /usr/local/named/etc/rndc.conf
tail -n10 /usr/local/named/etc/rndc.conf | head -n9 | sed -e s/#\ //g > /usr/local/named/etc/named.conf
修改named.conf文件,追加:

 

options {
           forward only;
           forwarders {
            8.8.8.8;
            8.8.4.4;
            };
           allow-query {
                 any;
            };
 };

上游DNS服务器为google的,只作为转发,设置完后调试bind9

/usr/local/named/sbin/named -uroot -g -d 9

没有错误后即可启动bind

/usr/local/named/sbin/named -uroot

打开tcp的53端口,设置路由器上的pdnsd即可

iptables -I INPUT -p tcp --dport 53 -j ACCEPT

本文来源:http://www.bbyears.com/jiaocheng/80284.html

热门标签

更多>>

本类排行