Skip to content

Commit

Permalink
tor-hs: init script improvements
Browse files Browse the repository at this point in the history
Including addition of IPv6

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
  • Loading branch information
systemcrash committed Oct 17, 2024
1 parent e727a1d commit c893416
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 50 deletions.
7 changes: 4 additions & 3 deletions net/tor-hs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=tor-hs
PKG_VERSION:=0.0.1
PKG_RELEASE:=3
PKG_VERSION:=0.0.2
PKG_RELEASE:=1

PKG_MAINTAINER:=Jan Pavlinec <jan.pavlinec1@gmail.com>
PKG_MAINTAINER:=Sergey Ponomarev <stokito@gmail.com>, \
Paul Donald <newtwen+github@gmail.com>
PKG_LICENSE:=GPL-3.0-or-later

include $(INCLUDE_DIR)/package.mk
Expand Down
8 changes: 8 additions & 0 deletions net/tor-hs/files/tor-hs.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ config tor-hs common
# #public port=2222, local port=22
# list PublicLocalPort '2222;22'

#config hidden-service
# option Name 'sshd'
# option Description "Hidden service for ssh"
# option Enabled 'false'
# option IPaddr '[::1]'
# #public port=2222, local port=22
# list PublicLocalPort '2222;22'

#config hidden-service
# option Name 'nextcloud'
# option Description "Hidden service for Nextcloud"
Expand Down
30 changes: 14 additions & 16 deletions net/tor-hs/files/tor-hs.init
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#!/bin/sh /etc/rc.common
# shellcheck disable=SC1091,SC3043,SC2086,SC2154,SC2034

START=52
STOP=52

USE_PROCD=1

TORRC_FILE=/etc/tor/torrc_generated # file with torrc config
HS_DIR_PATH=/etc/tor/hidden_service #hidden service directory path
HS_DIR_PATH=/etc/tor/hidden_service # hidden service directory path
TOR_USER=tor

clean_hs() {
Expand All @@ -18,15 +19,14 @@ config_tor() {
config_get_bool restart_tor "common" RestartTor
config_get_bool update_config "common" UpdateTorConf

tail_conf=$(uci show tor.conf.tail_include 2>/dev/null)
head_conf=$(uci show tor.conf.head_include 2>/dev/null)
tail_conf=$(uci -q show tor.conf.tail_include)
head_conf=$(uci -q show tor.conf.head_include)
echo "tail_conf $tail_conf"

if [ "$update_config" = "1" ]; then
if [ -n "$(echo $tail_conf | grep $TORRC_FILE)" ] || [ -n "$(echo $head_conf | grep $TORRC_FILE)" ]; then
if grep -q "$tail_conf" $TORRC_FILE || grep -q "$head_conf" $TORRC_FILE; then
echo "Info. Not updating tor configuration"
else
#uci add_list
echo "Info. Updating tor configuration"
uci add_list tor.conf.tail_include="$TORRC_FILE"
uci commit tor
Expand All @@ -41,33 +41,31 @@ config_tor() {
handle_hs_ports_conf() {
local public_port local_port
local value="$1"
local ipv4="$2"
local ipaddr="$2"
local name="$3"

public_port=$(echo "$value"|awk -F';' '{print $1}')
local_port=$(echo "$value"|awk -F';' '{print $2}')
echo "HiddenServicePort $public_port $ipv4:$local_port">>$TORRC_FILE
public_port=${value%%;*} local_port=${value#*;}
echo "HiddenServicePort $public_port $ipaddr:$local_port">>$TORRC_FILE
}

parse_hs_conf() {
local name public_port local_port enable_hs ipv4
local name public_port local_port enable_hs ipaddr
local config="$1"

config_get name "$config" Name
config_get description "$config" Description

config_get_bool enable_hs "$config" Enabled 0
config_get ipv4 "$config" IPv4
config_get ipaddr "$config" IPv4
config_get ipaddr "$config" IPaddr "$ipaddr" # IPv4 or IPv6; prefer IPaddr property, default to IPv4 value if absent

if [ "$enable_hs" = "1" ]; then
mkdir -p "$HS_DIR_PATH/$name"
chown "$TOR_USER":"$TOR_USER" "$HS_DIR_PATH/"
chown "$TOR_USER:$TOR_USER" "$HS_DIR_PATH/$name"
chmod 700 "$HS_DIR_PATH/"
chmod 700 "$HS_DIR_PATH/$name/"
chown -R "$TOR_USER":"$TOR_USER" "$HS_DIR_PATH/"
chmod -R 700 "$HS_DIR_PATH/"

echo "HiddenServiceDir $HS_DIR_PATH/$name" >>$TORRC_FILE
config_list_foreach "$config" PublicLocalPort handle_hs_ports_conf "$ipv4" "$name"
config_list_foreach "$config" PublicLocalPort handle_hs_ports_conf "$ipaddr" "$name"
fi
}

Expand Down
64 changes: 33 additions & 31 deletions net/tor-hs/files/tor_rpcd.sh
Original file line number Diff line number Diff line change
@@ -1,52 +1,54 @@
#!/bin/sh
# shellcheck disable=SC1091,SC3043,SC2086,SC2154,SC2034

. /lib/functions.sh

get_onion_hostname() {
local name="$1"
config_get hs_dir common HSDir
if [ -f "$hs_dir/$name/hostname" ]; then
cat "$hs_dir/$name/hostname"
fi
}

get_port_list() {
local config="$1"
config_get ports "$config" PublicLocalPort
tmp="$(echo $ports |sed "s| |','|g")"
echo -ne "['$tmp']"
}
. /usr/share/libubox/jshn.sh

parse_hs_conf() {
local name description public_port local_port enable_bool public_local_port ipv4
local name description public_port local_port enable_bool public_local_port ipv4 ipaddr
local config="$1"
local custom="$2"

config_get name "$config" Name
config_get description "$config" Description

config_get_bool enable_hs "$config" Enabled 0
config_get_bool enabled "$config" Enabled 0
config_get ipv4 "$config" IPv4
config_get ipaddr "$config" IPaddr
config_get ports "$config" PublicLocalPort
config_get hs_dir common HSDir

hostname="$(get_onion_hostname $name)"
port_list="$(get_port_list $config)"
echo "{"
echo \"name\":\"$name\",
echo \"description\":\"$description\",
echo \"enabled\":\"$enable_hs\",
echo \"ipv4\":\"$ipv4\",
echo \"hostname\":\"$hostname\",
echo \"ports\":$port_list
echo "},"
hostname="$([ -f "$hs_dir/$name/hostname" ] && cat "$hs_dir/$name/hostname")"

json_add_object
json_add_string 'name' "$name"
json_add_string 'description' "$description"
json_add_string 'enabled' "$enabled"
if [ -n "$ipaddr" ]
then
json_add_string 'ipaddr' "$ipaddr"
else
json_add_string 'ipv4' "$ipv4"
fi
json_add_string 'hostname' "$hostname"
json_add_array 'ports'
set -- $ports
for port; do
json_add_string '' "$port"
done
json_close_array
json_close_object
}

get_tor_hs_list() {
config_load tor-hs
echo "{"
echo '"hs-list":['

json_init
json_add_array 'hs-list'
config_foreach parse_hs_conf hidden-service
echo "]"
echo "}"
json_close_array
json_dump
json_cleanup
}


Expand Down

0 comments on commit c893416

Please sign in to comment.