shf19961008 发表于 2018-11-22 19:48:47

uTorrent 自动屏蔽迅雷脚本

本帖最后由 shf19961008 于 2018-11-23 10:20 编辑

uTorrent 自动屏蔽迅雷脚本
GitHub: https://github.com/ShenHongFei/utorrent-block-xunlei


功能
    每隔 3 分钟,自动检查 uTorrent 已连接的用户列表,找出迅雷客户端,强制断开,不给吸血雷上传任何数据,并将用户 IP 加入黑名单阻止其再次连接,把带宽留给正规 BT 客户端。


屏蔽列表
    -XL0012-***
    Xunlei/***
    7.x.x.x
    Xfplay


实现方法
    1.根据 uTorrent 的 WebUI API 发送 http request 获取所有已连接用户(peers)信息
    2.按照 client name 筛选出使用迅雷的 peer IP,写入 ipfilter.dat 文件
    3.发送 http request 让 uTorrent 重新加载 ipfilter.dat
    4.uTorrent 禁止 ipfilter.dat 中的 IP 连接


脚本


cheerio = require 'cheerio'
request = require 'request-promise-native'
Sugar   = require('sugar').extend()

log = console.log.bind console

# 自行修改脚本中 root_url, auth, ipfilter_path 相关内容
# 检查间隔时间可在脚本中自定义,IP黑名单(ipfilter.dat) 建议每天清空一次。

utorrent=
    init: ->
      @root_url= 'http://127.0.0.1:10000/gui/'
      @cookies= request.jar()
      token_html = await request
            uri: @root_url + 'token.html'
            auth:
                user: 'shf'
                pass: 'xxxxxx'
            jar: @cookies
      $ = cheerio.load token_html
      @token = $('div').text()
      await @get_torrents()
      
    call: ({api='', params, method='GET'}={})->
      JSON.parse await request
            uri: @root_url + api
            method: method
            qs:{
                token: @token
                params...
            }
            auth:
                user: 'shf'
                pass: 'xxxxxx'
            jar: @cookies
   
    get_torrents: ->
      @torrents = (await @call params: list: 1).torrents
      @hashes = @torrents.map (x)-> x
      
    get_peers: (hash)->
      resp = await @call params:
            action: 'getpeers'
            hash: hash
      resp.peers
   
    get_all_peers: ->
      peers = []
      for hash in @hashes
            peers.append((await @get_peers hash))
      peers = for peer in peers
            ip: peer
            client: peer
      peers.unique().sortBy 'client'
      
    block: ->
      await @get_torrents()
      peers = await @get_all_peers()
      blocks = peers.filter (x)-> x.client.match /(-XL0012-)|(Xunlei)|(^7\.)|(Xfplay)/i
      if blocks.isEmpty()
            log 'no xunlei clients detected, current peers:'
            log peers
            return
      log 'block', blocks
      
      ipfilter_path = 'C:/Users/shf/AppData/Roaming/uTorrent/ipfilter.dat'
      fs.writeFileSync(ipfilter_path, fs.readFileSync(ipfilter_path, 'UTF8').trim().split('\n').append(x.ip for x in blocks).unique().join('\n') + '\n')
      # log 'ipfilter.dat updated'
      
      await @call params:
            action: 'setsetting'
            s: 'ipfilter.enable'
            v: '1'
      # log 'ipfilter.dat reloaded'
      
    unblock: ->
      await @call params:
            action: 'setsetting'
            s: 'ipfilter.enable'
            v: '0'
   
    run: ->
      await @block()
      @task = setInterval =>
            await @block()
      , 3*60*1000
      
    stop: ->
      clearInterval @task

main= ->
    await utorrent.init()
    await utorrent.run()

main()



依赖
    uTorrent
      启用 uTorrent 网页界面
      在 uTorrent 目录下保证 ipfilter.dat 文件存在(若不存在则新建空白 ipfilter.dat 文件),脚本会在原有 ipfilter.dat 文件内容之后添加被屏蔽的迅雷 IP,不影响已有内容及其功能。
      高级选项
            ipfilter.enable: true
            bt.use_rangeblock: false
    Node.js
    CoffeeScript
    NPM Packages
      Sugar.js
      request-promise-native
      cheerio


日志
    未检测到迅雷时
当前已连接用户
[ { ip: '180.94.154.163', client: 'µTorrent/3.5.4.0' },
{ ip: '223.140.248.38', client: 'BitComet 1.53' },
{ ip: '101.88.108.19', client: 'µTorrent/2.2.1.0' },
{ ip: '39.161.242.50', client: 'Unknown FD/5.1.0.0' },
{ ip: '171.88.70.72', client: 'Transmission 2.94' },
{ ip: '218.79.69.196', client: ' µTorrent/3.0.0.0' },
{ ip: '123.204.251.13', client: 'BitComet 1.51' },
{ ip: '154.103.221.22', client: 'qBittorrent 4.1.3' },
    检测到迅雷时
使用迅雷的用户
[ { ip: '183.25.54.216', client: '-XL0012-溶S鑋亾#+4厓' },
{ ip: '223.81.192.235', client: '-XL0012-輓%??1涙鷉' },

reading C:/Users/shf/AppData/Roaming/uTorrent/ipfilter.dat
wrote C:/Users/shf/AppData/Roaming/uTorrent/ipfilter.dat
ipfilter.dat updated
ipfilter.dat reloaded
    uTorrent Log
      勾选 记录用户通讯信息 > 记录用户拦截连接
Loaded ipfilter.dat (51 entries)
IpFilter blocked peer 223.81.192.235
IpFilter blocked peer 223.81.192.235
IpFilter blocked peer 223.81.192.235
IpFilter blocked peer 183.25.54.216
IpFilter blocked peer 223.81.192.235
...









yswysc 发表于 2018-11-22 22:08:49

本帖最后由 yswysc 于 2018-11-23 09:46 编辑

首先感谢楼主提供的脚本,非常有帮助。
其次还是冒昧地问一下有没有什么办法能让此脚本搭配屏蔽离线服务器的过滤文件使用呢?
还有论坛支持代码插入功能,如果能将代码放入代码框中那就更好了。
再次感谢楼主的工作!
{:8_729:}贴一下我使用的屏蔽离线服务器的ipfilter.dat文件。

shf19961008 发表于 2018-11-23 00:19:37

yswysc 发表于 2018-11-22 22:08
首先感谢楼主提供的脚本,非常有帮助。
其次还是冒昧地问一下有没有什么办法能让此脚本搭配屏蔽离线服务器 ...

只要 ipfilter.dat 文件存在就可以了,脚本会在原有 ipfilter.dat 文件内容之后添加被屏蔽的迅雷 IP,不影响已有内容及其功能。

其实屏蔽迅雷的 IP 和屏蔽离线服务器的 IP 原理上是一样的,都是用的这个文件。这个脚本不过是把动态添加迅雷 IP 这个操作自动化了,也就是看到一个封一个。
如果离线服务器的客户端有相应的特征的话也可以动态的屏蔽,不过我没见过离线服务器的客户端,不知道它有什么特征。

编辑器“添加代码文字”这个功能不管我怎么粘贴代码,不管我用空格还是 Tab,粘贴之后缩进都没了,实在没法用。
测试“添加代码文字”:
无缩进
1缩进
2缩进

yswysc 发表于 2018-11-23 05:49:20

shf19961008 发表于 2018-11-23 04:19
只要 ipfilter.dat 文件存在就可以了,脚本会在原有 ipfilter.dat 文件内容之后添加被屏蔽的迅雷 IP,不 ...

可能论坛的编辑器出现了一些问题,直接粘贴的确会产生不预期的效果,对此造成的不便非常抱歉。
下面提供一种解决办法:
点开高级模式(已发出的帖子点击编辑按钮),在顶部的工具栏中找到纯文本(如没发现请点击“高级”展开工具栏)
直接将代码粘贴进去,并用正文(此处第二个“o”使用了特殊字符替代来举例以免bbcode失效)包住代码就可以啦。

e.g:

      1Tab
    4Spaces


ab222525 发表于 2018-11-23 09:30:25

请问,qbittorrent可以用吗

shf19961008 发表于 2018-11-23 10:27:31

ab222525 发表于 2018-11-23 09:30
请问,qbittorrent可以用吗

不能用,而且 qBittorrent 下载速度比 uTorrrent 慢,不推荐使用。

点点菌 发表于 2018-11-23 11:54:15

感谢楼主分享~

贴一个启用WebUI的教程:
https://tieba.baidu.com/p/2241281124?pn=1

WebUI.zio下载(3.0版本以上不用下载,直接启用即可):
(不需解压,直接放ut2的根目录下就行了)
http://pan.baidu.com/share/link?shareid=2392620671&uk=2466846431

c0re100 发表于 2018-11-23 13:38:43

ab222525 发表于 2018-11-23 09:30
请问,qbittorrent可以用吗

qBit請直接食用 ლ(́◕◞౪◟◕‵ლ)
github.com/c0re100/qBittorrent-Enhanced-Edition/releases

ltycomputer 发表于 2018-11-23 18:03:04

我就在用楼上的那个增强版,支持屏蔽国产BT软件和国产离线下载服务器,很好用

速度下行跑到100MB/s都没问题

wuskyjian 发表于 2018-12-3 18:31:29

ltycomputer 发表于 2018-11-23 18:03
我就在用楼上的那个增强版,支持屏蔽国产BT软件和国产离线下载服务器,很好用

速度下行跑到100MB/s都没问 ...

一样,我这里utorrent很慢只能到20-30MB这样,用那个增强版的qtorrent可以跑到90-100MB/s, 而且也自动屏蔽吸血客户端。
页: [1] 2 3
查看完整版本: uTorrent 自动屏蔽迅雷脚本