電訊茶室's Archiver

角色 發表於 2018-6-20 15:44

MikroTik hAP ac² - 在逆向翻墙自动修正断link的script

在逆向翻墙自动修正断link的script

我们从大陆VPN到香港,然后通过这条link反大陆,就如你身在大陆一样,用大陆IP看CCTV5,CCTV5+,奇艺网。但是这种逆向翻墙回大陆有一个不好的地方就是,大陆VPN过来的link经常会断,那么每次都有去router重置一些settings,这做成极不方便,为了解决这个问题,我写了一个script,能格5分钟就检查一次,如果断了就会自动更正。有时间我会解析一下这个script原理。

下面的script放在哪里?用GUI,点击[system]->[Scripts]就可以。

Script name: Res-China-IP[code]
:local a (/interface find name=pptp-China)
:local b ([/ip firewall nat get [find comment=China-IP] out-interface]="<pptp-China>")

:if (!b && a) do={
    /ip route set [find comment="China Gateway"] gateway=<pptp-China>
    /ip firewall nat set [find comment=China-IP] out-interface=<pptp-China>  
    :log info "China IP NAT and Route were updated.";         
}
[/code].

01. Define local variable a, which receives the truth value of (/interface find name=pptp-China).
China给大陆vpn过来的user login ID,大家根据自己的settings来决定。a 将会是 true or false. 用括号是提取expression的true value。

02. Define local variable b,which receives the truth value of "([/ip firewall nat get [find comment=China-IP] out-interface]="<pptp-China>")".
这句比较长,大家看先从里面看。首先ip firewall nat table里有那么多条rules,script怎样知道在哪条rule找呢?我用的方法是先把某条rule先一些comment,然后通过这个comment找到那条rule的ID。Rule ID是什么,你打开nat rule table就知道(/ip/firewall/nat/print),在表里左手边的数字就是rule ID。忘了说一句这个中括号[]有什么用? 这是把return value,不是truth value,truth value用括号(),其他用[], 因为ID是数字value,所以用[]。“[/ip firewall nat get [find comment=China-IP] out-interface]”, 就是提取了那个rule的out-interface是什么?然后做logical comparision with “pptp-China”。如果是就通过括号()给出true,otherwise给false。

04:if (!b && a) do,这一步就是看看是nat的b = not(out-interface="pptp-China") and 大陆vpn过来过来的Link是否ready?如果 (!b && a)是true的就进行修改,什么是true?如果不是就scheduler再trigger后再检查。

Scheduler[code]
/system scheduler
add interval=5m name=Res-China-Gateway on-event=\
    "/system script run Restore-China-IP"
[/code]

gfx86674 發表於 2018-7-1 18:55

[i=s] 本帖最後由 gfx86674 於 2018-7-1 19:04 編輯 [/i]

這用不到script啦{:5_199:}  
1.先修正/ppp profile裡的vpn-profile ,限制一組帳號只能有一筆連線
[img]https://i.imgur.com/v1dNTlY.png[/img]

2.選擇VPN Server Binding
[img]https://i.imgur.com/XttItXq.png[/img]
[img]https://i.imgur.com/Vzxc6lu.png[/img]
User輸入vpn-client連線時所用的該帳號,就完成設定了

3.如上[color=Blue]pptp-in1[/color] 即能當/ip route vpn的gateway ,不因dynamic斷線而消失
同樣/ip firewall nat處,也可當in-interface或out-interface使用.

角色 發表於 2018-7-1 19:53

谢谢CHing,有空会follow你的idea去试试。

vpnuser 發表於 2018-7-2 23:45

不怪得我沒有遇到要 修正的情況
因為我一開始跟了gfx86674的方法去做
only one 有沒有都沒關係
得確可以

gfx86674 發表於 2018-7-3 00:33

[i=s] 本帖最後由 gfx86674 於 2018-7-3 00:39 編輯 [/i]

[quote]不怪得我沒有遇到要 修正的情況
因為我一開始跟了gfx86674的方法去做
only one 有沒有都沒關係
得確可以 ...
[size=2][color=#999999]vpnuser 發表於 2018-7-2 23:45[/color] [url=http://www.telecom-cafe.com/forum/redirect.php?goto=findpost&pid=45116&ptid=7190][img]http://www.telecom-cafe.com/forum/images/common/back.gif[/img][/url][/size][/quote]
only one沒開若遇舊連線斷線,
server沒立即釋放,再次的連線會讓系統誤認是使用重複登入,
讓帳號abc出現<vpn-abc1>或<vpn-abc2>或更多的編號...

這些帶有編號的client用戶 ,VPN Server Binding可是不會抓進vpn-in1裡的.

hbmask 發表於 2018-12-23 10:53

有大陆资源没有香港资源,,大陆有一条下载200M 上行40M的宽带,希望能有资源互换

頁: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.