在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
nginx默认只能代理http,而且只能监听80端口,这个就不能忍了,自己撸一个 package main import ( "flag" "fmt" "io" "net" "os" "strings" "sync" ) var lock sync.Mutex var trueList []string var ip string var list string func main() { flag.StringVar(&ip, "l", ":9897", "-l=0.0.0.0:9897 指定服务监听的端口") flag.StringVar(&list, "d", "127.0.0.1:1789,127.0.0.1:1788", "-d=127.0.0.1:1789,127.0.0.1:1788 指定后端的IP和端口,多个用','隔开") flag.Parse() trueList = strings.Split(list, ",") if len(trueList) <= 0 { fmt.Println("-l=0.0.0.0:9897 -d=127.0.0.1:1789") os.Exit(1) } server() } func server() { lis, err := net.Listen("tcp", ip) if err != nil { fmt.Println(err) return } defer lis.Close() for { conn, err := lis.Accept() if err != nil { fmt.Println("建立连接错误:%v\n", err) continue } fmt.Println(conn.RemoteAddr(), conn.LocalAddr()) go handle(conn) } } func handle(sconn net.Conn) { defer sconn.Close() ip, ok := getIP() if !ok { return } dconn, err := net.Dial("tcp", ip) if err != nil { fmt.Printf("连接%v失败:%v\n", ip, err) return } ExitChan := make(chan bool, 1) go func(sconn net.Conn, dconn net.Conn, Exit chan bool) { _, err := io.Copy(dconn, sconn) if err != nil { fmt.Printf("往%v发送数据失败:%v\n", ip, err) } ExitChan <- true }(sconn, dconn, ExitChan) go func(sconn net.Conn, dconn net.Conn, Exit chan bool) { _, err := io.Copy(sconn, dconn) if err != nil { fmt.Printf("从%v接收数据失败:%v\n", ip, err) } ExitChan <- true }(sconn, dconn, ExitChan) <-ExitChan dconn.Close() } func getIP() (string, bool) { lock.Lock() defer lock.Unlock() if len(trueList) < 1 { return "", false } ip := trueList[0] trueList = append(trueList[1:], ip) return ip, true }
|
请发表评论