V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
myliyifei
V2EX  ›  Linux

如何屏蔽 SS 服务器访问大陆的 IP 段?

  •  
  •   myliyifei · 2015-01-31 15:12:39 +08:00 · 10448 次点击
    这是一个创建于 3379 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我已经下载并得到了chnroute.list,也创建好了ipset。


    查了一下 ss好像有个--acl选项,[--acl <acl_file>] config file of ACL (Access Control List)
    但是貌似给ss-server没有用。 这个是个ss-local用的吗?

    但是我先前尝试在OUTPUT reject所有匹配ipset的访问,并且用ownerid 识别出只有SS的流量,但是想起来这样也屏蔽了SS返回给客户端的报文。



    目前想到几个思路,

    A 是在INPUT标记连接到SS-SERVER的端口 connmark ,然后在OUTPUT的屏蔽访问IP段ACL之前accept 标记的连接,不过还没有测试

    B 是在OUTPUT 放行 SS-SERVER的SPORT端口发出的连接,然后屏蔽国内的段。但是缺点是多端口多用户,就要写多条了。

    C 在SS前面在做一个代理服务器,用PID 标识那个代理服务器并限制其访问IP端。缺点应该是耗内存,然后https的透明代理要生成证书。

    大家有更优雅的方法吗
    25 条回复    2015-02-12 15:16:38 +08:00
    Showfom
        1
    Showfom  
       2015-01-31 16:20:28 +08:00 via iPhone
    iptables?
    chinawrj
        2
    chinawrj  
       2015-01-31 16:22:17 +08:00
    iptables -A OUTPUT -p tcp --sport server_port -j ACCEPT
    iptables -A OUTPUT -p tcp -m geoip --dst-cc CN -j DROP

    另外iptables支持端口范围匹配
    chinawrj
        3
    chinawrj  
       2015-01-31 16:24:07 +08:00
    还有用啥,ipset啊,麻烦
    tobyxdd
        4
    tobyxdd  
       2015-01-31 16:29:18 +08:00
    tobyxdd
        5
    tobyxdd  
       2015-01-31 16:31:05 +08:00
    可以只屏蔽http/https协议 或者直接禁止访问大陆IP的80和443端口
    myliyifei
        6
    myliyifei  
    OP
       2015-01-31 16:35:36 +08:00
    @tobyxdd 比较旧了,我昨天看一下,最新的58XX条了。
    tobyxdd
        7
    tobyxdd  
       2015-01-31 16:49:59 +08:00
    @myliyifei 这个是每24小时更新的 行数少是因为相邻段自动合并了
    myliyifei
        9
    myliyifei  
    OP
       2015-01-31 18:17:44 +08:00
    @KyonLi 难点不是这张表啊,这个list已经有了
    KyonLi
        10
    KyonLi  
       2015-01-31 18:18:21 +08:00
    @myliyifei 对啊,所以一条iptables规则就行了
    myliyifei
        11
    myliyifei  
    OP
       2015-01-31 18:19:30 +08:00
    @tobyxdd 这个服务不错。。
    myliyifei
        12
    myliyifei  
    OP
       2015-01-31 18:26:00 +08:00
    @KyonLi NO, 你只加了一条规则,禁止localhost访问国内IP段,那么SS如何给客户端发送数据包呢? 我目前是实现了端口的方式,还要自定义链
    KyonLi
        13
    KyonLi  
       2015-01-31 18:46:25 +08:00
    @myliyifei 哦,对,忽略这个问题了
    ryd994
        14
    ryd994  
       2015-01-31 22:41:05 +08:00 via Android
    你先把related允许掉嘛,
    只reject new就行
    myliyifei
        15
    myliyifei  
    OP
       2015-01-31 22:43:05 +08:00
    @ryd994 related 是相关的连接啊,比如ftp-control 产生的ftp-data
    ryd994
        16
    ryd994  
       2015-01-31 22:55:46 +08:00 via Android
    @myliyifei 我错了……
    established
    advans
        17
    advans  
       2015-01-31 23:03:17 +08:00
    不明白为什么非要费力气做这么不讨好的事儿!
    myliyifei
        18
    myliyifei  
    OP
       2015-01-31 23:06:11 +08:00
    @ryd994 能完整的说一下吗?
    ryd994
        19
    ryd994  
       2015-01-31 23:14:45 +08:00 via Android
    @myliyifei
    output,如果是established,related ,允许
    output,如果是 new,判断geoip规则
    这样就是只限制出站的。
    因为入站连接的返回部分会被作为established允许。
    myliyifei
        20
    myliyifei  
    OP
       2015-01-31 23:18:33 +08:00   ❤️ 1
    @ryd994 恩,new的话,我直接用ipset判定好了。加不加NEW都没有意义吧?另外,SS会产生 related这个状态吗? netstat可以看到related状态吧
    liruqi
        21
    liruqi  
       2015-02-01 00:42:01 +08:00 via iPhone
    http://v2ex.com/t/149771#reply13

    这时我做过。不过自己用的时候加了参数,规则只对运行 shadowsocks 的 user 生效。
    ryd994
        22
    ryd994  
       2015-02-01 00:49:56 +08:00
    @myliyifei ss本身没有,related也包括tcp相关的icmp,这些最好还是allow,不过问题不大
    jerryjhou
        23
    jerryjhou  
       2015-02-02 06:11:24 +08:00 via iPad
    @myliyifei 点成感谢了。。。直接按照 @chinawrj 的方法就可以了,第一条命令就是放行所有SS所用端口的所有流量,然后屏蔽和中国的通信。不过你最好把SSH也放行,否则你得挂代理才能连上SSH
    myliyifei
        24
    myliyifei  
    OP
       2015-02-12 13:04:30 +08:00
    @ryd994 geoip准确吗? 和ipset的性能比较呢? 下面的文档说ipset用的是Jenkins3,但是搜不到这个



    geoip vs ipset
    xt_geoip uses the (probably) most efficient format, a (non-compressed) packed blob. Loading one country into the kernel costs as much as the file on disk.

    Since ipset does not support arbitrary IPaddr–IPaddr ranges, one would need to approximate that using, for example, multiple Network/Prefixlength entries. Furthermore, if a hash set type is used, you can assume that, by the nature of hashes and/or trees, some buckets remain empty and/or additional metadata is required. The memory footprint with an ipset-based geoip thus is naturally larger. User reports[1] indicate it can become two orders of magnitude higher in certain cases (iptreemap).

    xt_geoip's lookup time is O(log2(ranges)), so to lookup an address within 20,000 ranges, at most 15 iterations each with address comparisons (at most 3) are required. ipset uses Jenkins3 for hashing, which has a certain time cost of its own.
    ryd994
        25
    ryd994  
       2015-02-12 15:16:38 +08:00
    @myliyifei geoip准不准确看你编译的时候用的数据库准不准确啊
    效率的话可能geoip高点,毕竟是专门做这个,规则多了以后本身就是个负担
    上面那段说的就是ipset可能需要更多更复杂的规则来实现,内存占用高两个数量级
    从理论上来讲呢,ipset用的是hash,geoip是二分,O(1)比O(log(n))要好。
    但是复杂度只代表n足够大的时候的优劣,实际时间多少完全是另一回事。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2267 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 08:26 · PVG 16:26 · LAX 01:26 · JFK 04:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.