json.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package json
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "os"
  6. "github.com/v2ray/v2ray-core/app/router"
  7. "github.com/v2ray/v2ray-core/common/log"
  8. v2net "github.com/v2ray/v2ray-core/common/net"
  9. "github.com/v2ray/v2ray-core/shell/point"
  10. )
  11. // Config is the config for Point server.
  12. type Config struct {
  13. PortValue v2net.Port `json:"port"` // Port of this Point server.
  14. LogConfigValue *LogConfig `json:"log"`
  15. RouterConfigValue *router.Config `json:"routing"`
  16. InboundConfigValue *ConnectionConfig `json:"inbound"`
  17. OutboundConfigValue *ConnectionConfig `json:"outbound"`
  18. InboundDetoursValue []*InboundDetourConfig `json:"inboundDetour"`
  19. OutboundDetoursValue []*OutboundDetourConfig `json:"outboundDetour"`
  20. }
  21. func (config *Config) Port() v2net.Port {
  22. return config.PortValue
  23. }
  24. func (config *Config) LogConfig() point.LogConfig {
  25. if config.LogConfigValue == nil {
  26. return nil
  27. }
  28. return config.LogConfigValue
  29. }
  30. func (this *Config) RouterConfig() *router.Config {
  31. if this.RouterConfigValue == nil {
  32. return nil
  33. }
  34. return this.RouterConfigValue
  35. }
  36. func (config *Config) InboundConfig() point.ConnectionConfig {
  37. if config.InboundConfigValue == nil {
  38. return nil
  39. }
  40. return config.InboundConfigValue
  41. }
  42. func (config *Config) OutboundConfig() point.ConnectionConfig {
  43. if config.OutboundConfigValue == nil {
  44. return nil
  45. }
  46. return config.OutboundConfigValue
  47. }
  48. func (this *Config) InboundDetours() []point.InboundDetourConfig {
  49. detours := make([]point.InboundDetourConfig, len(this.InboundDetoursValue))
  50. for idx, detour := range this.InboundDetoursValue {
  51. detours[idx] = detour
  52. }
  53. return detours
  54. }
  55. func (this *Config) OutboundDetours() []point.OutboundDetourConfig {
  56. detours := make([]point.OutboundDetourConfig, len(this.OutboundDetoursValue))
  57. for idx, detour := range this.OutboundDetoursValue {
  58. detours[idx] = detour
  59. }
  60. return detours
  61. }
  62. func LoadConfig(file string) (*Config, error) {
  63. fixedFile := os.ExpandEnv(file)
  64. rawConfig, err := ioutil.ReadFile(fixedFile)
  65. if err != nil {
  66. log.Error("Failed to read server config file (%s): %v", file, err)
  67. return nil, err
  68. }
  69. jsonConfig := &Config{}
  70. err = json.Unmarshal(rawConfig, jsonConfig)
  71. if err != nil {
  72. log.Error("Failed to load server config: %v", err)
  73. return nil, err
  74. }
  75. return jsonConfig, err
  76. }