找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广告投放联系QQ68610888
楼主: stevemorrislian

RouterOS上的电信/移动双路由,按目标IP确定出口

[复制链接]
发表于 2018-10-20 23:15 | 显示全部楼层
本帖最后由 168kingmu 于 2018-10-20 23:19 编辑



我没有设置过负载均衡,是不是distance都是0就是负载均衡?
我现在感觉我用的那份路由表不够全..能否把你的大陆IP的列表发我一份

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×

点评

其实就是2步,1)建立一条需要打标签的缺省路由0.0.0.0/0 2)满足怎样的条件用mangle打上这个标签 ***** 然后就应该起作用,用traceroute测试  发表于 2018-10-21 12:55
https://github.com/fivesheep/chnroutes 或者在GitHub上找chnroutes,国内的BGP广播是需要不断更新的,一般用python的程序实时更新  详情 回复 发表于 2018-10-21 12:37
直接用文本贴上来,内容被遮住了  发表于 2018-10-21 12:32
print terse 可以显示你打的标签路由  发表于 2018-10-21 12:31
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2018-10-20 23:27 | 显示全部楼层
stevemorrislian 发表于 2018-10-20 22:42
没看懂你的桥接是啥功能,请说清楚

桥接那个就是lan口桥接 不用管了

点评

那你打标签最好还是用IP地址段,不要用LAN/Bridge这样的接口来描述  发表于 2018-10-21 12:58
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| 发表于 2018-10-21 12:37 | 显示全部楼层
168kingmu 发表于 2018-10-20 23:15
我没有设置过负载均衡,是不是distance都是0就是负载均衡?
我现在感觉我用的那份路由表不够全..能否 ...

https://github.com/fivesheep/chnroutes
或者在GitHub上找chnroutes,国内的BGP广播是需要不断更新的,一般用python的程序实时更新
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| 发表于 2018-10-21 12:41 | 显示全部楼层
本帖最后由 stevemorrislian 于 2018-10-21 12:46 编辑

做了点修改,产生的是适合ROS用的,最后一列是时间,可以用excel之类的表格软件排序,这样可以添加到原来的list中

#!/usr/bin/env python

import re
import urllib2
import sys
import argparse
import math
import textwrap


def generate_ovirtual**(metric):
    results = fetch_ip_data()  
    rfile=open('routes.txt','w')
    for ip,datenum,mask2 in results:
        route_item="add list=domesticList address=%s/%s \t %s \n"%(ip,mask2,datenum)
        rfile.write(route_item)
    rfile.close()
    print "Usage: Append the content of the newly created routes.txt to your openvirtual** config file," \
          " and also add 'max-routes %d', which takes a line, to the head of the file." % (len(results)+20)


def generate_linux(metric):
    results = fetch_ip_data()
    upscript_header=textwrap.dedent("""\
    #!/bin/bash
    export PATH="/bin:/sbin:/usr/sbin:/usr/bin"
   
    OLDGW=`ip route show | grep '^default' | sed -e 's/default via \\([^ ]*\\).*/\\1/'`
   
    if [ $OLDGW == '' ]; then
        exit 0
    fi
   
    if [ ! -e /tmp/virtual**_oldgw ]; then
        echo $OLDGW > /tmp/virtual**_oldgw
    fi
   
    """)
   
    downscript_header=textwrap.dedent("""\
    #!/bin/bash
    export PATH="/bin:/sbin:/usr/sbin:/usr/bin"
   
    OLDGW=`cat /tmp/virtual**_oldgw`
   
    """)
   
    upfile=open('ip-pre-up','w')
    downfile=open('ip-down','w')
   
    upfile.write(upscript_header)
    upfile.write('\n')
    downfile.write(downscript_header)
    downfile.write('\n')
   
    for ip,mask,_ in results:
        upfile.write('route add -net %s netmask %s gw $OLDGW\n'%(ip,mask))
        downfile.write('route del -net %s netmask %s\n'%(ip,mask))

    downfile.write('rm /tmp/virtual**_oldgw\n')


    print "For p p t p only, please copy the file ip-pre-up to the folder/etc/ppp," \
          "and copy the file ip-down to the folder /etc/ppp/ip-down.d."

def generate_mac(metric):
    results=fetch_ip_data()
   
    upscript_header=textwrap.dedent("""\
    #!/bin/sh
    export PATH="/bin:/sbin:/usr/sbin:/usr/bin"
   
    OLDGW=`netstat -nr | grep '^default' | grep -v 'ppp' | sed 's/default *\\([0-9\.]*\\) .*/\\1/' | awk '{if($1){print $1}}'`

    if [ ! -e /tmp/p p t p_oldgw ]; then
        echo "${OLDGW}" > /tmp/p p t p_oldgw
    fi
   
    dscacheutil -flushcache

    route add 10.0.0.0/8 "${OLDGW}"
    route add 172.16.0.0/12 "${OLDGW}"
    route add 192.168.0.0/16 "${OLDGW}"
    """)
   
    downscript_header=textwrap.dedent("""\
    #!/bin/sh
    export PATH="/bin:/sbin:/usr/sbin:/usr/bin"
   
    if [ ! -e /tmp/p p t p_oldgw ]; then
            exit 0
    fi
   
    ODLGW=`cat /tmp/p p t p_oldgw`

    route delete 10.0.0.0/8 "${OLDGW}"
    route delete 172.16.0.0/12 "${OLDGW}"
    route delete 192.168.0.0/16 "${OLDGW}"
    """)
   
    upfile=open('ip-up','w')
    downfile=open('ip-down','w')
   
    upfile.write(upscript_header)
    upfile.write('\n')
    downfile.write(downscript_header)
    downfile.write('\n')
   
    for ip,_,mask in results:
        upfile.write('route add %s/%s "${OLDGW}"\n'%(ip,mask))
        downfile.write('route delete %s/%s ${OLDGW}\n'%(ip,mask))
   
    downfile.write('\n\nrm /tmp/p p t p_oldgw\n')
    upfile.close()
    downfile.close()
   
    print "For p p t p on mac only, please copy ip-up and ip-down to the /etc/ppp folder," \
          "don't forget to make them executable with the chmod command."

def generate_win(metric):
    results = fetch_ip_data()  

    upscript_header=textwrap.dedent("""@ECHO off
    for /F "tokens=3" %%* in ('route print ^| findstr "\\<0.0.0.0\\>"') do set "gw=%%*"
   
    """)
   
    upfile=open('virtual**up.bat','w')
    downfile=open('virtual**down.bat','w')
   
    upfile.write(upscript_header)
    upfile.write('\n')
    upfile.write('ipconfig /flushdns\n\n')
   
    downfile.write("@echo off")
    downfile.write('\n')
   
    for ip,mask,_ in results:
        upfile.write('route add %s mask %s %s metric %d\n'%(ip,mask,"%gw%",metric))
        downfile.write('route delete %s\n'%(ip))
   
    upfile.close()
    downfile.close()
   
#    up_vbs_wrapper=open('virtual**up.vbs','w')
#    up_vbs_wrapper.write('Set objShell = CreateObject("Wscript.shell")\ncall objShell.Run("virtual**up.bat",0,FALSE)')
#    up_vbs_wrapper.close()
#    down_vbs_wrapper=open('virtual**down.vbs','w')
#    down_vbs_wrapper.write('Set objShell = CreateObject("Wscript.shell")\ncall objShell.Run("virtual**down.bat",0,FALSE)')
#    down_vbs_wrapper.close()
   
    print "For p p t p on windows only, run virtual**up.bat before dialing to virtual**," \
          "and run virtual**down.bat after disconnected from the virtual**."

def generate_android(metric):
    results = fetch_ip_data()
   
    upscript_header=textwrap.dedent("""\
    #!/bin/sh
    alias nestat='/system/xbin/busybox netstat'
    alias grep='/system/xbin/busybox grep'
    alias awk='/system/xbin/busybox awk'
    alias route='/system/xbin/busybox route'
   
    OLDGW=`netstat -rn | grep ^0\.0\.0\.0 | awk '{print $2}'`
   
    """)
   
    downscript_header=textwrap.dedent("""\
    #!/bin/sh
    alias route='/system/xbin/busybox route'
   
    """)
   
    upfile=open('virtual**up.sh','w')
    downfile=open('virtual**down.sh','w')
   
    upfile.write(upscript_header)
    upfile.write('\n')
    downfile.write(downscript_header)
    downfile.write('\n')
   
    for ip,mask,_ in results:
        upfile.write('route add -net %s netmask %s gw $OLDGW\n'%(ip,mask))
        downfile.write('route del -net %s netmask %s\n'%(ip,mask))
   
    upfile.close()
    downfile.close()
   
    print "Old school way to call up/down script from openvirtual** client. " \
          "use the regular openvirtual** 2.1 method to add routes if it's possible"


def fetch_ip_data():
    #fetch data from apnic
    print "Fetching data from apnic.net, it might take a few minutes, please wait..."
    url=r'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest'
    data=urllib2.urlopen(url).read()
   
    cnregex=re.compile(r'apnic\|cn\|ipv4\|[0-9\.]+\|[0-9]+\|[0-9]+\|a.*',re.IGNORECASE)
    cndata=cnregex.findall(data)
   
    results=[]

    for item in cndata:
        unit_items=item.split('|')
        starting_ip=unit_items[3]
        num_ip=int(unit_items[4])
        datenum=unit_items[5]
        
        imask=0xffffffff^(num_ip-1)
        #convert to string
        imask=hex(imask)[2:]
        mask=[0]*4
        mask[0]=imask[0:2]
        mask[1]=imask[2:4]
        mask[2]=imask[4:6]
        mask[3]=imask[6:8]
        
        #convert str to int
        mask=[ int(i,16 ) for i in mask]
        mask="%d.%d.%d.%d"%tuple(mask)
        
        #mask in *nix format
        mask2=32-int(math.log(num_ip,2))
        
        results.append((starting_ip,datenum,mask2))
         
    return results


if __name__=='__main__':
    parser=argparse.ArgumentParser(description="Generate routing rules for virtual**.")
    parser.add_argument('-p','--platform',
                        dest='platform',
                        default='openvirtual**',
                        nargs='?',
                        help="Target platforms, it can be openvirtual**, mac, linux,"
                        "win, android. openvirtual** by default.")
    parser.add_argument('-m','--metric',
                        dest='metric',
                        default=5,
                        nargs='?',
                        type=int,
                        help="Metric setting for the route rules")
   
    args = parser.parse_args()
   
    if args.platform.lower() == 'openvirtual**':
        generate_ovirtual**(args.metric)
    elif args.platform.lower() == 'linux':
        generate_linux(args.metric)
    elif args.platform.lower() == 'mac':
        generate_mac(args.metric)
    elif args.platform.lower() == 'win':
        generate_win(args.metric)
    elif args.platform.lower() == 'android':
        generate_android(args.metric)
    else:
        print>>sys.stderr, "Platform %s is not supported."%args.platform
        exit(1)

我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2018-10-22 10:54 | 显示全部楼层
#      DST-ADDRESS        PREF-SRC        GATEWAY            DISTANCE
0 A S  0.0.0.0/0                          pppoe-out2                1
1 ADS  0.0.0.0/0                          10.126.128.1              0
2  DS  0.0.0.0/0                          116.8.216.1               0
3 ADC  10.126.128.1/32    10.126.216.244  pppoe-out2                0
4 ADC  116.8.216.1/32     116.8.216.16    pppoe-out1                0
5 ADC  192.168.14.0/24    192.168.14.1    bridge                    0

点评

你打的标记呢?那一条是标记的路由?  详情 回复 发表于 2018-10-22 19:54
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| 发表于 2018-10-22 19:54 | 显示全部楼层
本帖最后由 stevemorrislian 于 2018-10-22 19:56 编辑
168kingmu 发表于 2018-10-22 10:54
#      DST-ADDRESS        PREF-SRC        GATEWAY            DISTANCE
0 A S  0.0.0.0/0            ...

你打的标记呢?那一条是标记的路由?
print terse可以显示你打的标签路由
发表于 2018-10-21 12:31


我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| 发表于 2018-11-12 00:15 | 显示全部楼层
tyrit 发表于 2018-11-11 14:17
楼主实现了嘛?

是的,按目标地址分流没问题。不过总有点不尽如人意,比如,dns经常上不了twitter,而换一下就好了。比如8.8.8.8换到8·8·4·4,问题解决,过几天又上不了,换回8·8·8·8又好了。看来要做2套dns缓冲,国内一套,国外一套。

点评

清空dns缓存没用,很奇怪  详情 回复 发表于 2018-11-12 00:16
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| 发表于 2018-11-12 00:16 | 显示全部楼层
本帖最后由 stevemorrislian 于 2019-3-19 20:08 编辑
stevemorrislian 发表于 2018-11-12 00:15
是的,按目标地址分流没问题。不过总有点不尽如人意,比如,dns经常上不了twitter,而换一下就好了。比如 ...

清空dns缓存没用,很奇怪

把最外面的路由器的上行forwards DNS改一下,就全部清空了,看来还是DNS劫持有了新的做法

发现了TCP也有connections在53端口,查了资料UDP在53端口是dns的解析,tcp在53端口是dns的数据传送,所以把防火墙上加了
      chain=input action=add-src-to-address-list protocol=tcp src-address=!192.168.0.0/16 src-address-list=!172.16.0.0/12 address-list=BlockIPs
      address-list-timeout=1d in-interface=pppoe-out1 dst-port=53 log=no log-prefix=""


是不是彻底解决还不清楚
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

有疑问请添加管理员QQ86788181|手机版|小黑屋|Archiver|恩山无线论坛(常州市恩山计算机开发有限公司版权所有) ( 苏ICP备05084872号 )

GMT+8, 2024-3-28 20:16

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

| 江苏省互联网有害信息举报中心 举报信箱:js12377 | @jischina.com.cn 举报电话:025-88802724 本站不良内容举报信箱:68610888@qq.com 举报电话:0519-86695797

快速回复 返回顶部 返回列表