config.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. Schema string `json:"$schema,omitempty"`
  10. Log *LogOptions `json:"log,omitempty"`
  11. DNS *DNSOptions `json:"dns,omitempty"`
  12. NTP *NTPOptions `json:"ntp,omitempty"`
  13. Inbounds []Inbound `json:"inbounds,omitempty"`
  14. Outbounds []Outbound `json:"outbounds,omitempty"`
  15. Route *RouteOptions `json:"route,omitempty"`
  16. Experimental *ExperimentalOptions `json:"experimental,omitempty"`
  17. }
  18. type Options _Options
  19. func (o *Options) UnmarshalJSON(content []byte) error {
  20. decoder := json.NewDecoder(json.NewCommentFilter(bytes.NewReader(content)))
  21. decoder.DisallowUnknownFields()
  22. err := decoder.Decode((*_Options)(o))
  23. if err == nil {
  24. return nil
  25. }
  26. if syntaxError, isSyntaxError := err.(*json.SyntaxError); isSyntaxError {
  27. prefix := string(content[:syntaxError.Offset])
  28. row := strings.Count(prefix, "\n") + 1
  29. column := len(prefix) - strings.LastIndex(prefix, "\n") - 1
  30. return E.Extend(syntaxError, "row ", row, ", column ", column)
  31. }
  32. return err
  33. }
  34. type LogOptions struct {
  35. Disabled bool `json:"disabled,omitempty"`
  36. Level string `json:"level,omitempty"`
  37. Output string `json:"output,omitempty"`
  38. Timestamp bool `json:"timestamp,omitempty"`
  39. DisableColor bool `json:"-"`
  40. }