trojan.go 5.4 KB

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