chore: renames (HyTCPConn, HyUDPConn)

This commit is contained in:
Toby 2022-10-23 16:19:57 -07:00
parent d9d80ecbb1
commit b247919a03
4 changed files with 23 additions and 22 deletions

View File

@ -233,14 +233,14 @@ func (c *Client) DialTCP(addr string) (net.Conn, error) {
_ = stream.Close() _ = stream.Close()
return nil, fmt.Errorf("connection rejected: %s", sr.Message) return nil, fmt.Errorf("connection rejected: %s", sr.Message)
} }
return &quicConn{ return &hyTCPConn{
Orig: stream, Orig: stream,
PseudoLocalAddr: session.LocalAddr(), PseudoLocalAddr: session.LocalAddr(),
PseudoRemoteAddr: session.RemoteAddr(), PseudoRemoteAddr: session.RemoteAddr(),
}, nil }, nil
} }
func (c *Client) DialUDP() (UDPConn, error) { func (c *Client) DialUDP() (HyUDPConn, error) {
session, stream, err := c.openStreamWithReconnect() session, stream, err := c.openStreamWithReconnect()
if err != nil { if err != nil {
return nil, err return nil, err
@ -275,7 +275,7 @@ func (c *Client) DialUDP() (UDPConn, error) {
sessionMap[sr.UDPSessionID] = nCh sessionMap[sr.UDPSessionID] = nCh
c.udpSessionMutex.Unlock() c.udpSessionMutex.Unlock()
pktConn := &quicPktConn{ pktConn := &hyUDPConn{
Session: session, Session: session,
Stream: stream, Stream: stream,
CloseFunc: func() { CloseFunc: func() {
@ -302,51 +302,52 @@ func (c *Client) Close() error {
return err return err
} }
type quicConn struct { // hyTCPConn wraps a QUIC stream and implements net.Conn returned by Client.DialTCP
type hyTCPConn struct {
Orig quic.Stream Orig quic.Stream
PseudoLocalAddr net.Addr PseudoLocalAddr net.Addr
PseudoRemoteAddr net.Addr PseudoRemoteAddr net.Addr
} }
func (w *quicConn) Read(b []byte) (n int, err error) { func (w *hyTCPConn) Read(b []byte) (n int, err error) {
return w.Orig.Read(b) return w.Orig.Read(b)
} }
func (w *quicConn) Write(b []byte) (n int, err error) { func (w *hyTCPConn) Write(b []byte) (n int, err error) {
return w.Orig.Write(b) return w.Orig.Write(b)
} }
func (w *quicConn) Close() error { func (w *hyTCPConn) Close() error {
return w.Orig.Close() return w.Orig.Close()
} }
func (w *quicConn) LocalAddr() net.Addr { func (w *hyTCPConn) LocalAddr() net.Addr {
return w.PseudoLocalAddr return w.PseudoLocalAddr
} }
func (w *quicConn) RemoteAddr() net.Addr { func (w *hyTCPConn) RemoteAddr() net.Addr {
return w.PseudoRemoteAddr return w.PseudoRemoteAddr
} }
func (w *quicConn) SetDeadline(t time.Time) error { func (w *hyTCPConn) SetDeadline(t time.Time) error {
return w.Orig.SetDeadline(t) return w.Orig.SetDeadline(t)
} }
func (w *quicConn) SetReadDeadline(t time.Time) error { func (w *hyTCPConn) SetReadDeadline(t time.Time) error {
return w.Orig.SetReadDeadline(t) return w.Orig.SetReadDeadline(t)
} }
func (w *quicConn) SetWriteDeadline(t time.Time) error { func (w *hyTCPConn) SetWriteDeadline(t time.Time) error {
return w.Orig.SetWriteDeadline(t) return w.Orig.SetWriteDeadline(t)
} }
type UDPConn interface { type HyUDPConn interface {
ReadFrom() ([]byte, string, error) ReadFrom() ([]byte, string, error)
WriteTo([]byte, string) error WriteTo([]byte, string) error
Close() error Close() error
} }
type quicPktConn struct { type hyUDPConn struct {
Session quic.Connection Session quic.Connection
Stream quic.Stream Stream quic.Stream
CloseFunc func() CloseFunc func()
@ -354,7 +355,7 @@ type quicPktConn struct {
MsgCh <-chan *udpMessage MsgCh <-chan *udpMessage
} }
func (c *quicPktConn) Hold() { func (c *hyUDPConn) Hold() {
// Hold the stream until it's closed // Hold the stream until it's closed
buf := make([]byte, 1024) buf := make([]byte, 1024)
for { for {
@ -366,7 +367,7 @@ func (c *quicPktConn) Hold() {
_ = c.Close() _ = c.Close()
} }
func (c *quicPktConn) ReadFrom() ([]byte, string, error) { func (c *hyUDPConn) ReadFrom() ([]byte, string, error) {
msg := <-c.MsgCh msg := <-c.MsgCh
if msg == nil { if msg == nil {
// Closed // Closed
@ -375,7 +376,7 @@ func (c *quicPktConn) ReadFrom() ([]byte, string, error) {
return msg.Data, net.JoinHostPort(msg.Host, strconv.Itoa(int(msg.Port))), nil return msg.Data, net.JoinHostPort(msg.Host, strconv.Itoa(int(msg.Port))), nil
} }
func (c *quicPktConn) WriteTo(p []byte, addr string) error { func (c *hyUDPConn) WriteTo(p []byte, addr string) error {
host, port, err := utils.SplitHostPort(addr) host, port, err := utils.SplitHostPort(addr)
if err != nil { if err != nil {
return err return err
@ -414,7 +415,7 @@ func (c *quicPktConn) WriteTo(p []byte, addr string) error {
} }
} }
func (c *quicPktConn) Close() error { func (c *hyUDPConn) Close() error {
c.CloseFunc() c.CloseFunc()
return c.Stream.Close() return c.Stream.Close()
} }

View File

@ -46,7 +46,7 @@ func NewUDPRelay(hyClient *core.Client, listen, remote string, timeout time.Dura
} }
type connEntry struct { type connEntry struct {
HyConn core.UDPConn HyConn core.HyUDPConn
Deadline atomic.Value Deadline atomic.Value
} }
@ -56,7 +56,7 @@ func (r *UDPRelay) ListenAndServe() error {
return err return err
} }
defer conn.Close() defer conn.Close()
// src <-> HyClient UDPConn // src <-> HyClient HyUDPConn
connMap := make(map[string]*connEntry) connMap := make(map[string]*connEntry)
var connMapMutex sync.RWMutex var connMapMutex sync.RWMutex
// Read loop // Read loop

View File

@ -320,7 +320,7 @@ func (s *Server) handleUDP(c *net.TCPConn, r *socks5.Request) error {
return nil return nil
} }
func (s *Server) udpServer(clientConn *net.UDPConn, localRelayConn *net.UDPConn, hyUDP core.UDPConn) { func (s *Server) udpServer(clientConn *net.UDPConn, localRelayConn *net.UDPConn, hyUDP core.HyUDPConn) {
var clientAddr *net.UDPAddr var clientAddr *net.UDPAddr
buf := make([]byte, udpBufferSize) buf := make([]byte, udpBufferSize)
// Local to remote // Local to remote

View File

@ -52,7 +52,7 @@ func (s *Server) handleUDPConn(conn adapter.UDPConn) {
err = s.relayUDP(conn, rc, &remoteAddr, s.Timeout) err = s.relayUDP(conn, rc, &remoteAddr, s.Timeout)
} }
func (s *Server) relayUDP(lc adapter.UDPConn, rc core.UDPConn, to *net.UDPAddr, timeout time.Duration) (err error) { func (s *Server) relayUDP(lc adapter.UDPConn, rc core.HyUDPConn, to *net.UDPAddr, timeout time.Duration) (err error) {
errChan := make(chan error, 2) errChan := make(chan error, 2)
// local => remote // local => remote
go func() { go func() {