kzkzzzz 2023-01-18 14:32:14 +08:00
parent 0d37cdbf1d
commit 11dc40b53f
3 changed files with 8 additions and 85 deletions

View File

@ -1,9 +1,9 @@
package app
type (
Conf map[string]ConfItem
Conf map[string]ProxyItem
ConfItem struct {
ProxyItem struct {
RemoteAddr []string
LocalPort int
}

3
go.mod
View File

@ -17,6 +17,9 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.14.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // 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

86
main.go
View File

@ -1,93 +1,13 @@
package main
import (
"bufio"
"fmt"
"git.makemake.in/test/mycommon/myconf"
"io"
"net"
"git.makemake.in/test/mycommon/mylog"
"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)
mylog.Init("debug", mylog.DefaultConfig)
app.Run()
}