mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-09-15 21:00:27 +00:00
Add netns and unshare support
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package option
|
||||
|
||||
import (
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
"github.com/sagernet/sing/common/json"
|
||||
"github.com/sagernet/sing/common/json/badjson"
|
||||
)
|
||||
|
||||
type _NetworkNamespace struct {
|
||||
Type string `json:"type,omitempty"`
|
||||
Tag string `json:"tag"`
|
||||
DefaultOptions DefaultNetworkNamespaceOptions `json:"-"`
|
||||
UnshareOptions UnshareNetworkNamespaceOptions `json:"-"`
|
||||
}
|
||||
|
||||
type NetworkNamespace _NetworkNamespace
|
||||
|
||||
func (o NetworkNamespace) MarshalJSON() ([]byte, error) {
|
||||
var v any
|
||||
switch o.Type {
|
||||
case C.NetNsTypeDefault:
|
||||
o.Type = ""
|
||||
v = o.DefaultOptions
|
||||
case C.NetNsTypeUnshare:
|
||||
v = o.UnshareOptions
|
||||
default:
|
||||
return nil, E.New("unknown network namespace type: ", o.Type)
|
||||
}
|
||||
return badjson.MarshallObjects((_NetworkNamespace)(o), v)
|
||||
}
|
||||
|
||||
func (o *NetworkNamespace) UnmarshalJSON(content []byte) error {
|
||||
err := json.Unmarshal(content, (*_NetworkNamespace)(o))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var v any
|
||||
switch o.Type {
|
||||
case "", C.NetNsTypeDefault:
|
||||
o.Type = C.NetNsTypeDefault
|
||||
v = &o.DefaultOptions
|
||||
case C.NetNsTypeUnshare:
|
||||
v = &o.UnshareOptions
|
||||
default:
|
||||
return E.New("unknown network namespace type: ", o.Type)
|
||||
}
|
||||
return badjson.UnmarshallExcluded(content, (*_NetworkNamespace)(o), v)
|
||||
}
|
||||
|
||||
type DefaultNetworkNamespaceOptions struct {
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
type UnshareNetworkNamespaceOptions struct {
|
||||
PidFile string `json:"pid_file,omitempty"`
|
||||
}
|
||||
@@ -19,6 +19,7 @@ type _Options struct {
|
||||
Certificate *CertificateOptions `json:"certificate,omitempty"`
|
||||
CertificateProviders []CertificateProvider `json:"certificate_providers,omitempty"`
|
||||
HTTPClients []HTTPClient `json:"http_clients,omitempty"`
|
||||
NetworkNamespaces []NetworkNamespace `json:"network_namespaces,omitempty"`
|
||||
Endpoints []Endpoint `json:"endpoints,omitempty"`
|
||||
Inbounds []Inbound `json:"inbounds,omitempty"`
|
||||
Outbounds []Outbound `json:"outbounds,omitempty"`
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
type TunInboundOptions struct {
|
||||
InterfaceName string `json:"interface_name,omitempty"`
|
||||
NetNs string `json:"netns,omitempty"`
|
||||
MTU uint32 `json:"mtu,omitempty"`
|
||||
Address badoption.Listable[netip.Prefix] `json:"address,omitempty"`
|
||||
DNSMode string `json:"dns_mode,omitempty"`
|
||||
|
||||
Reference in New Issue
Block a user