From: Dengfeng Liu <liudf0716@gmail.com>
Date: Mon, 27 May 2024 11:01:22 +0000 (+0800)
Subject: xfrpc: Revised the config file and adjusted the corresponding init file
X-Git-Url: http://git.cdn.openwrt.org/?a=commitdiff_plain;h=e39af317532bd67580e6d12d4b4c9590cafa574e;p=feed%2Fpackages.git

xfrpc: Revised the config file and adjusted the corresponding init file

Signed-off-by: Dengfeng Liu <liudf0716@gmail.com>
---

diff --git a/net/xfrpc/files/xfrpc.conf b/net/xfrpc/files/xfrpc.conf
index 2b4df213af..332c1ef89b 100644
--- a/net/xfrpc/files/xfrpc.conf
+++ b/net/xfrpc/files/xfrpc.conf
@@ -1,25 +1,29 @@
-config xfrp 'init'
-	option disabled 1
-	option loglevel 7
-
 config xfrpc 'common'
-	option server_addr frps_ip_address
+	option enabled 0
+	option loglevel 7
+	option server_addr frps.wifidogx.online
 	option server_port 7000
 
-config xfrpc 'ssh01'
-	option type tcp
+config tcp 'ssh01'
 	option local_ip 127.0.0.1
 	option local_port 22
 	option remote_port 6000
 	
-#config xfrpc 'web01'
-#	option type http
+#config http 'web01'
 #	option local_ip 127.0.0.1
 #	option local_port 8080
 #	option custom_domains your_domain_name
 	
-#config xfrpc 'web02'
-#	option type https
+#config https 'web02'
 #	option local_ip 127.0.0.1
 #	option local_port 8443
-#	option custom_domains your_domain_name
+#	option subdomain your_domain_name
+
+#config socks5 'socks01'
+#   option remote_port 6001
+
+#config plugin 'plugin01'
+#	option remote_port 6002
+#	option plugin_name http
+#	option plugin_param 'youtube-url'
+#  	option plugin_action 'download'
diff --git a/net/xfrpc/files/xfrpc.init b/net/xfrpc/files/xfrpc.init
index cc803b0a27..90cd8cca98 100755
--- a/net/xfrpc/files/xfrpc.init
+++ b/net/xfrpc/files/xfrpc.init
@@ -1,5 +1,5 @@
 #!/bin/sh /etc/rc.common
-# Copyright (C) 2022 Dengfeng Liu <liu_df@qq.com>
+# Copyright (C) 2022 Dengfeng Liu <liudf0716@gmail.com>
 #
 # This is free software, licensed under the GNU General Public License v3.
 # See /LICENSE for more information.
@@ -13,46 +13,107 @@ PROG=/usr/bin/$NAME
 
 
 handle_xfrpc() {
-	local name="$1"
+    local section="$1"
+    local config="$2"
+
+    case "$section" in
+        common)
+            uci_validate_section xfrpc xfrpc common \
+                'server_addr:host' \
+                'server_port:uinteger' \
+                'token:string:'
+            ;;
+    esac
+
+    # Write the validated settings to the config file
+    echo "[${section}]" >> "$config"
+    [ -z "$server_addr" ] || echo "server_addr = $server_addr" >> "$config"
+    [ -z "$server_port" ] || echo "server_port = $server_port" >> "$config"
+    [ -z "$token" ] || echo "token = $token" >> "$config"
+}
+
+handle_tcp() {
+	local section="$1"
 	local config="$2"
 
-	echo "[$name]" >> "$config"
-
-	handle_type() {
-		uci_validate_section xfrpc xfrpc "$name" \
-				'type:or("tcp", "http", "https")' \
-				'local_ip:ipaddr:127.0.0.1' \
-				'local_port:uinteger'
-
-		echo "type = $type" >> "$config"
-		echo "local_ip = $local_ip" >> "$config"
-		echo "local_port = $local_port" >> "$config"
-		case "$type" in
-		tcp|mstsc|socks5)
-			config_get remote_port "$name" remote_port
-			echo "remote_port = $remote_port" >> "$config"
-			;;
-		http|https)
-			config_get custom_domains "$name" custom_domains
-			[ -z "$custom_domains" ] || echo "custom_domains = $custom_domains" >> "$config"
-			config_get subdomain "$name" subdomain
-			[ -z "$subdomain" ] || echo "subdomain = $subdomain" >> "$config"
-			;;
-		esac
-	}
-
-	if [ "$name" = "common" ]; then
-		uci_validate_section xfrpc xfrp "$name" \
-				'server_addr:host' \
-				'server_port:uinteger' \
-				'token:string:'	
-
-		echo "server_addr = $server_addr" >> "$config"
-		echo "server_port = $server_port" >> "$config"
-		[ -z "$token" ] || echo "token = $token" >> "$config"
-	else
-		handle_type
-	fi
+	uci_validate_section xfrpc tcp $section \
+		'enabled:bool:1' \
+		'local_ip:host' \
+		'local_port:uinteger' \
+		'remote_port:uinteger' 
+	
+	# if enabled is 0, then return
+	[ $enabled = 0 ] && return
+
+	# Write the validated settings to the config file
+	echo "[${section}]" >> "$config"
+	echo "type = tcp" >> "$config"
+	[ -z "$local_ip" ] || echo "local_ip = $local_ip" >> "$config"
+	[ -z "$local_port" ] || echo "local_port = $local_port" >> "$config"
+	[ -z "$remote_port" ] || echo "remote_port = $remote_port" >> "$config"
+}
+
+handle_http() {
+	local section="$1"
+	local config="$2"
+
+	uci_validate_section xfrpc http $section \
+		'enabled:bool:1' \
+		'local_ip:host' \
+		'local_port:uinteger' \
+		'custom_domains:string' \
+		'subdomain:string' \
+
+	# if enabled is 0, then return
+	[ $enabled = 0 ] && return
+
+	# Write the validated settings to the config file
+	echo "[${section}]" >> "$config"
+	echo "type = http" >> "$config"
+	[ -z "$local_ip" ] || echo "local_ip = $local_ip" >> "$config"
+	[ -z "$local_port" ] || echo "local_port = $local_port" >> "$config"
+	[ -z "$custom_domains" ] || echo "custom_domains = $custom_domains" >> "$config"
+	[ -z "$subdomain" ] || echo "subdomain = $subdomain" >> "$config"
+}
+
+handle_https() {
+	local section="$1"
+	local config="$2"
+
+	uci_validate_section xfrpc https $section \
+		'enabled:bool:1' \
+		'local_ip:host' \
+		'local_port:uinteger' \
+		'custom_domains:string' \
+		'subdomain:string' 
+	
+	# if enabled is 0, then return
+	[ $enabled = 0 ] && return
+
+	# Write the validated settings to the config file
+	echo "[${section}]" >> "$config"
+	echo "type = https" >> "$config"
+	[ -z "$local_ip" ] || echo "local_ip = $local_ip" >> "$config"
+	[ -z "$local_port" ] || echo "local_port = $local_port" >> "$config"
+	[ -z "$custom_domains" ] || echo "custom_domains = $custom_domains" >> "$config"
+	[ -z "$subdomain" ] || echo "subdomain = $subdomain" >> "$config"
+}
+
+handle_socks5() {
+	local section="$1"
+	local config="$2"
+
+	uci_validate_section xfrpc socks5 $section \
+		'enabled:bool:1' \
+		'remote_port:uinteger' 
+
+	# if enabled is 0, then return
+	[ $enabled = 0 ] && return
+
+	# Write the validated settings to the config file
+	echo "[${section}]" >> "$config"
+	echo "type = socks5" >> "$config"
+	[ -z "$remote_port" ] || echo "remote_port = $remote_port" >> "$config"
 }
 
 service_triggers() {
@@ -65,16 +126,20 @@ start_service() {
 	> "$conf_file"
 	config_load "$NAME"
 
-	uci_validate_section xfrpc xfrp init \
-			'disabled:bool:0' \
+	uci_validate_section xfrpc xfrpc common \
+			'enabled:bool:0' \
 			'loglevel:uinteger:0'
 
-	if [ $disabled = 1 ]; then
+	if [ $enabled = 0 ]; then
 		echo "xfrpc service disabled"
 		return
 	fi
 
 	config_foreach handle_xfrpc xfrpc "$conf_file"
+	config_foreach handle_tcp tcp "$conf_file"
+	config_foreach handle_http http "$conf_file"
+	config_foreach handle_https https "$conf_file"
+	config_foreach handle_socks5 socks5 "$conf_file"
 
 	procd_open_instance
 	procd_set_param command "$PROG" -c "$conf_file" -f -d $loglevel