json.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package json
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "os"
  6. "github.com/v2ray/v2ray-core/common/log"
  7. "github.com/v2ray/v2ray-core/config"
  8. )
  9. // Config is the config for Point server.
  10. type Config struct {
  11. PortValue uint16 `json:"port"` // Port of this Point server.
  12. LogConfigValue *LogConfig `json:"log"`
  13. InboundConfigValue *ConnectionConfig `json:"inbound"`
  14. OutboundConfigValue *ConnectionConfig `json:"outbound"`
  15. }
  16. func (config *Config) Port() uint16 {
  17. return config.PortValue
  18. }
  19. func (config *Config) LogConfig() config.LogConfig {
  20. if config.LogConfigValue == nil {
  21. return nil
  22. }
  23. return config.LogConfigValue
  24. }
  25. func (config *Config) InboundConfig() config.ConnectionConfig {
  26. if config.InboundConfigValue == nil {
  27. return nil
  28. }
  29. return config.InboundConfigValue
  30. }
  31. func (config *Config) OutboundConfig() config.ConnectionConfig {
  32. if config.OutboundConfigValue == nil {
  33. return nil
  34. }
  35. return config.OutboundConfigValue
  36. }
  37. func LoadConfig(file string) (*Config, error) {
  38. fixedFile := os.ExpandEnv(file)
  39. rawConfig, err := ioutil.ReadFile(fixedFile)
  40. if err != nil {
  41. log.Error("Failed to read server config file (%s): %v", file, err)
  42. return nil, err
  43. }
  44. jsonConfig := &Config{}
  45. err = json.Unmarshal(rawConfig, jsonConfig)
  46. if err != nil {
  47. log.Error("Failed to load server config: %v", err)
  48. return nil, err
  49. }
  50. jsonConfig.InboundConfigValue.Type = config.TypeInbound
  51. jsonConfig.OutboundConfigValue.Type = config.TypeOutbound
  52. return jsonConfig, err
  53. }