From 21d698317d89f039bf6e7423d2c036766b66cfea Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sat, 22 Feb 2020 21:25:33 +0800 Subject: [PATCH] update log --- simple-tcp-proxy.go | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/simple-tcp-proxy.go b/simple-tcp-proxy.go index 967cb11..33b235a 100644 --- a/simple-tcp-proxy.go +++ b/simple-tcp-proxy.go @@ -1,17 +1,17 @@ package main - + import ( + "encoding/json" "fmt" "io" "io/ioutil" "net" "os" "strings" - "encoding/json" ) type TcpListen struct { - Listen string `json:"listen"` + Listen string `json:"listen"` Backend string `json:"backend"` } @@ -21,57 +21,58 @@ type TcpListenConfig struct { // { // "tcpListens": [ +// { // "listen": ":8080", // "backend": "11.22.33.44:8080" +// } // ] // } -// https://blog.csdn.net/fyxichen/article/details/51505542 +// https://blog.csdn.net/fyxichen/article/details/51505542 func main() { - tcpListenConfig, err := readConfigFile() if err != nil { - fmt.Printf("Error in load config file: %v\n", err) + fmt.Printf("[ERROR] Error in load config file: %v\n", err) return } if len(tcpListenConfig.TcpListens) == 0 { - fmt.Println("Not find any mapping in config") + fmt.Println("[ERROR] Not find any mapping in config") return } for _, tcpListen := range tcpListenConfig.TcpListens { - go server(tcpListen) + go serverTcpListen(tcpListen) } ExitChan := make(chan bool, 1) <-ExitChan } -func server(tcpListen TcpListen) { +func serverTcpListen(tcpListen TcpListen) { lis, err := net.Listen("tcp", tcpListen.Listen) if err != nil { fmt.Println(err) return } defer lis.Close() - fmt.Printf("Listen TCP at: %v --> %v\n", tcpListen.Listen, tcpListen.Backend) + fmt.Printf("[INFO] Listen TCP at: %v --> %v\n", tcpListen.Listen, tcpListen.Backend) for { conn, err := lis.Accept() if err != nil { - fmt.Printf("Listen error: %v\n", err) + fmt.Printf("[ERROR] Listen error: %v\n", err) continue } - fmt.Printf("TCP connection from %v, local: %v\n", conn.RemoteAddr(), conn.LocalAddr()) - go handle(tcpListen, conn) + fmt.Printf("[INFO] TCP connection from %v, local: %v\n", conn.RemoteAddr(), conn.LocalAddr()) + go handleRequest(tcpListen, conn) } } -func handle(tcpListen TcpListen, sconn net.Conn) { +func handleRequest(tcpListen TcpListen, sconn net.Conn) { defer sconn.Close() ip := tcpListen.Backend dconn, err := net.Dial("tcp", ip) if err != nil { - fmt.Printf("Connect to %v failed:%v\n", ip, err) + fmt.Printf("[ERROR] Connect to %v failed:%v\n", ip, err) return } ExitChan := make(chan bool, 1) @@ -80,9 +81,9 @@ func handle(tcpListen TcpListen, sconn net.Conn) { if err != nil { errStr := fmt.Sprintf("%v", err) if strings.Contains(errStr, "use of closed network connection") { - fmt.Printf("Connection closed: %v\n", ip) + fmt.Printf("[INFO] Connection closed: %v\n", ip) } else { - fmt.Printf("Send data to %v failed:%v\n", ip, err) + fmt.Printf("[WARN] Send data to %v failed:%v\n", ip, err) } } ExitChan <- true @@ -92,9 +93,9 @@ func handle(tcpListen TcpListen, sconn net.Conn) { if err != nil { errStr := fmt.Sprintf("%v", err) if strings.Contains(errStr, "use of closed network connection") { - fmt.Printf("Connection closed: %v\n", ip) + fmt.Printf("[INFO] Connection closed: %v\n", ip) } else { - fmt.Printf("Receive data from %v failed:%v\n", ip, err) + fmt.Printf("[WARN] Receive data from %v failed:%v\n", ip, err) } } ExitChan <- true @@ -114,11 +115,11 @@ func readConfigFile() (*TcpListenConfig, error) { if err != nil { return nil, err } - var tcpListenConfig TcpListenConfig + var tcpListenConfig TcpListenConfig err = json.Unmarshal(configFileBytes, &tcpListenConfig) if err != nil { return nil, err } - + return &tcpListenConfig, nil -} \ No newline at end of file +}