refactor to compatible with sing-box 1.11

This commit is contained in:
2025-01-20 15:08:49 +08:00
parent 22ae18120a
commit 0a6fe9da0c
13 changed files with 340 additions and 169 deletions

View File

@ -1,41 +0,0 @@
package model
import (
"bytes"
"encoding/json"
"github.com/sagernet/sing-box/option"
)
type _Options struct {
RawMessage json.RawMessage `json:"-"`
Schema string `json:"$schema,omitempty"`
Log *LogOptions `json:"log,omitempty"`
DNS *option.DNSOptions `json:"dns,omitempty"`
NTP *option.NTPOptions `json:"ntp,omitempty"`
Inbounds []option.Inbound `json:"inbounds,omitempty"`
Outbounds []Outbound `json:"outbounds,omitempty"`
Route *option.RouteOptions `json:"route,omitempty"`
Experimental *option.ExperimentalOptions `json:"experimental,omitempty"`
}
type Options _Options
func (o *Options) UnmarshalJSON(content []byte) error {
decoder := json.NewDecoder(bytes.NewReader(content))
decoder.DisallowUnknownFields()
err := decoder.Decode((*_Options)(o))
if err != nil {
return err
}
o.RawMessage = content
return nil
}
type LogOptions struct {
Disabled bool `json:"disabled,omitempty"`
Level string `json:"level,omitempty"`
Output string `json:"output,omitempty"`
Timestamp bool `json:"timestamp,omitempty"`
DisableColor bool `json:"-"`
}

58
model/options.go Normal file
View File

@ -0,0 +1,58 @@
package model
import (
"bytes"
"context"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common/json"
"github.com/sagernet/sing/common/json/badjson"
)
type _Options struct {
option.Options
Endpoints []Endpoint `json:"endpoints,omitempty"`
Inbounds []Inbound `json:"inbounds,omitempty"`
Outbounds []Outbound `json:"outbounds,omitempty"`
}
type Options _Options
func (o *Options) UnmarshalJSONContext(ctx context.Context, content []byte) error {
decoder := json.NewDecoderContext(ctx, bytes.NewReader(content))
decoder.DisallowUnknownFields()
err := decoder.Decode((*_Options)(o))
if err != nil {
return err
}
o.RawMessage = content
return nil
}
type LogOptions struct {
Disabled bool `json:"disabled,omitempty"`
Level string `json:"level,omitempty"`
Output string `json:"output,omitempty"`
Timestamp bool `json:"timestamp,omitempty"`
DisableColor bool `json:"-"`
}
type StubOptions struct{}
type Endpoint option.Endpoint
func (e *Endpoint) MarshalJSON() ([]byte, error) {
return badjson.MarshallObjects((*option.Endpoint)(e), e.Options)
}
type Inbound option.Inbound
func (i *Inbound) MarshalJSON() ([]byte, error) {
return badjson.MarshallObjects((*option.Inbound)(i), i.Options)
}
type Outbound option.Outbound
func (o *Outbound) MarshalJSON() ([]byte, error) {
return badjson.MarshallObjects((*option.Outbound)(o), o.Options)
}

View File

@ -1,30 +0,0 @@
package model
import (
"github.com/sagernet/sing-box/option"
)
type Outbound struct {
option.Outbound
}
func (h *Outbound) GetOutbounds() []string {
switch v := h.Options.(type) {
case option.URLTestOutboundOptions:
return v.Outbounds
case option.SelectorOutboundOptions:
return v.Outbounds
}
return nil
}
func (h *Outbound) SetOutbounds(outbounds []string) {
switch v := h.Options.(type) {
case option.URLTestOutboundOptions:
v.Outbounds = outbounds
h.Options = v
case option.SelectorOutboundOptions:
v.Outbounds = outbounds
h.Options = v
}
}