proxyport/main.go

94 lines
1.5 KiB
Go

package main
import (
"bufio"
"fmt"
"git.makemake.in/test/mycommon/myconf"
"io"
"net"
"proxyport/app"
"time"
)
func main() {
myconf.Init("config.toml", &app.ProxyMap)
fmt.Printf("%+v\n", app.ProxyMap)
return
addr := ":5500"
listener, err := net.Listen("tcp", addr)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("listen on [%s]\n", addr)
for {
conn, err := listener.Accept()
if err != nil {
fmt.Printf("accept err: %s\n", err)
return
}
//go handleTcp(conn)
go forwardTcp(conn)
}
}
func handleTcp(conn net.Conn) {
reader := bufio.NewReader(conn)
defer conn.Close()
for {
res, err := reader.ReadBytes('\n')
if err != nil {
if err == io.EOF {
fmt.Printf("close conn: %s\n", err)
return
} else {
fmt.Printf("read err: %s\n", err)
continue
}
}
fmt.Printf("read: %+v", string(res))
msg := fmt.Sprintf("[%s] [%s] Server res: %s", conn.RemoteAddr().Network(), time.Now().Format("2006-01-02 15:04:05.000"), res)
conn.Write([]byte(msg))
}
}
func forwardTcp(conn net.Conn) {
//defer conn.Close()
remoteConn, err := net.Dial("tcp", "119.29.187.200:3306")
if err != nil {
fmt.Printf("connect remote err: %s\n", err)
return
}
fmt.Println("connect remote ok")
go copyIO(conn, remoteConn)
go copyIO(remoteConn, conn)
}
func copyIO(src, dst net.Conn) {
defer func() {
src.Close()
dst.Close()
fmt.Println("close conn")
}()
written, err := io.Copy(src, dst)
if err != nil {
fmt.Println(err)
}
fmt.Println(written)
}