mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-06-12 22:38:15 +03:00
Init commit
This commit is contained in:
50
config/address.go
Normal file
50
config/address.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/netip"
|
||||
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
)
|
||||
|
||||
type ListenAddress netip.Addr
|
||||
|
||||
func (a *ListenAddress) MarshalJSON() ([]byte, error) {
|
||||
value := netip.Addr(*a).String()
|
||||
return json.Marshal(value)
|
||||
}
|
||||
|
||||
func (a *ListenAddress) UnmarshalJSON(bytes []byte) error {
|
||||
var value string
|
||||
err := json.Unmarshal(bytes, &value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
addr, err := netip.ParseAddr(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*a = ListenAddress(addr)
|
||||
return nil
|
||||
}
|
||||
|
||||
type ServerAddress M.Socksaddr
|
||||
|
||||
func (a *ServerAddress) MarshalJSON() ([]byte, error) {
|
||||
value := M.Socksaddr(*a).String()
|
||||
return json.Marshal(value)
|
||||
}
|
||||
|
||||
func (a *ServerAddress) UnmarshalJSON(bytes []byte) error {
|
||||
var value string
|
||||
err := json.Unmarshal(bytes, &value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if value == "" {
|
||||
return E.New("empty server address")
|
||||
}
|
||||
*a = ServerAddress(M.ParseSocksaddr(value))
|
||||
return nil
|
||||
}
|
||||
12
config/config.go
Normal file
12
config/config.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package config
|
||||
|
||||
type Config struct {
|
||||
Log *LogConfig `json:"log"`
|
||||
Inbounds []Inbound `json:"inbounds,omitempty"`
|
||||
Outbounds []Outbound `json:"outbounds,omitempty"`
|
||||
Routes []Route `json:"routes,omitempty"`
|
||||
}
|
||||
|
||||
type LogConfig struct {
|
||||
Level string `json:"level,omitempty"`
|
||||
}
|
||||
115
config/inbound.go
Normal file
115
config/inbound.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/sagernet/sing/common/auth"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
)
|
||||
|
||||
var ErrUnknownInboundType = E.New("unknown inbound type")
|
||||
|
||||
type _Inbound struct {
|
||||
Tag string `json:"tag,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
DirectOptions *DirectInboundOptions `json:"directOptions,omitempty"`
|
||||
SocksOptions *SimpleInboundOptions `json:"socksOptions,omitempty"`
|
||||
HTTPOptions *SimpleInboundOptions `json:"httpOptions,omitempty"`
|
||||
MixedOptions *SimpleInboundOptions `json:"mixedOptions,omitempty"`
|
||||
ShadowsocksOptions *ShadowsocksInboundOptions `json:"shadowsocksOptions,omitempty"`
|
||||
}
|
||||
|
||||
type Inbound _Inbound
|
||||
|
||||
func (i *Inbound) MarshalJSON() ([]byte, error) {
|
||||
var options []byte
|
||||
var err error
|
||||
switch i.Type {
|
||||
case "direct":
|
||||
options, err = json.Marshal(i.DirectOptions)
|
||||
case "socks":
|
||||
options, err = json.Marshal(i.SocksOptions)
|
||||
case "http":
|
||||
options, err = json.Marshal(i.HTTPOptions)
|
||||
case "mixed":
|
||||
options, err = json.Marshal(i.MixedOptions)
|
||||
case "shadowsocks":
|
||||
options, err = json.Marshal(i.ShadowsocksOptions)
|
||||
default:
|
||||
return nil, E.Extend(ErrUnknownInboundType, i.Type)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var content map[string]any
|
||||
err = json.Unmarshal(options, &content)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
content["tag"] = i.Tag
|
||||
content["type"] = i.Type
|
||||
return json.Marshal(content)
|
||||
}
|
||||
|
||||
func (i *Inbound) UnmarshalJSON(bytes []byte) error {
|
||||
err := json.Unmarshal(bytes, (*_Inbound)(i))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch i.Type {
|
||||
case "direct":
|
||||
if i.DirectOptions != nil {
|
||||
break
|
||||
}
|
||||
err = json.Unmarshal(bytes, &i.DirectOptions)
|
||||
case "socks":
|
||||
if i.SocksOptions != nil {
|
||||
break
|
||||
}
|
||||
err = json.Unmarshal(bytes, &i.SocksOptions)
|
||||
case "http":
|
||||
if i.HTTPOptions != nil {
|
||||
break
|
||||
}
|
||||
err = json.Unmarshal(bytes, &i.HTTPOptions)
|
||||
case "mixed":
|
||||
if i.MixedOptions != nil {
|
||||
break
|
||||
}
|
||||
err = json.Unmarshal(bytes, &i.MixedOptions)
|
||||
case "shadowsocks":
|
||||
if i.ShadowsocksOptions != nil {
|
||||
break
|
||||
}
|
||||
err = json.Unmarshal(bytes, &i.ShadowsocksOptions)
|
||||
default:
|
||||
return E.Extend(ErrUnknownInboundType, i.Type)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
type ListenOptions struct {
|
||||
Listen ListenAddress `json:"listen"`
|
||||
Port uint16 `json:"listen_port"`
|
||||
TCPFastOpen bool `json:"tcpFastOpen,omitempty"`
|
||||
UDPTimeout int64 `json:"udpTimeout,omitempty"`
|
||||
}
|
||||
|
||||
type SimpleInboundOptions struct {
|
||||
ListenOptions
|
||||
Users []auth.User `json:"users,omitempty"`
|
||||
}
|
||||
|
||||
type DirectInboundOptions struct {
|
||||
ListenOptions
|
||||
Network NetworkList `json:"network,omitempty"`
|
||||
OverrideAddress string `json:"overrideAddress,omitempty"`
|
||||
OverridePort uint16 `json:"overridePort,omitempty"`
|
||||
}
|
||||
|
||||
type ShadowsocksInboundOptions struct {
|
||||
ListenOptions
|
||||
Network NetworkList `json:"network,omitempty"`
|
||||
Method string `json:"method"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
35
config/network.go
Normal file
35
config/network.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
)
|
||||
|
||||
type NetworkList []string
|
||||
|
||||
func (v *NetworkList) UnmarshalJSON(data []byte) error {
|
||||
var networkList string
|
||||
err := json.Unmarshal(data, &networkList)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, networkName := range strings.Split(networkList, ",") {
|
||||
switch networkName {
|
||||
case "tcp", "udp":
|
||||
*v = append(*v, networkName)
|
||||
default:
|
||||
return E.New("unknown network: " + networkName)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *NetworkList) Build() []string {
|
||||
if len(*v) == 0 {
|
||||
return []string{C.NetworkTCP, C.NetworkUDP}
|
||||
}
|
||||
return *v
|
||||
}
|
||||
90
config/outbound.go
Normal file
90
config/outbound.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
)
|
||||
|
||||
var ErrUnknownOutboundType = E.New("unknown outbound type")
|
||||
|
||||
type _Outbound struct {
|
||||
Tag string `json:"tag,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
DirectOptions *DirectOutboundOptions `json:"directOptions,omitempty"`
|
||||
ShadowsocksOptions *ShadowsocksOutboundOptions `json:"shadowsocksOptions,omitempty"`
|
||||
}
|
||||
|
||||
type Outbound _Outbound
|
||||
|
||||
func (i *Outbound) MarshalJSON() ([]byte, error) {
|
||||
var options []byte
|
||||
var err error
|
||||
switch i.Type {
|
||||
case "direct":
|
||||
options, err = json.Marshal(i.DirectOptions)
|
||||
case "shadowsocks":
|
||||
options, err = json.Marshal(i.ShadowsocksOptions)
|
||||
default:
|
||||
return nil, E.Extend(ErrUnknownOutboundType, i.Type)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var content map[string]any
|
||||
err = json.Unmarshal(options, &content)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
content["tag"] = i.Tag
|
||||
content["type"] = i.Type
|
||||
return json.Marshal(content)
|
||||
}
|
||||
|
||||
func (i *Outbound) UnmarshalJSON(bytes []byte) error {
|
||||
if err := json.Unmarshal(bytes, (*_Outbound)(i)); err != nil {
|
||||
return err
|
||||
}
|
||||
switch i.Type {
|
||||
case "direct":
|
||||
if i.DirectOptions != nil {
|
||||
break
|
||||
}
|
||||
if err := json.Unmarshal(bytes, &i.DirectOptions); err != nil {
|
||||
return err
|
||||
}
|
||||
case "shadowsocks":
|
||||
if i.ShadowsocksOptions != nil {
|
||||
break
|
||||
}
|
||||
if err := json.Unmarshal(bytes, &i.ShadowsocksOptions); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return E.Extend(ErrUnknownOutboundType, i.Type)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type DialerOptions struct {
|
||||
Detour string `json:"detour,omitempty"`
|
||||
BindInterface string `json:"bind_interface,omitempty"`
|
||||
RoutingMark int `json:"routing_mark,omitempty"`
|
||||
ReuseAddr bool `json:"reuse_addr,omitempty"`
|
||||
ConnectTimeout int `json:"connect_timeout,omitempty"`
|
||||
TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
|
||||
}
|
||||
|
||||
type DirectOutboundOptions struct {
|
||||
DialerOptions
|
||||
OverrideAddress string `json:"override_address,omitempty"`
|
||||
OverridePort uint16 `json:"override_port,omitempty"`
|
||||
}
|
||||
|
||||
type ShadowsocksOutboundOptions struct {
|
||||
DialerOptions
|
||||
Server string `json:"server"`
|
||||
ServerPort uint16 `json:"server_port"`
|
||||
Method string `json:"method"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
13
config/route.go
Normal file
13
config/route.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package config
|
||||
|
||||
type Route struct {
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
type SimpleRule struct {
|
||||
Inbound []string `json:"inbound,omitempty"`
|
||||
IPVersion []int `json:"ip_version,omitempty"`
|
||||
Network []string `json:"network,omitempty"`
|
||||
Protocol []string `json:"protocol,omitempty"`
|
||||
Outbound string `json:"outbound,omitempty"`
|
||||
}
|
||||
Reference in New Issue
Block a user