v2
kzkzzzz 2023-01-18 14:33:17 +08:00
parent 11dc40b53f
commit f5ce40eef7
1 changed files with 72 additions and 0 deletions

72
app/proxy.go Normal file
View File

@ -0,0 +1,72 @@
package app
import (
"fmt"
"git.makemake.in/test/mycommon/mylog"
"io"
"net"
)
func Run() {
for _, item := range ProxyMap {
go listenTcp(item)
}
select {}
}
func listenTcp(item ProxyItem) {
localAddr := fmt.Sprintf(":%d", item.LocalPort)
listen, err := net.Listen("tcp", localAddr)
if err != nil {
mylog.Errorf("listen err: %s", localAddr, err)
return
}
mylog.Infof("listen %s", localAddr)
for {
conn, err := listen.Accept()
if err != nil {
mylog.Errorf("conn accept err: %s", localAddr, err)
return
}
go proxyTcp(conn, item)
}
}
func proxyTcp(conn net.Conn, item ProxyItem) {
//defer conn.Close()
for _, addr := range 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)
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 {
mylog.Error(err)
return
}
//fmt.Println(written)
}