json.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. type LogConfig struct {
  30. AccessLogValue string `json:"access"`
  31. }
  32. func (config *LogConfig) AccessLog() string {
  33. return config.AccessLogValue
  34. }
  35. // Config is the config for Point server.
  36. type Config struct {
  37. PortValue uint16 `json:"port"` // Port of this Point server.
  38. LogConfigValue *LogConfig `json:"log"`
  39. InboundConfigValue *ConnectionConfig `json:"inbound"`
  40. OutboundConfigValue *ConnectionConfig `json:"outbound"`
  41. }
  42. func (config *Config) Port() uint16 {
  43. return config.PortValue
  44. }
  45. func (config *Config) LogConfig() config.LogConfig {
  46. return config.LogConfigValue
  47. }
  48. func (config *Config) InboundConfig() config.ConnectionConfig {
  49. return config.InboundConfigValue
  50. }
  51. func (config *Config) OutboundConfig() config.ConnectionConfig {
  52. return config.OutboundConfigValue
  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. config := &Config{}
  62. err = json.Unmarshal(rawConfig, config)
  63. if err != nil {
  64. log.Error("Failed to load server config: %v", err)
  65. return nil, err
  66. }
  67. return config, err
  68. }