trojan.go 4.6 KB

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