diff --git a/transport/tuic/server.go b/transport/tuic/server.go index 152c9d1b..0b6a250d 100644 --- a/transport/tuic/server.go +++ b/transport/tuic/server.go @@ -12,6 +12,7 @@ import ( C "github.com/metacubex/mihomo/constant" "github.com/metacubex/mihomo/transport/socks5" "github.com/metacubex/mihomo/transport/tuic/common" + "github.com/metacubex/mihomo/transport/tuic/types" v4 "github.com/metacubex/mihomo/transport/tuic/v4" v5 "github.com/metacubex/mihomo/transport/tuic/v5" @@ -72,8 +73,8 @@ type serverHandler struct { quicConn *quic.Conn uuid uuid.UUID - v4Handler common.ServerHandler - v5Handler common.ServerHandler + v4Handler types.ServerHandler + v5Handler types.ServerHandler } func (s *serverHandler) handle() { @@ -148,7 +149,7 @@ func (s *serverHandler) handleStream() (err error) { return err } go func() (err error) { - stream := common.NewQuicStreamConn( + stream := types.NewQuicStreamConn( quicStream, s.quicConn.LocalAddr(), s.quicConn.RemoteAddr(), diff --git a/transport/tuic/tuic.go b/transport/tuic/tuic.go index ae52c07a..f3595688 100644 --- a/transport/tuic/tuic.go +++ b/transport/tuic/tuic.go @@ -2,6 +2,7 @@ package tuic import ( "github.com/metacubex/mihomo/transport/tuic/common" + "github.com/metacubex/mihomo/transport/tuic/types" v4 "github.com/metacubex/mihomo/transport/tuic/v4" v5 "github.com/metacubex/mihomo/transport/tuic/v5" ) @@ -9,7 +10,7 @@ import ( type ClientOptionV4 = v4.ClientOption type ClientOptionV5 = v5.ClientOption -type Client = common.Client +type Client = types.Client func NewClientV4(clientOption *ClientOptionV4, udp bool, dialFn DialFunc) Client { return v4.NewClient(clientOption, udp, dialFn) @@ -19,9 +20,9 @@ func NewClientV5(clientOption *ClientOptionV5, udp bool, dialFn DialFunc) Client return v5.NewClient(clientOption, udp, dialFn) } -type DialFunc = common.DialFunc +type DialFunc = types.DialFunc -var TooManyOpenStreams = common.TooManyOpenStreams +var TooManyOpenStreams = types.TooManyOpenStreams const DefaultStreamReceiveWindow = common.DefaultStreamReceiveWindow const DefaultConnectionReceiveWindow = common.DefaultConnectionReceiveWindow @@ -31,9 +32,9 @@ var PacketOverHeadV4 = v4.PacketOverHead var PacketOverHeadV5 = v5.PacketOverHead var MaxFragSizeV5 = v5.MaxFragSize -type UdpRelayMode = common.UdpRelayMode +type UdpRelayMode = types.UdpRelayMode const ( - QUIC = common.QUIC - NATIVE = common.NATIVE + QUIC = types.QUIC + NATIVE = types.NATIVE ) diff --git a/transport/tuic/common/stream.go b/transport/tuic/types/stream.go similarity index 99% rename from transport/tuic/common/stream.go rename to transport/tuic/types/stream.go index 54f91d1c..2f6c18a8 100644 --- a/transport/tuic/common/stream.go +++ b/transport/tuic/types/stream.go @@ -1,4 +1,4 @@ -package common +package types import ( "net" diff --git a/transport/tuic/common/type.go b/transport/tuic/types/type.go similarity index 98% rename from transport/tuic/common/type.go rename to transport/tuic/types/type.go index 9a2504eb..0d11191d 100644 --- a/transport/tuic/common/type.go +++ b/transport/tuic/types/type.go @@ -1,4 +1,4 @@ -package common +package types import ( "bufio" diff --git a/transport/tuic/v4/client.go b/transport/tuic/v4/client.go index f5b3db14..57d2d1de 100644 --- a/transport/tuic/v4/client.go +++ b/transport/tuic/v4/client.go @@ -17,7 +17,7 @@ import ( "github.com/metacubex/mihomo/common/xsync" C "github.com/metacubex/mihomo/constant" "github.com/metacubex/mihomo/log" - "github.com/metacubex/mihomo/transport/tuic/common" + "github.com/metacubex/mihomo/transport/tuic/types" "github.com/metacubex/quic-go" "github.com/metacubex/randv2" @@ -25,7 +25,7 @@ import ( type ClientOption struct { Token [32]byte - UdpRelayMode common.UdpRelayMode + UdpRelayMode types.UdpRelayMode RequestTimeout time.Duration MaxUdpRelayPacketSize int FastOpen bool @@ -34,7 +34,7 @@ type ClientOption struct { type clientImpl struct { *ClientOption - dialFn common.DialFunc + dialFn types.DialFunc udp bool quicConn *quic.Conn @@ -79,7 +79,7 @@ func (t *clientImpl) getQuicConn(ctx context.Context) (*quic.Conn, error) { if t.udp { go func() { switch t.UdpRelayMode { - case common.QUIC: + case types.QUIC: _ = t.handleUniStream(quicConn) default: // native _ = t.handleMessage(quicConn) @@ -152,7 +152,7 @@ func (t *clientImpl) handleUniStream(quicConn *quic.Conn) (err error) { if err != nil { return } - if t.udp && t.UdpRelayMode == common.QUIC { + if t.udp && t.UdpRelayMode == types.QUIC { assocId = packet.ASSOC_ID if val, ok := t.udpInputMap.Load(assocId); ok { if conn, ok := val.(net.Conn); ok { @@ -202,7 +202,7 @@ func (t *clientImpl) handleMessage(quicConn *quic.Conn) (err error) { if err != nil { return } - if t.udp && t.UdpRelayMode == common.NATIVE { + if t.udp && t.UdpRelayMode == types.NATIVE { assocId = packet.ASSOC_ID if val, ok := t.udpInputMap.Load(assocId); ok { if conn, ok := val.(net.Conn); ok { @@ -253,7 +253,7 @@ func (t *clientImpl) forceClose(quicConn *quic.Conn, err error) { func (t *clientImpl) Close() { t.closed.Store(true) if t.openStreams.Load() == 0 { - t.forceClose(nil, common.ClientClosed) + t.forceClose(nil, types.ClientClosed) } } @@ -265,7 +265,7 @@ func (t *clientImpl) DialContext(ctx context.Context, metadata *C.Metadata) (net openStreams := t.openStreams.Add(1) if openStreams >= t.MaxOpenStreams { t.openStreams.Add(-1) - return nil, common.TooManyOpenStreams + return nil, types.TooManyOpenStreams } stream, err := func() (stream net.Conn, err error) { defer func() { @@ -281,7 +281,7 @@ func (t *clientImpl) DialContext(ctx context.Context, metadata *C.Metadata) (net if err != nil { return nil, err } - stream = common.NewQuicStreamConn( + stream = types.NewQuicStreamConn( quicStream, quicConn.LocalAddr(), quicConn.RemoteAddr(), @@ -289,7 +289,7 @@ func (t *clientImpl) DialContext(ctx context.Context, metadata *C.Metadata) (net time.AfterFunc(C.DefaultTCPTimeout, func() { openStreams := t.openStreams.Add(-1) if openStreams == 0 && t.closed.Load() { - t.forceClose(quicConn, common.ClientClosed) + t.forceClose(quicConn, types.ClientClosed) } }) }, @@ -340,7 +340,7 @@ func (t *clientImpl) ListenPacket(ctx context.Context, metadata *C.Metadata) (ne openStreams := t.openStreams.Add(1) if openStreams >= t.MaxOpenStreams { t.openStreams.Add(-1) - return nil, common.TooManyOpenStreams + return nil, types.TooManyOpenStreams } pipe1, pipe2 := N.Pipe() @@ -364,7 +364,7 @@ func (t *clientImpl) ListenPacket(ctx context.Context, metadata *C.Metadata) (ne time.AfterFunc(C.DefaultUDPTimeout, func() { openStreams := t.openStreams.Add(-1) if openStreams == 0 && t.closed.Load() { - t.forceClose(quicConn, common.ClientClosed) + t.forceClose(quicConn, types.ClientClosed) } }) }, @@ -393,10 +393,10 @@ func (t *Client) ListenPacket(ctx context.Context, metadata *C.Metadata) (net.Pa } func (t *Client) forceClose() { - t.clientImpl.forceClose(nil, common.ClientClosed) + t.clientImpl.forceClose(nil, types.ClientClosed) } -func NewClient(clientOption *ClientOption, udp bool, dialFn common.DialFunc) *Client { +func NewClient(clientOption *ClientOption, udp bool, dialFn types.DialFunc) *Client { ci := &clientImpl{ ClientOption: clientOption, dialFn: dialFn, diff --git a/transport/tuic/v4/packet.go b/transport/tuic/v4/packet.go index f463e78c..ae96df01 100644 --- a/transport/tuic/v4/packet.go +++ b/transport/tuic/v4/packet.go @@ -8,7 +8,7 @@ import ( "github.com/metacubex/mihomo/common/atomic" N "github.com/metacubex/mihomo/common/net" "github.com/metacubex/mihomo/common/pool" - "github.com/metacubex/mihomo/transport/tuic/common" + "github.com/metacubex/mihomo/transport/tuic/types" "github.com/metacubex/quic-go" ) @@ -18,7 +18,7 @@ type quicStreamPacketConn struct { quicConn *quic.Conn inputConn *N.BufferedConn - udpRelayMode common.UdpRelayMode + udpRelayMode types.UdpRelayMode maxUdpRelayPacketSize int deferQuicConnFn func(quicConn *quic.Conn, err error) @@ -122,7 +122,7 @@ func (q *quicStreamPacketConn) WaitReadFrom() (data []byte, put func(), addr net } func (q *quicStreamPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) { - if q.udpRelayMode != common.QUIC && len(p) > q.maxUdpRelayPacketSize { + if q.udpRelayMode != types.QUIC && len(p) > q.maxUdpRelayPacketSize { return 0, &quic.DatagramTooLargeError{MaxDatagramPayloadSize: int64(q.maxUdpRelayPacketSize)} } if q.closed { @@ -148,7 +148,7 @@ func (q *quicStreamPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err erro return } switch q.udpRelayMode { - case common.QUIC: + case types.QUIC: var stream *quic.SendStream stream, err = q.quicConn.OpenUniStream() if err != nil { diff --git a/transport/tuic/v4/server.go b/transport/tuic/v4/server.go index 9d0e0378..ba78f289 100644 --- a/transport/tuic/v4/server.go +++ b/transport/tuic/v4/server.go @@ -14,7 +14,7 @@ import ( "github.com/metacubex/mihomo/common/xsync" C "github.com/metacubex/mihomo/constant" "github.com/metacubex/mihomo/transport/socks5" - "github.com/metacubex/mihomo/transport/tuic/common" + "github.com/metacubex/mihomo/transport/tuic/types" "github.com/gofrs/uuid/v5" "github.com/metacubex/quic-go" @@ -28,7 +28,7 @@ type ServerOption struct { MaxUdpRelayPacketSize int } -func NewServerHandler(option *ServerOption, quicConn *quic.Conn, uuid uuid.UUID) common.ServerHandler { +func NewServerHandler(option *ServerOption, quicConn *quic.Conn, uuid uuid.UUID) types.ServerHandler { return &serverHandler{ ServerOption: option, quicConn: quicConn, @@ -67,10 +67,10 @@ func (s *serverHandler) HandleMessage(message []byte) (err error) { if err != nil { return } - return s.parsePacket(&packet, common.NATIVE) + return s.parsePacket(&packet, types.NATIVE) } -func (s *serverHandler) parsePacket(packet *Packet, udpRelayMode common.UdpRelayMode) (err error) { +func (s *serverHandler) parsePacket(packet *Packet, udpRelayMode types.UdpRelayMode) (err error) { <-s.authCh if !s.authOk.Load() { return @@ -166,7 +166,7 @@ func (s *serverHandler) HandleUniStream(reader *bufio.Reader) (err error) { if err != nil { return } - return s.parsePacket(&packet, common.QUIC) + return s.parsePacket(&packet, types.QUIC) case DissociateType: var disassociate Dissociate disassociate, err = ReadDissociateWithHead(commandHead, reader) diff --git a/transport/tuic/v5/client.go b/transport/tuic/v5/client.go index 6b4cb4f3..8d91d4ec 100644 --- a/transport/tuic/v5/client.go +++ b/transport/tuic/v5/client.go @@ -17,7 +17,7 @@ import ( "github.com/metacubex/mihomo/common/xsync" C "github.com/metacubex/mihomo/constant" "github.com/metacubex/mihomo/log" - "github.com/metacubex/mihomo/transport/tuic/common" + "github.com/metacubex/mihomo/transport/tuic/types" "github.com/metacubex/quic-go" "github.com/metacubex/randv2" @@ -26,14 +26,14 @@ import ( type ClientOption struct { Uuid [16]byte Password string - UdpRelayMode common.UdpRelayMode + UdpRelayMode types.UdpRelayMode MaxUdpRelayPacketSize int MaxOpenStreams int64 } type clientImpl struct { *ClientOption - dialFn common.DialFunc + dialFn types.DialFunc udp bool quicConn *quic.Conn @@ -75,7 +75,7 @@ func (t *clientImpl) getQuicConn(ctx context.Context) (*quic.Conn, error) { _ = t.sendAuthentication(quicConn) }() - if t.udp && t.UdpRelayMode == common.QUIC { + if t.udp && t.UdpRelayMode == types.QUIC { go func() { _ = t.handleUniStream(quicConn) }() @@ -153,7 +153,7 @@ func (t *clientImpl) handleUniStream(quicConn *quic.Conn) (err error) { if err != nil { return } - if t.udp && t.UdpRelayMode == common.QUIC { + if t.udp && t.UdpRelayMode == types.QUIC { assocId = packet.ASSOC_ID if val, ok := t.udpInputMap.Load(assocId); ok { if conn, ok := val.(net.Conn); ok { @@ -203,7 +203,7 @@ func (t *clientImpl) handleMessage(quicConn *quic.Conn) (err error) { if err != nil { return } - if t.udp && t.UdpRelayMode == common.NATIVE { + if t.udp && t.UdpRelayMode == types.NATIVE { assocId = packet.ASSOC_ID if val, ok := t.udpInputMap.Load(assocId); ok { if conn, ok := val.(net.Conn); ok { @@ -261,7 +261,7 @@ func (t *clientImpl) forceClose(quicConn *quic.Conn, err error) { func (t *clientImpl) Close() { t.closed.Store(true) if t.openStreams.Load() == 0 { - t.forceClose(nil, common.ClientClosed) + t.forceClose(nil, types.ClientClosed) } } @@ -273,7 +273,7 @@ func (t *clientImpl) DialContext(ctx context.Context, metadata *C.Metadata) (net openStreams := t.openStreams.Add(1) if openStreams >= t.MaxOpenStreams { t.openStreams.Add(-1) - return nil, common.TooManyOpenStreams + return nil, types.TooManyOpenStreams } stream, err := func() (stream net.Conn, err error) { defer func() { @@ -289,7 +289,7 @@ func (t *clientImpl) DialContext(ctx context.Context, metadata *C.Metadata) (net if err != nil { return nil, err } - stream = common.NewQuicStreamConn( + stream = types.NewQuicStreamConn( quicStream, quicConn.LocalAddr(), quicConn.RemoteAddr(), @@ -297,7 +297,7 @@ func (t *clientImpl) DialContext(ctx context.Context, metadata *C.Metadata) (net time.AfterFunc(C.DefaultTCPTimeout, func() { openStreams := t.openStreams.Add(-1) if openStreams == 0 && t.closed.Load() { - t.forceClose(quicConn, common.ClientClosed) + t.forceClose(quicConn, types.ClientClosed) } }) }, @@ -324,7 +324,7 @@ func (t *clientImpl) ListenPacket(ctx context.Context, metadata *C.Metadata) (ne openStreams := t.openStreams.Add(1) if openStreams >= t.MaxOpenStreams { t.openStreams.Add(-1) - return nil, common.TooManyOpenStreams + return nil, types.TooManyOpenStreams } pipe1, pipe2 := N.Pipe() @@ -348,7 +348,7 @@ func (t *clientImpl) ListenPacket(ctx context.Context, metadata *C.Metadata) (ne time.AfterFunc(C.DefaultUDPTimeout, func() { openStreams := t.openStreams.Add(-1) if openStreams == 0 && t.closed.Load() { - t.forceClose(quicConn, common.ClientClosed) + t.forceClose(quicConn, types.ClientClosed) } }) }, @@ -377,10 +377,10 @@ func (t *Client) ListenPacket(ctx context.Context, metadata *C.Metadata) (net.Pa } func (t *Client) forceClose() { - t.clientImpl.forceClose(nil, common.ClientClosed) + t.clientImpl.forceClose(nil, types.ClientClosed) } -func NewClient(clientOption *ClientOption, udp bool, dialFn common.DialFunc) *Client { +func NewClient(clientOption *ClientOption, udp bool, dialFn types.DialFunc) *Client { ci := &clientImpl{ ClientOption: clientOption, dialFn: dialFn, diff --git a/transport/tuic/v5/packet.go b/transport/tuic/v5/packet.go index 5fc706f9..9612244f 100644 --- a/transport/tuic/v5/packet.go +++ b/transport/tuic/v5/packet.go @@ -9,7 +9,7 @@ import ( "github.com/metacubex/mihomo/common/atomic" N "github.com/metacubex/mihomo/common/net" "github.com/metacubex/mihomo/common/pool" - "github.com/metacubex/mihomo/transport/tuic/common" + "github.com/metacubex/mihomo/transport/tuic/types" "github.com/metacubex/quic-go" "github.com/metacubex/randv2" @@ -20,7 +20,7 @@ type quicStreamPacketConn struct { quicConn *quic.Conn inputConn *N.BufferedConn - udpRelayMode common.UdpRelayMode + udpRelayMode types.UdpRelayMode maxUdpRelayPacketSize int deferQuicConnFn func(quicConn *quic.Conn, err error) @@ -160,7 +160,7 @@ func (q *quicStreamPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err erro pktId := uint16(randv2.Uint32()) packet := NewPacket(q.connId, pktId, 1, 0, uint16(len(p)), address, p) switch q.udpRelayMode { - case common.QUIC: + case types.QUIC: err = packet.WriteTo(buf) if err != nil { return diff --git a/transport/tuic/v5/server.go b/transport/tuic/v5/server.go index 18ace9f1..541420f5 100644 --- a/transport/tuic/v5/server.go +++ b/transport/tuic/v5/server.go @@ -13,7 +13,7 @@ import ( "github.com/metacubex/mihomo/common/xsync" C "github.com/metacubex/mihomo/constant" "github.com/metacubex/mihomo/transport/socks5" - "github.com/metacubex/mihomo/transport/tuic/common" + "github.com/metacubex/mihomo/transport/tuic/types" "github.com/gofrs/uuid/v5" "github.com/metacubex/quic-go" @@ -27,7 +27,7 @@ type ServerOption struct { MaxUdpRelayPacketSize int } -func NewServerHandler(option *ServerOption, quicConn *quic.Conn, uuid uuid.UUID) common.ServerHandler { +func NewServerHandler(option *ServerOption, quicConn *quic.Conn, uuid uuid.UUID) types.ServerHandler { return &serverHandler{ ServerOption: option, quicConn: quicConn, @@ -74,7 +74,7 @@ func (s *serverHandler) HandleMessage(message []byte) (err error) { if err != nil { return } - return s.parsePacket(&packet, common.NATIVE) + return s.parsePacket(&packet, types.NATIVE) case HeartbeatType: var heartbeat Heartbeat heartbeat, err = ReadHeartbeatWithHead(commandHead, reader) @@ -86,7 +86,7 @@ func (s *serverHandler) HandleMessage(message []byte) (err error) { return } -func (s *serverHandler) parsePacket(packet *Packet, udpRelayMode common.UdpRelayMode) (err error) { +func (s *serverHandler) parsePacket(packet *Packet, udpRelayMode types.UdpRelayMode) (err error) { <-s.authCh if !s.authOk.Load() { return @@ -179,7 +179,7 @@ func (s *serverHandler) HandleUniStream(reader *bufio.Reader) (err error) { if err != nil { return } - return s.parsePacket(&packet, common.QUIC) + return s.parsePacket(&packet, types.QUIC) case DissociateType: var disassociate Dissociate disassociate, err = ReadDissociateWithHead(commandHead, reader)