|
|
|
@ -39,6 +39,9 @@ type ProxyClient struct {
|
|
|
|
|
|
|
|
|
|
udpTunnel *conn.Conn
|
|
|
|
|
once sync.Once
|
|
|
|
|
closeFlag bool
|
|
|
|
|
|
|
|
|
|
mutex sync.RWMutex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if proxy type is udp, keep a tcp connection for transferring udp packages
|
|
|
|
@ -48,7 +51,7 @@ func (pc *ProxyClient) StartUdpTunnelOnce(addr string, port int64) {
|
|
|
|
|
var c *conn.Conn
|
|
|
|
|
udpProcessor := NewUdpProcesser(nil, pc.LocalIp, pc.LocalPort)
|
|
|
|
|
for {
|
|
|
|
|
if pc.udpTunnel == nil || pc.udpTunnel.IsClosed() {
|
|
|
|
|
if !pc.IsClosed() && (pc.udpTunnel == nil || pc.udpTunnel.IsClosed()) {
|
|
|
|
|
if HttpProxy == "" {
|
|
|
|
|
c, err = conn.ConnectServer(fmt.Sprintf("%s:%d", addr, port))
|
|
|
|
|
} else {
|
|
|
|
@ -82,8 +85,11 @@ func (pc *ProxyClient) StartUdpTunnelOnce(addr string, port int64) {
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
pc.mutex.Lock()
|
|
|
|
|
pc.udpTunnel = c
|
|
|
|
|
udpProcessor.UpdateTcpConn(pc.udpTunnel)
|
|
|
|
|
pc.mutex.Unlock()
|
|
|
|
|
|
|
|
|
|
udpProcessor.Run()
|
|
|
|
|
}
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
@ -91,6 +97,14 @@ func (pc *ProxyClient) StartUdpTunnelOnce(addr string, port int64) {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (pc *ProxyClient) CloseUdpTunnel() {
|
|
|
|
|
pc.mutex.RLock()
|
|
|
|
|
defer pc.mutex.RUnlock()
|
|
|
|
|
if pc.udpTunnel != nil {
|
|
|
|
|
pc.udpTunnel.Close()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (pc *ProxyClient) GetLocalConn() (c *conn.Conn, err error) {
|
|
|
|
|
c, err = conn.ConnectServer(fmt.Sprintf("%s:%d", pc.LocalIp, pc.LocalPort))
|
|
|
|
|
if err != nil {
|
|
|
|
@ -158,3 +172,15 @@ func (pc *ProxyClient) StartTunnel(serverAddr string, serverPort int64) (err err
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (pc *ProxyClient) SetCloseFlag(closeFlag bool) {
|
|
|
|
|
pc.mutex.Lock()
|
|
|
|
|
defer pc.mutex.Unlock()
|
|
|
|
|
pc.closeFlag = closeFlag
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (pc *ProxyClient) IsClosed() bool {
|
|
|
|
|
pc.mutex.RLock()
|
|
|
|
|
defer pc.mutex.RUnlock()
|
|
|
|
|
return pc.closeFlag
|
|
|
|
|
}
|
|
|
|
|