json.go 2.5 KB

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