json.go 2.2 KB

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