config.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package option
  2. import (
  3. "bytes"
  4. "strings"
  5. "github.com/sagernet/sing/common"
  6. E "github.com/sagernet/sing/common/exceptions"
  7. "github.com/goccy/go-json"
  8. )
  9. type _Options struct {
  10. Log *LogOptions `json:"log,omitempty"`
  11. DNS *DNSOptions `json:"dns,omitempty"`
  12. Inbounds []Inbound `json:"inbounds,omitempty"`
  13. Outbounds []Outbound `json:"outbounds,omitempty"`
  14. Route *RouteOptions `json:"route,omitempty"`
  15. Experimental *ExperimentalOptions `json:"experimental,omitempty"`
  16. }
  17. type Options _Options
  18. func (o *Options) UnmarshalJSON(content []byte) error {
  19. decoder := json.NewDecoder(bytes.NewReader(content))
  20. decoder.DisallowUnknownFields()
  21. err := decoder.Decode((*_Options)(o))
  22. if err == nil {
  23. return nil
  24. }
  25. if syntaxError, isSyntaxError := err.(*json.SyntaxError); isSyntaxError {
  26. prefix := string(content[:syntaxError.Offset])
  27. row := strings.Count(prefix, "\n") + 1
  28. column := len(prefix) - strings.LastIndex(prefix, "\n") - 1
  29. return E.Extend(syntaxError, "row ", row, ", column ", column)
  30. }
  31. return err
  32. }
  33. func (o Options) Equals(other Options) bool {
  34. return common.ComparablePtrEquals(o.Log, other.Log) &&
  35. common.PtrEquals(o.DNS, other.DNS) &&
  36. common.SliceEquals(o.Inbounds, other.Inbounds) &&
  37. common.SliceEquals(o.Outbounds, other.Outbounds) &&
  38. common.PtrEquals(o.Route, other.Route) &&
  39. common.ComparablePtrEquals(o.Experimental, other.Experimental)
  40. }
  41. type LogOptions struct {
  42. Disabled bool `json:"disabled,omitempty"`
  43. Level string `json:"level,omitempty"`
  44. Output string `json:"output,omitempty"`
  45. Timestamp bool `json:"timestamp,omitempty"`
  46. DisableColor bool `json:"-"`
  47. }