-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtun
More file actions
executable file
·383 lines (326 loc) · 11 KB
/
tun
File metadata and controls
executable file
·383 lines (326 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/bin/bash
# frp 隧道管理脚本
# 用法: tun <local_port> [name] [type]
# 示例: tun 8080
# tun 3000 my-app tcp
set -e
# 配置就变量
CONFD_DIR="/opt/homebrew/etc/frp/confd"
FRPC_CONFIG="/opt/homebrew/etc/frp/frpc.toml"
FRPC_SERVICE="frpc"
REMOTE_HOST="xx.x.xxx.xxx"
PORT_RANGE_START=7010
PORT_RANGE_END=7020
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 打印带颜色的消息
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 显示帮助信息
show_help() {
echo "frp 隧道管理脚本"
echo ""
echo "用法:"
echo " tun <local_port> [name] [type]"
echo ""
echo "参数:"
echo " local_port 本地端口号 (必需)"
echo " name 隧道名称 (可选,默认为端口号)"
echo " type 代理类型 (可选,默认为 tcp)"
echo ""
echo "示例:"
echo " tun 8080 # 创建名为 8080 的 tcp 隧道"
echo " tun 3000 my-app # 创建名为 my-app 的 tcp 隧道"
echo " tun 8080 web-server http # 创建名为 web-server 的 http 隧道"
echo ""
echo "管理命令:"
echo " tun list # 列出所有隧道"
echo " tun stop <name> # 停止指定隧道"
echo " tun restart # 重启 frpc 服务"
echo " tun status # 查看 frpc 服务状态"
}
# 检查端口是否已被使用
check_port_used() {
local port=$1
local config_files=("$CONFD_DIR"/*.toml)
for file in "${config_files[@]}"; do
if [[ -f "$file" ]]; then
if grep -q "remotePort = $port" "$file"; then
return 0 # 端口已被使用
fi
fi
done
return 1 # 端口未被使用
}
# 获取下一个可用端口
get_next_available_port() {
for ((port=PORT_RANGE_START; port<=PORT_RANGE_END; port++)); do
if ! check_port_used $port; then
echo $port
return
fi
done
print_error "端口范围 $PORT_RANGE_START-$PORT_RANGE_END 已满,无法分配新端口"
exit 1
}
# 从配置文件读取端口信息并验证
get_remote_port_from_config() {
local config_file="$1"
local name="$2"
if [[ ! -f "$config_file" ]]; then
print_error "配置文件不存在: $config_file"
return 1
fi
# 从配置文件中提取端口信息
local remote_port=$(grep 'remotePort = ' "$config_file" | head -1 | sed 's/.*remotePort = \(.*\).*/\1/')
if [[ -z "$remote_port" ]]; then
print_error "无法从配置文件中读取远程端口信息: $config_file"
return 1
fi
# 验证端口是否在有效范围内
if [[ $remote_port -lt $PORT_RANGE_START ]] || [[ $remote_port -gt $PORT_RANGE_END ]]; then
print_error "端口 $remote_port 不在有效范围内 ($PORT_RANGE_START-$PORT_RANGE_END)"
return 1
fi
echo "$remote_port"
return 0
}
# 检查隧道名称是否已存在
check_name_exists() {
local name=$1
local config_files=("$CONFD_DIR"/*.toml)
for file in "${config_files[@]}"; do
if [[ -f "$file" ]]; then
if grep -q "name = \"$name\"" "$file"; then
return 0 # 名称已存在
fi
fi
done
return 1 # 名称不存在
}
# 创建隧道配置
create_tunnel() {
local local_port=$1
local name=${2:-$local_port}
local type=${3:-tcp}
local remote_port=$(get_next_available_port)
local config_file="$CONFD_DIR/${name}.toml"
# 检查名称是否已存在
if check_name_exists "$name"; then
print_error "隧道名称 '$name' 已存在,请使用其他名称"
exit 1
fi
# 创建配置文件
cat > "$config_file" << EOF
[[proxies]]
name = "$name"
type = "$type"
localIP = "127.0.0.1"
localPort = $local_port
EOF
# 根据类型添加特定配置
if [[ "$type" == "tcp" ]]; then
echo "remotePort = $remote_port" >> "$config_file"
elif [[ "$type" == "http" ]]; then
echo "subDomain = \"$name\"" >> "$config_file"
fi
print_success "隧道配置已创建: $config_file"
echo " 名称: $name"
echo " 类型: $type"
echo " 本地端口: $local_port"
if [[ "$type" == "tcp" ]]; then
echo " 远程端口: $remote_port"
fi
# 设置全局变量供后续使用
CREATED_REMOTE_PORT="$remote_port"
}
# 重启 frpc 服务
restart_frpc() {
print_info "正在重启 frpc 服务..."
# 首先停止现有的 frpc 进程
pkill -f "frpc.*$FRPC_CONFIG" 2>/dev/null || true
sleep 1
# 检查 frpc 命令是否存在
if ! command -v frpc >/dev/null 2>&1; then
print_error "未找到 frpc 命令,请确保 frp 已正确安装"
print_info "可以通过以下方式安装: brew install frp"
return 1
fi
# 检查配置文件是否存在
if [[ ! -f "$FRPC_CONFIG" ]]; then
print_error "配置文件不存在: $FRPC_CONFIG"
return 1
fi
# 启动 frpc 并指定配置文件
print_info "启动 frpc 服务: frpc -c $FRPC_CONFIG"
# 在后台启动 frpc
nohup frpc -c "$FRPC_CONFIG" > /dev/null 2>&1 &
local frpc_pid=$!
# 等待服务启动
sleep 3
# 检查是否有frpc进程在运行(检查配置文件匹配的进程)
local running_pid=$(pgrep -f "frpc.*$FRPC_CONFIG" | head -1)
if [[ -n "$running_pid" ]]; then
print_success "frpc 服务已启动 (PID: $running_pid)"
print_info "配置文件: $FRPC_CONFIG"
else
print_error "frpc 服务启动失败"
return 1
fi
}
# 列出所有隧道
list_tunnels() {
print_info "当前隧道列表:"
echo ""
local config_files=("$CONFD_DIR"/*.toml)
local found=false
for file in "${config_files[@]}"; do
if [[ -f "$file" ]]; then
local name=$(grep 'name = ' "$file" | head -1 | sed 's/.*name = "\(.*\)".*/\1/')
local type=$(grep 'type = ' "$file" | head -1 | sed 's/.*type = "\(.*\)".*/\1/')
local local_port=$(grep 'localPort = ' "$file" | head -1 | sed 's/.*localPort = \(.*\).*/\1/')
local remote_port=$(grep 'remotePort = ' "$file" | head -1 | sed 's/.*remotePort = \(.*\).*/\1/')
local subdomain=$(grep 'subDomain = ' "$file" | head -1 | sed 's/.*subDomain = "\(.*\)".*/\1/')
if [[ -n "$name" ]]; then
found=true
echo -e " ${GREEN}名称:${NC} $name"
echo -e " ${BLUE}类型:${NC} $type"
echo -e " ${YELLOW}本地端口:${NC} $local_port"
if [[ -n "$remote_port" ]]; then
echo -e " ${YELLOW}远程端口:${NC} $remote_port"
echo -e " ${GREEN}访问地址:${NC} $REMOTE_HOST:$remote_port"
elif [[ -n "$subdomain" ]]; then
echo -e " ${GREEN}访问地址:${NC} http://$subdomain.$REMOTE_HOST"
fi
echo ""
fi
fi
done
if [[ "$found" == false ]]; then
print_warning "未找到任何隧道配置"
fi
}
# 停止隧道
stop_tunnel() {
local name=$1
local config_file="$CONFD_DIR/${name}.toml"
if [[ ! -f "$config_file" ]]; then
print_error "隧道 '$name' 不存在"
exit 1
fi
rm "$config_file"
print_success "隧道 '$name' 已停止"
# 重启服务使配置生效
restart_frpc
}
# 查看服务状态
check_status() {
print_info "frpc 服务状态:"
# 检查 frpc 进程是否在运行
local frpc_process=$(ps aux | grep -v grep | grep "frpc.*$FRPC_CONFIG")
if [[ -n "$frpc_process" ]]; then
print_success "frpc 服务正在运行"
echo "进程信息:"
echo "$frpc_process" | while read line; do
echo " $line"
done
echo ""
print_info "配置文件: $FRPC_CONFIG"
else
print_warning "frpc 服务未运行"
print_info "使用 'tun restart' 启动服务"
fi
}
# 主函数
main() {
# 检查参数
if [[ $# -eq 0 ]]; then
show_help
exit 0
fi
# 检查 confd 目录是否存在
if [[ ! -d "$CONFD_DIR" ]]; then
print_error "配置目录 $CONFD_DIR 不存在"
exit 1
fi
case "$1" in
"list")
list_tunnels
;;
"stop")
if [[ -z "$2" ]]; then
print_error "请指定要停止的隧道名称"
exit 1
fi
stop_tunnel "$2"
;;
"restart")
restart_frpc
;;
"status")
check_status
;;
"help"|"-h"|"--help")
show_help
;;
*)
# 创建隧道
local local_port=$1
local name=${2:-$local_port} # 如果没有提供名称,使用端口号作为名称
local type=${3:-tcp} # 如果没有提供类型,默认为tcp
# 验证端口号
if ! [[ "$local_port" =~ ^[0-9]+$ ]] || [[ "$local_port" -lt 1 ]] || [[ "$local_port" -gt 65535 ]]; then
print_error "无效的端口号: $local_port"
exit 1
fi
# 验证代理类型
if [[ -n "$type" ]] && [[ "$type" != "tcp" ]] && [[ "$type" != "http" ]]; then
print_error "不支持的代理类型: $type (支持: tcp, http)"
exit 1
fi
# 创建隧道
create_tunnel "$local_port" "$name" "$type"
restart_frpc
# 显示连接信息
echo ""
print_success "隧道创建完成!"
echo ""
if [[ "${type:-tcp}" == "tcp" ]]; then
# 验证端口信息一致性
local config_file="$CONFD_DIR/${name}.toml"
local verified_port=$(get_remote_port_from_config "$config_file" "$name")
local verify_result=$?
if [[ $verify_result -eq 0 ]] && [[ "$verified_port" == "$CREATED_REMOTE_PORT" ]]; then
echo -e "${GREEN}连接信息:${NC}"
echo -e " 本地地址: ${YELLOW}127.0.0.1:$local_port${NC}"
echo -e " 远程地址: ${YELLOW}$REMOTE_HOST:$CREATED_REMOTE_PORT${NC}"
echo -e " 访问命令: ${BLUE}curl $REMOTE_HOST:$CREATED_REMOTE_PORT${NC}"
else
print_error "端口验证失败,请检查配置文件"
exit 1
fi
else
echo -e "${GREEN}连接信息:${NC}"
echo -e " 本地地址: ${YELLOW}127.0.0.1:$local_port${NC}"
echo -e " 远程地址: ${YELLOW}http://${name:-$local_port}.$REMOTE_HOST${NC}"
echo -e " 访问命令: ${BLUE}curl http://${name:-$local_port}.$REMOTE_HOST${NC}"
fi
;;
esac
}
# 运行主函数
main "$@"