DialerProxy: Fix SplitHTTP H3 dialerProxy (#3570)

* wip

* wip

* formatting

* cnc connection no longer needs to be a Packetconn

* dialerProxy: do not cancel connection when Dial context is cancelled
pull/3674/head
mmmray 4 months ago committed by GitHub
parent 0c7303960a
commit 498d8eb3cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -111,7 +111,7 @@ func canLookupIP(ctx context.Context, dst net.Destination, sockopt *SocketConfig
} }
func redirect(ctx context.Context, dst net.Destination, obt string) net.Conn { func redirect(ctx context.Context, dst net.Destination, obt string) net.Conn {
errors.LogInfo(ctx, "redirecting request " + dst.String() + " to " + obt) errors.LogInfo(ctx, "redirecting request "+dst.String()+" to "+obt)
h := obm.GetHandler(obt) h := obm.GetHandler(obt)
outbounds := session.OutboundsFromContext(ctx) outbounds := session.OutboundsFromContext(ctx)
ctx = session.ContextWithOutbounds(ctx, append(outbounds, &session.Outbound{ ctx = session.ContextWithOutbounds(ctx, append(outbounds, &session.Outbound{
@ -123,10 +123,16 @@ func redirect(ctx context.Context, dst net.Destination, obt string) net.Conn {
ur, uw := pipe.New(pipe.OptionsFromContext(ctx)...) ur, uw := pipe.New(pipe.OptionsFromContext(ctx)...)
dr, dw := pipe.New(pipe.OptionsFromContext(ctx)...) dr, dw := pipe.New(pipe.OptionsFromContext(ctx)...)
go h.Dispatch(ctx, &transport.Link{Reader: ur, Writer: dw}) go h.Dispatch(context.WithoutCancel(ctx), &transport.Link{Reader: ur, Writer: dw})
var readerOpt cnc.ConnectionOption
if dst.Network == net.Network_TCP {
readerOpt = cnc.ConnectionOutputMulti(dr)
} else {
readerOpt = cnc.ConnectionOutputMultiUDP(dr)
}
nc := cnc.NewConnection( nc := cnc.NewConnection(
cnc.ConnectionInputMulti(uw), cnc.ConnectionInputMulti(uw),
cnc.ConnectionOutputMulti(dr), readerOpt,
cnc.ConnectionOnClose(common.ChainedClosable{uw, dw}), cnc.ConnectionOnClose(common.ChainedClosable{uw, dw}),
) )
return nc return nc
@ -150,7 +156,7 @@ func DialSystem(ctx context.Context, dest net.Destination, sockopt *SocketConfig
ips, err := lookupIP(dest.Address.String(), sockopt.DomainStrategy, src) ips, err := lookupIP(dest.Address.String(), sockopt.DomainStrategy, src)
if err == nil && len(ips) > 0 { if err == nil && len(ips) > 0 {
dest.Address = net.IPAddress(ips[dice.Roll(len(ips))]) dest.Address = net.IPAddress(ips[dice.Roll(len(ips))])
errors.LogInfo(ctx, "replace destination with " + dest.String()) errors.LogInfo(ctx, "replace destination with "+dest.String())
} else if err != nil { } else if err != nil {
errors.LogWarningInner(ctx, err, "failed to resolve ip") errors.LogWarningInner(ctx, err, "failed to resolve ip")
} }

@ -118,7 +118,7 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in
return nil, err return nil, err
} }
var udpConn *net.UDPConn var udpConn net.PacketConn
var udpAddr *net.UDPAddr var udpAddr *net.UDPAddr
switch c := conn.(type) { switch c := conn.(type) {
@ -139,7 +139,11 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in
return nil, err return nil, err
} }
default: default:
return nil, errors.New("unsupported connection type: %T", conn) udpConn = &internet.FakePacketConn{c}
udpAddr, err = net.ResolveUDPAddr("udp", c.RemoteAddr().String())
if err != nil {
return nil, err
}
} }
return quic.DialEarly(ctx, udpConn, udpAddr, tlsCfg, cfg) return quic.DialEarly(ctx, udpConn, udpAddr, tlsCfg, cfg)

@ -2,6 +2,7 @@ package internet
import ( import (
"context" "context"
"math/rand"
"syscall" "syscall"
"time" "time"
@ -48,7 +49,7 @@ func hasBindAddr(sockopt *SocketConfig) bool {
} }
func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest net.Destination, sockopt *SocketConfig) (net.Conn, error) { func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest net.Destination, sockopt *SocketConfig) (net.Conn, error) {
errors.LogDebug(ctx, "dialing to " + dest.String()) errors.LogDebug(ctx, "dialing to "+dest.String())
if dest.Network == net.Network_UDP && !hasBindAddr(sockopt) { if dest.Network == net.Network_UDP && !hasBindAddr(sockopt) {
srcAddr := resolveSrcAddr(net.Network_UDP, src) srcAddr := resolveSrcAddr(net.Network_UDP, src)
@ -221,3 +222,29 @@ func RegisterDialerController(ctl control.Func) error {
dialer.controllers = append(dialer.controllers, ctl) dialer.controllers = append(dialer.controllers, ctl)
return nil return nil
} }
type FakePacketConn struct {
net.Conn
}
func (c *FakePacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
n, err = c.Read(p)
return n, c.RemoteAddr(), err
}
func (c *FakePacketConn) WriteTo(p []byte, _ net.Addr) (n int, err error) {
return c.Write(p)
}
func (c *FakePacketConn) LocalAddr() net.Addr {
return &net.TCPAddr{
IP: net.IP{byte(rand.Intn(256)), byte(rand.Intn(256)), byte(rand.Intn(256)), byte(rand.Intn(256))},
Port: rand.Intn(65536),
}
}
func (c *FakePacketConn) SetReadBuffer(bytes int) error {
// do nothing, this function is only there to suppress quic-go printing
// random warnings about UDP buffers to stdout
return nil
}

Loading…
Cancel
Save