最新消息:重新回归WordPress,我要比较认真的开始更新我的博客了。

给openwrt配置xfrpc

杂七杂八 hanlei 866浏览

有一个闲置的小米 mini路由器,查了一下配置还不错,CPU是MT7620,内存128M,ROM16M。可以刷openwrt。然后就学着自己编译了一下openwrt

编译过程只是跟着教程做https://www.moewah.com/archives/4003.html

结果成功了,但是我想要装的frpc太大了,编译安装后,虽然好像是装上了但用不了,估计还是因为体积的问题。查了一下就发现有xfrpc这个替代品。

服务器端

用了DOCKER镜像ppjoin/xfrps,配置和frps差不多,可以用DOCKER命令进到容器内修改配置文件,下面附上默认的配置

[common]
bind_addr = 0.0.0.0
# 监听端口
bind_port = 7000

# 这两个配置是分别给http和https 类型的tunnel用的
vhost_http_port = 7001
vhost_https_port = 7002

# 访问xfrps控制面板的端口
dashboard_port = 7003

# xfrps控制面板 登录用户名
dashboard_user = admin
# xfrps控制面板 用户密码
dashboard_pwd = 12345678

# 日志文件路径
log_file = /config/frps.log

# 日志级别,可选的值有 trace, debug, info, warn, error
log_level = info

# 日志保留天数
log_max_days = 7

# privilege 模式(当前唯一支持的模式)密码,客户端填写时需要与这个一样
privilege_token = 12345678

# 心跳超时设置,不建议修改默认值(默认是90)
# heartbeat_timeout = 90

# 允许客户端绑定的端口,如果留空,表示没有任何限制
privilege_allow_ports = 7004-7100

# pool_count in each proxy will change to max_pool_count if they exceed the maximum value
max_pool_count = 5

# authentication_timeout means the timeout interval (seconds) when the frpc connects frps
# if authentication_timeout is zero, the time is not verified, default is 900s
authentication_timeout = 900

# 配置http或https类型的tunnel时需要这个
# 假设这里填写的是 ppjoin.com , 客户端那边的subdomain值是test,
# 那么最终访问的域名是: test.ppjoin.com
subdomain_host = ppjoin.com

# 是否开启tcp_mux, 注意客户端这个项的值要与这里一致
tcp_mux = false

客户端

xfrpc是有WEB管理界面的,但是好像得自己下载,我编译的时候没找到,就没装。xfrpc的配置在“/etc/config/“目录的xfrpc文件内如下

config xfrp 'init'
	option disabled 0
	option loglevel 7

config xfrpc 'common'
	option server_addr 127.0.0.1
	option server_port 7000
        option privilege_token abcdef
	option auth_token abcdef
config xfrpc 'ssh01'
	option type tcp
	option local_ip 192.168.1.1
	option local_port 80
	option remote_port 7100

#config xfrpc 'ftp01'
#	option type ftp
#	option local_ip 127.0.01
#	option local_port 21
#	option remote_port 8021
#	option remote_data_port 8022
	
#config xfrpc 'web01'
#	option type http
#	option local_ip 127.0.0.1
#	option local_port 8080
	
#config xfrpc 'web02'
#	option type https
#	option local_ip 127.0.0.1
#	option local_port 8443

这里的配置和frpc有点区别,其实是一样的,frpc的服务起动时会根据上面的内容在“/var/etc/”目录下面生成一个名为xfrpc.ini的配置文件。xfrpc的服务脚本如下

#!/bin/sh /etc/rc.common
# Copyright (C) 2022 Dengfeng Liu <liu_df@qq.com>
#
# This is free software, licensed under the GNU General Public License v3.
# See /LICENSE for more information.
#

START=99
USE_PROCD=1

NAME=xfrpc
PROG=/usr/bin/$NAME


handle_xfrpc() {
	local name="$1"
	local config="$2"

	echo "[$name]" >> "$config"

	handle_type() {
		uci_validate_section xfrpc xfrpc "$name" \
				'type:or("tcp", "udp", "ftp", "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"|"udp")
			config_get remote_port "$name" remote_port
			echo "remote_port = $remote_port" >> "$config"
			;;
		"ftp")
			config_get remote_port "$name" remote_port
			config_get remote_data_port "$name" remote_data_port
			echo "remote_port = $remote_port" >> "$config"
			echo "remote_data_port = $remote_data_port" >> "$config"
			;;
		esac
	}

	if [ "$name" = "common" ]; then
		uci_validate_section xfrpc xfrpc "$name" \
				'server_addr:ipaddr' \
				'server_port:uinteger' \
				'privilege_token:string'

		[ -z "$privilege_token" ] && {
			echo "no privilege_token"
			exit
		}
		echo "server_addr = $server_addr" >> "$config"
		echo "server_port = $server_port" >> "$config"
		echo "privilege_token = $privilege_token" >> "$config"
	else
		handle_type
	fi
}

service_triggers() {
	procd_add_reload_trigger "$NAME"
}

start_service() {
	local conf_file="/var/etc/$NAME.ini"

	> "$conf_file"
	config_load "$NAME"

	uci_validate_section xfrpc xfrpc init \
			'disabled:bool:1' \
			'loglevel:uinteger:0'

	if [ $disabled = 1 ]; then
		echo "xfrpc service disabled"
		return
	fi

	config_foreach handle_xfrpc xfrpc "$conf_file"

	procd_open_instance
	procd_set_param command "$PROG" -c "$conf_file" -f -d $loglevel
	procd_set_param file "$conf_file"
	procd_set_param respawn
	procd_close_instance
}

reload_service() {
	stop
	start
}

这期间出现一个问题,我用的这个服务器需要客户端提供“privilege_token”,而我装的客户端里用的身份验证字段是“auth_token”,于是我把客户端的配置文件和服务文件内的auth_token全替换成了privilege_token。可以看到上面的代码是已经替换过的。

然后就可以正常使用了,非常的小,16M的ROM装还能剩下7M左右空闲。

转载请注明:HANLEI'BLOG » 给openwrt配置xfrpc