json.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. }
  18. func (config *Config) Port() uint16 {
  19. return config.PortValue
  20. }
  21. func (config *Config) LogConfig() config.LogConfig {
  22. if config.LogConfigValue == nil {
  23. return nil
  24. }
  25. return config.LogConfigValue
  26. }
  27. func (config *Config) InboundConfig() config.ConnectionConfig {
  28. if config.InboundConfigValue == nil {
  29. return nil
  30. }
  31. return config.InboundConfigValue
  32. }
  33. func (config *Config) OutboundConfig() config.ConnectionConfig {
  34. if config.OutboundConfigValue == nil {
  35. return nil
  36. }
  37. return config.OutboundConfigValue
  38. }
  39. func (this *Config) InboundDetours() []config.InboundDetourConfig {
  40. detours := make([]config.InboundDetourConfig, len(this.InboundDetoursValue))
  41. for idx, detour := range this.InboundDetoursValue {
  42. detours[idx] = detour
  43. }
  44. return detours
  45. }
  46. func LoadConfig(file string) (*Config, error) {
  47. fixedFile := os.ExpandEnv(file)
  48. rawConfig, err := ioutil.ReadFile(fixedFile)
  49. if err != nil {
  50. log.Error("Failed to read server config file (%s): %v", file, err)
  51. return nil, err
  52. }
  53. jsonConfig := &Config{}
  54. err = json.Unmarshal(rawConfig, jsonConfig)
  55. if err != nil {
  56. log.Error("Failed to load server config: %v", err)
  57. return nil, err
  58. }
  59. jsonConfig.InboundConfigValue.Type = proxyconfig.TypeInbound
  60. jsonConfig.OutboundConfigValue.Type = proxyconfig.TypeOutbound
  61. return jsonConfig, err
  62. }