dhtd: new package 22649/head
authorMoritz Warning <moritzwarning@web.de>
Sun, 12 Nov 2023 16:31:28 +0000 (17:31 +0100)
committerMoritz Warning <moritzwarning@web.de>
Thu, 16 Nov 2023 06:57:26 +0000 (07:57 +0100)
Co-authored-by: Tianling Shen <cnsztl@gmail.com>
Signed-off-by: Moritz Warning <moritzwarning@web.de>
net/dhtd/Makefile [new file with mode: 0644]
net/dhtd/files/dhtd.config [new file with mode: 0644]
net/dhtd/files/dhtd.init [new file with mode: 0755]

diff --git a/net/dhtd/Makefile b/net/dhtd/Makefile
new file mode 100644 (file)
index 0000000..42cb6e6
--- /dev/null
@@ -0,0 +1,44 @@
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=dhtd
+PKG_VERSION:=0.2.0
+PKG_RELEASE:=1
+
+PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
+PKG_SOURCE_URL:=https://codeload.github.com/mwarning/dhtd/tar.gz/v$(PKG_VERSION)?
+PKG_HASH:=044dcdcf760929daba4661bf9eac461d822ad94493aa4d9dc9623625deca72a2
+
+PKG_MAINTAINER:=Moritz Warning <moritzwarning@web.de>
+PKG_LICENSE:=MIT
+PKG_LICENSE_FILES:=LICENSE
+
+PKG_BUILD_PARALLEL:=1
+
+include $(INCLUDE_DIR)/package.mk
+
+define Package/dhtd
+  SECTION:=net
+  CATEGORY:=Network
+  SUBMENU:=IP Addresses and Names
+  TITLE:=DHT Daemon
+  URL:=https://github.com/mwarning/dhtd
+endef
+
+define Package/dhtd/description
+  Standalone BitTorrent DHT daemon. Lookup and announce
+  hash identifiers via command line interface.
+endef
+
+MAKE_FLAGS += FEATURES="cli lpd"
+
+define Package/dhtd/install
+       $(INSTALL_DIR) $(1)/usr/bin
+       $(INSTALL_BIN) $(PKG_BUILD_DIR)/build/dhtd $(1)/usr/bin/
+       $(LN) /usr/bin/dhtd $(1)/usr/bin/dhtd-ctl
+       $(INSTALL_DIR) $(1)/etc/init.d
+       $(INSTALL_BIN) files/dhtd.init $(1)/etc/init.d/dhtd
+       $(INSTALL_DIR) $(1)/etc/config
+       $(INSTALL_CONF) files/dhtd.config $(1)/etc/config/dhtd
+endef
+
+$(eval $(call BuildPackage,dhtd))
diff --git a/net/dhtd/files/dhtd.config b/net/dhtd/files/dhtd.config
new file mode 100644 (file)
index 0000000..60b15b8
--- /dev/null
@@ -0,0 +1,31 @@
+##
+## DHTd is a distributed hash table daemon that uses the BitTorrent network.
+##
+
+config dhtd
+       option enabled 1
+
+## Add hashes to announce them to the network
+#      list announce '00112233445566778899aabbcceeff0011223344'
+
+## Load and store good nodes every 24h and on start/shutdown.
+#      option peerfile '/etc/dhtd/peers.txt'
+
+## Add static peer addresses.
+       list peer 'bttracker.debian.org:6881'
+       list peer 'router.bittorrent.com:6881'
+
+## Bind the DHT to this port.
+#      option port '6881'
+
+## Limit DHT communication to this interface.
+#      option ifname 'lan'
+
+## Verbosity: quiet, verbose or debug
+#      option verbosity 'quiet'
+
+## Disable multicast peer discovery on the LAN.
+#      option lpd_disable '1'
+
+## Path for dhtd-cli to connect to.
+#      option cli_path '/tmp/dhtd.sock'
diff --git a/net/dhtd/files/dhtd.init b/net/dhtd/files/dhtd.init
new file mode 100755 (executable)
index 0000000..3cd7970
--- /dev/null
@@ -0,0 +1,116 @@
+#!/bin/sh /etc/rc.common
+
+START=95
+USE_PROCD=1
+PROG=/usr/bin/dhtd
+OPTS=""
+
+
+boot() {
+       # Wait for the loopback interface to be ready
+       ubus -t 30 wait_for network.interface network.loopback 2>/dev/null
+       rc_procd start_service
+}
+
+xappend() {
+       local name="$2" value="$1"
+       OPTS="$OPTS\n--${name//_/-} ${value//'/\\'}"
+}
+
+append_opts_list() {
+       local name cfg="$1"; shift
+       for name in $*; do
+               config_list_foreach "$cfg" "$name" xappend "$name"
+       done
+}
+
+append_opts() {
+       local name value cfg="$1"; shift
+       for name in $*; do
+               config_get value "$cfg" "$name"
+               [ -n "$value" ] && xappend "$value" "$name"
+       done
+}
+
+append_opts_boolean() {
+       local name value cfg="$1"; shift
+       for name in $*; do
+               config_get_bool value "$cfg" "$name" 0
+               [ $value -gt 0 ] && xappend '' $name
+       done
+}
+
+section_enabled() {
+       config_get_bool enabled "$1" 'enabled' 0
+       [ $enabled -gt 0 ]
+}
+
+start_instance() {
+       local cfg="$1"
+       local CONFIG_FILE=/tmp/dhtd.${cfg}.conf
+
+       section_enabled "$cfg" || return
+       . /lib/functions/network.sh
+
+       OPTS=""
+
+       append_opts "$cfg" verbosity peerfile port
+
+       config_get ifname "$cfg" "ifname"
+       if network_get_device IFNAME "$ifname";then
+               xappend "$IFNAME" "ifname"
+       else
+               [ -n "$ifname" ] && xappend "$ifname" "ifname"
+       fi
+
+       append_opts_list "$cfg" announce peer
+
+       append_opts_boolean "$cfg" lpd_disable ipv4 ipv6
+
+       # Close stdin when command line interface is present
+       if [ $($PROG --version | grep -c cli) -eq 1 ]; then
+               xappend "" "cli_disable_stdin"
+       fi
+
+       echo -e "$OPTS" > $CONFIG_FILE
+
+       procd_open_instance
+       procd_set_param command $PROG
+       procd_set_param file $CONFIG_FILE
+       procd_set_param stderr 1
+       procd_set_param stdout 1
+       procd_append_param command --config $CONFIG_FILE
+       procd_close_instance
+}
+
+stop_instance() {
+       local cfg="$1"
+       local CONFIG_FILE=/tmp/dhtd.${cfg}.conf
+
+       rm -f $CONFIG_FILE
+}
+
+add_interface_trigger() {
+       local ifname
+
+       config_get ifname "$1" ifname
+
+       [ -n "$ifname" ] && procd_add_interface_trigger "interface.*" "$ifname" /etc/init.d/dhtd restart
+}
+
+service_triggers() {
+       procd_add_reload_trigger "dhtd"
+
+       config_load dhtd
+       config_foreach add_interface_trigger dhtd
+}
+
+start_service() {
+       config_load 'dhtd'
+       config_foreach start_instance 'dhtd'
+}
+
+stop_service() {
+       config_load 'dhtd'
+       config_foreach stop_instance 'dhtd'
+}