Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

配置文件设计 #1

Closed
clowwindy opened this issue Sep 14, 2014 · 35 comments
Closed

配置文件设计 #1

clowwindy opened this issue Sep 14, 2014 · 35 comments
Assignees

Comments

@clowwindy
Copy link
Owner

command line:

-s start/stop/restart
-c config_file

config_file:

mode=服务器/客户端
mtu=MTU
inft=网卡名
pidfile=pidfile
server=服务器地址
port=服务器端口
key=密码
up=启动脚本
down=退出脚本

启动脚本是一个 bash 脚本,在创建好网卡之后会执行,以便用户可以灵活配置网卡
执行时用参数传递:是服务器还是客户端,网卡名字
默认的脚本会设置网卡 IP 和 MTU、MSSFIX
如果是服务器端,启动 IP 转发和 NAT
如果是客户端,包含一段注释掉的代码:启动 NAT,修改默认路由,并由用户自行决定如何配置该文件

退出脚本会包含注释掉的代码:关闭 NAT,修改默认路由,由用户自行决定如何配置该文件

@aa65535

@clowwindy
Copy link
Owner Author

考虑如何设计使得整合 chnrouteschinadns 打包方便。

@aa65535
Copy link
Collaborator

aa65535 commented Sep 14, 2014

好的,我看一下。

@aa65535
Copy link
Collaborator

aa65535 commented Sep 14, 2014

可以保持目前 chinadns 的配置文件状态,
ShadowVPN 默认不带 chnroutes (默认全局代理),
通过读取 chinadns 的 chnroutes 来生成路由表实现分流。

主要问题是 4700+ 条的 route add 命令执行速度可能会比较慢。

@clowwindy
Copy link
Owner Author

可以把临时文件写到 tempfs (内存)里,然后用 ip -batch 加载。

@clowwindy clowwindy self-assigned this Sep 14, 2014
@aa65535
Copy link
Collaborator

aa65535 commented Sep 14, 2014

在路由器测试了一下,速度在可接受范围。

# suf=$(ip route show | awk '/^default/ {printf("%s %s %s %s",$2,$3,$4,$5)}')
# time awk -v suf="$suf" '$1 ~ /^([0-9]{1,3}\.){3}[0-9]{1,3}/{printf("route add %s %s\n",$1,suf)}' $IGNORE > /tmp/route
real    0m 0.72s
user    0m 0.59s
sys     0m 0.14s
# wc -l /tmp/route
4743 /tmp/route
# time ip -batch /tmp/route
real    0m 2.60s
user    0m 0.66s
sys     0m 1.12s

@clowwindy
Copy link
Owner Author

发布的时候可以分成两个版本:一个给所有人用的普通版,和一个国内用户专用的整合版,就脚本不一样。

@clowwindy
Copy link
Owner Author

@aa65535
Copy link
Collaborator

aa65535 commented Oct 3, 2014

@clowwindy

已经编译好在 openwrt 上测试通过。

up.sh 部分
要做 chnroutes 的话需要在 old_gw= 下面加上一条命令获取原来的网络

old_intf=$(ip route show | awk '/^default/ {print $NF}')

下面就是添加 route 的部分

############################################
# insert chnroutes rules here if you need! #
suf="via $old_gw dev $old_intf"
awk -v suf="$suf" '$1 ~ /^([0-9]{1,3}\.){3}[0-9]{1,3}/\
  {printf("route add %s %s\n",$1,suf)}' /etc/chinadns_chnroute.txt > /tmp/routes
ip -batch /tmp/routes
############################################

down.sh部分

############################################
# remove chnroutes rules here if you need! #
if [ -f /tmp/routes ]; then
  sed -i 's#route add#route del#g' /tmp/routes
  ip -batch /tmp/routes
  rm -f /tmp/routes
fi
############################################

1080P 测试效果
20141003193830

@aa65535
Copy link
Collaborator

aa65535 commented Oct 3, 2014

另外有个小问题就是运行 /etc/init.d/shadowvpn start 启动时会打印一个 started 提示信息,不过这个提示信息会有很大的概率变成下一条命令,需要使用 ctrl + c 来中断。

20141003195155
20141003195342

@aa65535
Copy link
Collaborator

aa65535 commented Oct 3, 2014

可以在 client.conf 里添加一个参数来指定 chnroutes 列表文件(默认留空),这样 up.sh 可以做成通用的了。

############################################
# insert chnroutes rules here if you need! #
if [ ! -z "$chnroutes" ] && [ -f $chnroutes ]; then
  suf="via $old_gw dev $old_intf"
  awk -v suf="$suf" '$1 ~ /^([0-9]{1,3}\.){3}[0-9]{1,3}/\
    {printf("route add %s %s\n",$1,suf)}' $chnroutes > /tmp/routes
  ip -batch /tmp/routes
fi
############################################

@clowwindy
Copy link
Owner Author

started 打印到下一行的问题已经修正。

chnroutes 文件的路径可以在 up.sh 里写死,因为一般这个文件是由打包者决定摆在哪里的,打包者也可以随意定义 up.sh 的内容,不一定需要使用 samples 目录里的文件。

@aa65535
Copy link
Collaborator

aa65535 commented Oct 3, 2014

嗯,这样的话可以写成这样

############################################
# insert chnroutes rules here if you need! #
chnroutes=/etc/chinadns_chnroute.txt
if [ -f $chnroutes ]; then
  suf="via $old_gw dev $old_intf"
  awk -v suf="$suf" '$1 ~ /^([0-9]{1,3}\.){3}[0-9]{1,3}/\
    {printf("route add %s %s\n",$1,suf)}' $chnroutes > /tmp/routes
  ip -batch /tmp/routes
fi
############################################

@clowwindy
Copy link
Owner Author

我来维护通用的(不含 chnroutes)的包,你来维护和 chinadns chnroutes 的整合版如何?

@aa65535
Copy link
Collaborator

aa65535 commented Oct 3, 2014

可以的,整合版的话 chinadns 就不用动了,只改 up.sh 和 down.sh 就可以了。

@cj1324
Copy link
Collaborator

cj1324 commented Oct 5, 2014

为什么要把服务管理的事情也给做了 -s start/stop/restart 。 那不应该是linux signal 和init程序的责任吗? 就算在OpenWRT里面也有相应的procd ,你让sysvinitsystemdupstart 情何以堪。

@clowwindy
Copy link
Owner Author

@cj1324

  1. 不加 -s 就是直接前台运行,此时可以用任何工具来管理
  2. 你漏了 launchd……与其针对不同系统和发行版写一堆 init 脚本和 readme,不如直接内置了这个功能,这样节省了大量教用户学 Linux 的时间。apache,nginx,OpenVPN 都内置 daemon 功能

@clowwindy
Copy link
Owner Author

@aa65535
这个方案可能更好,在重新拨号、路由重启等情况下可以自动快速恢复:
https://github.com/clowwindy/ShadowVPN/wiki/Configure-Via-LuCI-on-OpenWRT

不知道有没有办法更快捷的帮助用户配置这些,比如一键更新配置?

@clowwindy clowwindy reopened this Oct 5, 2014
@aa65535
Copy link
Collaborator

aa65535 commented Oct 6, 2014

好的,我参考一下。

@aa65535
Copy link
Collaborator

aa65535 commented Oct 6, 2014

新的脚本好像有问题: https://v2ex.com/t/137015#r_1391392

我测试了一下网关一样都是 0.0.0.0
stop 后也是一样上不了网,
应该在设置路由表的时候没指定 gw 的问题。

# /etc/init.d/shadowvpn start
started
# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.7.0.1        0.0.0.0         UG    0      0        0 tun0
10.7.0.0        0.0.0.0         255.255.255.0   U     0      0        0 tun0
162.243.131.41  0.0.0.0         255.255.255.255 UH    0      0        0 br-lan
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 br-lan
# /etc/init.d/shadowvpn stop
stopped
# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         0.0.0.0         0.0.0.0         U     0      0        0 br-lan
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 br-lan

这个路由是作为二级路由使用的,不过应该不是这个原因。

觉得在 LuCI 中建立 Interfaces 有点复杂,还是在 hotplug 里直接重启简单有效。

@aa65535
Copy link
Collaborator

aa65535 commented Oct 6, 2014

另外看到讨论到 iptables 和 ipset ,不过这两个都有内核依赖,
不支持的话只能换固件解决,还是没有路由表支持的广泛。

@clowwindy
Copy link
Owner Author

新的脚本是为 pppoe 设计的,旧脚本在重新拨号的时候记的网关是错的,导致不能恢复。似乎对 DHCP 的 wan 来说工作就不正常了。

@aa65535
Copy link
Collaborator

aa65535 commented Oct 6, 2014

因为我用的光纤是固定IP的,所以没有测试。
是因为 pppoe 重新拨号后 IP 变了,然后网关也变了?
如果重启的话,是根据之前保存的旧网关删除路由表(这里应该可以正确删除的),
然后重新建立路由表,在重新建立的时候应该是会重新获取网关的,
这样的话按理说是没问题的。

相当于在 IP 变化的时候重启了一下 VPN,只是交给 Hotplug 处理。

@clowwindy
Copy link
Owner Author

@aa65535
现在改为检测 pppoe 用 interface,其它用 gw。
感觉还是不如 LuCI 配置省心。

@aa65535
Copy link
Collaborator

aa65535 commented Oct 7, 2014

可是使用 LuCI 不容易做到通用。

下面是根据 Configure via LuCI on OpenWRT 写的 UCI 命令。

#!/bin/sh

uci get network.wan || exit 0

# add a new interface named tun, select tun0.
uci set network.tun='interface'
uci set network.tun.proto='static'
uci set network.tun.ifname='tun0'
uci set network.tun.ipaddr='10.7.0.2'
uci set network.tun.netmask='255.255.255.0'
uci set network.tun.gateway='10.7.0.1'
# disable use default gateway of wan.
uci set network.wan.defaultroute='0'
# add static routes for tun
uci set network.tun_r='route'
uci set network.tun_r.interface='tun'
uci set network.tun_r.target='0.0.0.0'
uci set network.tun_r.netmask='0.0.0.0'
uci set network.tun_r.gateway='10.7.0.1'
uci set network.tun_r.metric='5'
# add static routes for wan
uci set network.wan_r='route'
uci set network.wan_r.interface='wan'
uci set network.wan_r.target='$server'
uci set network.wan_r.metric='10'
# commit the settings
uci commit network

# add a new zone called tun.
# add interface tun to tun zone.
uci set firewall.tun_z='zone'
uci set firewall.tun_z.input='ACCEPT'
uci set firewall.tun_z.forward='REJECT'
uci set firewall.tun_z.output='ACCEPT'
uci set firewall.tun_z.name='tun'
uci set firewall.tun_z.network='tun'
uci set firewall.tun_z.masq='1'
# update forward rules: tun => wan.
uci set firewall.wan_f='forwarding'
uci set firewall.wan_f.dest='wan'
uci set firewall.wan_f.src='tun'
# update forward rules: lan => tun.
uci set firewall.lan_f='forwarding'
uci set firewall.lan_f.dest='tun'
uci set firewall.lan_f.src='lan'
# commit the settings
uci commit firewall

但是我这路由器没有 wan 这个 interface, 只有一个 lan.

@clowwindy
Copy link
Owner Author

@aa65535
Copy link
Collaborator

aa65535 commented Oct 7, 2014

那个是直接修改 uci 配置文件,跟使用命令没区别。

@clowwindy
Copy link
Owner Author

用命令应该更灵活,可以做一些判断。如果没有 wan,可以走另一套配置。

@aa65535
Copy link
Collaborator

aa65535 commented Oct 7, 2014

使用这种形式的话,可以将上面的命令拆开写, 建立 tun 可以在 ipk 的 postinst 中完成。

剩下部分可以根据需要做一些判断。

@hfcorriez
Copy link

@aa65535 不小心看到这个命令形式的配置觉得很不错,如果关闭该如何使用 udi 恢复?全部改为 delete?

@aa65535
Copy link
Collaborator

aa65535 commented Nov 5, 2014

@hfcorriez 使用 uci del 即可

uci del network.tun
uci del network.tun_r
uci del network.wan_r
uci commit network

uci del firewall.tun_z
uci del firewall.wan_f
uci del firewall.lan_f
uci commit firewall

@cj1324
Copy link
Collaborator

cj1324 commented Nov 5, 2014

需要讨论的issues 先Reopen

@hfcorriez
Copy link

@aa65535 多谢。。。

@hfcorriez
Copy link

@cj1324 噢,好的!

@cj1324 cj1324 reopened this Nov 5, 2014
@cj1324
Copy link
Collaborator

cj1324 commented Nov 5, 2014

@aa65535 那个是直接修改 uci 配置文件 和通过uci接口配置还有一些,细微的差别。
比如

  • 配置项目顺序
  • 对应配置文件注释文案会得到保留。

对配置的阅读有不少帮助。当然不影响功能效果。

@hfcorriez
Copy link

@aa65535 其实把 client_up 和 client_down 换成 udi 脚本也行,如果提供这种方式可能会解决很多 openwrt 的问题。

cappiewu pushed a commit to cappiewu/ShadowVPN that referenced this issue Jun 16, 2016
pexcn pushed a commit to pexcn-archived/ShadowVPN that referenced this issue Apr 4, 2018
* Update .travis.yml

* Update README.md
pexcn added a commit to pexcn-archived/ShadowVPN that referenced this issue Apr 4, 2018
pexcn added a commit to pexcn-archived/ShadowVPN that referenced this issue Apr 4, 2018
pexcn added a commit to pexcn-archived/ShadowVPN that referenced this issue Apr 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants