v2
kzkzzzz 2023-01-19 11:34:30 +08:00
parent ab4b6d94b7
commit ac53e3fe9a
4 changed files with 64 additions and 39 deletions

View File

@ -5,7 +5,7 @@ type (
ProxyItem struct { ProxyItem struct {
RemoteAddr []string RemoteAddr []string
LocalPort int LocalAddr string
} }
) )

View File

@ -1,34 +1,44 @@
package app package app
import ( import (
"fmt"
"git.makemake.in/test/mycommon/mylog" "git.makemake.in/test/mycommon/mylog"
"io" "io"
"math/rand"
"net" "net"
"sync"
"time"
)
var (
wg = &sync.WaitGroup{}
) )
func Run() { func Run() {
for _, item := range ProxyMap { rand.Seed(time.Now().UnixNano())
go listenTcp(item)
for name, item := range ProxyMap {
wg.Add(1)
go listenTcp(name, item)
} }
select {} wg.Wait()
} }
func listenTcp(item ProxyItem) { func listenTcp(name string, item ProxyItem) {
localAddr := fmt.Sprintf(":%d", item.LocalPort) defer wg.Done()
listen, err := net.Listen("tcp", localAddr)
listen, err := net.Listen("tcp", item.LocalAddr)
if err != nil { if err != nil {
mylog.Errorf("listen err: %s", localAddr, err) mylog.Errorf("[%s] listen err: %s", name, err)
return return
} }
mylog.Infof("listen %s", localAddr) mylog.Infof("[%s] listen %s", name, item.LocalAddr)
for { for {
conn, err := listen.Accept() conn, err := listen.Accept()
if err != nil { if err != nil {
mylog.Errorf("conn accept err: %s", localAddr, err) mylog.Errorf("conn accept err: %s", item.LocalAddr, err)
return return
} }
@ -37,36 +47,45 @@ func listenTcp(item ProxyItem) {
} }
func proxyTcp(conn net.Conn, item ProxyItem) { func proxyTcp(conn net.Conn, item ProxyItem) {
//defer conn.Close()
for _, addr := range item.RemoteAddr { randRemoteAddr := item.RemoteAddr[rand.Intn(len(item.RemoteAddr))]
remoteConn, err := net.Dial("tcp", addr)
if err != nil {
mylog.Errorf("connect remote err: %s\n", err)
return
}
mylog.Infof("connect remote %s", addr) remoteConn, err := net.Dial("tcp", randRemoteAddr)
go copyConn(conn, remoteConn)
go copyConn(remoteConn, conn)
}
}
func copyConn(src, dst net.Conn) {
defer func() {
src.Close()
dst.Close()
mylog.Warn("close conn")
}()
_, err := io.Copy(src, dst)
if err != nil { if err != nil {
mylog.Error(err) mylog.Errorf("connect remote err: %s\n", err)
return return
} }
//fmt.Println(written) go copyConn(randRemoteAddr, conn, remoteConn)
}
func copyConn(addr string, conn, remoteConn net.Conn) {
mylog.Infof("start proxy: %s", addr)
wg2 := &sync.WaitGroup{}
wg2.Add(2)
go func() {
_, err := io.Copy(conn, remoteConn)
if err != nil {
mylog.Error(err)
return
}
wg2.Done()
}()
go func() {
_, err := io.Copy(remoteConn, conn)
if err != nil {
mylog.Error(err)
return
}
wg2.Done()
}()
wg2.Wait()
mylog.Infof("stop proxy: %s", addr)
} }

2
go.mod
View File

@ -2,7 +2,7 @@ module proxyport
go 1.18 go 1.18
require git.makemake.in/test/mycommon v0.0.0-20230114162108-0fdfb59809db require git.makemake.in/test/mycommon v0.0.0-20230119033226-3679bd502caf
require ( require (
github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect

View File

@ -1,13 +1,19 @@
package main package main
import ( import (
"flag"
"git.makemake.in/test/mycommon/myconf" "git.makemake.in/test/mycommon/myconf"
"git.makemake.in/test/mycommon/mylog" "git.makemake.in/test/mycommon/mylog"
"proxyport/app" "proxyport/app"
) )
var configPath string
func main() { func main() {
myconf.Init("config.toml", &app.ProxyMap) flag.StringVar(&configPath, "c", "config.toml", "配置文件路径")
flag.Parse()
myconf.Init(configPath, &app.ProxyMap)
mylog.Init("debug", mylog.DefaultConfig) mylog.Init("debug", mylog.DefaultConfig)
app.Run() app.Run()
} }