config.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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(network string) 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. var dest v2net.Destination
  55. if network == "tcp" {
  56. dest = v2net.NewTCPDestination(address)
  57. } else {
  58. dest = v2net.NewUDPDestination(address)
  59. }
  60. return VNextServer{
  61. Destination: dest,
  62. Users: users,
  63. }
  64. }
  65. type VMessOutboundConfig struct {
  66. VNextList []VNextConfig `json:"vnext"`
  67. }
  68. func loadOutboundConfig(rawConfig []byte) (VMessOutboundConfig, error) {
  69. config := VMessOutboundConfig{}
  70. err := json.Unmarshal(rawConfig, &config)
  71. return config, err
  72. }