fix(matcher,plugin): case-insensitive matching, port accumulation, nil guards, doc comments

matcher: fix IPv6 normalization via netip.ParseAddr, port-range overwrite
by switching to []*PortRange map, case-insensitive domain/host matching,
glob.MustCompile→Compile to avoid panic on invalid patterns.

plugin: guard nil *Options in NewGRPCConn and NewHTTPClient; add
HTTPClientTransport helper for idle-connection cleanup.

net/*: add doc comments for all exported symbols (no code changes).

Add 30 tests (matcher_test.go 22, plugin_test.go 8) covering all
matcher types, nil safety, case insensitivity, IPv6 normalization,
port ranges, and plugin option composition.
This commit is contained in:
ginuerzh
2026-05-24 18:18:42 +08:00
parent 51455da96f
commit 0e96f602fa
17 changed files with 719 additions and 31 deletions
+4
View File
@@ -7,6 +7,8 @@ import (
xnet "github.com/go-gost/x/internal/net"
)
// Conn is a net.PacketConn that also supports io.Reader, io.Writer, buffer
// sizing, syscall access, and remote address exposure.
type Conn interface {
net.PacketConn
io.Reader
@@ -18,11 +20,13 @@ type Conn interface {
xnet.RemoteAddr
}
// ReadUDP supports reading UDP datagrams with metadata.
type ReadUDP interface {
ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error)
ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *net.UDPAddr, err error)
}
// WriteUDP supports writing UDP datagrams with metadata.
type WriteUDP interface {
WriteToUDP(b []byte, addr *net.UDPAddr) (int, error)
WriteMsgUDP(b, oob []byte, addr *net.UDPAddr) (n, oobn int, err error)
+4
View File
@@ -11,6 +11,7 @@ import (
"github.com/go-gost/core/logger"
)
// ListenConfig configures a UDP-to-TCP-stream listener.
type ListenConfig struct {
Addr net.Addr
Backlog int
@@ -29,6 +30,9 @@ type listener struct {
config *ListenConfig
}
// NewListener creates a net.Listener from a net.PacketConn by demultiplexing
// UDP datagrams into per-client net.Conn streams. Idle connections are cleaned
// up according to cfg.TTL.
func NewListener(conn net.PacketConn, cfg *ListenConfig) net.Listener {
if cfg == nil {
cfg = &ListenConfig{}
+7
View File
@@ -13,6 +13,7 @@ const (
defaultBufferSize = 4096
)
// Relay copies UDP datagrams between two net.PacketConns bidirectionally.
type Relay struct {
service string
pc1 net.PacketConn
@@ -22,6 +23,7 @@ type Relay struct {
logger logger.Logger
}
// NewRelay creates a Relay that copies datagrams between pc1 and pc2.
func NewRelay(pc1, pc2 net.PacketConn) *Relay {
return &Relay{
pc1: pc1,
@@ -29,26 +31,31 @@ func NewRelay(pc1, pc2 net.PacketConn) *Relay {
}
}
// WithService sets the service name for bypass matching.
func (r *Relay) WithService(service string) *Relay {
r.service = service
return r
}
// WithBypass sets the bypass matcher to skip certain addresses.
func (r *Relay) WithBypass(bp bypass.Bypass) *Relay {
r.bypass = bp
return r
}
// WithLogger sets the logger.
func (r *Relay) WithLogger(logger logger.Logger) *Relay {
r.logger = logger
return r
}
// WithBufferSize sets the buffer size for copy operations.
func (r *Relay) WithBufferSize(n int) *Relay {
r.bufferSize = n
return r
}
// Run starts the relay. It blocks until an error occurs or ctx is cancelled.
func (r *Relay) Run(ctx context.Context) (err error) {
errc := make(chan error, 2)