config.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package vmess
  2. import (
  3. "encoding/json"
  4. "net"
  5. "github.com/v2ray/v2ray-core/common/log"
  6. v2net "github.com/v2ray/v2ray-core/common/net"
  7. "github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
  8. )
  9. type VMessUser struct {
  10. Id string `json:"id"`
  11. Email string `json:"email"`
  12. }
  13. func (u *VMessUser) ToUser() (user.User, error) {
  14. id, err := user.NewID(u.Id)
  15. return user.User{
  16. Id: id,
  17. }, err
  18. }
  19. type VMessInboundConfig struct {
  20. AllowedClients []VMessUser `json:"clients"`
  21. }
  22. func loadInboundConfig(rawConfig []byte) (VMessInboundConfig, error) {
  23. config := VMessInboundConfig{}
  24. err := json.Unmarshal(rawConfig, &config)
  25. return config, err
  26. }
  27. type VNextConfig struct {
  28. Address string `json:"address"`
  29. Port uint16 `json:"port"`
  30. Users []VMessUser `json:"users"`
  31. Network string `json:"network"`
  32. }
  33. func (config VNextConfig) ToVNextServer() VNextServer {
  34. users := make([]user.User, 0, len(config.Users))
  35. for _, user := range config.Users {
  36. vuser, err := user.ToUser()
  37. if err != nil {
  38. panic(log.Error("Failed to convert %v to User.", user))
  39. }
  40. users = append(users, vuser)
  41. }
  42. ip := net.ParseIP(config.Address)
  43. if ip == nil {
  44. panic(log.Error("Unable to parse VNext IP: %s", config.Address))
  45. }
  46. address := v2net.IPAddress(ip, config.Port)
  47. dest := v2net.NewTCPDestination(address)
  48. if config.Network == "udp" {
  49. dest = v2net.NewUDPDestination(address)
  50. }
  51. return VNextServer{
  52. Destination: dest,
  53. Users: users,
  54. }
  55. }
  56. type VMessOutboundConfig struct {
  57. VNextList []VNextConfig `json:"vnext"`
  58. }
  59. func loadOutboundConfig(rawConfig []byte) (VMessOutboundConfig, error) {
  60. config := VMessOutboundConfig{}
  61. err := json.Unmarshal(rawConfig, &config)
  62. return config, err
  63. }