add UDP support for reverse proxy tunnel
This commit is contained in:
@ -92,11 +92,9 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
|
||||
var rw io.ReadWriter = conn
|
||||
var host string
|
||||
var protocol string
|
||||
if h.md.sniffing {
|
||||
if network == "tcp" {
|
||||
rw, host, protocol, _ = forward.Sniffing(ctx, conn)
|
||||
log.Debugf("sniffing: host=%s, protocol=%s", host, protocol)
|
||||
}
|
||||
if network == "tcp" && h.md.sniffing {
|
||||
rw, host, protocol, _ = forward.Sniffing(ctx, conn)
|
||||
log.Debugf("sniffing: host=%s, protocol=%s", host, protocol)
|
||||
}
|
||||
|
||||
if protocol == forward.ProtoHTTP {
|
||||
@ -107,19 +105,23 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
|
||||
if _, _, err := net.SplitHostPort(host); err != nil {
|
||||
host = net.JoinHostPort(host, "0")
|
||||
}
|
||||
target := &chain.Node{
|
||||
Addr: host,
|
||||
|
||||
var target *chain.Node
|
||||
if host != "" {
|
||||
target = &chain.Node{
|
||||
Addr: host,
|
||||
}
|
||||
}
|
||||
if h.hop != nil {
|
||||
target = h.hop.Select(ctx,
|
||||
chain.HostSelectOption(host),
|
||||
chain.ProtocolSelectOption(protocol),
|
||||
)
|
||||
if target == nil {
|
||||
err := errors.New("target not available")
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
if target == nil {
|
||||
err := errors.New("target not available")
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
log = log.WithFields(map[string]any{
|
||||
@ -172,7 +174,9 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, log l
|
||||
return err
|
||||
}
|
||||
|
||||
var target *chain.Node
|
||||
target := &chain.Node{
|
||||
Addr: req.Host,
|
||||
}
|
||||
if h.hop != nil {
|
||||
target = h.hop.Select(ctx,
|
||||
chain.HostSelectOption(req.Host),
|
||||
@ -235,6 +239,9 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, log l
|
||||
go func() {
|
||||
defer cc.Close()
|
||||
err := xnet.CopyBuffer(rw, cc, 8192)
|
||||
if err != nil {
|
||||
resp.Write(rw)
|
||||
}
|
||||
log.Debugf("close connection to node %s(%s), reason: %v", target.Name, target.Addr, err)
|
||||
connPool.Delete(target)
|
||||
}()
|
||||
@ -266,9 +273,12 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, log l
|
||||
return err
|
||||
}
|
||||
|
||||
// cc.SetReadDeadline(time.Now().Add(10 * time.Second))
|
||||
|
||||
return nil
|
||||
}()
|
||||
if err != nil {
|
||||
// log.Error(err)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -92,11 +92,9 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
|
||||
var rw io.ReadWriter = conn
|
||||
var host string
|
||||
var protocol string
|
||||
if h.md.sniffing {
|
||||
if network == "tcp" {
|
||||
rw, host, protocol, _ = forward.Sniffing(ctx, conn)
|
||||
log.Debugf("sniffing: host=%s, protocol=%s", host, protocol)
|
||||
}
|
||||
if network == "tcp" && h.md.sniffing {
|
||||
rw, host, protocol, _ = forward.Sniffing(ctx, conn)
|
||||
log.Debugf("sniffing: host=%s, protocol=%s", host, protocol)
|
||||
}
|
||||
if protocol == forward.ProtoHTTP {
|
||||
h.handleHTTP(ctx, rw, log)
|
||||
@ -109,6 +107,11 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
|
||||
}
|
||||
}
|
||||
var target *chain.Node
|
||||
if host != "" {
|
||||
target = &chain.Node{
|
||||
Addr: host,
|
||||
}
|
||||
}
|
||||
if h.hop != nil {
|
||||
target = h.hop.Select(ctx,
|
||||
chain.HostSelectOption(host),
|
||||
@ -172,7 +175,9 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, log l
|
||||
return err
|
||||
}
|
||||
|
||||
var target *chain.Node
|
||||
target := &chain.Node{
|
||||
Addr: req.Host,
|
||||
}
|
||||
if h.hop != nil {
|
||||
target = h.hop.Select(ctx,
|
||||
chain.HostSelectOption(req.Host),
|
||||
@ -235,6 +240,9 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, log l
|
||||
go func() {
|
||||
defer cc.Close()
|
||||
err := xnet.CopyBuffer(rw, cc, 8192)
|
||||
if err != nil {
|
||||
resp.Write(rw)
|
||||
}
|
||||
log.Debugf("close connection to node %s(%s), reason: %v", target.Name, target.Addr, err)
|
||||
connPool.Delete(target)
|
||||
}()
|
||||
@ -266,6 +274,8 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, log l
|
||||
return err
|
||||
}
|
||||
|
||||
// cc.SetReadDeadline(time.Now().Add(10 * time.Second))
|
||||
|
||||
return nil
|
||||
}()
|
||||
if err != nil {
|
||||
|
@ -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
|
||||
|
@ -37,7 +37,7 @@ func (h *socks5Handler) 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.compatibilityMode = mdutil.GetBool(md, compatibilityMode)
|
||||
|
@ -27,7 +27,7 @@ func (h *ssuHandler) parseMetadata(md mdata.Metadata) (err error) {
|
||||
if bs := mdutil.GetInt(md, bufferSize); bs > 0 {
|
||||
h.md.bufferSize = int(math.Min(math.Max(float64(bs), 512), 64*1024))
|
||||
} else {
|
||||
h.md.bufferSize = 1500
|
||||
h.md.bufferSize = 4096
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ func (h *tapHandler) parseMetadata(md mdata.Metadata) (err error) {
|
||||
h.md.key = mdutil.GetString(md, key)
|
||||
h.md.bufferSize = mdutil.GetInt(md, bufferSize)
|
||||
if h.md.bufferSize <= 0 {
|
||||
h.md.bufferSize = 1500
|
||||
h.md.bufferSize = 4096
|
||||
}
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user