48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
|
package main
|
|||
|
|
|||
|
import (
|
|||
|
"context"
|
|||
|
"flag"
|
|||
|
"fmt"
|
|||
|
"os"
|
|||
|
)
|
|||
|
|
|||
|
// 主循环,为每个端口映射启动一个代理
|
|||
|
func Loop(config Config) {
|
|||
|
ctx, cancel := context.WithCancel(context.Background())
|
|||
|
|
|||
|
// 启动 TCP 代理
|
|||
|
for tcp_listenAddr, tcp_targetAddr := range config.Global.LT {
|
|||
|
tcp_wg.Add(1)
|
|||
|
go StartTcpProxy(tcp_listenAddr, tcp_targetAddr)
|
|||
|
}
|
|||
|
|
|||
|
// 启动 UDP 代理
|
|||
|
for udp_listenAddr, udp_targetAddr := range config.Global.LU {
|
|||
|
udp_wg.Add(1)
|
|||
|
go StartUdpProxy(ctx, udp_listenAddr, udp_targetAddr)
|
|||
|
}
|
|||
|
|
|||
|
// 监听退出信号,触发 `cancel()` 让所有 Goroutine 退出
|
|||
|
WaitForExit(cancel)
|
|||
|
}
|
|||
|
|
|||
|
func main() {
|
|||
|
daemon := flag.Bool("d", false, "守护进程模式") // 解析命令行参数,是否以守护进程模式运行
|
|||
|
config := flag.String("c", "4to6.conf", "指定配置文件") // 解析命令行参数,是否以守护进程模式运行
|
|||
|
flag.Parse()
|
|||
|
|
|||
|
if *daemon {
|
|||
|
Daemon() // 如果设置了-d参数,则进入守护进程模式
|
|||
|
}
|
|||
|
|
|||
|
LT, err := parseConfig(*config)
|
|||
|
if err != nil {
|
|||
|
fmt.Fprintf(os.Stderr, "读取配置错误: %v\n", err)
|
|||
|
os.Exit(1)
|
|||
|
}
|
|||
|
|
|||
|
Loop(LT) // 启动所有代理服务
|
|||
|
|
|||
|
}
|