trojan.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "":
  49. default:
  50. return nil, newError(`Trojan servers: "flow" doesn't support "` + account.Flow + `" in this version`)
  51. }
  52. trojan := &protocol.ServerEndpoint{
  53. Address: rec.Address.Build(),
  54. Port: uint32(rec.Port),
  55. User: []*protocol.User{
  56. {
  57. Level: uint32(rec.Level),
  58. Email: rec.Email,
  59. Account: serial.ToTypedMessage(account),
  60. },
  61. },
  62. }
  63. serverSpecs[idx] = trojan
  64. }
  65. config.Server = serverSpecs
  66. return config, nil
  67. }
  68. // TrojanInboundFallback is fallback configuration
  69. type TrojanInboundFallback struct {
  70. Name string `json:"name"`
  71. Alpn string `json:"alpn"`
  72. Path string `json:"path"`
  73. Type string `json:"type"`
  74. Dest json.RawMessage `json:"dest"`
  75. Xver uint64 `json:"xver"`
  76. }
  77. // TrojanUserConfig is user configuration
  78. type TrojanUserConfig struct {
  79. Password string `json:"password"`
  80. Level byte `json:"level"`
  81. Email string `json:"email"`
  82. Flow string `json:"flow"`
  83. }
  84. // TrojanServerConfig is Inbound configuration
  85. type TrojanServerConfig struct {
  86. Clients []*TrojanUserConfig `json:"clients"`
  87. Fallback *TrojanInboundFallback `json:"fallback"`
  88. Fallbacks []*TrojanInboundFallback `json:"fallbacks"`
  89. }
  90. // Build implements Buildable
  91. func (c *TrojanServerConfig) Build() (proto.Message, error) {
  92. config := new(trojan.ServerConfig)
  93. config.Users = make([]*protocol.User, len(c.Clients))
  94. for idx, rawUser := range c.Clients {
  95. user := new(protocol.User)
  96. account := &trojan.Account{
  97. Password: rawUser.Password,
  98. Flow: rawUser.Flow,
  99. }
  100. switch account.Flow {
  101. case "":
  102. default:
  103. return nil, newError(`Trojan clients: "flow" doesn't support "` + account.Flow + `" in this version`)
  104. }
  105. user.Email = rawUser.Email
  106. user.Level = uint32(rawUser.Level)
  107. user.Account = serial.ToTypedMessage(account)
  108. config.Users[idx] = user
  109. }
  110. if c.Fallback != nil {
  111. return nil, newError(`Trojan settings: please use "fallbacks":[{}] instead of "fallback":{}`)
  112. }
  113. for _, fb := range c.Fallbacks {
  114. var i uint16
  115. var s string
  116. if err := json.Unmarshal(fb.Dest, &i); err == nil {
  117. s = strconv.Itoa(int(i))
  118. } else {
  119. _ = json.Unmarshal(fb.Dest, &s)
  120. }
  121. config.Fallbacks = append(config.Fallbacks, &trojan.Fallback{
  122. Name: fb.Name,
  123. Alpn: fb.Alpn,
  124. Path: fb.Path,
  125. Type: fb.Type,
  126. Dest: s,
  127. Xver: fb.Xver,
  128. })
  129. }
  130. for _, fb := range config.Fallbacks {
  131. /*
  132. if fb.Alpn == "h2" && fb.Path != "" {
  133. return nil, newError(`Trojan fallbacks: "alpn":"h2" doesn't support "path"`)
  134. }
  135. */
  136. if fb.Path != "" && fb.Path[0] != '/' {
  137. return nil, newError(`Trojan fallbacks: "path" must be empty or start with "/"`)
  138. }
  139. if fb.Type == "" && fb.Dest != "" {
  140. if fb.Dest == "serve-ws-none" {
  141. fb.Type = "serve"
  142. } else {
  143. switch fb.Dest[0] {
  144. case '@', '/':
  145. fb.Type = "unix"
  146. if fb.Dest[0] == '@' && len(fb.Dest) > 1 && fb.Dest[1] == '@' && (runtime.GOOS == "linux" || runtime.GOOS == "android") {
  147. fullAddr := make([]byte, len(syscall.RawSockaddrUnix{}.Path)) // may need padding to work with haproxy
  148. copy(fullAddr, fb.Dest[1:])
  149. fb.Dest = string(fullAddr)
  150. }
  151. default:
  152. if _, err := strconv.Atoi(fb.Dest); err == nil {
  153. fb.Dest = "127.0.0.1:" + fb.Dest
  154. }
  155. if _, _, err := net.SplitHostPort(fb.Dest); err == nil {
  156. fb.Type = "tcp"
  157. }
  158. }
  159. }
  160. }
  161. if fb.Type == "" {
  162. return nil, newError(`Trojan fallbacks: please fill in a valid value for every "dest"`)
  163. }
  164. if fb.Xver > 2 {
  165. return nil, newError(`Trojan fallbacks: invalid PROXY protocol version, "xver" only accepts 0, 1, 2`)
  166. }
  167. }
  168. return config, nil
  169. }