add UDP support for reverse proxy tunnel
This commit is contained in:
@ -193,20 +193,12 @@ func (h *relayHandler) serveTCPBind(ctx context.Context, conn net.Conn, ln net.L
|
||||
}
|
||||
}
|
||||
|
||||
func (h *relayHandler) handleTunnel(ctx context.Context, conn net.Conn, tunnelID relay.TunnelID, log logger.Logger) (err error) {
|
||||
func (h *relayHandler) handleBindTunnel(ctx context.Context, conn net.Conn, network string, tunnelID relay.TunnelID, log logger.Logger) (err error) {
|
||||
resp := relay.Response{
|
||||
Version: relay.Version1,
|
||||
Status: relay.StatusOK,
|
||||
}
|
||||
|
||||
/*
|
||||
if h.ep == nil {
|
||||
resp.Status = relay.StatusServiceUnavailable
|
||||
resp.WriteTo(conn)
|
||||
return
|
||||
}
|
||||
*/
|
||||
|
||||
uuid, err := uuid.NewRandom()
|
||||
if err != nil {
|
||||
resp.Status = relay.StatusInternalServerError
|
||||
@ -214,7 +206,10 @@ func (h *relayHandler) handleTunnel(ctx context.Context, conn net.Conn, tunnelID
|
||||
return
|
||||
}
|
||||
|
||||
connectorID := relay.NewTunnelID(uuid[:])
|
||||
connectorID := relay.NewConnectorID(uuid[:])
|
||||
if network == "udp" {
|
||||
connectorID = relay.NewUDPConnectorID(uuid[:])
|
||||
}
|
||||
|
||||
addr := ":0"
|
||||
if h.ep != nil {
|
||||
@ -227,7 +222,7 @@ func (h *relayHandler) handleTunnel(ctx context.Context, conn net.Conn, tunnelID
|
||||
}
|
||||
resp.Features = append(resp.Features, af,
|
||||
&relay.TunnelFeature{
|
||||
ID: connectorID,
|
||||
ID: connectorID.ID(),
|
||||
},
|
||||
)
|
||||
resp.WriteTo(conn)
|
||||
@ -239,7 +234,7 @@ func (h *relayHandler) handleTunnel(ctx context.Context, conn net.Conn, tunnelID
|
||||
}
|
||||
|
||||
h.pool.Add(tunnelID, NewConnector(connectorID, session))
|
||||
log.Debugf("tunnel %s connector %s established", tunnelID, connectorID)
|
||||
log.Debugf("tunnel %s connector %s/%s established", tunnelID, connectorID, network)
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -15,9 +15,6 @@ type tcpConn struct {
|
||||
}
|
||||
|
||||
func (c *tcpConn) Read(b []byte) (n int, err error) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return c.Conn.Read(b)
|
||||
}
|
||||
|
||||
|
@ -133,7 +133,7 @@ func (h *relayHandler) handleConnectTunnel(ctx context.Context, conn net.Conn, n
|
||||
return err
|
||||
}
|
||||
|
||||
cc, err := getTunnelConn(h.pool, tunnelID, 3, log)
|
||||
cc, err := getTunnelConn(network, h.pool, tunnelID, 3, log)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
|
@ -128,7 +128,7 @@ func (h *epHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.H
|
||||
"tunnel": tunnelID.String(),
|
||||
})
|
||||
|
||||
cc, err := getTunnelConn(h.pool, tunnelID, 3, log)
|
||||
cc, err := getTunnelConn("tcp", h.pool, tunnelID, 3, log)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
|
@ -173,7 +173,7 @@ func (h *relayHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
|
||||
}
|
||||
case relay.FeatureTunnel:
|
||||
if feature, _ := f.(*relay.TunnelFeature); feature != nil {
|
||||
tunnelID = feature.ID
|
||||
tunnelID = relay.NewTunnelID(feature.ID[:])
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -210,7 +210,7 @@ func (h *relayHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
|
||||
return h.handleConnect(ctx, conn, network, address, log)
|
||||
case relay.CmdBind:
|
||||
if !tunnelID.IsZero() {
|
||||
return h.handleTunnel(ctx, conn, tunnelID, log)
|
||||
return h.handleBindTunnel(ctx, conn, network, tunnelID, log)
|
||||
}
|
||||
|
||||
defer conn.Close()
|
||||
|
@ -38,7 +38,7 @@ func (h *relayHandler) parseMetadata(md mdata.Metadata) (err error) {
|
||||
if bs := mdutil.GetInt(md, udpBufferSize); bs > 0 {
|
||||
h.md.udpBufferSize = int(math.Min(math.Max(float64(bs), 512), 64*1024))
|
||||
} else {
|
||||
h.md.udpBufferSize = 1500
|
||||
h.md.udpBufferSize = 4096
|
||||
}
|
||||
|
||||
h.md.hash = mdutil.GetString(md, hash)
|
||||
|
@ -81,16 +81,27 @@ func (t *Tunnel) AddConnector(c *Connector) {
|
||||
t.connectors = append(t.connectors, c)
|
||||
}
|
||||
|
||||
func (t *Tunnel) GetConnector() *Connector {
|
||||
func (t *Tunnel) GetConnector(network string) *Connector {
|
||||
t.mu.RLock()
|
||||
defer t.mu.RUnlock()
|
||||
|
||||
if len(t.connectors) == 0 {
|
||||
var connectors []*Connector
|
||||
for _, c := range t.connectors {
|
||||
if network == "udp" {
|
||||
if c.id.IsUDP() {
|
||||
connectors = append(connectors, c)
|
||||
}
|
||||
} else {
|
||||
if !c.id.IsUDP() {
|
||||
connectors = append(connectors, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(connectors) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
n := atomic.AddUint64(&t.n, 1) - 1
|
||||
return t.connectors[n%uint64(len(t.connectors))]
|
||||
return connectors[n%uint64(len(connectors))]
|
||||
}
|
||||
|
||||
func (t *Tunnel) clean() {
|
||||
@ -137,7 +148,7 @@ func (p *ConnectorPool) Add(tid relay.TunnelID, c *Connector) {
|
||||
t.AddConnector(c)
|
||||
}
|
||||
|
||||
func (p *ConnectorPool) Get(tid relay.TunnelID) *Connector {
|
||||
func (p *ConnectorPool) Get(network string, tid relay.TunnelID) *Connector {
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
@ -150,7 +161,7 @@ func (p *ConnectorPool) Get(tid relay.TunnelID) *Connector {
|
||||
return nil
|
||||
}
|
||||
|
||||
return t.GetConnector()
|
||||
return t.GetConnector(network)
|
||||
}
|
||||
|
||||
func parseTunnelID(s string) (tid relay.TunnelID) {
|
||||
@ -170,12 +181,12 @@ func parseTunnelID(s string) (tid relay.TunnelID) {
|
||||
return relay.NewTunnelID(uuid[:])
|
||||
}
|
||||
|
||||
func getTunnelConn(pool *ConnectorPool, tunnelID relay.TunnelID, retry int, log logger.Logger) (conn net.Conn, err error) {
|
||||
func getTunnelConn(network string, pool *ConnectorPool, tunnelID relay.TunnelID, retry int, log logger.Logger) (conn net.Conn, err error) {
|
||||
if retry <= 0 {
|
||||
retry = 1
|
||||
}
|
||||
for i := 0; i < retry; i++ {
|
||||
c := pool.Get(tunnelID)
|
||||
c := pool.Get(network, tunnelID)
|
||||
if c == nil {
|
||||
err = fmt.Errorf("tunnel %s not available", tunnelID.String())
|
||||
break
|
||||
|
Reference in New Issue
Block a user