add support for linux network namespace
This commit is contained in:
parent
8d554ddcf7
commit
5aede9a2b3
@ -38,6 +38,7 @@ func (*route) Dial(ctx context.Context, network, address string, opts ...DialOpt
|
|||||||
netd := dialer.NetDialer{
|
netd := dialer.NetDialer{
|
||||||
Timeout: options.Timeout,
|
Timeout: options.Timeout,
|
||||||
Interface: options.Interface,
|
Interface: options.Interface,
|
||||||
|
Netns: options.Netns,
|
||||||
Logger: options.Logger,
|
Logger: options.Logger,
|
||||||
}
|
}
|
||||||
if options.SockOpts != nil {
|
if options.SockOpts != nil {
|
||||||
@ -95,6 +96,7 @@ func (r *route) Nodes() []*Node {
|
|||||||
type DialOptions struct {
|
type DialOptions struct {
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
Interface string
|
Interface string
|
||||||
|
Netns string
|
||||||
SockOpts *SockOpts
|
SockOpts *SockOpts
|
||||||
Logger logger.Logger
|
Logger logger.Logger
|
||||||
}
|
}
|
||||||
@ -113,6 +115,12 @@ func InterfaceDialOption(ifName string) DialOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NetnsDialOption(netns string) DialOption {
|
||||||
|
return func(opts *DialOptions) {
|
||||||
|
opts.Netns = netns
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func SockOptsDialOption(so *SockOpts) DialOption {
|
func SockOptsDialOption(so *SockOpts) DialOption {
|
||||||
return func(opts *DialOptions) {
|
return func(opts *DialOptions) {
|
||||||
opts.SockOpts = so
|
opts.SockOpts = so
|
||||||
|
@ -21,6 +21,7 @@ type RouterOptions struct {
|
|||||||
Retries int
|
Retries int
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
IfceName string
|
IfceName string
|
||||||
|
Netns string
|
||||||
SockOpts *SockOpts
|
SockOpts *SockOpts
|
||||||
Chain Chainer
|
Chain Chainer
|
||||||
Resolver resolver.Resolver
|
Resolver resolver.Resolver
|
||||||
@ -37,6 +38,12 @@ func InterfaceRouterOption(ifceName string) RouterOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NetnsRouterOption(netns string) RouterOption {
|
||||||
|
return func(o *RouterOptions) {
|
||||||
|
o.Netns = netns
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func SockOptsRouterOption(so *SockOpts) RouterOption {
|
func SockOptsRouterOption(so *SockOpts) RouterOption {
|
||||||
return func(o *RouterOptions) {
|
return func(o *RouterOptions) {
|
||||||
o.SockOpts = so
|
o.SockOpts = so
|
||||||
@ -181,6 +188,7 @@ func (r *Router) dial(ctx context.Context, network, address string) (conn net.Co
|
|||||||
}
|
}
|
||||||
conn, err = route.Dial(ctx, network, ipAddr,
|
conn, err = route.Dial(ctx, network, ipAddr,
|
||||||
InterfaceDialOption(r.options.IfceName),
|
InterfaceDialOption(r.options.IfceName),
|
||||||
|
NetnsDialOption(r.options.Netns),
|
||||||
SockOptsDialOption(r.options.SockOpts),
|
SockOptsDialOption(r.options.SockOpts),
|
||||||
LoggerDialOption(r.options.Logger),
|
LoggerDialOption(r.options.Logger),
|
||||||
TimeoutDialOption(r.options.Timeout),
|
TimeoutDialOption(r.options.Timeout),
|
||||||
|
@ -13,6 +13,7 @@ import (
|
|||||||
type TransportOptions struct {
|
type TransportOptions struct {
|
||||||
Addr string
|
Addr string
|
||||||
IfceName string
|
IfceName string
|
||||||
|
Netns string
|
||||||
SockOpts *SockOpts
|
SockOpts *SockOpts
|
||||||
Route Route
|
Route Route
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
@ -32,6 +33,12 @@ func InterfaceTransportOption(ifceName string) TransportOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NetnsTransportOption(netns string) TransportOption {
|
||||||
|
return func(o *TransportOptions) {
|
||||||
|
o.Netns = netns
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func SockOptsTransportOption(so *SockOpts) TransportOption {
|
func SockOptsTransportOption(so *SockOpts) TransportOption {
|
||||||
return func(o *TransportOptions) {
|
return func(o *TransportOptions) {
|
||||||
o.SockOpts = so
|
o.SockOpts = so
|
||||||
@ -73,6 +80,7 @@ func NewTransport(d dialer.Dialer, c connector.Connector, opts ...TransportOptio
|
|||||||
func (tr *Transport) Dial(ctx context.Context, addr string) (net.Conn, error) {
|
func (tr *Transport) Dial(ctx context.Context, addr string) (net.Conn, error) {
|
||||||
netd := &net_dialer.NetDialer{
|
netd := &net_dialer.NetDialer{
|
||||||
Interface: tr.options.IfceName,
|
Interface: tr.options.IfceName,
|
||||||
|
Netns: tr.options.Netns,
|
||||||
Timeout: tr.options.Timeout,
|
Timeout: tr.options.Timeout,
|
||||||
}
|
}
|
||||||
if tr.options.SockOpts != nil {
|
if tr.options.SockOpts != nil {
|
||||||
@ -108,6 +116,7 @@ func (tr *Transport) Handshake(ctx context.Context, conn net.Conn) (net.Conn, er
|
|||||||
func (tr *Transport) Connect(ctx context.Context, conn net.Conn, network, address string) (net.Conn, error) {
|
func (tr *Transport) Connect(ctx context.Context, conn net.Conn, network, address string) (net.Conn, error) {
|
||||||
netd := &net_dialer.NetDialer{
|
netd := &net_dialer.NetDialer{
|
||||||
Interface: tr.options.IfceName,
|
Interface: tr.options.IfceName,
|
||||||
|
Netns: tr.options.Netns,
|
||||||
Timeout: tr.options.Timeout,
|
Timeout: tr.options.Timeout,
|
||||||
}
|
}
|
||||||
if tr.options.SockOpts != nil {
|
if tr.options.SockOpts != nil {
|
||||||
|
@ -4,12 +4,14 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
xnet "github.com/go-gost/core/common/net"
|
xnet "github.com/go-gost/core/common/net"
|
||||||
"github.com/go-gost/core/logger"
|
"github.com/go-gost/core/logger"
|
||||||
|
"github.com/vishvananda/netns"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -22,6 +24,7 @@ var (
|
|||||||
|
|
||||||
type NetDialer struct {
|
type NetDialer struct {
|
||||||
Interface string
|
Interface string
|
||||||
|
Netns string
|
||||||
Mark int
|
Mark int
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
DialFunc func(ctx context.Context, network, addr string) (net.Conn, error)
|
DialFunc func(ctx context.Context, network, addr string) (net.Conn, error)
|
||||||
@ -33,6 +36,32 @@ func (d *NetDialer) Dial(ctx context.Context, network, addr string) (conn net.Co
|
|||||||
d = DefaultNetDialer
|
d = DefaultNetDialer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log := d.Logger
|
||||||
|
if log == nil {
|
||||||
|
log = logger.Default()
|
||||||
|
}
|
||||||
|
|
||||||
|
if d.Netns != "" {
|
||||||
|
runtime.LockOSThread()
|
||||||
|
defer runtime.UnlockOSThread()
|
||||||
|
|
||||||
|
originNs, err := netns.Get()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("netns.Get(): %v", err)
|
||||||
|
}
|
||||||
|
defer netns.Set(originNs)
|
||||||
|
|
||||||
|
ns, err := netns.GetFromName(d.Netns)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("netns.GetFromName(%s): %v", d.Netns, err)
|
||||||
|
}
|
||||||
|
defer ns.Close()
|
||||||
|
|
||||||
|
if err := netns.Set(ns); err != nil {
|
||||||
|
return nil, fmt.Errorf("netns.Set(%s): %v", d.Netns, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
timeout := d.Timeout
|
timeout := d.Timeout
|
||||||
if timeout <= 0 {
|
if timeout <= 0 {
|
||||||
timeout = DefaultTimeout
|
timeout = DefaultTimeout
|
||||||
@ -42,11 +71,6 @@ func (d *NetDialer) Dial(ctx context.Context, network, addr string) (conn net.Co
|
|||||||
return d.DialFunc(ctx, network, addr)
|
return d.DialFunc(ctx, network, addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
log := d.Logger
|
|
||||||
if log == nil {
|
|
||||||
log = logger.Default()
|
|
||||||
}
|
|
||||||
|
|
||||||
switch network {
|
switch network {
|
||||||
case "unix":
|
case "unix":
|
||||||
netd := net.Dialer{}
|
netd := net.Dialer{}
|
||||||
@ -150,5 +174,10 @@ func (d *NetDialer) dialOnce(ctx context.Context, network, addr, ifceName string
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
if d.Netns != "" {
|
||||||
|
// https://github.com/golang/go/issues/44922#issuecomment-796645858
|
||||||
|
netd.FallbackDelay = -1
|
||||||
|
}
|
||||||
|
|
||||||
return netd.DialContext(ctx, network, addr)
|
return netd.DialContext(ctx, network, addr)
|
||||||
}
|
}
|
||||||
|
3
go.mod
3
go.mod
@ -6,5 +6,6 @@ toolchain go1.22.2
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/go-gost/x v0.0.0-20240131151842-25dcf536c6f5
|
github.com/go-gost/x v0.0.0-20240131151842-25dcf536c6f5
|
||||||
golang.org/x/sys v0.18.0
|
github.com/vishvananda/netns v0.0.4
|
||||||
|
golang.org/x/sys v0.21.0
|
||||||
)
|
)
|
||||||
|
6
go.sum
6
go.sum
@ -1,4 +1,6 @@
|
|||||||
github.com/go-gost/x v0.0.0-20240131151842-25dcf536c6f5 h1:IiZLdqGMx0lGVbDBy/N9LPu10qSlxm939EBvZ77qJNI=
|
github.com/go-gost/x v0.0.0-20240131151842-25dcf536c6f5 h1:IiZLdqGMx0lGVbDBy/N9LPu10qSlxm939EBvZ77qJNI=
|
||||||
github.com/go-gost/x v0.0.0-20240131151842-25dcf536c6f5/go.mod h1:FDqjiiPbCqJLU/wY+q2IZCBVcYnfTJTw+SJLrspLQms=
|
github.com/go-gost/x v0.0.0-20240131151842-25dcf536c6f5/go.mod h1:FDqjiiPbCqJLU/wY+q2IZCBVcYnfTJTw+SJLrspLQms=
|
||||||
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8=
|
||||||
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
|
||||||
|
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
|
||||||
|
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
Loading…
Reference in New Issue
Block a user