config.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package option
  2. import (
  3. "bytes"
  4. "strings"
  5. "github.com/sagernet/sing-box/common/json"
  6. E "github.com/sagernet/sing/common/exceptions"
  7. )
  8. type _Options struct {
  9. Log *LogOptions `json:"log,omitempty"`
  10. DNS *DNSOptions `json:"dns,omitempty"`
  11. Inbounds []Inbound `json:"inbounds,omitempty"`
  12. Outbounds []Outbound `json:"outbounds,omitempty"`
  13. Route *RouteOptions `json:"route,omitempty"`
  14. Experimental *ExperimentalOptions `json:"experimental,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. type LogOptions struct {
  33. Disabled bool `json:"disabled,omitempty"`
  34. Level string `json:"level,omitempty"`
  35. Output string `json:"output,omitempty"`
  36. Timestamp bool `json:"timestamp,omitempty"`
  37. DisableColor bool `json:"-"`
  38. }