trojan.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package conf
  2. import (
  3. "strconv"
  4. "github.com/golang/protobuf/proto" // nolint: staticcheck
  5. "v2ray.com/core/common/net"
  6. "v2ray.com/core/common/protocol"
  7. "v2ray.com/core/common/serial"
  8. "v2ray.com/core/proxy/trojan"
  9. )
  10. // TrojanServerTarget is configuration of a single trojan server
  11. type TrojanServerTarget struct {
  12. Address *Address `json:"address"`
  13. Port uint16 `json:"port"`
  14. Password string `json:"password"`
  15. Email string `json:"email"`
  16. Level byte `json:"level"`
  17. }
  18. // TrojanClientConfig is configuration of trojan servers
  19. type TrojanClientConfig struct {
  20. Servers []*TrojanServerTarget `json:"servers"`
  21. }
  22. // Build implements Buildable
  23. func (c *TrojanClientConfig) Build() (proto.Message, error) {
  24. config := new(trojan.ClientConfig)
  25. if len(c.Servers) == 0 {
  26. return nil, newError("0 Trojan server configured.")
  27. }
  28. serverSpecs := make([]*protocol.ServerEndpoint, len(c.Servers))
  29. for idx, rec := range c.Servers {
  30. if rec.Address == nil {
  31. return nil, newError("Trojan server address is not set.")
  32. }
  33. if rec.Port == 0 {
  34. return nil, newError("Invalid Trojan port.")
  35. }
  36. if rec.Password == "" {
  37. return nil, newError("Trojan password is not specified.")
  38. }
  39. account := &trojan.Account{
  40. Password: rec.Password,
  41. }
  42. trojan := &protocol.ServerEndpoint{
  43. Address: rec.Address.Build(),
  44. Port: uint32(rec.Port),
  45. User: []*protocol.User{
  46. {
  47. Level: uint32(rec.Level),
  48. Email: rec.Email,
  49. Account: serial.ToTypedMessage(account),
  50. },
  51. },
  52. }
  53. serverSpecs[idx] = trojan
  54. }
  55. config.Server = serverSpecs
  56. return config, nil
  57. }
  58. // TrojanInboundFallback is fallback configuration
  59. type TrojanInboundFallback struct {
  60. Type string `json:"type"`
  61. Dest string `json:"dest"`
  62. }
  63. // TrojanUserConfig is user configuration
  64. type TrojanUserConfig struct {
  65. Password string `json:"password"`
  66. Level byte `json:"level"`
  67. Email string `json:"email"`
  68. }
  69. // TrojanServerConfig is Inbound configuration
  70. type TrojanServerConfig struct {
  71. Clients []*TrojanUserConfig `json:"clients"`
  72. Fallback *TrojanInboundFallback `json:"fallback"`
  73. }
  74. // Build implements Buildable
  75. func (c *TrojanServerConfig) Build() (proto.Message, error) {
  76. config := new(trojan.ServerConfig)
  77. if len(c.Clients) == 0 {
  78. return nil, newError("No trojan user settings.")
  79. }
  80. config.Users = make([]*protocol.User, len(c.Clients))
  81. for idx, rawUser := range c.Clients {
  82. user := new(protocol.User)
  83. account := &trojan.Account{
  84. Password: rawUser.Password,
  85. }
  86. user.Email = rawUser.Email
  87. user.Level = uint32(rawUser.Level)
  88. user.Account = serial.ToTypedMessage(account)
  89. config.Users[idx] = user
  90. }
  91. if c.Fallback != nil {
  92. fb := &trojan.Fallback{
  93. Dest: c.Fallback.Dest,
  94. }
  95. if fb.Type == "" && fb.Dest != "" {
  96. switch fb.Dest[0] {
  97. case '@', '/':
  98. fb.Type = "unix"
  99. default:
  100. if _, err := strconv.Atoi(fb.Dest); err == nil {
  101. fb.Dest = "127.0.0.1:" + fb.Dest
  102. }
  103. if _, _, err := net.SplitHostPort(fb.Dest); err == nil {
  104. fb.Type = "tcp"
  105. }
  106. }
  107. }
  108. if fb.Type == "" {
  109. return nil, newError("please fill in a valid value for trojan fallback type")
  110. }
  111. config.Fallback = fb
  112. }
  113. return config, nil
  114. }