Skip to content

Commit

Permalink
fix hysteria2OnTCP and use ListenSystemPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyHuang454 committed May 9, 2024
1 parent 76051df commit 08ea4ef
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
3 changes: 2 additions & 1 deletion proxy/hysteria2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
}
hyConn, IsHy2Transport := iConn.(*hy2_transport.HyConn)

if !IsHy2Transport && !hyConn.IsUDPExtension { // is not hysteria2 and proxing UDP
if !IsHy2Transport && network == net.Network_UDP {
// hysteria2 need to use udp extension to proxy UDP.
return newError(hy2_transport.CanNotUseUdpExtension)
}

Expand Down
8 changes: 4 additions & 4 deletions testing/scenarios/hy2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,9 @@ func testHysteria2Offical(t *testing.T, isUDP bool) {
Security: &protocol.SecurityConfig{
Type: protocol.SecurityType_NONE,
},
Congestion: &hy2_transport.Congestion{Type: "brutal", UpMbps: 100, DownMbps: 100},
UseUdpExtension: true,
Password: "password",
Congestion: &hy2_transport.Congestion{Type: "brutal", UpMbps: 100, DownMbps: 100},
UseUdpExtension: true,
Password: "password",
}),
},
},
Expand Down Expand Up @@ -321,7 +321,7 @@ func testHysteria2Offical(t *testing.T, isUDP bool) {
}
}

func TestHysteria2TCP(t *testing.T) {
func TestHysteria2OnTCP(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: xor,
}
Expand Down
22 changes: 22 additions & 0 deletions transport/internet/hysteria2/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ func InitAddress(dest net.Destination) (net.Addr, error) {
return destAddr, nil
}

type connFactory struct {
hy_client.ConnFactory

NewFunc func(addr net.Addr) (net.PacketConn, error)
}

func (f *connFactory) New(addr net.Addr) (net.PacketConn, error) {
return f.NewFunc(addr)
}

func NewHyClient(dest net.Destination, streamSettings *internet.MemoryStreamConfig) (hy_client.Client, error) {
tlsConfig, err := InitTLSConifg(streamSettings)
if err != nil {
Expand All @@ -68,6 +78,18 @@ func NewHyClient(dest net.Destination, streamSettings *internet.MemoryStreamConf
TLSConfig: *tlsConfig,
Auth: config.GetPassword(),
ServerAddr: serverAddr,
ConnFactory: &connFactory{
NewFunc: func(addr net.Addr) (net.PacketConn, error) {
rawConn, err := internet.ListenSystemPacket(context.Background(), &net.UDPAddr{
IP: []byte{0, 0, 0, 0},
Port: 0,
}, streamSettings.SocketSettings)
if err != nil {
return nil, err
}
return rawConn.(*net.UDPConn), nil
},
},
})
if err != nil {
return nil, err
Expand Down

0 comments on commit 08ea4ef

Please sign in to comment.