json.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package json
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "path/filepath"
  6. "github.com/v2ray/v2ray-core"
  7. "github.com/v2ray/v2ray-core/log"
  8. )
  9. type ConnectionConfig struct {
  10. ProtocolString string `json:"protocol"`
  11. File string `json:"file"`
  12. }
  13. func (config *ConnectionConfig) Protocol() string {
  14. return config.ProtocolString
  15. }
  16. func (config *ConnectionConfig) Content() []byte {
  17. if len(config.File) == 0 {
  18. return nil
  19. }
  20. content, err := ioutil.ReadFile(config.File)
  21. if err != nil {
  22. panic(log.Error("Failed to read config file (%s): %v", config.File, err))
  23. }
  24. return content
  25. }
  26. // Config is the config for Point server.
  27. type Config struct {
  28. PortValue uint16 `json:"port"` // Port of this Point server.
  29. InboundConfigValue *ConnectionConfig `json:"inbound"`
  30. OutboundConfigValue *ConnectionConfig `json:"outbound"`
  31. }
  32. func (config *Config) Port() uint16 {
  33. return config.PortValue
  34. }
  35. func (config *Config) InboundConfig() core.ConnectionConfig {
  36. return config.InboundConfigValue
  37. }
  38. func (config *Config) OutboundConfig() core.ConnectionConfig {
  39. return config.OutboundConfigValue
  40. }
  41. func LoadConfig(file string) (*Config, error) {
  42. rawConfig, err := ioutil.ReadFile(file)
  43. if err != nil {
  44. log.Error("Failed to read point config file (%s): %v", file, err)
  45. return nil, err
  46. }
  47. config := &Config{}
  48. err = json.Unmarshal(rawConfig, config)
  49. if !filepath.IsAbs(config.InboundConfigValue.File) && len(config.InboundConfigValue.File) > 0 {
  50. config.InboundConfigValue.File = filepath.Join(filepath.Dir(file), config.InboundConfigValue.File)
  51. }
  52. if !filepath.IsAbs(config.OutboundConfigValue.File) && len(config.OutboundConfigValue.File) > 0 {
  53. config.OutboundConfigValue.File = filepath.Join(filepath.Dir(file), config.OutboundConfigValue.File)
  54. }
  55. return config, err
  56. }