mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-06-21 18:29:13 +03:00
Add format config support
This commit is contained in:
@@ -7,9 +7,12 @@ import (
|
||||
|
||||
type ListenAddress netip.Addr
|
||||
|
||||
func (a *ListenAddress) MarshalJSON() ([]byte, error) {
|
||||
value := netip.Addr(*a).String()
|
||||
return json.Marshal(value)
|
||||
func (a ListenAddress) MarshalJSON() ([]byte, error) {
|
||||
addr := netip.Addr(a)
|
||||
if !addr.IsValid() {
|
||||
return json.Marshal("")
|
||||
}
|
||||
return json.Marshal(addr.String())
|
||||
}
|
||||
|
||||
func (a *ListenAddress) UnmarshalJSON(bytes []byte) error {
|
||||
|
||||
@@ -3,7 +3,7 @@ package option
|
||||
import "github.com/sagernet/sing/common"
|
||||
|
||||
type Options struct {
|
||||
Log *LogOption `json:"log"`
|
||||
Log *LogOption `json:"log,omitempty"`
|
||||
Inbounds []Inbound `json:"inbounds,omitempty"`
|
||||
Outbounds []Outbound `json:"outbounds,omitempty"`
|
||||
Route *RouteOptions `json:"route,omitempty"`
|
||||
|
||||
@@ -20,52 +20,52 @@ type _Inbound struct {
|
||||
|
||||
type Inbound _Inbound
|
||||
|
||||
func (i Inbound) Equals(other Inbound) bool {
|
||||
return i.Type == other.Type &&
|
||||
i.Tag == other.Tag &&
|
||||
common.Equals(i.DirectOptions, other.DirectOptions) &&
|
||||
common.Equals(i.SocksOptions, other.SocksOptions) &&
|
||||
common.Equals(i.HTTPOptions, other.HTTPOptions) &&
|
||||
common.Equals(i.MixedOptions, other.MixedOptions) &&
|
||||
common.Equals(i.ShadowsocksOptions, other.ShadowsocksOptions)
|
||||
func (h Inbound) Equals(other Inbound) bool {
|
||||
return h.Type == other.Type &&
|
||||
h.Tag == other.Tag &&
|
||||
common.Equals(h.DirectOptions, other.DirectOptions) &&
|
||||
common.Equals(h.SocksOptions, other.SocksOptions) &&
|
||||
common.Equals(h.HTTPOptions, other.HTTPOptions) &&
|
||||
common.Equals(h.MixedOptions, other.MixedOptions) &&
|
||||
common.Equals(h.ShadowsocksOptions, other.ShadowsocksOptions)
|
||||
}
|
||||
|
||||
func (i *Inbound) MarshalJSON() ([]byte, error) {
|
||||
func (h Inbound) MarshalJSON() ([]byte, error) {
|
||||
var v any
|
||||
switch i.Type {
|
||||
switch h.Type {
|
||||
case "direct":
|
||||
v = i.DirectOptions
|
||||
v = h.DirectOptions
|
||||
case "socks":
|
||||
v = i.SocksOptions
|
||||
v = h.SocksOptions
|
||||
case "http":
|
||||
v = i.HTTPOptions
|
||||
v = h.HTTPOptions
|
||||
case "mixed":
|
||||
v = i.MixedOptions
|
||||
v = h.MixedOptions
|
||||
case "shadowsocks":
|
||||
v = i.ShadowsocksOptions
|
||||
v = h.ShadowsocksOptions
|
||||
default:
|
||||
return nil, E.New("unknown inbound type: ", i.Type)
|
||||
return nil, E.New("unknown inbound type: ", h.Type)
|
||||
}
|
||||
return MarshallObjects(i, v)
|
||||
return MarshallObjects((_Inbound)(h), v)
|
||||
}
|
||||
|
||||
func (i *Inbound) UnmarshalJSON(bytes []byte) error {
|
||||
err := json.Unmarshal(bytes, (*_Inbound)(i))
|
||||
func (h *Inbound) UnmarshalJSON(bytes []byte) error {
|
||||
err := json.Unmarshal(bytes, (*_Inbound)(h))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var v any
|
||||
switch i.Type {
|
||||
switch h.Type {
|
||||
case "direct":
|
||||
v = &i.DirectOptions
|
||||
v = &h.DirectOptions
|
||||
case "socks":
|
||||
v = &i.SocksOptions
|
||||
v = &h.SocksOptions
|
||||
case "http":
|
||||
v = &i.HTTPOptions
|
||||
v = &h.HTTPOptions
|
||||
case "mixed":
|
||||
v = &i.MixedOptions
|
||||
v = &h.MixedOptions
|
||||
case "shadowsocks":
|
||||
v = &i.ShadowsocksOptions
|
||||
v = &h.ShadowsocksOptions
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,33 +2,33 @@ package option
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/sagernet/sing/common/x/linkedhashmap"
|
||||
)
|
||||
|
||||
func ToMap(v any) (map[string]any, error) {
|
||||
func ToMap(v any) (*linkedhashmap.Map[string, any], error) {
|
||||
bytes, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var content map[string]any
|
||||
var content linkedhashmap.Map[string, any]
|
||||
err = json.Unmarshal(bytes, &content)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return content, nil
|
||||
return &content, nil
|
||||
}
|
||||
|
||||
func MergeObjects(objects ...any) (map[string]any, error) {
|
||||
content := make(map[string]any)
|
||||
func MergeObjects(objects ...any) (*linkedhashmap.Map[string, any], error) {
|
||||
var content linkedhashmap.Map[string, any]
|
||||
for _, object := range objects {
|
||||
objectMap, err := ToMap(object)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for k, v := range objectMap {
|
||||
content[k] = v
|
||||
}
|
||||
content.PutAll(objectMap)
|
||||
}
|
||||
return content, nil
|
||||
return &content, nil
|
||||
}
|
||||
|
||||
func MarshallObjects(objects ...any) ([]byte, error) {
|
||||
|
||||
@@ -4,8 +4,8 @@ import "encoding/json"
|
||||
|
||||
type Listable[T comparable] []T
|
||||
|
||||
func (l *Listable[T]) MarshalJSON() ([]byte, error) {
|
||||
arrayList := []T(*l)
|
||||
func (l Listable[T]) MarshalJSON() ([]byte, error) {
|
||||
arrayList := []T(l)
|
||||
if len(arrayList) == 1 {
|
||||
return json.Marshal(arrayList[0])
|
||||
}
|
||||
|
||||
@@ -16,30 +16,30 @@ type _Outbound struct {
|
||||
|
||||
type Outbound _Outbound
|
||||
|
||||
func (i *Outbound) MarshalJSON() ([]byte, error) {
|
||||
func (h Outbound) MarshalJSON() ([]byte, error) {
|
||||
var v any
|
||||
switch i.Type {
|
||||
switch h.Type {
|
||||
case "direct":
|
||||
v = i.DirectOptions
|
||||
v = h.DirectOptions
|
||||
case "shadowsocks":
|
||||
v = i.ShadowsocksOptions
|
||||
v = h.ShadowsocksOptions
|
||||
default:
|
||||
return nil, E.New("unknown outbound type: ", i.Type)
|
||||
return nil, E.New("unknown outbound type: ", h.Type)
|
||||
}
|
||||
return MarshallObjects(i, v)
|
||||
return MarshallObjects((_Outbound)(h), v)
|
||||
}
|
||||
|
||||
func (i *Outbound) UnmarshalJSON(bytes []byte) error {
|
||||
err := json.Unmarshal(bytes, (*_Outbound)(i))
|
||||
func (h *Outbound) UnmarshalJSON(bytes []byte) error {
|
||||
err := json.Unmarshal(bytes, (*_Outbound)(h))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var v any
|
||||
switch i.Type {
|
||||
switch h.Type {
|
||||
case "direct":
|
||||
v = &i.DirectOptions
|
||||
v = &h.DirectOptions
|
||||
case "shadowsocks":
|
||||
v = &i.ShadowsocksOptions
|
||||
v = &h.ShadowsocksOptions
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -26,8 +26,8 @@ type GeoIPOptions struct {
|
||||
|
||||
type _Rule struct {
|
||||
Type string `json:"type,omitempty"`
|
||||
DefaultOptions *DefaultRule `json:"default_options,omitempty"`
|
||||
LogicalOptions *LogicalRule `json:"logical_options,omitempty"`
|
||||
DefaultOptions *DefaultRule `json:"-"`
|
||||
LogicalOptions *LogicalRule `json:"-"`
|
||||
}
|
||||
|
||||
type Rule _Rule
|
||||
@@ -38,7 +38,7 @@ func (r Rule) Equals(other Rule) bool {
|
||||
common.PtrEquals(r.LogicalOptions, other.LogicalOptions)
|
||||
}
|
||||
|
||||
func (r *Rule) MarshalJSON() ([]byte, error) {
|
||||
func (r Rule) MarshalJSON() ([]byte, error) {
|
||||
var v any
|
||||
switch r.Type {
|
||||
case C.RuleTypeDefault:
|
||||
@@ -48,7 +48,7 @@ func (r *Rule) MarshalJSON() ([]byte, error) {
|
||||
default:
|
||||
return nil, E.New("unknown rule type: " + r.Type)
|
||||
}
|
||||
return MarshallObjects(r, v)
|
||||
return MarshallObjects((_Rule)(r), v)
|
||||
}
|
||||
|
||||
func (r *Rule) UnmarshalJSON(bytes []byte) error {
|
||||
|
||||
Reference in New Issue
Block a user