mirror of
https://github.com/cmz0228/hysteria-dev.git
synced 2025-06-22 20:40:12 +00:00
cleanup: drop legacy v2 protocol support
This commit is contained in:
parent
e3c3088596
commit
223a9a4203
@ -5,9 +5,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
protocolVersion = uint8(3)
|
protocolVersion = uint8(3)
|
||||||
protocolVersionV2 = uint8(2)
|
protocolTimeout = 10 * time.Second
|
||||||
protocolTimeout = 10 * time.Second
|
|
||||||
|
|
||||||
closeErrorCodeGeneric = 0
|
closeErrorCodeGeneric = 0
|
||||||
closeErrorCodeProtocol = 1
|
closeErrorCodeProtocol = 1
|
||||||
@ -65,12 +64,3 @@ func (m udpMessage) HeaderSize() int {
|
|||||||
func (m udpMessage) Size() int {
|
func (m udpMessage) Size() int {
|
||||||
return m.HeaderSize() + len(m.Data)
|
return m.HeaderSize() + len(m.Data)
|
||||||
}
|
}
|
||||||
|
|
||||||
type udpMessageV2 struct {
|
|
||||||
SessionID uint32
|
|
||||||
HostLen uint16 `struc:"sizeof=Host"`
|
|
||||||
Host string
|
|
||||||
Port uint16
|
|
||||||
DataLen uint16 `struc:"sizeof=Data"`
|
|
||||||
Data []byte
|
|
||||||
}
|
|
||||||
|
@ -121,7 +121,7 @@ func (s *Server) handleClient(cs quic.Connection) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Handle the control stream
|
// Handle the control stream
|
||||||
auth, ok, v2, err := s.handleControlStream(cs, stream)
|
auth, ok, err := s.handleControlStream(cs, stream)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = cs.CloseWithError(closeErrorCodeProtocol, "protocol error")
|
_ = cs.CloseWithError(closeErrorCodeProtocol, "protocol error")
|
||||||
return
|
return
|
||||||
@ -131,7 +131,7 @@ func (s *Server) handleClient(cs quic.Connection) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Start accepting streams and messages
|
// Start accepting streams and messages
|
||||||
sc := newServerClient(v2, cs, s.transport, auth, s.disableUDP, s.aclEngine,
|
sc := newServerClient(cs, s.transport, auth, s.disableUDP, s.aclEngine,
|
||||||
s.tcpRequestFunc, s.tcpErrorFunc, s.udpRequestFunc, s.udpErrorFunc,
|
s.tcpRequestFunc, s.tcpErrorFunc, s.udpRequestFunc, s.udpErrorFunc,
|
||||||
s.upCounterVec, s.downCounterVec, s.connGaugeVec)
|
s.upCounterVec, s.downCounterVec, s.connGaugeVec)
|
||||||
err = sc.Run()
|
err = sc.Run()
|
||||||
@ -140,26 +140,25 @@ func (s *Server) handleClient(cs quic.Connection) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Auth & negotiate speed
|
// Auth & negotiate speed
|
||||||
func (s *Server) handleControlStream(cs quic.Connection, stream quic.Stream) ([]byte, bool, bool, error) {
|
func (s *Server) handleControlStream(cs quic.Connection, stream quic.Stream) ([]byte, bool, error) {
|
||||||
// Check version
|
// Check version
|
||||||
vb := make([]byte, 1)
|
vb := make([]byte, 1)
|
||||||
_, err := stream.Read(vb)
|
_, err := stream.Read(vb)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, false, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
if vb[0] != protocolVersion && vb[0] != protocolVersionV2 {
|
if vb[0] != protocolVersion {
|
||||||
return nil, false, false, fmt.Errorf("unsupported protocol version %d, expecting %d/%d",
|
return nil, false, fmt.Errorf("unsupported protocol version %d, expecting %d", vb[0], protocolVersion)
|
||||||
vb[0], protocolVersionV2, protocolVersion)
|
|
||||||
}
|
}
|
||||||
// Parse client hello
|
// Parse client hello
|
||||||
var ch clientHello
|
var ch clientHello
|
||||||
err = struc.Unpack(stream, &ch)
|
err = struc.Unpack(stream, &ch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, false, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
// Speed
|
// Speed
|
||||||
if ch.Rate.SendBPS == 0 || ch.Rate.RecvBPS == 0 {
|
if ch.Rate.SendBPS == 0 || ch.Rate.RecvBPS == 0 {
|
||||||
return nil, false, false, errors.New("invalid rate from client")
|
return nil, false, errors.New("invalid rate from client")
|
||||||
}
|
}
|
||||||
serverSendBPS, serverRecvBPS := ch.Rate.RecvBPS, ch.Rate.SendBPS
|
serverSendBPS, serverRecvBPS := ch.Rate.RecvBPS, ch.Rate.SendBPS
|
||||||
if s.sendBPS > 0 && serverSendBPS > s.sendBPS {
|
if s.sendBPS > 0 && serverSendBPS > s.sendBPS {
|
||||||
@ -180,11 +179,11 @@ func (s *Server) handleControlStream(cs quic.Connection, stream quic.Stream) ([]
|
|||||||
Message: msg,
|
Message: msg,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, false, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
// Set the congestion accordingly
|
// Set the congestion accordingly
|
||||||
if ok {
|
if ok {
|
||||||
cs.SetCongestionControl(congestion.NewBrutalSender(serverSendBPS))
|
cs.SetCongestionControl(congestion.NewBrutalSender(serverSendBPS))
|
||||||
}
|
}
|
||||||
return ch.Auth, ok, vb[0] == protocolVersionV2, nil
|
return ch.Auth, ok, nil
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,6 @@ import (
|
|||||||
const udpBufferSize = 65535
|
const udpBufferSize = 65535
|
||||||
|
|
||||||
type serverClient struct {
|
type serverClient struct {
|
||||||
V2 bool
|
|
||||||
CS quic.Connection
|
CS quic.Connection
|
||||||
Transport *transport.ServerTransport
|
Transport *transport.ServerTransport
|
||||||
Auth []byte
|
Auth []byte
|
||||||
@ -41,14 +40,13 @@ type serverClient struct {
|
|||||||
udpDefragger defragger
|
udpDefragger defragger
|
||||||
}
|
}
|
||||||
|
|
||||||
func newServerClient(v2 bool, cs quic.Connection, tr *transport.ServerTransport, auth []byte, disableUDP bool, ACLEngine *acl.Engine,
|
func newServerClient(cs quic.Connection, tr *transport.ServerTransport, auth []byte, disableUDP bool, ACLEngine *acl.Engine,
|
||||||
CTCPRequestFunc TCPRequestFunc, CTCPErrorFunc TCPErrorFunc,
|
CTCPRequestFunc TCPRequestFunc, CTCPErrorFunc TCPErrorFunc,
|
||||||
CUDPRequestFunc UDPRequestFunc, CUDPErrorFunc UDPErrorFunc,
|
CUDPRequestFunc UDPRequestFunc, CUDPErrorFunc UDPErrorFunc,
|
||||||
UpCounterVec, DownCounterVec *prometheus.CounterVec,
|
UpCounterVec, DownCounterVec *prometheus.CounterVec,
|
||||||
ConnGaugeVec *prometheus.GaugeVec,
|
ConnGaugeVec *prometheus.GaugeVec,
|
||||||
) *serverClient {
|
) *serverClient {
|
||||||
sc := &serverClient{
|
sc := &serverClient{
|
||||||
V2: v2,
|
|
||||||
CS: cs,
|
CS: cs,
|
||||||
Transport: tr,
|
Transport: tr,
|
||||||
Auth: auth,
|
Auth: auth,
|
||||||
@ -125,26 +123,9 @@ func (c *serverClient) handleStream(stream quic.Stream) {
|
|||||||
|
|
||||||
func (c *serverClient) handleMessage(msg []byte) {
|
func (c *serverClient) handleMessage(msg []byte) {
|
||||||
var udpMsg udpMessage
|
var udpMsg udpMessage
|
||||||
if c.V2 {
|
err := struc.Unpack(bytes.NewBuffer(msg), &udpMsg)
|
||||||
var udpMsgV2 udpMessageV2
|
if err != nil {
|
||||||
err := struc.Unpack(bytes.NewBuffer(msg), &udpMsgV2)
|
return
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
udpMsg = udpMessage{
|
|
||||||
SessionID: udpMsgV2.SessionID,
|
|
||||||
HostLen: udpMsgV2.HostLen,
|
|
||||||
Host: udpMsgV2.Host,
|
|
||||||
Port: udpMsgV2.Port,
|
|
||||||
FragCount: 1,
|
|
||||||
DataLen: udpMsgV2.DataLen,
|
|
||||||
Data: udpMsgV2.Data,
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
err := struc.Unpack(bytes.NewBuffer(msg), &udpMsg)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
dfMsg := c.udpDefragger.Feed(udpMsg)
|
dfMsg := c.udpDefragger.Feed(udpMsg)
|
||||||
if dfMsg == nil {
|
if dfMsg == nil {
|
||||||
@ -340,36 +321,25 @@ func (c *serverClient) handleUDP(stream quic.Stream) {
|
|||||||
n, rAddr, err := conn.ReadFromUDP(buf)
|
n, rAddr, err := conn.ReadFromUDP(buf)
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
var msgBuf bytes.Buffer
|
var msgBuf bytes.Buffer
|
||||||
if c.V2 {
|
msg := udpMessage{
|
||||||
msg := udpMessageV2{
|
SessionID: id,
|
||||||
SessionID: id,
|
Host: rAddr.IP.String(),
|
||||||
Host: rAddr.IP.String(),
|
Port: uint16(rAddr.Port),
|
||||||
Port: uint16(rAddr.Port),
|
FragCount: 1,
|
||||||
Data: buf[:n],
|
Data: buf[:n],
|
||||||
}
|
}
|
||||||
_ = struc.Pack(&msgBuf, &msg)
|
// try no frag first
|
||||||
_ = c.CS.SendMessage(msgBuf.Bytes())
|
_ = struc.Pack(&msgBuf, &msg)
|
||||||
} else {
|
sendErr := c.CS.SendMessage(msgBuf.Bytes())
|
||||||
msg := udpMessage{
|
if sendErr != nil {
|
||||||
SessionID: id,
|
if errSize, ok := sendErr.(quic.ErrMessageToLarge); ok {
|
||||||
Host: rAddr.IP.String(),
|
// need to frag
|
||||||
Port: uint16(rAddr.Port),
|
msg.MsgID = uint16(rand.Intn(0xFFFF)) + 1 // msgID must be > 0 when fragCount > 1
|
||||||
FragCount: 1,
|
fragMsgs := fragUDPMessage(msg, int(errSize))
|
||||||
Data: buf[:n],
|
for _, fragMsg := range fragMsgs {
|
||||||
}
|
msgBuf.Reset()
|
||||||
// try no frag first
|
_ = struc.Pack(&msgBuf, &fragMsg)
|
||||||
_ = struc.Pack(&msgBuf, &msg)
|
_ = c.CS.SendMessage(msgBuf.Bytes())
|
||||||
sendErr := c.CS.SendMessage(msgBuf.Bytes())
|
|
||||||
if sendErr != nil {
|
|
||||||
if errSize, ok := sendErr.(quic.ErrMessageToLarge); ok {
|
|
||||||
// need to frag
|
|
||||||
msg.MsgID = uint16(rand.Intn(0xFFFF)) + 1 // msgID must be > 0 when fragCount > 1
|
|
||||||
fragMsgs := fragUDPMessage(msg, int(errSize))
|
|
||||||
for _, fragMsg := range fragMsgs {
|
|
||||||
msgBuf.Reset()
|
|
||||||
_ = struc.Pack(&msgBuf, &fragMsg)
|
|
||||||
_ = c.CS.SendMessage(msgBuf.Bytes())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user