一、自编译安装方式

1、安装编译环境:apt install -y wget libcurl4-openssl-dev libevent-dev ca-certificates libssl-dev pkg-config build-essential intltool libxml2-dev libgcrypt-dev libssl-dev git

2、下载源码并解压

# 源码地址
https://github.com/aria2/aria2/releases/

bz2 格式解压:tar xvf *.bz2
cd aria2-1.34.0

3、修改增加线程数

nano ./src/OptionHandlerFactory.cc

# 查找以下代码
OptionHandler* op(new NumberOptionHandler(PREF_MAX_CONNECTION_PER_SERVER,
                                               TEXT_MAX_CONNECTION_PER_SERVER,
                                               "1", 1, 16, 'x'));

PREF_MIN_SPLIT_SIZE, TEXT_MIN_SPLIT_SIZE, "20M", 1_m, 1_g, 'k'));

PREF_CONNECT_TIMEOUT, TEXT_CONNECT_TIMEOUT, "60", 1, 600));

PREF_PIECE_LENGTH, TEXT_PIECE_LENGTH, "1M", 1_m, 1_g));

new NumberOptionHandler(PREF_RETRY_WAIT, TEXT_RETRY_WAIT, "0", 0, 600));

new NumberOptionHandler(PREF_SPLIT, TEXT_SPLIT, "5", 1, -1, 's'));

# 修改为
OptionHandler* op(new NumberOptionHandler(PREF_MAX_CONNECTION_PER_SERVER,
                                               TEXT_MAX_CONNECTION_PER_SERVER,
                                               "128", 1, -1, 'x'));

PREF_MIN_SPLIT_SIZE, TEXT_MIN_SPLIT_SIZE, "4K", 1_k, 1_g, 'k'));

PREF_CONNECT_TIMEOUT, TEXT_CONNECT_TIMEOUT, "30", 1, 600));

PREF_PIECE_LENGTH, TEXT_PIECE_LENGTH, "4k", 1_k, 1_g));

new NumberOptionHandler(PREF_RETRY_WAIT, TEXT_RETRY_WAIT, "2", 0, 600));

new NumberOptionHandler(PREF_SPLIT, TEXT_SPLIT, "8", 1, -1, 's'));

4、开始编译

./configure
make
make install

5、一键编译脚本

git clone https://github.com/king567/Aria2-static-build-128-thread.git
cd Aria2-static-build-128-thread
chmod +x Compiler-Aria2.sh
sh Compiler-Aria2.sh

PS:下载完aria2原码后,就不要更动脚本存放位置

二、已编译完成 直接安装方式

# 源码地址
https://github.com/king567/Aria2-static-build-128-thread/releases

解压:tar xvf *.gz
cd aria2*
./install.sh

三、扩展

将 Aria2 配置成服务:

nano /etc/init.d/aria2

#!/bin/sh
# https://gist.github.com/jereksel/8217470

### BEGIN INIT INFO
# Provides: aria2
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: aria2c init script.
# Description: Starts and stops aria2 daemon.
### END INIT INFO

USER="root"
DAEMON=/usr/bin/aria2c
CONF="/root/.aria2/aria2.conf"

start() {
    if [ -f $CONF ]; then
        echo "Starting aria2 daemon..."
        start-stop-daemon -S -c $USER:$USER -x $DAEMON -- -D --conf-path=$CONF || echo "start fail"
        pid=`pgrep -fu $USER $DAEMON`
        echo "pid=$pid"
    else
        echo "$CONF was not found"
    fi
}

stop() {
    echo  "Stopping..."
    start-stop-daemon -K -c $USER:$USER -x $DAEMON

    if [ $? = "0" ];then
        sleep 5
        echo "stop ok"
    else
        echo "stop fail"
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "Usage: /etc/init.d/aria2 {start|stop|restart}"
        exit 1
        ;;
esac

Aria2 启动:/etc/init.d/aria2 start
Aria2 停止:/etc/init.d/aria2 stop
Aria2 重启:/etc/init.d/aria2 restart

此代码来自:https://gist.github.com/jereksel/8217470

3、bt-tracker 跟踪服务器列表自动更新:

nano /etc/trackers-list-aria2.sh

#!/bin/bash
/etc/init.d/aria2 stop
list=`wget -qO- https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt|awk NF|sed ":a;N;s/\n/,/g;ta"`
if [ -z "`grep "bt-tracker" /root/.aria2/aria2.conf`" ]; then
    sed -i '$a bt-tracker='${list} /root/.aria2/aria2.conf
    echo add......
else
    sed -i "s@bt-tracker.*@bt-tracker=$list@g" /root/.aria2/aria2.conf
    echo update......
fi
/etc/init.d/aria2 start

chmod +x /etc/trackers-list-aria2.sh
配置计划任务:crontab -e
# 每30分钟更新一次tracker列表(按需定义更新频率)
*/30 * * * * /etc/trackers-list-aria2.sh

此代码来自:https://www.feng.ee/aria2-trackers-auto-update.html

4、Aria2 启动后自动继续未完成的下载 并清理已删除任务的文件:

nano /root/cleanAria2.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 1. start all paused tasks
# 2. delete other files on disk

# API: https://aria2.github.io/manual/en/html/aria2c.html#rpc-interface

from xmlrpc import client as xmlc
import os

# 修改 IP、密码、地址
rpcUrl = 'http://127.0.0.1:6800/rpc'
rpcToken = 'token:PASSWORD'
downloadPath = '/root/usb/nas/download/'  # same to aria2 config
# 不想要删除的文件的白名单
fileWhiteList = ['/bypy', '/PROTECTED']   # while list for delete

s = xmlc.ServerProxy(rpcUrl)
api = s.aria2
# start all tasks
api.unpauseAll(rpcToken)

tasks = api.tellActive(rpcToken)
tasks += api.tellStopped(rpcToken, 0, 99)
tasks += api.tellWaiting(rpcToken, 0, 99)

for task in tasks:
    # started BT tasks
    if ('bittorrent' in task) and ('info' in task['bittorrent']):
        filename = task['bittorrent']['info']['name']
        fileWhiteList.append(filename)
    # other tasks
    else:
        for file in task['files']:
            path = file['path']
            if path.startswith('[METADATA]'):
                path = path.replace('[METADATA]', '')
            else:
                path = os.path.basename(path)

            fileWhiteList.append(path)

# del same items
fileWhiteList = set(fileWhiteList)

print('fileWhiteList', fileWhiteList)

def isStrContainItemInList(str, list):
    for item in list:
        if item in str:
            return True
    return False

for parent, dirnames, filenames in os.walk(downloadPath, topdown=False):
    for filename in filenames:
        path = os.path.join(parent, filename)
        if not isStrContainItemInList(path, fileWhiteList):
            os.remove(path)
            print('del file: ', filename)
    for dirname in dirnames:
        path = os.path.join(parent, dirname)
        if not isStrContainItemInList(path, fileWhiteList):
            try:
                os.rmdir(path)
                print('del dir:  ', dirname)
            finally:
                pass

此代码来自:https://hzy.pw/p/2475
Last modification:January 6th, 2020 at 05:00 pm
如果觉得我的文章对你有用,请随意赞赏