proxyport/app/proxy.go

78 lines
1.4 KiB
Go
Raw Normal View History

2023-01-18 14:33:17 +08:00
package app
import (
"git.makemake.in/test/mycommon/mylog"
"io"
2023-01-19 11:34:30 +08:00
"math/rand"
2023-01-18 14:33:17 +08:00
"net"
2023-01-19 11:34:30 +08:00
"sync"
"time"
)
2023-01-18 14:33:17 +08:00
func Run() {
2023-01-19 11:34:30 +08:00
rand.Seed(time.Now().UnixNano())
for name, item := range ProxyMap {
go listenTcp(name, item)
2023-01-18 14:33:17 +08:00
}
2023-04-18 11:07:36 +08:00
select {}
2023-01-18 14:33:17 +08:00
}
2023-01-19 11:34:30 +08:00
func listenTcp(name string, item ProxyItem) {
listen, err := net.Listen("tcp", item.LocalAddr)
2023-01-18 14:33:17 +08:00
if err != nil {
2023-01-19 11:34:30 +08:00
mylog.Errorf("[%s] listen err: %s", name, err)
2023-01-18 14:33:17 +08:00
return
}
2023-01-19 11:34:30 +08:00
mylog.Infof("[%s] listen %s", name, item.LocalAddr)
2023-01-18 14:33:17 +08:00
for {
2023-04-18 11:07:36 +08:00
localConn, err := listen.Accept()
2023-01-18 14:33:17 +08:00
if err != nil {
2023-01-19 11:34:30 +08:00
mylog.Errorf("conn accept err: %s", item.LocalAddr, err)
2023-01-18 14:33:17 +08:00
return
}
2023-04-18 11:07:36 +08:00
go proxyTcp(localConn, item)
2023-01-18 14:33:17 +08:00
}
}
2023-04-18 11:07:36 +08:00
func proxyTcp(localConn net.Conn, item ProxyItem) {
2023-01-19 11:34:30 +08:00
randRemoteAddr := item.RemoteAddr[rand.Intn(len(item.RemoteAddr))]
2023-01-18 14:33:17 +08:00
2023-04-18 11:07:36 +08:00
remoteConn, err := net.DialTimeout("tcp", randRemoteAddr, time.Second*3)
2023-01-19 11:34:30 +08:00
if err != nil {
2023-04-18 11:07:36 +08:00
localConn.Close()
mylog.Errorf("connect remote err: %s", err)
2023-01-19 11:34:30 +08:00
return
2023-01-18 14:33:17 +08:00
}
2023-04-18 11:07:36 +08:00
mylog.Infof("start proxy %s -> %s", localConn.RemoteAddr(), randRemoteAddr)
2023-01-18 14:33:17 +08:00
2023-04-18 11:07:36 +08:00
wg0 := &sync.WaitGroup{}
wg0.Add(2)
go copyConn(wg0, localConn, remoteConn)
go copyConn(wg0, remoteConn, localConn)
wg0.Wait()
2023-01-19 11:34:30 +08:00
2023-04-18 11:07:36 +08:00
mylog.Infof("stop proxy %s -> %s", localConn.RemoteAddr(), randRemoteAddr)
2023-01-18 14:33:17 +08:00
2023-04-18 11:07:36 +08:00
}
2023-01-18 14:33:17 +08:00
2023-04-18 11:07:36 +08:00
func copyConn(wg0 *sync.WaitGroup, localConn, remoteConn net.Conn) {
defer func() {
wg0.Done()
localConn.Close()
remoteConn.Close()
2023-01-19 11:34:30 +08:00
}()
2023-04-18 11:07:36 +08:00
_, err := io.Copy(remoteConn, localConn)
if err != nil {
mylog.Error(err)
}
2023-01-18 14:33:17 +08:00
}