ashi009 / bestroutetb

Generating the most optimized route table for VPN users.
MIT License
879 stars 99 forks source link

Linux 通用那里生成的up.sh和down.sh严重有问题 #17

Open yhlfh opened 10 years ago

yhlfh commented 10 years ago

首先说up.sh的问题,顶部的#!/bin/shnetgw=$(cat /tmp/net_gateway)read target dummy vpngw dummy <<< $(ip route get 8.8.8.8)全部粘到一行去了,导致脚本根本不能用。

还有两个脚本共有的问题:因为route命令是在/sbin/目录下的,这两个脚本没有把route命令的路径放进去,导致那一堆route命令全部无法运行。经过本人测试,在两个脚本的第二行插入export PATH="/bin:/sbin:/usr/sbin:/usr/bin",才可以!

另外,这三个脚本对应的位置应该是: /etc/ppp/ip-pre-up /etc/ppp/ip-up.local /etc/ppp/ip-down.local

使用说明里写得不是很清楚,再加上生成的脚本有误,导致我走了许多弯路,望改进!

ashi009 commented 10 years ago

对不起,我没有测试过linux的几个脚本,如果方便希望你可以把经验分享一下。 On Jan 16, 2014 12:45 PM, "yhlfh" notifications@github.com wrote:

首先说up.sh的问题,顶部的#!/bin/shnetgw=$(cat /tmp/net_gateway)read target dummy vpngw dummy <<< $(ip route get 8.8.8.8)全部粘到一行去了,导致脚本根本不能用。

还有两个脚本共有的问题:因为route命令是在/sbin/目录下的,这两个脚本没有把route命令的路径放进去,导致那一堆route命令全部无法运行。经过本人测试,在两个脚本的第二行插入export PATH="/bin:/sbin:/usr/sbin:/usr/bin",才可以!

另外,这三个脚本对应的位置应该是: /etc/ppp/ip-pre-up /etc/ppp/ip-up.local /etc/ppp/ip-down.local

使用说明里写得不是很清楚,再加上生成的脚本有误,导致我走了许多弯路,望改进!

— Reply to this email directly or view it on GitHubhttps://github.com/ashi009/bestroutetb/issues/17 .

yhlfh commented 10 years ago

ashi009,经验在上面都写了,就是按上面说的把脚本修复一下,然后放到正确的位置就OK了。不过现在又碰到了个问题,能上youtube跟facebook,但是上不了推。

yhlfh commented 10 years ago

自动生成的up和down脚本就是个大水坑啊!route命令语法都错了,搞了半天终于修复了。我直接贴代码吧,方便看。

先看/etc/ppp/ip-up.local

#!/bin/sh
export PATH="/bin:/sbin:/usr/sbin:/usr/bin"

netgw=$(cat /tmp/net_gateway)
#这里要延迟一秒执行,要不然取不到正确的$vpngw值。
sleep 1s && read target dummy vpngw dummy <<< $(ip route get 8.8.8.8)

route add -net 0.0.0.0 netmask 0.0.0.0 dev $vpngw
route add -net 0.0.0.0 netmask 254.0.0.0 gw $netgw
……

自动生成的脚本的错误在于:

再来看/etc/ppp/ip-down.local

#!/bin/sh
export PATH="/bin:/sbin:/usr/sbin:/usr/bin"

route del -net 0.0.0.0 netmask 0.0.0.0
route del -net 0.0.0.0 netmask 254.0.0.0
……

自动生成的脚本的错误在于:

情况就是这样!望修复!

ashi009 commented 10 years ago

在新的分支里我修改了代码,请帮忙测试。新的指令分别是:

generate.sh ip-up.local --net=CN,0.0.0.0/0 --nodefaultgw=1 --profile=route_up

generate.sh ip-down.local --net=CN,0.0.0.0/0 --nodefaultgw=1 --profile=route_down

关于 dev 的问题我稍微查了一下资料,通常用 gw 是正确的,因为用网关 ip 放在 dev 后边有可能会执行失败(这似乎与使用的 VPN 连接方式有关)。请帮忙查证这一点。

   gw GW  route  packets  via a gateway.  NOTE: The specified gateway must
          be reachable first. This usually means that you have to set up a
          static  route  to  the  gateway  beforehand.  If you specify the
          address of one of your local interfaces,  it  will  be  used  to
          decide about the interface to which the packets should be routed
          to. This is a BSDism compatibility hack.

   dev If force the route to be associated with the specified  device,  as
          the kernel will otherwise try to determine the device on its own
          (by checking already existing routes and device  specifications,
          and  where  the  route is added to). In most normal networks you
          won't need this.

          If dev If is the last option on the command line, the  word  dev
          may  be omitted, as it's the default. Otherwise the order of the
          route modifiers (metric - netmask - gw - dev) doesn't matter.
yhlfh commented 10 years ago

刚刚亲测,连到VPN观察下路由表再看了段youtube视频,再断开VPN观察下路由表再来这里comment。

只有一个问题:$vpngw前面一定要写dev!这跟$netgw不一样,$netgw对应的值是IP地址,比如我这里是192.168.1.1,所以$netgw前面是gw;而$vpngw对应的值是vpn接口的名称,比如我这里是ppp0,所以$vpngw前面要写dev

还有我不太理解--net=CN,0.0.0.0/0有什么作用?

ashi009 commented 10 years ago

你用的Linux发行版是什么?我的ip route get 8.8.8.8返回的是8.8.8.8 via xxx.xxx.xxx.xxx dev xxx 所以 $vpngw 是个 ip,不能写成你要求的样子。

--net=CN,0.0.0.0/0 是强制默认网关使用非vpn,配合 --nodefaultgw=1 是强制不输出默认网关规则,这样也就是让系统去管理默认网关。

刚刚亲测,连到VPN观察下路由表再看了段youtube视频,再断开VPN观察下路由表再来这里comment。

只有一个问题:$vpngw前面一定要写dev!这跟$netgw不一样,$netgw对应的值是IP地址,比如我这里是192.168.1.1,所以 $netgw前面是gw;而$vpngw对应的值是vpn接口的名称,比如我这里是ppp0,所以$vpngw前面要写dev

还有我不太理解--net=CN,0.0.0.0/0有什么作用?

— Reply to this email directly or view it on GitHubhttps://github.com/ashi009/bestroutetb/issues/17#issuecomment-32610320 .

yhlfh commented 10 years ago

我用的发行版是openSUSE 13.1。连上VPN后ip route get 8.8.8.8返回的是: 8.8.8.8 dev ppp0 src 10.0.6.7

未连VPN时,ip route get 8.8.8.8返回的是: 8.8.8.8 via 192.168.1.1 dev wlan0 src 192.168.1.111

连上VPN后部分路由表规则输出如下:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.1     254.0.0.0       UG    0      0        0 wlan0
default         *               192.0.0.0       U     0      0        0 ppp0
default         *               0.0.0.0         U     0      0        0 ppp0
……

其中第三条规则是系统自己设置的,可以看到凡是通过VPN走的,gateway那里都是个*。只有走wlan0的才对应一个IP地址的网关。

你用的是什么发行版?你贴的这个输出是连上VPN之后的输出?把你的路由表贴出来看看,看看你的VPN对应的网关是个什么IP地址。

ashi009 commented 10 years ago

我在用 OpenWRT。执行一次 PPPoE 连接到网络外加一次 PPTP 连接到 VPN,但是第二次连接不设置网关。所以和你的情况不太一样。

$ ip route get 8.8.8.8
8.8.8.8 via 192.2xx.1xx.1 dev pptp-vpn  src 192.2xx.1xx.67
yhlfh commented 10 years ago

OpenWRT是个firmware,不是发行版。我这个应该是Linux下的普遍情况。要不你再找其他Linux用户论证一下?

ashi009 commented 10 years ago

我恐怕没法找到别的 linux 用户去验证,要不你验证之后发一个 pull request,我帮你合并?另外既然你可以用 iproute2,你可以参考一下我在用的脚本。

yhlfh commented 10 years ago

一位用ubuntu的朋友帮我验证了一下,跟我的结果是一样的,走VPN那没有一个IP地址的gateway,只是显示*。具体看这:http://forum.ubuntu.org.cn/viewtopic.php?f=73&t=454795

pull request我不会搞,还是您直接改吧。