config.go 1.9 KB

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