[搜片神器]直接从DHT网络下载BT种子的方法
DHT抓取程序开源地址:https://github.com/h31h31/H31DHTDEMO
数据处理程序开源地址:https://github.com/h31h31/H31DHTMgr
DHT系列文章:
--------------------------------------------------------------------------------------------------------------------
看懂此文章需要提前看明白上面的系列文章,还需要你有TCP网络编程和bencode编码方法基础上,如果都看不明白,可以到娱乐区http://www.sosobta.com 去看看,休息下...
在介绍了这么多期文章后,最后介绍BT网络里面一个比较重要种子下载协议,方便大家知道如何从DHT网络直接下载种子的问题.
先说下我们目前下载电影等文件是如何下载的,比如我们有个BT种子,就可以去下载对应的文件,但如果我们只有个文件名字,如何去找BT种子呢?
首先我们可以去通过搜索得到磁连接,然后就由此字符串去下载对应的种子文件和电影等信息,但如果没有网站让你下载种子,我们又当如何去搜索这个种子呢?
目前我们下载BT种子有两种方式:
- 通过HTTP直接从WEB服务器上下载,这种直接方便,比如从迅雷服务器上下载种子,
- 再就是通过BT软件从网络里面去获取BT网络里面专门有个下载种子的协议文件,只能下载种子,然后种子下载好后就可以交给BT软件来下载数据了.
如何从DHT网络下载种子,必须先看两个协议文章:
http://www.bittorrent.org/beps/bep_0009.html
http://www.bittorrent.org/beps/bep_0010.html
这里面有介绍,但还是需要说明一下如何操作的流程方便大家更好的理解.
我们的代码流程必须还是基于 DHT抓取程序开源地址:https://github.com/h31h31/H31DHTDEMO 之上,因为是从DHT网络里面获取数据,
需要我们在此之上操作后续流程.
之前的DHT有SEARCH的相关代码来搜索这个HASH对应的哪些IP在提供下载.
/* This is how you trigger a search for a torrent hash. If port (the second argument) is non-zero, it also performs an announce.
Since peers expire announced data after 30 minutes, it's a good idea to reannounce every 28 minutes or so. */
if(searching) {
//m_dht.dht_random_bytes((void*)hashList[2],20);
if(m_soListen >= )
m_dht.dht_search(hashList[], , AF_INET, DHT_callback, this);
if(s6 >= )
m_dht.dht_search(hashList[], , AF_INET6, DHT_callback, this);
searching = ;
}
搜索到对方返回的IP信息和端口号后,大家可以分析dht.c里面的函数代码dht_periodic(const void *buf, size_t buflen,const struct sockaddr *fromAddr, int fromlen,time_t *tosleep,dht_callback *callback, void *closure)函数里面的ANNOUNCE_PEER返回请求里面带有对方表明自己此BT种子对应的认证码peerid.
dht_periodic(const void *buf, size_t buflen,const struct sockaddr *fromAddr, int fromlen,time_t *tosleep,dht_callback *callback, void *closure)
函数里面的ANNOUNCE_PEER case ANNOUNCE_PEER:
_dout("Announce peer!From IP:%s:%d\n",inet_ntoa(tempip->sin_addr),tempip->sin_port);
new_node(id, fromAddr, fromlen, ); if(id_cmp(info_hash, zeroes) == )
{
_dout("Announce_peer with no info_hash.\n");
send_error(fromAddr, fromlen, tid, tid_len,, "Announce_peer with no info_hash");
break;
}
if(!token_match(token, token_len, fromAddr)) {
_dout("Incorrect token for announce_peer.\n");
send_error(fromAddr, fromlen, tid, tid_len,, "Announce_peer with wrong token");
break;
}
if(port == ) {
_dout("Announce_peer with forbidden port %d.\n", port);
send_error(fromAddr, fromlen, tid, tid_len,, "Announce_peer with forbidden port number");
break;
}
if(callback)
{
(*callback)(closure, DHT_EVENT_ANNOUNCE_PEER_VALUES, info_hash,(void *)fromAddr, port,id);//此ID就是peerid,
}
知道了对应的IP,端口号,还有种子ID号,就可以向对方发送请求了.
获取HASH是通过UDP网络,但下载BT种子是通过TCP来处理,相当于别人是TCP服务器,我们连接过去,直接下载对应PEERID的种子就行了.
BT种子在DHT网络下载流程
先看http://www.bittorrent.org/beps/bep_0010.html协议介绍,我们必须先握手
此包构造比较简单,按照格式进行组装就行了,然后发送出去,对方就会回应自己是什么客户端的软件提供种子下载.
void CH31BTMgr::Encode_handshake()
{
//a byte with value 19 (the length of the string that follows);
//the UTF-8 string "BitTorrent protocol" (which is the same as in ASCII);
//eight reserved bytes used to mark extensions;
//the 20 bytes of the torrent info hash;
//the 20 bytes of the peer ID.
char btname[256];
memset(btname,0,sizeof(btname));
sprintf(btname,"BitTorrent protocol");
char msg[1280];
memset(msg,0,sizeof(msg));
msg[0]=19;
memcpy(&msg[1],btname,19);
char ext[8];
memset(ext,0,sizeof(ext));
ext[5]=0x10; memcpy(&msg[20],ext,8);
memcpy(&msg[28],m_hash,20);
memcpy(&msg[48],m_peer_id,20);
int res1=Write(msg, 68);//TCP发送消息
}
在发送握手后,我们可以接着发送种子数据请求包,需要学习http://www.bittorrent.org/beps/bep_0009.html 里面的内容:
extension header
The metadata extension uses the extension protocol (specified in BEP ) to advertize its existence. It adds the "ut_metadata" entry to the "m" dictionary in the extension header hand-shake message. This identifies the message code used for this message. It also adds "metadata_size" to the handshake message (not the "m" dictionary) specifying an integer value of the number of bytes of the metadata. Example extension handshake message: {'m': {'ut_metadata', }, 'metadata_size': }
extension message
The extension messages are bencoded. There are different kinds of messages: 0 request
1 data
2 reject
The bencoded messages have a key "msg_type" which value is an integer corresponding to the type of message. They also have a key "piece", which indicates which part of the metadata this message refers to. In order to support future extensability, an unrecognized message ID MUST be ignored.
这就需要会bencode的相关代码,这个大家可以网上搜索进行编译,如果实现搞不定,可以留下邮箱我将此类代码发送给你,其实也是网上收集整理的.
void CH31BTMgr::Encode_Ext_handshake()
{
entry m;
m["ut_metadata"] = ;
entry e;
e["m"]=m; char msg[];
char* header = msg;
char* p = &msg[];
int len = bencode(p, e);
int total_size = + len;
namespace io = detail;
io::write_uint32(total_size, header);
io::write_uint8(, header);
io::write_uint8(, header); int res1=Write(msg, len + );
}
如果别人回应的是2,那就直接退出吧,说明别人拒绝了你.
如果回应是1,则返回的是数据区,每块是16K大小,最后一包不是.
data
The data message adds another entry to the dictionary, "total_size". This key has the same semantics as the "metadata_size" in the extension header. This is an integer. The metadata piece is appended to the bencoded dictionary, it is not a part of the dictionary, but it is a part of the message (the length prefix MUST include it). If the piece is the last piece of the metadata, it may be less than 16kiB. If it is not the last piece of the metadata, it MUST be 16kiB. Example: {'msg_type': , 'piece': , 'total_size': }
d8:msg_typei1e5:piecei0e10:total_sizei34256eexxxxxxxx...
The x represents binary data (the metadata).
下面给出如何进行提交我需要第几包的数据代码:
void CH31BTMgr::write_metadata_packet(int type, int piece)
{
ASSERT(type >= && type <= );
ASSERT(piece >= ); entry e;
e["msg_type"] = type;
e["piece"] = piece; char const* metadata = ;
int metadata_piece_size = ; if (type == )
{
e["total_size"] = ;
int offset = piece * * ;
//metadata = m_tp.metadata().begin + offset;
metadata_piece_size = (std::min)(int( - offset), * );
} char msg[];
char* header = msg;
char* p = &msg[];
int len = bencode(p, e);
int total_size = + len + metadata_piece_size;
namespace io = detail;
io::write_uint32(total_size, header);
io::write_uint8(, header);
io::write_uint8(m_message_index, header); int res1=Write(msg, len + );
}
在接收到一包请求后我们才可以继续下一包的请求,下面给了我们如何解析这一包的问题代码:
// 处理一个完整的包数据
bool CH31BTMgr::DeCodeFrameData(char * buffer,int buflen)
{
char * p = (char *)mhFindstr(buffer, buflen, "ut_metadatai", );
if(p)
{
m_message_index=atoi(&p[]);
if(m_message_index==)
{
return false;
}
write_metadata_packet(,);
char filename[];
memset(filename,,sizeof(filename));
sprintf(filename,"%s\\torrent.txt",m_workPath);
DelFile(filename);
} p = (char *)mhFindstr(buffer, buflen, "metadata_sizei", );
if(p)
{
m_metadata_size=atoi(&p[]);
m_fileCnt=(int)(m_metadata_size/)+;
} p = (char *)mhFindstr(buffer, buflen, "msg_typei", );
if(p)
{
int type1=atoi(&p[]);
if(type1==)
{
p = (char *)mhFindstr(buffer, buflen, "piecei", );
if(p)
{
int piece=atoi(&p[]);
p = (char *)mhFindstr(buffer, buflen, "total_sizei", );
if(p)
{
int total_size=atoi(&p[]);
p = (char *)mhFindstr(buffer, buflen, "ee", );
if(p)
{
//保存数据
FILE* pfile=NULL;
char filename[]; memset(filename,,sizeof(filename));
sprintf(filename,"%s\\torrent.txt",m_workPath);
char openmethod[]="a";
if(piece==)
sprintf(openmethod,"w");
if((pfile=fopen(filename,openmethod))!=NULL)
{
if((piece+)**<total_size)
{
fseek(pfile,(piece)**,SEEK_SET);
fwrite(&p[],,*,pfile);
write_metadata_packet(,piece+);
fclose(pfile);
}
else
{
fwrite(&p[],,total_size-(piece)**,pfile);
fclose(pfile);
ManageTorrentFileToRealFile(filename);
}
}
}
}
}
}
else if(type1==)
{
return false;
}
} return true;
}
void * mhFindstr(const void *haystack, size_t haystacklen,const void *needle, size_t needlelen)
{
const char *h =(const char *) haystack;
const char *n =(const char *) needle;
size_t i;
/* size_t is unsigned */
if(needlelen > haystacklen)
return NULL;
for(i = 0; i <= haystacklen - needlelen; i++) {
if(memcmp(h + i, n, needlelen) == 0)
return (void*)(h + i);
}
return NULL;
}
接下来说下如何进行快速调试的问题:
第一次调试也很天真的等着DHT网络上的数据过来,需要等很久,而且调试总是发现别人不回应,要么就是拒绝,经过一段时间后,
问朋友总是不对问题,结果是协议没有构造对.下面就需要注意的地方总结下:
1.一定要接收到别的人PEERID后才能够与别人交流,不然别人肯定不理你;
2.构造协议调试不能够在外网络上调试,最好大家将mono-monotorrent源代码下载回来,调试分析下,本地开启服务器;
3.通过本地与mono-monotorrent进行调试,你就可以分析出是哪里不对的问题,是不是协议哪些封装得不对的问题.
4.通过DHT网络下载回来的种子肯定是最新的,WEB下载的可能还没有呢..
5.通过协议下载回来的种子好像没有announce-list,不知道为什么不提供一些内容,可能还有些什么关键地方没有下载,分析mono-monotorrent代码里面就是不提供下载,希望高手指点.
6.TCPClient接收数据区需要开到16K以上,这样方便处理,当然如果会前后拼接包就更好.
7.如果需要bencode相关的编码C++代码,可以在此留言或者给h31h31#163.com发邮件.
如果此文章看不太明白,请先看看之前的文章,分析调试下代码,再来学习此文章可能就比较懂一些.
希望有了解的朋友更好的交流和进步.在此留言学习讨论.
希望大家多多推荐哦...大家的推荐才是下一篇介绍的动力...
[搜片神器]直接从DHT网络下载BT种子的方法的更多相关文章
- Linux使用Aria2命令下载BT种子/磁力/直链文件 转载
Linux使用Aria2命令下载BT种子/磁力/直链文件 博主: Rat's 发布时间:2017 年 10 月 10 日 26725 次浏览 8 条评论 1073 字数 分类:主机教程 首页 正文 分 ...
- [搜片神器]之DHT网络爬虫的代码实现方法
继续接着第一篇写:使用C#实现DHT磁力搜索的BT种子后端管理程序+数据库设计(开源)[搜片神器] 谢谢园子朋友的支持,已经找到个VPS进行测试,国外的服务器: http://www.sosobta. ...
- PHP语言编写的磁力搜索工具下载BT种子 支持transmission、qBittorrent
磁力搜索网站2020/01/12更新 https://www.cnblogs.com/cilisousuo/p/12099547.html PT种子.BT种子搜索功能 IYUU自动辅种工具,目前能对国 ...
- 在Linux服务器上配置Transmission来离线下载BT种子
Transmission简介 Transmission是一种BitTorrent客户端,特点是跨平台的后端和简洁的用户界面,硬件资源消耗极少,支持包括Linux.BSD.Solaris.Mac OS ...
- [C#搜片神器] 之P2P中DHT网络爬虫原理
继续接着上一篇写:使用C#实现DHT磁力搜索的BT种子后端管理程序+数据库设计(开源)[搜片神器] 昨天由于开源的时候没有注意运行环境,直接没有考虑下载BT种子文件时生成子文件夹,可能导致有的朋友运行 ...
- [搜片神器]之DHT网络爬虫的C++程序初步开源
回应大家的要求,特地整理了一开始自己整合的代码,这样最简单,最直接的可以分析流程,至于文章里面提供的程序界面更多,需要大家自己开发. 谢谢园子朋友的支持,已经找到个VPS进行测试,国外的服务器: ht ...
- [搜片神器]使用C#实现DHT磁力搜索的BT种子后端管理程序+数据库设计(开源)
谢谢园子朋友的支持,已经找到个VPS进行测试,国外的服务器:http://www.sosobta.com 大家可以给提点意见... 出售商业网站代码,万元起,非诚勿扰,谢谢. 联系h31h31 a ...
- 使用C#实现DHT磁力搜索的BT种子后端管理程序+数据库设计(开源)
使用C#实现DHT磁力搜索的BT种子后端管理程序+数据库设计(开源) 先直接上程序界面,了解整体工作流程是什么样子的,求服务器进行挂机测试,需要固定IP,空间大概需要10G左右(主要是BT种子占用空间 ...
- [搜片神器]BT种子下载超时很多的问题分析
继续接着第一篇写:使用C#实现DHT磁力搜索的BT种子后端管理程序+数据库设计(开源)[搜片神器] 谢谢园子朋友的支持,已经找到个VPS进行测试,国外的服务器: h31bt.org 大家可以给提点意 ...
随机推荐
- SpringMVC + MyBatis 环境搭建(转)
本文转自:http://blog.csdn.net/zoutongyuan/article/details/41379851 源码地址:https://github.com/starzou/quick ...
- IDEA中利用JUnit进行单元测试
打开IntelliJ IDEA工具,Alt+Ctrl+S,打开设置窗口,点击进入Plugins. 从插件资源库中搜索JunitGenerator V2.0版本
- 两则C++知识点
返回引用遵守的两条准则: 1. 不能返回局部变量: 2. 不能返回new出的量,因为可能是临时对象. const的用法: 1. 基本数据类型的写限制: 2. 函数的传入以及返回参数: 3. 类内的数据 ...
- Spring中通配符
一.加载路径中的通配符:?(匹配单个字符),*(匹配除/外任意字符).**/(匹配任意多个目录) classpath:app-Beans.xml 说明:无通配符,必须完全匹配 classpath: ...
- Partitioner
partitioner 是map中的数据映射到不同的reduce时的根据.一般情况下,partitioner会根据数据的key来把数据平均分配给不同的reduce,同时保证相同的key分发到同一个re ...
- USACO Section 2.3: Controlling Companies
这题的dp里的check里的函数要考虑k control i control j和i control j control k的情况 /* ID: yingzho1 LANG: C++ TASK: co ...
- JS实现 页面提交防刷新等待提示
//关闭等待窗口 function closediv() { //Close Div document.body.removeChild(document.getElementById("b ...
- 每天一个小算法(Shell Sort2)
希尔排序: 伪代码: input: an array a of length n with array elements numbered 0 to n − 1 inc ← round(n/2) wh ...
- DHT11温湿度传感器
一.硬件介绍 RH是相对湿度,是用零点温度来定义的,一般,RH在45%~65%之间最为合适. 注:NTC为热敏电阻,输出为:单总线数字信号,单线双向串行通讯. 注:上拉电阻情况下,配置为开漏输出,可以 ...
- 1.Cadence16.5的安装教程[原创]
http://jingyan.baidu.com/article/6d704a1319107a28db51cac9.html