Skip to content

Commit 498d8eb

Browse files
authored
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
1 parent 0c73039 commit 498d8eb

3 files changed

Lines changed: 44 additions & 7 deletions

File tree

transport/internet/dialer.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func canLookupIP(ctx context.Context, dst net.Destination, sockopt *SocketConfig
111111
}
112112

113113
func redirect(ctx context.Context, dst net.Destination, obt string) net.Conn {
114-
errors.LogInfo(ctx, "redirecting request " + dst.String() + " to " + obt)
114+
errors.LogInfo(ctx, "redirecting request "+dst.String()+" to "+obt)
115115
h := obm.GetHandler(obt)
116116
outbounds := session.OutboundsFromContext(ctx)
117117
ctx = session.ContextWithOutbounds(ctx, append(outbounds, &session.Outbound{
@@ -123,10 +123,16 @@ func redirect(ctx context.Context, dst net.Destination, obt string) net.Conn {
123123
ur, uw := pipe.New(pipe.OptionsFromContext(ctx)...)
124124
dr, dw := pipe.New(pipe.OptionsFromContext(ctx)...)
125125

126-
go h.Dispatch(ctx, &transport.Link{Reader: ur, Writer: dw})
126+
go h.Dispatch(context.WithoutCancel(ctx), &transport.Link{Reader: ur, Writer: dw})
127+
var readerOpt cnc.ConnectionOption
128+
if dst.Network == net.Network_TCP {
129+
readerOpt = cnc.ConnectionOutputMulti(dr)
130+
} else {
131+
readerOpt = cnc.ConnectionOutputMultiUDP(dr)
132+
}
127133
nc := cnc.NewConnection(
128134
cnc.ConnectionInputMulti(uw),
129-
cnc.ConnectionOutputMulti(dr),
135+
readerOpt,
130136
cnc.ConnectionOnClose(common.ChainedClosable{uw, dw}),
131137
)
132138
return nc
@@ -150,7 +156,7 @@ func DialSystem(ctx context.Context, dest net.Destination, sockopt *SocketConfig
150156
ips, err := lookupIP(dest.Address.String(), sockopt.DomainStrategy, src)
151157
if err == nil && len(ips) > 0 {
152158
dest.Address = net.IPAddress(ips[dice.Roll(len(ips))])
153-
errors.LogInfo(ctx, "replace destination with " + dest.String())
159+
errors.LogInfo(ctx, "replace destination with "+dest.String())
154160
} else if err != nil {
155161
errors.LogWarningInner(ctx, err, "failed to resolve ip")
156162
}

transport/internet/splithttp/dialer.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in
118118
return nil, err
119119
}
120120

121-
var udpConn *net.UDPConn
121+
var udpConn net.PacketConn
122122
var udpAddr *net.UDPAddr
123123

124124
switch c := conn.(type) {
@@ -139,7 +139,11 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in
139139
return nil, err
140140
}
141141
default:
142-
return nil, errors.New("unsupported connection type: %T", conn)
142+
udpConn = &internet.FakePacketConn{c}
143+
udpAddr, err = net.ResolveUDPAddr("udp", c.RemoteAddr().String())
144+
if err != nil {
145+
return nil, err
146+
}
143147
}
144148

145149
return quic.DialEarly(ctx, udpConn, udpAddr, tlsCfg, cfg)

transport/internet/system_dialer.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package internet
22

33
import (
44
"context"
5+
"math/rand"
56
"syscall"
67
"time"
78

@@ -48,7 +49,7 @@ func hasBindAddr(sockopt *SocketConfig) bool {
4849
}
4950

5051
func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest net.Destination, sockopt *SocketConfig) (net.Conn, error) {
51-
errors.LogDebug(ctx, "dialing to " + dest.String())
52+
errors.LogDebug(ctx, "dialing to "+dest.String())
5253

5354
if dest.Network == net.Network_UDP && !hasBindAddr(sockopt) {
5455
srcAddr := resolveSrcAddr(net.Network_UDP, src)
@@ -221,3 +222,29 @@ func RegisterDialerController(ctl control.Func) error {
221222
dialer.controllers = append(dialer.controllers, ctl)
222223
return nil
223224
}
225+
226+
type FakePacketConn struct {
227+
net.Conn
228+
}
229+
230+
func (c *FakePacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
231+
n, err = c.Read(p)
232+
return n, c.RemoteAddr(), err
233+
}
234+
235+
func (c *FakePacketConn) WriteTo(p []byte, _ net.Addr) (n int, err error) {
236+
return c.Write(p)
237+
}
238+
239+
func (c *FakePacketConn) LocalAddr() net.Addr {
240+
return &net.TCPAddr{
241+
IP: net.IP{byte(rand.Intn(256)), byte(rand.Intn(256)), byte(rand.Intn(256)), byte(rand.Intn(256))},
242+
Port: rand.Intn(65536),
243+
}
244+
}
245+
246+
func (c *FakePacketConn) SetReadBuffer(bytes int) error {
247+
// do nothing, this function is only there to suppress quic-go printing
248+
// random warnings about UDP buffers to stdout
249+
return nil
250+
}

0 commit comments

Comments
 (0)