sing-box/option/config.go

56 lines
1.7 KiB
Go
Raw Normal View History

2022-07-02 06:07:50 +00:00
package option
import (
"bytes"
2022-07-07 13:47:21 +00:00
"strings"
2022-07-02 17:57:04 +00:00
"github.com/sagernet/sing/common"
2022-07-07 13:47:21 +00:00
E "github.com/sagernet/sing/common/exceptions"
2022-07-06 07:01:09 +00:00
"github.com/goccy/go-json"
)
type _Options struct {
2022-07-19 14:16:49 +00:00
Log *LogOptions `json:"log,omitempty"`
DNS *DNSOptions `json:"dns,omitempty"`
Inbounds []Inbound `json:"inbounds,omitempty"`
Outbounds []Outbound `json:"outbounds,omitempty"`
Route *RouteOptions `json:"route,omitempty"`
Experimental *ExperimentalOptions `json:"experimental,omitempty"`
2022-07-02 06:07:50 +00:00
}
type Options _Options
func (o *Options) UnmarshalJSON(content []byte) error {
decoder := json.NewDecoder(bytes.NewReader(content))
decoder.DisallowUnknownFields()
2022-07-07 13:47:21 +00:00
err := decoder.Decode((*_Options)(o))
if err == nil {
return nil
}
if syntaxError, isSyntaxError := err.(*json.SyntaxError); isSyntaxError {
prefix := string(content[:syntaxError.Offset])
row := strings.Count(prefix, "\n") + 1
column := len(prefix) - strings.LastIndex(prefix, "\n") - 1
return E.Extend(syntaxError, "row ", row, ", column ", column)
}
return err
}
2022-07-02 17:57:04 +00:00
func (o Options) Equals(other Options) bool {
return common.ComparablePtrEquals(o.Log, other.Log) &&
2022-07-07 13:47:21 +00:00
common.PtrEquals(o.DNS, other.DNS) &&
2022-07-02 17:57:04 +00:00
common.SliceEquals(o.Inbounds, other.Inbounds) &&
2022-07-21 13:03:41 +00:00
common.SliceEquals(o.Outbounds, other.Outbounds) &&
2022-07-19 14:16:49 +00:00
common.PtrEquals(o.Route, other.Route) &&
common.ComparablePtrEquals(o.Experimental, other.Experimental)
2022-07-02 17:57:04 +00:00
}
2022-07-19 14:16:49 +00:00
type LogOptions struct {
2022-07-04 08:45:32 +00:00
Disabled bool `json:"disabled,omitempty"`
Level string `json:"level,omitempty"`
Output string `json:"output,omitempty"`
Timestamp bool `json:"timestamp,omitempty"`
DisableColor bool `json:"-"`
2022-07-02 06:07:50 +00:00
}