feat(listener/runix): support remote UDS forwarding (#102)
This commit is contained in:
@@ -42,13 +42,13 @@ func (h *forwardHandler) dialTarget(ctx context.Context, conn net.Conn, ro *xrec
|
||||
}
|
||||
addr := target.Addr
|
||||
if opts := target.Options(); opts != nil {
|
||||
switch opts.Network {
|
||||
case "unix":
|
||||
if opts.Network != "" {
|
||||
network = opts.Network
|
||||
default:
|
||||
if _, _, err := net.SplitHostPort(addr); err != nil {
|
||||
addr += ":0"
|
||||
}
|
||||
}
|
||||
}
|
||||
if network != "unix" {
|
||||
if _, _, err := net.SplitHostPort(addr); err != nil {
|
||||
addr += ":0"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,13 +28,13 @@ func (h *forwardHandler) handleRawForwarding(ctx context.Context, conn net.Conn,
|
||||
}
|
||||
addr := target.Addr
|
||||
if opts := target.Options(); opts != nil {
|
||||
switch opts.Network {
|
||||
case "unix":
|
||||
if opts.Network != "" {
|
||||
network = opts.Network
|
||||
default:
|
||||
if _, _, err := net.SplitHostPort(addr); err != nil {
|
||||
addr += ":0"
|
||||
}
|
||||
}
|
||||
}
|
||||
if network != "unix" {
|
||||
if _, _, err := net.SplitHostPort(addr); err != nil {
|
||||
addr += ":0"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// Package remote implements a reverse forwarding handler for connections
|
||||
// received from remote GOST nodes. It listens for "rtcp" and "rudp" protocols
|
||||
// received from remote GOST nodes. It listens for "rtcp", "rudp", and "runix" protocols
|
||||
// and forwards the accepted connections to the configured hop.
|
||||
//
|
||||
// The handler is registered under the names "rtcp" and "rudp" via
|
||||
// The handler is registered under the names "rtcp", "rudp", and "runix" via
|
||||
// NewHandler in init().
|
||||
//
|
||||
// # Connection processing flow
|
||||
@@ -85,6 +85,7 @@ import (
|
||||
func init() {
|
||||
registry.HandlerRegistry().Register("rtcp", NewHandler)
|
||||
registry.HandlerRegistry().Register("rudp", NewHandler)
|
||||
registry.HandlerRegistry().Register("runix", NewHandler)
|
||||
}
|
||||
|
||||
type forwardHandler struct {
|
||||
@@ -98,7 +99,7 @@ type forwardHandler struct {
|
||||
}
|
||||
|
||||
// NewHandler creates a remote forwarding handler with the given options.
|
||||
// The handler registers for "rtcp" and "rudp" protocols.
|
||||
// The handler registers for "rtcp", "rudp", and "runix" protocols.
|
||||
func NewHandler(opts ...handler.Option) handler.Handler {
|
||||
options := handler.Options{}
|
||||
for _, opt := range opts {
|
||||
|
||||
@@ -22,7 +22,7 @@ func (h *forwardHandler) newRecorderObject(ctx context.Context, conn net.Conn, s
|
||||
Service: h.options.Service,
|
||||
RemoteAddr: conn.RemoteAddr().String(),
|
||||
LocalAddr: conn.LocalAddr().String(),
|
||||
Network: "tcp",
|
||||
Network: conn.LocalAddr().Network(),
|
||||
Time: start,
|
||||
SID: xctx.SidFromContext(ctx).String(),
|
||||
}
|
||||
|
||||
@@ -82,6 +82,19 @@ func TestNewRecorderObject_UDP(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewRecorderObject_Unix(t *testing.T) {
|
||||
h := newInitdHandler()
|
||||
conn := newStringConn(nil)
|
||||
conn.local = &net.UnixAddr{Name: "/tmp/local.sock", Net: "unix"}
|
||||
conn.remote = &net.UnixAddr{Name: "/tmp/remote.sock", Net: "unix"}
|
||||
|
||||
ro := h.newRecorderObject(context.Background(), conn, time.Now())
|
||||
|
||||
if ro.Network != "unix" {
|
||||
t.Errorf("expected network unix, got %s", ro.Network)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// checkRateLimit
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -99,10 +99,15 @@ func (h *relayHandler) handleBind(ctx context.Context, conn net.Conn, network, a
|
||||
return err
|
||||
}
|
||||
|
||||
if network == "tcp" {
|
||||
switch network {
|
||||
case "tcp", "tcp4", "tcp6", "unix":
|
||||
return h.bindTCP(ctx, conn, network, address, ro, log)
|
||||
} else {
|
||||
case "udp", "udp4", "udp6":
|
||||
return h.bindUDP(ctx, conn, network, address, ro, log)
|
||||
default:
|
||||
resp.Status = relay.StatusBadRequest
|
||||
resp.WriteTo(conn)
|
||||
return fmt.Errorf("network %s is unsupported", network)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,7 +154,7 @@ func (h *relayHandler) bindTCP(ctx context.Context, conn net.Conn, network, addr
|
||||
serviceName := fmt.Sprintf("%s-ep-%s", h.options.Service, ln.Addr())
|
||||
log = log.WithFields(map[string]any{
|
||||
"service": serviceName,
|
||||
"listener": "tcp",
|
||||
"listener": network,
|
||||
"handler": "ep-tcp",
|
||||
"bind": fmt.Sprintf("%s/%s", ln.Addr(), ln.Addr().Network()),
|
||||
"src": ln.Addr().String(),
|
||||
@@ -288,4 +293,4 @@ func (h *relayHandler) bindUDP(ctx context.Context, conn net.Conn, network, addr
|
||||
"duration": time.Since(t),
|
||||
}).Debugf("%s >-< %s", conn.RemoteAddr(), pc.LocalAddr())
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,6 +236,7 @@ func (h *relayHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
|
||||
|
||||
var user, pass string
|
||||
var address string
|
||||
var addrFeature *relay.AddrFeature
|
||||
var networkID relay.NetworkID
|
||||
for _, f := range req.Features {
|
||||
switch f.Type() {
|
||||
@@ -245,7 +246,7 @@ func (h *relayHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
|
||||
}
|
||||
case relay.FeatureAddr:
|
||||
if feature, _ := f.(*relay.AddrFeature); feature != nil {
|
||||
address = net.JoinHostPort(feature.Host, strconv.Itoa(int(feature.Port)))
|
||||
addrFeature = feature
|
||||
}
|
||||
case relay.FeatureNetwork:
|
||||
if feature, _ := f.(*relay.NetworkFeature); feature != nil {
|
||||
@@ -275,6 +276,14 @@ func (h *relayHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
|
||||
if (req.Cmd & relay.FUDP) == relay.FUDP {
|
||||
network = "udp"
|
||||
}
|
||||
if addrFeature != nil {
|
||||
switch network {
|
||||
case "unix", "serial":
|
||||
address = addrFeature.Host
|
||||
default:
|
||||
address = net.JoinHostPort(addrFeature.Host, strconv.Itoa(int(addrFeature.Port)))
|
||||
}
|
||||
}
|
||||
ro.Network = network
|
||||
ro.Host = address
|
||||
log = log.WithFields(map[string]any{"network": network})
|
||||
@@ -302,4 +311,4 @@ func (h *relayHandler) Close() error {
|
||||
h.cancel()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user