config.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 *LogOption `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. }
  16. type Options _Options
  17. func (o *Options) UnmarshalJSON(content []byte) error {
  18. decoder := json.NewDecoder(bytes.NewReader(content))
  19. decoder.DisallowUnknownFields()
  20. err := decoder.Decode((*_Options)(o))
  21. if err == nil {
  22. return nil
  23. }
  24. if syntaxError, isSyntaxError := err.(*json.SyntaxError); isSyntaxError {
  25. prefix := string(content[:syntaxError.Offset])
  26. row := strings.Count(prefix, "\n") + 1
  27. column := len(prefix) - strings.LastIndex(prefix, "\n") - 1
  28. return E.Extend(syntaxError, "row ", row, ", column ", column)
  29. }
  30. return err
  31. }
  32. func (o Options) Equals(other Options) bool {
  33. return common.ComparablePtrEquals(o.Log, other.Log) &&
  34. common.PtrEquals(o.DNS, other.DNS) &&
  35. common.SliceEquals(o.Inbounds, other.Inbounds) &&
  36. common.ComparableSliceEquals(o.Outbounds, other.Outbounds) &&
  37. common.PtrEquals(o.Route, other.Route)
  38. }
  39. type LogOption struct {
  40. Disabled bool `json:"disabled,omitempty"`
  41. Level string `json:"level,omitempty"`
  42. Output string `json:"output,omitempty"`
  43. Timestamp bool `json:"timestamp,omitempty"`
  44. DisableColor bool `json:"-"`
  45. }