From 97a18f2a53d113dcbaaabc3dd85ca1d6ddc1838a Mon Sep 17 00:00:00 2001 From: Andrey Khrolenok Date: Sun, 14 Feb 2021 18:16:53 +0300 Subject: [PATCH 1/5] Add FreeBSD compatibility and FreeBSD services support --- .gitignore | 1 + README.md | 31 +++++++++++-- tracker-add-auto.sh | 98 ++++++++++++++++++++++------------------ transmission_tracker_add | 22 +++++++++ 4 files changed, 104 insertions(+), 48 deletions(-) create mode 100644 .gitignore mode change 100644 => 100755 tracker-add-auto.sh create mode 100755 transmission_tracker_add diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/README.md b/README.md index bb75ba8..b2feb91 100644 --- a/README.md +++ b/README.md @@ -43,11 +43,13 @@ Nice=19 #### Choose your destiny: -[Docker way](https://github.com/AndrewMarchukov/tracker-add#-docker-way) +[Docker way](#-docker-way) -[Systemd way](https://github.com/AndrewMarchukov/tracker-add#-systemd-way) +[Systemd way](#-systemd-way) -[Simple way (for routers)](https://github.com/AndrewMarchukov/tracker-add#-simple-way-for-routers) +[FreeBSD service way](#-freebsd-service-way) + +[Simple way (for routers)](#-simple-way-for-routers) ## Installation and usage @@ -92,6 +94,29 @@ systemctl status transmission-tracker-add.service ``` + +#### * FreeBSD service way + +Edit settings.json for transmission set rpc-enabled, rpc-username and rpc-password + +Download scripts and make it executable: +```bash +install -d /opt/bin +fetch -o /opt/bin/add-trackers-auto.sh https://raw.githubusercontent.com/AndrewMarchukov/tracker-add/master/tracker-add-auto.sh +fetch -o /usr/local/etc/rc.d/transmission_tracker_add https://raw.githubusercontent.com/AndrewMarchukov/tracker-add/master/transmission_tracker_add +chmod +x /opt/bin/add-trackers-auto.sh /usr/local/etc/rc.d/transmission_tracker_add +``` + +Set user and password in `add-trackers-auto.sh`: +```bash +nano /opt/bin/add-trackers-auto.sh +``` + +Start service: +```bash +service transmission_tracker_add start +``` + #### * Simple way (for routers) Requirements: curl, transmission-remote diff --git a/tracker-add-auto.sh b/tracker-add-auto.sh old mode 100644 new mode 100755 index 683a354..fc7d480 --- a/tracker-add-auto.sh +++ b/tracker-add-auto.sh @@ -1,52 +1,60 @@ -#!/bin/bash +#!/bin/sh # Get transmission credentials and ip or dns address auth=user:password host=localhost +trans="$(which transmission-remote) $host --auth=$auth" while true ; do -sleep 25 -add_trackers () { - torrent_hash=$1 - id=$2 - trackerslist=/tmp/trackers.txt -for base_url in https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt ; do -if [ ! -f $trackerslist ]; then -curl -o "$trackerslist" "${base_url}" -fi -Local=$(wc -c < $trackerslist) -Remote=$(curl -sI "${base_url}" | awk '/Content-Length/ {sub("\r",""); print $2}') -if [ "$Local" != "$Remote" ]; then -curl -o "$trackerslist" "${base_url}" -fi - echo "URL for ${base_url}" - echo "Adding trackers for $torrent_name..." -for tracker in $(cat $trackerslist) ; do - echo -n "${tracker}..." -if transmission-remote "$host" --auth="$auth" --torrent "${torrent_hash}" -td "${tracker}" | grep -q 'success'; then - echo ' done.' -else - echo ' already added.' -fi -done -done - sleep 3m - rm -f "/tmp/TTAA.$id.lock" -} -# Get list of active torrents - ids="$(transmission-remote "$host" --auth="$auth" --list | grep -vE 'Seeding|Stopped|Finished|[[:space:]]100%[[:space:]]' | grep '^ ' | awk '{ print $1 }')" -for id in $ids ; do - add_date="$(transmission-remote "$host" --auth="$auth" --torrent "$id" --info| grep '^ Date added: ' |cut -c 21-)" - add_date_t="$(date -d "$add_date" "+%Y-%m-%d %H:%M")" - dater="$(date "+%Y-%m-%d %H:%M")" - dateo="$(date -d "1 minutes ago" "+%Y-%m-%d %H:%M")" + sleep 25 -if [ ! -f "/tmp/TTAA.$id.lock" ]; then -if [[ "( "$add_date_t" == "$dater" || "$add_date_t" == "$dateo" )" ]]; then - hash="$(transmission-remote "$host" --auth="$auth" --torrent "$id" --info | grep '^ Hash: ' | awk '{ print $2 }')" - torrent_name="$(transmission-remote "$host" --auth="$auth" --torrent "$id" --info | grep '^ Name: ' |cut -c 9-)" - add_trackers "$hash" "$id" & - touch "/tmp/TTAA.$id.lock" -fi -fi -done + add_trackers () { + torrent_hash=$1 + id=$2 + trackerslist=/tmp/trackers.txt + for base_url in https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt ; do + if [ ! -f $trackerslist ]; then + curl -o "$trackerslist" "${base_url}" + fi + Local=$(wc -c < $trackerslist) + Remote=$(curl -sI "${base_url}" | awk '/Content-Length/ {sub("\r",""); print $2}') + if [ "$Local" != "$Remote" ]; then + curl -o "$trackerslist" "${base_url}" + fi + echo "URL for ${base_url}" + echo "Adding trackers for $torrent_name..." + for tracker in $(cat $trackerslist) ; do + echo -n "${tracker}..." + if ${trans} --torrent "${torrent_hash}" -td "${tracker}" | grep -q 'success'; then + echo ' done.' + else + echo ' already added.' + fi + done + done + sleep 3m + rm -f "/tmp/TTAA.$id.lock" + } + + # Get list of active torrents + ids="$(${trans} --list | grep -vE 'Seeding|Stopped|Finished|[[:space:]]100%[[:space:]]' | grep '^ ' | awk '{ print $1 }' | grep -vE 'ID')" + for id in $ids ; do + add_date="$(${trans} --torrent "$id" --info| grep '^ Date added: ' |cut -c 21-)" + if [ $(uname) = "FreeBSD" ]; then + add_date_t="$(date -jf "%+" "$add_date" "+%Y-%m-%d %H:%M")" + dateo="$(date -jv-1M "+%Y-%m-%d %H:%M")" + else + add_date_t="$(date -d "$add_date" "+%Y-%m-%d %H:%M")" + dateo="$(date -d "1 minutes ago" "+%Y-%m-%d %H:%M")" + fi + dater="$(date "+%Y-%m-%d %H:%M")" + + if [ ! -f "/tmp/TTAA.$id.lock" ]; then + if [ "$add_date_t" = "$dater" -o "$add_date_t" = "$dateo" ]; then + hash="$(${trans} --torrent "$id" --info | grep '^ Hash: ' | awk '{ print $2 }')" + torrent_name="$(${trans} --torrent "$id" --info | grep '^ Name: ' |cut -c 9-)" + add_trackers "$hash" "$id" & + touch "/tmp/TTAA.$id.lock" + fi + fi + done done diff --git a/transmission_tracker_add b/transmission_tracker_add new file mode 100755 index 0000000..616332e --- /dev/null +++ b/transmission_tracker_add @@ -0,0 +1,22 @@ +#!/bin/sh +# +# PROVIDE: transmission_tracker_add +# REQUIRE: DAEMON +# KEYWORD: shutdown + +. /etc/rc.subr + +name=transmission_tracker_add +rcvar=transmission_tracker_add_enable + +load_rc_config $name + +: ${transmission_tracker_add_enable:=YES} + +pidfile=/var/run/transmission/tracker-add.pid +command="/usr/sbin/daemon" +task="/opt/bin/add-trackers-auto.sh" +procname="/bin/sh" +command_args="-p ${pidfile} ${task}" + +run_rc_command "$1" From 2b972a5a77313c8f5f38ae339650707ea3db31ad Mon Sep 17 00:00:00 2001 From: Andrey Khrolenok Date: Sun, 14 Feb 2021 18:33:14 +0300 Subject: [PATCH 2/5] Script optimization by @otapi (partially) --- tracker-add-auto-router.sh | 0 tracker-add-auto.sh | 58 +++++++++++++++++++------------------- 2 files changed, 29 insertions(+), 29 deletions(-) mode change 100644 => 100755 tracker-add-auto-router.sh diff --git a/tracker-add-auto-router.sh b/tracker-add-auto-router.sh old mode 100644 new mode 100755 diff --git a/tracker-add-auto.sh b/tracker-add-auto.sh index fc7d480..983fd95 100755 --- a/tracker-add-auto.sh +++ b/tracker-add-auto.sh @@ -3,38 +3,38 @@ auth=user:password host=localhost +add_trackers () { + torrent_hash=$1 + id=$2 + trackerslist=/tmp/trackers.txt + for base_url in https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt ; do + if [ ! -f $trackerslist ]; then + curl -o "$trackerslist" "${base_url}" + fi + Local=$(wc -c < $trackerslist) + Remote=$(curl -sI "${base_url}" | awk '/Content-Length/ {sub("\r",""); print $2}') + if [ "$Local" != "$Remote" ]; then + curl -o "$trackerslist" "${base_url}" + fi + echo "URL for ${base_url}" + echo "Adding trackers for $torrent_name..." + for tracker in $(cat $trackerslist) ; do + echo -n "${tracker}..." + if ${trans} --torrent "${torrent_hash}" -td "${tracker}" | grep -q 'success'; then + echo ' failed.' + else + echo ' done.' + fi + done + done + sleep 3m + rm -f "/tmp/TTAA.$id.lock" +} + trans="$(which transmission-remote) $host --auth=$auth" while true ; do sleep 25 - add_trackers () { - torrent_hash=$1 - id=$2 - trackerslist=/tmp/trackers.txt - for base_url in https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt ; do - if [ ! -f $trackerslist ]; then - curl -o "$trackerslist" "${base_url}" - fi - Local=$(wc -c < $trackerslist) - Remote=$(curl -sI "${base_url}" | awk '/Content-Length/ {sub("\r",""); print $2}') - if [ "$Local" != "$Remote" ]; then - curl -o "$trackerslist" "${base_url}" - fi - echo "URL for ${base_url}" - echo "Adding trackers for $torrent_name..." - for tracker in $(cat $trackerslist) ; do - echo -n "${tracker}..." - if ${trans} --torrent "${torrent_hash}" -td "${tracker}" | grep -q 'success'; then - echo ' done.' - else - echo ' already added.' - fi - done - done - sleep 3m - rm -f "/tmp/TTAA.$id.lock" - } - # Get list of active torrents ids="$(${trans} --list | grep -vE 'Seeding|Stopped|Finished|[[:space:]]100%[[:space:]]' | grep '^ ' | awk '{ print $1 }' | grep -vE 'ID')" for id in $ids ; do @@ -52,8 +52,8 @@ while true ; do if [ "$add_date_t" = "$dater" -o "$add_date_t" = "$dateo" ]; then hash="$(${trans} --torrent "$id" --info | grep '^ Hash: ' | awk '{ print $2 }')" torrent_name="$(${trans} --torrent "$id" --info | grep '^ Name: ' |cut -c 9-)" - add_trackers "$hash" "$id" & touch "/tmp/TTAA.$id.lock" + add_trackers "$hash" "$id" & fi fi done From 6cb56fe6ed121e3bde2965da19a855f4df82338e Mon Sep 17 00:00:00 2001 From: Andrey Khrolenok Date: Sun, 14 Feb 2021 18:49:40 +0300 Subject: [PATCH 3/5] Which command does not work into service :( --- tracker-add-auto.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tracker-add-auto.sh b/tracker-add-auto.sh index 983fd95..c1cb28c 100755 --- a/tracker-add-auto.sh +++ b/tracker-add-auto.sh @@ -31,7 +31,7 @@ add_trackers () { rm -f "/tmp/TTAA.$id.lock" } -trans="$(which transmission-remote) $host --auth=$auth" +trans="/usr/local/bin/transmission-remote $host --auth=$auth" while true ; do sleep 25 From e7f2b8e237dc91737d77c8106003aaeda726a973 Mon Sep 17 00:00:00 2001 From: Andrey Khrolenok Date: Sun, 14 Feb 2021 18:57:30 +0300 Subject: [PATCH 4/5] Fix some errors. --- tracker-add-auto.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tracker-add-auto.sh b/tracker-add-auto.sh index c1cb28c..c67ddfb 100755 --- a/tracker-add-auto.sh +++ b/tracker-add-auto.sh @@ -21,13 +21,13 @@ add_trackers () { for tracker in $(cat $trackerslist) ; do echo -n "${tracker}..." if ${trans} --torrent "${torrent_hash}" -td "${tracker}" | grep -q 'success'; then - echo ' failed.' - else echo ' done.' + else + echo ' failed.' fi done done - sleep 3m + sleep 180 rm -f "/tmp/TTAA.$id.lock" } From df9f4728fc25a343c4de6da9c5e07897d53bf10f Mon Sep 17 00:00:00 2001 From: Andrey Khrolenok Date: Thu, 18 Feb 2021 01:02:25 +0300 Subject: [PATCH 5/5] "$(uname)" just in case, prevent word splitting --- tracker-add-auto.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tracker-add-auto.sh b/tracker-add-auto.sh index c67ddfb..c7212b6 100755 --- a/tracker-add-auto.sh +++ b/tracker-add-auto.sh @@ -39,7 +39,7 @@ while true ; do ids="$(${trans} --list | grep -vE 'Seeding|Stopped|Finished|[[:space:]]100%[[:space:]]' | grep '^ ' | awk '{ print $1 }' | grep -vE 'ID')" for id in $ids ; do add_date="$(${trans} --torrent "$id" --info| grep '^ Date added: ' |cut -c 21-)" - if [ $(uname) = "FreeBSD" ]; then + if [ "$(uname)" = "FreeBSD" ]; then add_date_t="$(date -jf "%+" "$add_date" "+%Y-%m-%d %H:%M")" dateo="$(date -jv-1M "+%Y-%m-%d %H:%M")" else