kzkzzzz 2023-01-18 14:13:22 +08:00
commit 0d37cdbf1d
4 changed files with 133 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.idea
bin
go.sum

12
app/config.go Normal file
View File

@ -0,0 +1,12 @@
package app
type (
Conf map[string]ConfItem
ConfItem struct {
RemoteAddr []string
LocalPort int
}
)
var ProxyMap = make(Conf)

25
go.mod Normal file
View File

@ -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
)

93
main.go Normal file
View File

@ -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)
}