json.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. type ConnectionConfig struct {
  10. ProtocolString string `json:"protocol"`
  11. SettingsMessage json.RawMessage `json:"settings"`
  12. }
  13. func (config *ConnectionConfig) Protocol() string {
  14. return config.ProtocolString
  15. }
  16. func (config *ConnectionConfig) Settings(configType config.Type) interface{} {
  17. creator, found := configCache[getConfigKey(config.Protocol(), configType)]
  18. if !found {
  19. panic("Unknown protocol " + config.Protocol())
  20. }
  21. configObj := creator()
  22. err := json.Unmarshal(config.SettingsMessage, configObj)
  23. if err != nil {
  24. log.Error("Unable to parse connection config: %v", err)
  25. panic("Failed to parse connection config.")
  26. }
  27. return configObj
  28. }
  29. // Config is the config for Point server.
  30. type Config struct {
  31. PortValue uint16 `json:"port"` // Port of this Point server.
  32. InboundConfigValue *ConnectionConfig `json:"inbound"`
  33. OutboundConfigValue *ConnectionConfig `json:"outbound"`
  34. }
  35. func (config *Config) Port() uint16 {
  36. return config.PortValue
  37. }
  38. func (config *Config) InboundConfig() config.ConnectionConfig {
  39. return config.InboundConfigValue
  40. }
  41. func (config *Config) OutboundConfig() config.ConnectionConfig {
  42. return config.OutboundConfigValue
  43. }
  44. func LoadConfig(file string) (*Config, error) {
  45. fixedFile := os.ExpandEnv(file)
  46. rawConfig, err := ioutil.ReadFile(fixedFile)
  47. if err != nil {
  48. log.Error("Failed to read server config file (%s): %v", file, err)
  49. return nil, err
  50. }
  51. config := &Config{}
  52. err = json.Unmarshal(rawConfig, config)
  53. if err != nil {
  54. log.Error("Failed to load server config: %v", err)
  55. return nil, err
  56. }
  57. return config, err
  58. }