trojan.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package conf
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "github.com/golang/protobuf/proto" // nolint: staticcheck
  6. "v2ray.com/core/common/net"
  7. "v2ray.com/core/common/protocol"
  8. "v2ray.com/core/common/serial"
  9. "v2ray.com/core/proxy/trojan"
  10. )
  11. // TrojanServerTarget is configuration of a single trojan server
  12. type TrojanServerTarget struct {
  13. Address *Address `json:"address"`
  14. Port uint16 `json:"port"`
  15. Password string `json:"password"`
  16. Email string `json:"email"`
  17. Level byte `json:"level"`
  18. }
  19. // TrojanClientConfig is configuration of trojan servers
  20. type TrojanClientConfig struct {
  21. Servers []*TrojanServerTarget `json:"servers"`
  22. }
  23. // Build implements Buildable
  24. func (c *TrojanClientConfig) Build() (proto.Message, error) {
  25. config := new(trojan.ClientConfig)
  26. if len(c.Servers) == 0 {
  27. return nil, newError("0 Trojan server configured.")
  28. }
  29. serverSpecs := make([]*protocol.ServerEndpoint, len(c.Servers))
  30. for idx, rec := range c.Servers {
  31. if rec.Address == nil {
  32. return nil, newError("Trojan server address is not set.")
  33. }
  34. if rec.Port == 0 {
  35. return nil, newError("Invalid Trojan port.")
  36. }
  37. if rec.Password == "" {
  38. return nil, newError("Trojan password is not specified.")
  39. }
  40. account := &trojan.Account{
  41. Password: rec.Password,
  42. }
  43. trojan := &protocol.ServerEndpoint{
  44. Address: rec.Address.Build(),
  45. Port: uint32(rec.Port),
  46. User: []*protocol.User{
  47. {
  48. Level: uint32(rec.Level),
  49. Email: rec.Email,
  50. Account: serial.ToTypedMessage(account),
  51. },
  52. },
  53. }
  54. serverSpecs[idx] = trojan
  55. }
  56. config.Server = serverSpecs
  57. return config, nil
  58. }
  59. // TrojanInboundFallback is fallback configuration
  60. type TrojanInboundFallback struct {
  61. Alpn string `json:"alpn"`
  62. Path string `json:"path"`
  63. Type string `json:"type"`
  64. Dest json.RawMessage `json:"dest"`
  65. Xver uint64 `json:"xver"`
  66. }
  67. // TrojanUserConfig is user configuration
  68. type TrojanUserConfig struct {
  69. Password string `json:"password"`
  70. Level byte `json:"level"`
  71. Email string `json:"email"`
  72. }
  73. // TrojanServerConfig is Inbound configuration
  74. type TrojanServerConfig struct {
  75. Clients []*TrojanUserConfig `json:"clients"`
  76. Fallback json.RawMessage `json:"fallback"`
  77. Fallbacks []*TrojanInboundFallback `json:"fallbacks"`
  78. }
  79. // Build implements Buildable
  80. func (c *TrojanServerConfig) Build() (proto.Message, error) {
  81. config := new(trojan.ServerConfig)
  82. if len(c.Clients) == 0 {
  83. return nil, newError("No trojan user settings.")
  84. }
  85. config.Users = make([]*protocol.User, len(c.Clients))
  86. for idx, rawUser := range c.Clients {
  87. user := new(protocol.User)
  88. account := &trojan.Account{
  89. Password: rawUser.Password,
  90. }
  91. user.Email = rawUser.Email
  92. user.Level = uint32(rawUser.Level)
  93. user.Account = serial.ToTypedMessage(account)
  94. config.Users[idx] = user
  95. }
  96. if c.Fallback != nil {
  97. return nil, newError(`Trojan settings: please use "fallbacks":[{}] instead of "fallback":{}`)
  98. }
  99. for _, fb := range c.Fallbacks {
  100. var i uint16
  101. var s string
  102. if err := json.Unmarshal(fb.Dest, &i); err == nil {
  103. s = strconv.Itoa(int(i))
  104. } else {
  105. _ = json.Unmarshal(fb.Dest, &s)
  106. }
  107. config.Fallbacks = append(config.Fallbacks, &trojan.Fallback{
  108. Alpn: fb.Alpn,
  109. Path: fb.Path,
  110. Type: fb.Type,
  111. Dest: s,
  112. Xver: fb.Xver,
  113. })
  114. }
  115. for _, fb := range config.Fallbacks {
  116. /*
  117. if fb.Alpn == "h2" && fb.Path != "" {
  118. return nil, newError(`Trojan fallbacks: "alpn":"h2" doesn't support "path"`)
  119. }
  120. */
  121. if fb.Path != "" && fb.Path[0] != '/' {
  122. return nil, newError(`Trojan fallbacks: "path" must be empty or start with "/"`)
  123. }
  124. if fb.Type == "" && fb.Dest != "" {
  125. if fb.Dest == "serve-ws-none" {
  126. fb.Type = "serve"
  127. } else {
  128. switch fb.Dest[0] {
  129. case '@', '/':
  130. fb.Type = "unix"
  131. default:
  132. if _, err := strconv.Atoi(fb.Dest); err == nil {
  133. fb.Dest = "127.0.0.1:" + fb.Dest
  134. }
  135. if _, _, err := net.SplitHostPort(fb.Dest); err == nil {
  136. fb.Type = "tcp"
  137. }
  138. }
  139. }
  140. }
  141. if fb.Type == "" {
  142. return nil, newError(`Trojan fallbacks: please fill in a valid value for every "dest"`)
  143. }
  144. if fb.Xver > 2 {
  145. return nil, newError(`Trojan fallbacks: invalid PROXY protocol version, "xver" only accepts 0, 1, 2`)
  146. }
  147. }
  148. return config, nil
  149. }