开源软件应用

在ubuntu上运行ctorrent 进行p2p文件分享

2018-01-28  本文已影响214人  中道心

前言

当有大的文件在一定时间内需要传输到很多接收端时, 采用ftp的中心化传输并不是明智的方案,比较高效的方法是采用p2p的分享模式。图形界面的程序需要人手工操作,若是命令行程序则灵活得多,可以自动化处理一些任务。若是在Linux系统下,支持命令行的p2p客户端不多,ctorrent是其中一个。
由此引出了如下的DIY操作步骤。

0. 编译源码

0.1 源码直接下载并编译

wget http://www.rahul.net/dholmes/ctorrent/ctorrent-dnh3.3.2.tar.gz

tar -zxvf ctorrent-dnh3.3.2.tar.gz

0.2 ubuntu 下下载源码并制作安装包

  1. 增加源代码的源
    /etc/apt/sources.list中添加了deb-src
  2. 查看是否有源码
    sudo apt-cache showsrc ctorrent
  3. 下载源码
    sudo apt-get source ctorrent
  4. 下载依赖
    sudo apt-get build-dep ctorrent
  5. 进入源码目录 cd ctorrent-1.3.4.dnh3.3.2
  6. 修改源码
  7. 提交源码修改
    dpkg-source --commit
  8. 编译更新的代码
    sudo dpkg-buildpackage
  9. 回到父目录
    cd ..
  10. 安装 sudo dpkg -i ctorrent_1.3.4.dnh3.3.2-4_amd64.deb

0.3 源码中的torrent大小限制修改.

vi btconfig.h 修改meta文件大小限制以便支持超大文件传输.

#define MAX_METAINFO_FILESIZ    (48*1024*1024)
slice 
piece length

0.4 可以从此下载

wget http://39.106.122.67/ctorrent-1.3.4.dnh3.3.2.largefile.tgz
wget http://39.106.122.67/ctorrent_1.3.4.dnh3.3.2-4_amd64.deb

1. 环境准备

1.1 系统环境

服务器或者客户端都可以设置为 ubuntu 16.04 ,方便快速安装。

sudo apt install cvs git
sudo apt install sshpass
sudo apt-get install zlib1g-dev
sudo apt-get install nethogs

1.2 防火墙的设置

如果Tracker服务器是在内网,那么需要在路由器或者防火墙上提供公网ip到内网ip的映射,并且添加对外提供服务的端口:

service port
tracker: 80, 6969
ctorrent: 2706 -- 2106
ctorrent control server: 2780
ssh: 22

需要确保运行相应程序的服务器上的端口没有被占用,或者不冲突。

为了排查问题可以清理防火墙策略

iptables -F

1.3 搭建Tracker索引服务器

下载OpenTracker依赖库及源码,编译。

cvs -d :pserver:cvs@cvs.fefe.de:/cvs -z9 co libowfat
cd libowfat
make
cd ..
# LEGACY: cvs -d:pserver:anoncvs@cvs.erdgeist.org:/home/cvsroot co opentracker
git clone git://erdgeist.org/opentracker
cd opentracker
make

1.3.1 Tracker服务器具有内网ip

执行, 同时通过80,6969对外提供服务

./opentracker  -i 172.16.252.249 -p 6969 -P 6969

或者不指定ip

./opentracker  -p 6969 -P 6969

其中 172.16.252.249 是内网ip,
对外公开的tracker地址则替换为外网ip
如果对外的外网ip是 39.106.199.67那么announce地址为:

任选其一即可:

http://39.106.199.67:6969/announce
udp://39.106.199.67:6969/announce

1.3.2 Tracker服务器本身有公网IP

无需做防火墙或者路由器上的映射,直接运行即可。若有防火墙,只开放对外服务的端口即可。

1.4 安装 CTorrent Control Server

wget http://www.rahul.net/dholmes/ctorrent/ctcs-1.4.1.tar.gz
tar -zxvf ctcs-1.4.1.tar.gz

1.5 加入服务器启动项

可以编辑 /etc/rc.local
加入
以便每次启动后自动运行

/dir_where_install/opentracker  -p 6969 -P 6969

#download & upload limit 100Mbps
/dir_where_install/ctcs  -d 12500 -u 12500

2. 运行ctorrent 制作种子与文件分享

2.1 文件分享端制作种子提供下载

可以使用官方的包安装,也可以使用前面提供的网址。

sudo apt install ctorrent

在需要分享种子并且对外提供下载的客户端上,可以执行如下命令:

ctorrent -t -u "http://39.106.199.67:6969/announce"  -s file15G.dat.torrent file15G.dat 

2.2 文件下载端下载种子并下载

ctorrent  file15G.dat.torrent

3. 运行流程与测试脚本

3.0 整个过程:

  1. 在具有公网IP的服务器上运行tracker server
  2. 运行CTorrent Control Server
  3. 生成或者得到原始文件.
  4. 在具有原始文件的服务器上制作种子文件并进行scp或者web分发
  5. 在接收文件的客户端运行ctorrent,具有原始文件的服务器上运行ctorrent

备注:

3.1 Server Tracker运行

nohup ./opentracker  -i 172.16.252.249 -p 6969 -P 6969 &

访问
http://39.106.199.67:6969/stats
进行验证

或者运行lsof -i
查看是否有6969端口监听

opentrack 21947 nobody    3u  IPv4  9350721      0t0  TCP 172.16.252.249:6969 (LISTEN)
opentrack 21947 nobody    5u  IPv4  9350722      0t0  UDP 172.16.252.249:6969 

3.2 运行CTorrent Control Server

cd ctcs-1.4.1
nohup ./ctcs > /dev/null &

访问
http://124.193.192.168:2780/
进行验证

3.3 构造测试用文件

cat makefile.sh

#!/bin/bash

# This script create a xM or xG file by parameter
# which added a 1M random head

if [ ! $# == 1 ]; then
  echo "Usage: $0 filesize "
  echo "          filesize example: 100M, 10G "
  exit 1
fi

size=$1
fn=file${size}.dat
## add random head
dd if=/dev/urandom of=$fn count=1024 bs=1024
truncate -s +$size $fn
./makefile.sh  150G

3.4 制作种子并分发到接收端

注意: 使用sshpass之前需要先建立两个主机之间的公私钥存储.避免运行后没有反应。 可以直接手工ssh 链接,回答yes存储即可。

cat seedp2p.sh

if [ ! $# == 1 ]; then
  echo "Usage: $0 filename/dirname "
  echo "      if dirname, please copy to current dirtory."
  exit 1
fi

fn=$1
#md5sum $fn > $fn.md5

# make seed.
rm -f $fn.torrent
ctorrent -t -u "http://39.106.199.67:6969/announce"  -s $fn.torrent $fn

# transfer seed to all clients.

## server passowd
server=10.1.36.100
user=root
passwd=XXX
storedir=/data

sshpass -p $passwd  scp $fn.torrent $user@$server:/data/
sshpass -p $passwd  scp $fn.md5 $user@$server:/data/

# after clean file , reomte to call daemon to p2p file transfer, when finished, all client will exit without seeding with -e0 parameter.
sshpass -p $passwd ssh $user@$server "cd $storedir; rm -rf $fn; ctorrent -d -e0 $fn.torrent"

# start p2p transfer in daemon
ctorrent -d -U 100000  -S localhost:2780  $fn.torrent

举例:

#filename
./seedp2p.sh file150G.dat

# dirname
cp /home/test .
./seedp2p.sh test

3.5 查看最后文件校验结果

可以用md5sum 或者 sha256 校验
或者  
ctorrent -c xx.torrent

References

CTorrent: Command-line Based Simple BitTorrent Client for Ubuntu Linux
https://www.hecticgeek.com/2012/02/command-line-bittorrent-ubuntu-linux/

OpenTracker:
http://erdgeist.org/arts/software/opentracker/

CTorrent
https://sourceforge.net/projects/ctorrent/

Enhanced CTorrent
http://www.rahul.net/dholmes/ctorrent/
https://sourceforge.net/projects/dtorrent/files/

在linux下安装ctorrent & Enhanced CTorrent下载种子文件
https://blog.phpgao.com/ctorrent.html

Thread: BitTorrent optimization and troubleshooting guide
https://ubuntuforums.org/showthread.php?t=1259923

https://torrentfreak.com/how-to-make-the-best-torrents-081121/

上一篇下一篇

猜你喜欢

热点阅读