chore: naming adjustments

This commit is contained in:
Toby 2023-07-23 12:10:10 -07:00
parent a2fbcc6507
commit 1f499f07c7
3 changed files with 34 additions and 38 deletions

View File

@ -146,8 +146,8 @@ func (h *h3sHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !h.config.DisableUDP { if !h.config.DisableUDP {
h.udpOnce.Do(func() { h.udpOnce.Do(func() {
sm := newUDPSessionManager( sm := newUDPSessionManager(
&udpsmIO{h.conn, id, h.config.TrafficLogger, h.config.Outbound}, &udpIOImpl{h.conn, id, h.config.TrafficLogger, h.config.Outbound},
&udpsmEventLogger{h.conn, id, h.config.EventLogger}, &udpEventLoggerImpl{h.conn, id, h.config.EventLogger},
udpSessionIdleTimeout) udpSessionIdleTimeout)
h.udpSM = sm h.udpSM = sm
go sm.Run() go sm.Run()
@ -231,15 +231,15 @@ func (h *h3sHandler) masqHandler(w http.ResponseWriter, r *http.Request) {
} }
} }
// udpsmIO is the IO implementation for udpSessionManager with TrafficLogger support // udpIOImpl is the IO implementation for udpSessionManager with TrafficLogger support
type udpsmIO struct { type udpIOImpl struct {
Conn quic.Connection Conn quic.Connection
AuthID string AuthID string
TrafficLogger TrafficLogger TrafficLogger TrafficLogger
Outbound Outbound Outbound Outbound
} }
func (io *udpsmIO) ReceiveMessage() (*protocol.UDPMessage, error) { func (io *udpIOImpl) ReceiveMessage() (*protocol.UDPMessage, error) {
for { for {
msg, err := io.Conn.ReceiveMessage() msg, err := io.Conn.ReceiveMessage()
if err != nil { if err != nil {
@ -263,7 +263,7 @@ func (io *udpsmIO) ReceiveMessage() (*protocol.UDPMessage, error) {
} }
} }
func (io *udpsmIO) SendMessage(buf []byte, msg *protocol.UDPMessage) error { func (io *udpIOImpl) SendMessage(buf []byte, msg *protocol.UDPMessage) error {
if io.TrafficLogger != nil { if io.TrafficLogger != nil {
ok := io.TrafficLogger.Log(io.AuthID, 0, uint64(len(msg.Data))) ok := io.TrafficLogger.Log(io.AuthID, 0, uint64(len(msg.Data)))
if !ok { if !ok {
@ -280,23 +280,23 @@ func (io *udpsmIO) SendMessage(buf []byte, msg *protocol.UDPMessage) error {
return io.Conn.SendMessage(buf[:msgN]) return io.Conn.SendMessage(buf[:msgN])
} }
func (io *udpsmIO) DialUDP(reqAddr string) (UDPConn, error) { func (io *udpIOImpl) DialUDP(reqAddr string) (UDPConn, error) {
return io.Outbound.DialUDP(reqAddr) return io.Outbound.DialUDP(reqAddr)
} }
type udpsmEventLogger struct { type udpEventLoggerImpl struct {
Conn quic.Connection Conn quic.Connection
AuthID string AuthID string
EventLogger EventLogger EventLogger EventLogger
} }
func (l *udpsmEventLogger) New(sessionID uint32, reqAddr string) { func (l *udpEventLoggerImpl) New(sessionID uint32, reqAddr string) {
if l.EventLogger != nil { if l.EventLogger != nil {
l.EventLogger.UDPRequest(l.Conn.RemoteAddr(), l.AuthID, sessionID, reqAddr) l.EventLogger.UDPRequest(l.Conn.RemoteAddr(), l.AuthID, sessionID, reqAddr)
} }
} }
func (l *udpsmEventLogger) Closed(sessionID uint32, err error) { func (l *udpEventLoggerImpl) Closed(sessionID uint32, err error) {
if l.EventLogger != nil { if l.EventLogger != nil {
l.EventLogger.UDPError(l.Conn.RemoteAddr(), l.AuthID, sessionID, err) l.EventLogger.UDPError(l.Conn.RemoteAddr(), l.AuthID, sessionID, err)
} }

View File

@ -17,13 +17,13 @@ const (
idleCleanupInterval = 1 * time.Second idleCleanupInterval = 1 * time.Second
) )
type udpSessionManagerIO interface { type udpIO interface {
ReceiveMessage() (*protocol.UDPMessage, error) ReceiveMessage() (*protocol.UDPMessage, error)
SendMessage([]byte, *protocol.UDPMessage) error SendMessage([]byte, *protocol.UDPMessage) error
DialUDP(reqAddr string) (UDPConn, error) DialUDP(reqAddr string) (UDPConn, error)
} }
type udpSessionManagerEventLogger interface { type udpEventLogger interface {
New(sessionID uint32, reqAddr string) New(sessionID uint32, reqAddr string)
Closed(sessionID uint32, err error) Closed(sessionID uint32, err error)
} }
@ -54,7 +54,7 @@ func (e *udpSessionEntry) Feed(msg *protocol.UDPMessage) (int, error) {
// and sends using the provided io. // and sends using the provided io.
// Exit and returns error when either the underlying UDP connection returns // Exit and returns error when either the underlying UDP connection returns
// error (e.g. closed), or the provided io returns error when sending. // error (e.g. closed), or the provided io returns error when sending.
func (e *udpSessionEntry) ReceiveLoop(io udpSessionManagerIO) error { func (e *udpSessionEntry) ReceiveLoop(io udpIO) error {
udpBuf := make([]byte, protocol.MaxUDPSize) udpBuf := make([]byte, protocol.MaxUDPSize)
msgBuf := make([]byte, protocol.MaxUDPSize) msgBuf := make([]byte, protocol.MaxUDPSize)
for { for {
@ -82,7 +82,7 @@ func (e *udpSessionEntry) ReceiveLoop(io udpSessionManagerIO) error {
// sendMessageAutoFrag tries to send a UDP message as a whole first, // sendMessageAutoFrag tries to send a UDP message as a whole first,
// but if it fails due to quic.ErrMessageTooLarge, it tries again by // but if it fails due to quic.ErrMessageTooLarge, it tries again by
// fragmenting the message. // fragmenting the message.
func sendMessageAutoFrag(io udpSessionManagerIO, buf []byte, msg *protocol.UDPMessage) error { func sendMessageAutoFrag(io udpIO, buf []byte, msg *protocol.UDPMessage) error {
err := io.SendMessage(buf, msg) err := io.SendMessage(buf, msg)
var errTooLarge quic.ErrMessageTooLarge var errTooLarge quic.ErrMessageTooLarge
if errors.As(err, &errTooLarge) { if errors.As(err, &errTooLarge) {
@ -107,8 +107,8 @@ func sendMessageAutoFrag(io udpSessionManagerIO, buf []byte, msg *protocol.UDPMe
// Similar to standard NAT, a UDP session is destroyed when no UDP message is received // Similar to standard NAT, a UDP session is destroyed when no UDP message is received
// for a certain period of time (specified by idleTimeout). // for a certain period of time (specified by idleTimeout).
type udpSessionManager struct { type udpSessionManager struct {
io udpSessionManagerIO io udpIO
eventLogger udpSessionManagerEventLogger eventLogger udpEventLogger
idleTimeout time.Duration idleTimeout time.Duration
mutex sync.Mutex mutex sync.Mutex
@ -116,11 +116,7 @@ type udpSessionManager struct {
nextID uint32 nextID uint32
} }
func newUDPSessionManager( func newUDPSessionManager(io udpIO, eventLogger udpEventLogger, idleTimeout time.Duration) *udpSessionManager {
io udpSessionManagerIO,
eventLogger udpSessionManagerEventLogger,
idleTimeout time.Duration,
) *udpSessionManager {
return &udpSessionManager{ return &udpSessionManager{
io: io, io: io,
eventLogger: eventLogger, eventLogger: eventLogger,

View File

@ -46,12 +46,12 @@ func (c *echoUDPConn) Close() error {
return nil return nil
} }
type udpsmMockIO struct { type udpMockIO struct {
ReceiveCh <-chan *protocol.UDPMessage ReceiveCh <-chan *protocol.UDPMessage
SendCh chan<- *protocol.UDPMessage SendCh chan<- *protocol.UDPMessage
} }
func (io *udpsmMockIO) ReceiveMessage() (*protocol.UDPMessage, error) { func (io *udpMockIO) ReceiveMessage() (*protocol.UDPMessage, error) {
m := <-io.ReceiveCh m := <-io.ReceiveCh
if m == nil { if m == nil {
return nil, errors.New("closed") return nil, errors.New("closed")
@ -59,7 +59,7 @@ func (io *udpsmMockIO) ReceiveMessage() (*protocol.UDPMessage, error) {
return m, nil return m, nil
} }
func (io *udpsmMockIO) SendMessage(buf []byte, msg *protocol.UDPMessage) error { func (io *udpMockIO) SendMessage(buf []byte, msg *protocol.UDPMessage) error {
nMsg := *msg nMsg := *msg
nMsg.Data = make([]byte, len(msg.Data)) nMsg.Data = make([]byte, len(msg.Data))
copy(nMsg.Data, msg.Data) copy(nMsg.Data, msg.Data)
@ -67,45 +67,45 @@ func (io *udpsmMockIO) SendMessage(buf []byte, msg *protocol.UDPMessage) error {
return nil return nil
} }
func (io *udpsmMockIO) DialUDP(reqAddr string) (UDPConn, error) { func (io *udpMockIO) DialUDP(reqAddr string) (UDPConn, error) {
return &echoUDPConn{ return &echoUDPConn{
PktCh: make(chan echoUDPConnPkt, 10), PktCh: make(chan echoUDPConnPkt, 10),
}, nil }, nil
} }
type udpsmMockEventNew struct { type udpMockEventNew struct {
SessionID uint32 SessionID uint32
ReqAddr string ReqAddr string
} }
type udpsmMockEventClosed struct { type udpMockEventClosed struct {
SessionID uint32 SessionID uint32
Err error Err error
} }
type udpsmMockEventLogger struct { type udpMockEventLogger struct {
NewCh chan<- udpsmMockEventNew NewCh chan<- udpMockEventNew
ClosedCh chan<- udpsmMockEventClosed ClosedCh chan<- udpMockEventClosed
} }
func (l *udpsmMockEventLogger) New(sessionID uint32, reqAddr string) { func (l *udpMockEventLogger) New(sessionID uint32, reqAddr string) {
l.NewCh <- udpsmMockEventNew{sessionID, reqAddr} l.NewCh <- udpMockEventNew{sessionID, reqAddr}
} }
func (l *udpsmMockEventLogger) Closed(sessionID uint32, err error) { func (l *udpMockEventLogger) Closed(sessionID uint32, err error) {
l.ClosedCh <- udpsmMockEventClosed{sessionID, err} l.ClosedCh <- udpMockEventClosed{sessionID, err}
} }
func TestUDPSessionManager(t *testing.T) { func TestUDPSessionManager(t *testing.T) {
msgReceiveCh := make(chan *protocol.UDPMessage, 10) msgReceiveCh := make(chan *protocol.UDPMessage, 10)
msgSendCh := make(chan *protocol.UDPMessage, 10) msgSendCh := make(chan *protocol.UDPMessage, 10)
io := &udpsmMockIO{ io := &udpMockIO{
ReceiveCh: msgReceiveCh, ReceiveCh: msgReceiveCh,
SendCh: msgSendCh, SendCh: msgSendCh,
} }
eventNewCh := make(chan udpsmMockEventNew, 10) eventNewCh := make(chan udpMockEventNew, 10)
eventClosedCh := make(chan udpsmMockEventClosed, 10) eventClosedCh := make(chan udpMockEventClosed, 10)
eventLogger := &udpsmMockEventLogger{ eventLogger := &udpMockEventLogger{
NewCh: eventNewCh, NewCh: eventNewCh,
ClosedCh: eventClosedCh, ClosedCh: eventClosedCh,
} }