fix: tuic relay tuic

This commit is contained in:
wwqgtxx 2023-03-12 19:03:03 +08:00
parent 7d230139a0
commit 5de043acc6
3 changed files with 32 additions and 42 deletions

View file

@ -200,7 +200,7 @@ func (q *quicStreamPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err err
} }
func (q *quicStreamPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) { func (q *quicStreamPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
if len(p) > q.maxUdpRelayPacketSize { if q.udpRelayMode != "quic" && len(p) > q.maxUdpRelayPacketSize {
return 0, fmt.Errorf("udp packet too large(%d > %d)", len(p), q.maxUdpRelayPacketSize) return 0, fmt.Errorf("udp packet too large(%d > %d)", len(p), q.maxUdpRelayPacketSize)
} }
if q.closed { if q.closed {
@ -215,7 +215,6 @@ func (q *quicStreamPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err erro
q.deferQuicConnFn(q.quicConn, err) q.deferQuicConnFn(q.quicConn, err)
}() }()
} }
addr.String()
buf := pool.GetBuffer() buf := pool.GetBuffer()
defer pool.PutBuffer(buf) defer pool.PutBuffer(buf)
addrPort, err := netip.ParseAddrPort(addr.String()) addrPort, err := netip.ParseAddrPort(addr.String())
@ -239,7 +238,8 @@ func (q *quicStreamPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err erro
return return
} }
default: // native default: // native
err = q.quicConn.SendMessage(buf.Bytes()) data := buf.Bytes()
err = q.quicConn.SendMessage(data)
if err != nil { if err != nil {
return return
} }
@ -250,7 +250,29 @@ func (q *quicStreamPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err erro
} }
func (q *quicStreamPacketConn) LocalAddr() net.Addr { func (q *quicStreamPacketConn) LocalAddr() net.Addr {
return q.quicConn.LocalAddr() addr := q.quicConn.LocalAddr()
if q.inputConn != nil { // client
return &packetAddr{addrStr: q.quicConn.LocalAddr().String(), connId: q.connId, rawAddr: addr}
}
return addr // server
} }
var _ net.PacketConn = &quicStreamPacketConn{} var _ net.PacketConn = &quicStreamPacketConn{}
type packetAddr struct {
addrStr string
connId uint32
rawAddr net.Addr
}
func (a packetAddr) Network() string {
return "tuic"
}
func (a packetAddr) String() string {
return fmt.Sprintf("%s-%d", a.addrStr, a.connId)
}
func (a packetAddr) RawAddr() net.Addr {
return a.rawAddr
}

View file

@ -114,9 +114,6 @@ func NewAuthenticate(TKN [32]byte) Authenticate {
func ReadAuthenticateWithHead(head CommandHead, reader BufferedReader) (c Authenticate, err error) { func ReadAuthenticateWithHead(head CommandHead, reader BufferedReader) (c Authenticate, err error) {
c.CommandHead = head c.CommandHead = head
if err != nil {
return
}
if c.CommandHead.TYPE != AuthenticateType { if c.CommandHead.TYPE != AuthenticateType {
err = fmt.Errorf("error command type: %s", c.CommandHead.TYPE) err = fmt.Errorf("error command type: %s", c.CommandHead.TYPE)
return return
@ -170,9 +167,6 @@ func NewConnect(ADDR Address) Connect {
func ReadConnectWithHead(head CommandHead, reader BufferedReader) (c Connect, err error) { func ReadConnectWithHead(head CommandHead, reader BufferedReader) (c Connect, err error) {
c.CommandHead = head c.CommandHead = head
if err != nil {
return
}
if c.CommandHead.TYPE != ConnectType { if c.CommandHead.TYPE != ConnectType {
err = fmt.Errorf("error command type: %s", c.CommandHead.TYPE) err = fmt.Errorf("error command type: %s", c.CommandHead.TYPE)
return return
@ -228,9 +222,6 @@ func NewPacket(ASSOC_ID uint32, LEN uint16, ADDR Address, DATA []byte) Packet {
func ReadPacketWithHead(head CommandHead, reader BufferedReader) (c Packet, err error) { func ReadPacketWithHead(head CommandHead, reader BufferedReader) (c Packet, err error) {
c.CommandHead = head c.CommandHead = head
if err != nil {
return
}
if c.CommandHead.TYPE != PacketType { if c.CommandHead.TYPE != PacketType {
err = fmt.Errorf("error command type: %s", c.CommandHead.TYPE) err = fmt.Errorf("error command type: %s", c.CommandHead.TYPE)
return return
@ -305,9 +296,6 @@ func NewDissociate(ASSOC_ID uint32) Dissociate {
func ReadDissociateWithHead(head CommandHead, reader BufferedReader) (c Dissociate, err error) { func ReadDissociateWithHead(head CommandHead, reader BufferedReader) (c Dissociate, err error) {
c.CommandHead = head c.CommandHead = head
if err != nil {
return
}
if c.CommandHead.TYPE != DissociateType { if c.CommandHead.TYPE != DissociateType {
err = fmt.Errorf("error command type: %s", c.CommandHead.TYPE) err = fmt.Errorf("error command type: %s", c.CommandHead.TYPE)
return return
@ -476,15 +464,17 @@ func NewAddress(metadata *C.Metadata) Address {
func NewAddressAddrPort(addrPort netip.AddrPort) Address { func NewAddressAddrPort(addrPort netip.AddrPort) Address {
var addrType byte var addrType byte
if addrPort.Addr().Is4() { port := addrPort.Port()
addr := addrPort.Addr().Unmap()
if addr.Is4() {
addrType = AtypIPv4 addrType = AtypIPv4
} else { } else {
addrType = AtypIPv6 addrType = AtypIPv6
} }
return Address{ return Address{
TYPE: addrType, TYPE: addrType,
ADDR: addrPort.Addr().AsSlice(), ADDR: addr.AsSlice(),
PORT: addrPort.Port(), PORT: port,
} }
} }

View file

@ -5,7 +5,6 @@ import (
"bytes" "bytes"
"context" "context"
"crypto/tls" "crypto/tls"
"fmt"
"net" "net"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -154,14 +153,10 @@ func (s *serverHandler) parsePacket(packet Packet, udpRelayMode string) (err err
return s.HandleUdpFn(packet.ADDR.SocksAddr(), &serverUDPPacket{ return s.HandleUdpFn(packet.ADDR.SocksAddr(), &serverUDPPacket{
pc: pc, pc: pc,
packet: &packet, packet: &packet,
rAddr: s.genServerAssocIdAddr(assocId, s.quicConn.RemoteAddr()), rAddr: &packetAddr{addrStr: "tuic-" + s.uuid.String(), connId: assocId, rawAddr: s.quicConn.RemoteAddr()},
}) })
} }
func (s *serverHandler) genServerAssocIdAddr(assocId uint32, addr net.Addr) net.Addr {
return &ServerAssocIdAddr{assocId: fmt.Sprintf("tuic-%s-%d", s.uuid.String(), assocId), addr: addr}
}
func (s *serverHandler) handleStream() (err error) { func (s *serverHandler) handleStream() (err error) {
for { for {
var quicStream quic.Stream var quicStream quic.Stream
@ -276,23 +271,6 @@ func (s *serverHandler) handleUniStream() (err error) {
} }
} }
type ServerAssocIdAddr struct {
assocId string
addr net.Addr
}
func (a ServerAssocIdAddr) Network() string {
return "ServerAssocIdAddr"
}
func (a ServerAssocIdAddr) String() string {
return a.assocId
}
func (a ServerAssocIdAddr) RawAddr() net.Addr {
return a.addr
}
type serverUDPPacket struct { type serverUDPPacket struct {
pc *quicStreamPacketConn pc *quicStreamPacketConn
packet *Packet packet *Packet