commit 0d37cdbf1d7fce71c71b587dfd5211bb08639a5d Author: kzkzzzz Date: Wed Jan 18 14:13:22 2023 +0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..85eae03 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +bin +go.sum \ No newline at end of file diff --git a/app/config.go b/app/config.go new file mode 100644 index 0000000..887d4cd --- /dev/null +++ b/app/config.go @@ -0,0 +1,12 @@ +package app + +type ( + Conf map[string]ConfItem + + ConfItem struct { + RemoteAddr []string + LocalPort int + } +) + +var ProxyMap = make(Conf) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..130cb09 --- /dev/null +++ b/go.mod @@ -0,0 +1,25 @@ +module proxyport + +go 1.18 + +require git.makemake.in/test/mycommon v0.0.0-20230114162108-0fdfb59809db + +require ( + github.com/fsnotify/fsnotify v1.6.0 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect + github.com/magiconair/properties v1.8.7 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/pelletier/go-toml v1.9.5 // indirect + github.com/pelletier/go-toml/v2 v2.0.6 // indirect + github.com/spf13/afero v1.9.3 // indirect + github.com/spf13/cast v1.5.0 // indirect + github.com/spf13/jwalterweatherman v1.1.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/spf13/viper v1.14.0 // indirect + github.com/subosito/gotenv v1.4.2 // indirect + golang.org/x/sys v0.4.0 // indirect + golang.org/x/text v0.6.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/main.go b/main.go new file mode 100644 index 0000000..b742add --- /dev/null +++ b/main.go @@ -0,0 +1,93 @@ +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) +}