config.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. network := v2net.NetTCP
  47. if config.Network == "udp" {
  48. network = v2net.NetUDP
  49. }
  50. return VNextServer{
  51. Destination: v2net.NewDestination(network, v2net.IPAddress(ip, config.Port)),
  52. Users: users,
  53. }
  54. }
  55. type VMessOutboundConfig struct {
  56. VNextList []VNextConfig `json:"vnext"`
  57. }
  58. func loadOutboundConfig(rawConfig []byte) (VMessOutboundConfig, error) {
  59. config := VMessOutboundConfig{}
  60. err := json.Unmarshal(rawConfig, &config)
  61. return config, err
  62. }