json.go 2.6 KB

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