update log
This commit is contained in:
@@ -1,17 +1,17 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"encoding/json"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type TcpListen struct {
|
type TcpListen struct {
|
||||||
Listen string `json:"listen"`
|
Listen string `json:"listen"`
|
||||||
Backend string `json:"backend"`
|
Backend string `json:"backend"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,57 +21,58 @@ type TcpListenConfig struct {
|
|||||||
|
|
||||||
// {
|
// {
|
||||||
// "tcpListens": [
|
// "tcpListens": [
|
||||||
|
// {
|
||||||
// "listen": ":8080",
|
// "listen": ":8080",
|
||||||
// "backend": "11.22.33.44: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() {
|
func main() {
|
||||||
|
|
||||||
tcpListenConfig, err := readConfigFile()
|
tcpListenConfig, err := readConfigFile()
|
||||||
|
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(tcpListenConfig.TcpListens) == 0 {
|
if len(tcpListenConfig.TcpListens) == 0 {
|
||||||
fmt.Println("Not find any mapping in config")
|
fmt.Println("[ERROR] Not find any mapping in config")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tcpListen := range tcpListenConfig.TcpListens {
|
for _, tcpListen := range tcpListenConfig.TcpListens {
|
||||||
go server(tcpListen)
|
go serverTcpListen(tcpListen)
|
||||||
}
|
}
|
||||||
ExitChan := make(chan bool, 1)
|
ExitChan := make(chan bool, 1)
|
||||||
<-ExitChan
|
<-ExitChan
|
||||||
}
|
}
|
||||||
|
|
||||||
func server(tcpListen TcpListen) {
|
func serverTcpListen(tcpListen TcpListen) {
|
||||||
lis, err := net.Listen("tcp", tcpListen.Listen)
|
lis, err := net.Listen("tcp", tcpListen.Listen)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer lis.Close()
|
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 {
|
for {
|
||||||
conn, err := lis.Accept()
|
conn, err := lis.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Listen error: %v\n", err)
|
fmt.Printf("[ERROR] Listen error: %v\n", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Printf("TCP connection from %v, local: %v\n", conn.RemoteAddr(), conn.LocalAddr())
|
fmt.Printf("[INFO] TCP connection from %v, local: %v\n", conn.RemoteAddr(), conn.LocalAddr())
|
||||||
go handle(tcpListen, conn)
|
go handleRequest(tcpListen, conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handle(tcpListen TcpListen, sconn net.Conn) {
|
func handleRequest(tcpListen TcpListen, sconn net.Conn) {
|
||||||
defer sconn.Close()
|
defer sconn.Close()
|
||||||
ip := tcpListen.Backend
|
ip := tcpListen.Backend
|
||||||
dconn, err := net.Dial("tcp", ip)
|
dconn, err := net.Dial("tcp", ip)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
ExitChan := make(chan bool, 1)
|
ExitChan := make(chan bool, 1)
|
||||||
@@ -80,9 +81,9 @@ func handle(tcpListen TcpListen, sconn net.Conn) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
errStr := fmt.Sprintf("%v", err)
|
errStr := fmt.Sprintf("%v", err)
|
||||||
if strings.Contains(errStr, "use of closed network connection") {
|
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 {
|
} 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
|
ExitChan <- true
|
||||||
@@ -92,9 +93,9 @@ func handle(tcpListen TcpListen, sconn net.Conn) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
errStr := fmt.Sprintf("%v", err)
|
errStr := fmt.Sprintf("%v", err)
|
||||||
if strings.Contains(errStr, "use of closed network connection") {
|
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 {
|
} 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
|
ExitChan <- true
|
||||||
|
|||||||
Reference in New Issue
Block a user