-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some fixes about Hysteria 2 #3147
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
import ( | ||
"io" | ||
gonet "net" | ||
"math/rand" | ||
|
||
hyProtocol "github.com/apernet/hysteria/core/v2/international/protocol" | ||
"github.com/apernet/quic-go/quicvarint" | ||
|
@@ -12,6 +12,10 @@ | |
hyTransport "github.com/v2fly/v2ray-core/v5/transport/internet/hysteria2" | ||
) | ||
|
||
const ( | ||
paddingChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | ||
) | ||
|
||
// ConnWriter is TCP Connection Writer Wrapper | ||
type ConnWriter struct { | ||
io.Writer | ||
|
@@ -61,17 +65,17 @@ | |
func (c *ConnWriter) writeTCPHeader() error { | ||
c.TCPHeaderSent = true | ||
|
||
// TODO: the padding length here should be randomized | ||
|
||
padding := "Jimmy Was Here" | ||
paddingLen := len(padding) | ||
paddingLen := 64 + rand.Intn(512-64) | ||
padding := make([]byte, paddingLen) | ||
for i := range padding { | ||
padding[i] = paddingChars[rand.Intn(len(paddingChars))] | ||
} | ||
addressAndPort := c.Target.NetAddr() | ||
addressLen := len(addressAndPort) | ||
size := QuicLen(addressLen) + addressLen + QuicLen(paddingLen) + paddingLen | ||
|
||
if size > hyProtocol.MaxAddressLength+hyProtocol.MaxPaddingLength { | ||
return newError("invalid header length") | ||
if addressLen > hyProtocol.MaxAddressLength { | ||
return newError("address length too large: ", addressLen) | ||
} | ||
size := QuicLen(addressLen) + addressLen + QuicLen(paddingLen) + paddingLen | ||
Check failure Code scanning / CodeQL Size computation for allocation may overflow High
This operation, which is used in an
allocation Error loading related location Loading potentially large value Error loading related location Loading There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a corrent complaint. Maximum QuicLen is 8, addressLen is validated not to overflow (<= 2048), and paddingLen has a maximum length (511). |
||
|
||
buf := make([]byte, size) | ||
i := hyProtocol.VarintPut(buf, uint64(addressLen)) | ||
|
@@ -120,7 +124,7 @@ | |
return nil | ||
} | ||
|
||
func (w *PacketWriter) WriteTo(payload []byte, addr gonet.Addr) (int, error) { | ||
func (w *PacketWriter) WriteTo(payload []byte, addr net.Addr) (int, error) { | ||
dest := net.DestinationFromAddr(addr) | ||
|
||
return w.writePacket(payload, dest) | ||
|
@@ -145,7 +149,10 @@ | |
func (c *ConnReader) ReadMultiBuffer() (buf.MultiBuffer, error) { | ||
b := buf.New() | ||
_, err := b.ReadFrom(c) | ||
return buf.MultiBuffer{b}, err | ||
if err != nil { | ||
return nil, err | ||
} | ||
return buf.MultiBuffer{b}, nil | ||
} | ||
|
||
// PacketPayload combines udp payload and destination | ||
|
@@ -178,26 +185,3 @@ | |
b := buf.FromBytes(data) | ||
return &PacketPayload{Target: *dest, Buffer: buf.MultiBuffer{b}}, nil | ||
} | ||
|
||
type PacketConnectionReader struct { | ||
reader *PacketReader | ||
payload *PacketPayload | ||
} | ||
|
||
func (r *PacketConnectionReader) ReadFrom(p []byte) (n int, addr gonet.Addr, err error) { | ||
if r.payload == nil || r.payload.Buffer.IsEmpty() { | ||
r.payload, err = r.reader.ReadMultiBufferWithMetadata() | ||
if err != nil { | ||
return | ||
} | ||
} | ||
|
||
addr = &gonet.UDPAddr{ | ||
IP: r.payload.Target.Address.IP(), | ||
Port: int(r.payload.Target.Port), | ||
} | ||
|
||
r.payload.Buffer, n = buf.SplitFirstBytes(r.payload.Buffer, p) | ||
|
||
return | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,6 @@ import ( | |
"github.com/v2fly/v2ray-core/v5/common/errors" | ||
"github.com/v2fly/v2ray-core/v5/common/log" | ||
"github.com/v2fly/v2ray-core/v5/common/net" | ||
"github.com/v2fly/v2ray-core/v5/common/net/packetaddr" | ||
udp_proto "github.com/v2fly/v2ray-core/v5/common/protocol/udp" | ||
"github.com/v2fly/v2ray-core/v5/common/session" | ||
"github.com/v2fly/v2ray-core/v5/common/signal" | ||
|
@@ -33,8 +32,7 @@ func init() { | |
|
||
// Server is an inbound connection handler that handles messages in protocol. | ||
type Server struct { | ||
policyManager policy.Manager | ||
packetEncoding packetaddr.PacketAddrType | ||
policyManager policy.Manager | ||
} | ||
|
||
// NewServer creates a new inbound handler. | ||
|
@@ -171,12 +169,6 @@ func (s *Server) handleConnection(ctx context.Context, sessionPolicy policy.Sess | |
|
||
func (s *Server) handleUDPPayload(ctx context.Context, clientReader *PacketReader, clientWriter *PacketWriter, dispatcher routing.Dispatcher) error { // {{{ | ||
udpDispatcherConstructor := udp.NewSplitDispatcher | ||
switch s.packetEncoding { | ||
case packetaddr.PacketAddrType_None: | ||
case packetaddr.PacketAddrType_Packet: | ||
packetAddrDispatcherFactory := udp.NewPacketAddrDispatcherCreator(ctx) | ||
udpDispatcherConstructor = packetAddrDispatcherFactory.NewPacketAddrDispatcher | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In order to correctly process packet addr fullcone traffic, the incoming stream should be decoded: https://github.com/v2fly/v2ray-core/blob/master/proxy/trojan/client.go#L90 . I can merge this now without requiring additional changes, but another merge request to support packetaddr is welcomed. |
||
|
||
udpServer := udpDispatcherConstructor(dispatcher, func(ctx context.Context, packet *udp_proto.Packet) { | ||
if err := clientWriter.WriteMultiBufferWithMetadata(buf.MultiBuffer{packet.Payload}, packet.Source); err != nil { | ||
|
@@ -185,7 +177,6 @@ func (s *Server) handleUDPPayload(ctx context.Context, clientReader *PacketReade | |
}) | ||
|
||
inbound := session.InboundFromContext(ctx) | ||
// user := inbound.User | ||
|
||
for { | ||
select { | ||
|
@@ -213,4 +204,4 @@ func (s *Server) handleUDPPayload(ctx context.Context, clientReader *PacketReade | |
} | ||
} | ||
} | ||
} // }}} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the padding have to be filled with something, although it is always a good habit to initialize a buffer. It might be changed in the future, but let's keep it that way for now.