trojan.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package conf
  2. import (
  3. "encoding/json"
  4. "path/filepath"
  5. "runtime"
  6. "strconv"
  7. "strings"
  8. "syscall"
  9. "github.com/xtls/xray-core/common/errors"
  10. "github.com/xtls/xray-core/common/net"
  11. "github.com/xtls/xray-core/common/protocol"
  12. "github.com/xtls/xray-core/common/serial"
  13. "github.com/xtls/xray-core/proxy/trojan"
  14. "google.golang.org/protobuf/proto"
  15. )
  16. // TrojanServerTarget is configuration of a single trojan server
  17. type TrojanServerTarget struct {
  18. Address *Address `json:"address"`
  19. Port uint16 `json:"port"`
  20. Level byte `json:"level"`
  21. Email string `json:"email"`
  22. Password string `json:"password"`
  23. Flow string `json:"flow"`
  24. }
  25. // TrojanClientConfig is configuration of trojan servers
  26. type TrojanClientConfig struct {
  27. Address *Address `json:"address"`
  28. Port uint16 `json:"port"`
  29. Level byte `json:"level"`
  30. Email string `json:"email"`
  31. Password string `json:"password"`
  32. Flow string `json:"flow"`
  33. Servers []*TrojanServerTarget `json:"servers"`
  34. }
  35. // Build implements Buildable
  36. func (c *TrojanClientConfig) Build() (proto.Message, error) {
  37. if c.Address != nil {
  38. c.Servers = []*TrojanServerTarget{
  39. {
  40. Address: c.Address,
  41. Port: c.Port,
  42. Level: c.Level,
  43. Email: c.Email,
  44. Password: c.Password,
  45. Flow: c.Flow,
  46. },
  47. }
  48. }
  49. if len(c.Servers) != 1 {
  50. return nil, errors.New(`Trojan settings: "servers" should have one and only one member. Multiple endpoints in "servers" should use multiple Trojan outbounds and routing balancer instead`)
  51. }
  52. config := &trojan.ClientConfig{}
  53. for _, rec := range c.Servers {
  54. if rec.Address == nil {
  55. return nil, errors.New("Trojan server address is not set.")
  56. }
  57. if rec.Port == 0 {
  58. return nil, errors.New("Invalid Trojan port.")
  59. }
  60. if rec.Password == "" {
  61. return nil, errors.New("Trojan password is not specified.")
  62. }
  63. if rec.Flow != "" {
  64. return nil, errors.PrintRemovedFeatureError(`Flow for Trojan`, ``)
  65. }
  66. config.Server = &protocol.ServerEndpoint{
  67. Address: rec.Address.Build(),
  68. Port: uint32(rec.Port),
  69. User: &protocol.User{
  70. Level: uint32(rec.Level),
  71. Email: rec.Email,
  72. Account: serial.ToTypedMessage(&trojan.Account{
  73. Password: rec.Password,
  74. }),
  75. },
  76. }
  77. break
  78. }
  79. return config, nil
  80. }
  81. // TrojanInboundFallback is fallback configuration
  82. type TrojanInboundFallback struct {
  83. Name string `json:"name"`
  84. Alpn string `json:"alpn"`
  85. Path string `json:"path"`
  86. Type string `json:"type"`
  87. Dest json.RawMessage `json:"dest"`
  88. Xver uint64 `json:"xver"`
  89. }
  90. // TrojanUserConfig is user configuration
  91. type TrojanUserConfig struct {
  92. Password string `json:"password"`
  93. Level byte `json:"level"`
  94. Email string `json:"email"`
  95. Flow string `json:"flow"`
  96. }
  97. // TrojanServerConfig is Inbound configuration
  98. type TrojanServerConfig struct {
  99. Clients []*TrojanUserConfig `json:"clients"`
  100. Fallbacks []*TrojanInboundFallback `json:"fallbacks"`
  101. }
  102. // Build implements Buildable
  103. func (c *TrojanServerConfig) Build() (proto.Message, error) {
  104. config := &trojan.ServerConfig{
  105. Users: make([]*protocol.User, len(c.Clients)),
  106. }
  107. for idx, rawUser := range c.Clients {
  108. if rawUser.Flow != "" {
  109. return nil, errors.PrintRemovedFeatureError(`Flow for Trojan`, ``)
  110. }
  111. config.Users[idx] = &protocol.User{
  112. Level: uint32(rawUser.Level),
  113. Email: rawUser.Email,
  114. Account: serial.ToTypedMessage(&trojan.Account{
  115. Password: rawUser.Password,
  116. }),
  117. }
  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, errors.New(`Trojan fallbacks: "alpn":"h2" doesn't support "path"`)
  140. }
  141. */
  142. if fb.Path != "" && fb.Path[0] != '/' {
  143. return nil, errors.New(`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 if filepath.IsAbs(fb.Dest) || fb.Dest[0] == '@' {
  149. fb.Type = "unix"
  150. if strings.HasPrefix(fb.Dest, "@@") && (runtime.GOOS == "linux" || runtime.GOOS == "android") {
  151. fullAddr := make([]byte, len(syscall.RawSockaddrUnix{}.Path)) // may need padding to work with haproxy
  152. copy(fullAddr, fb.Dest[1:])
  153. fb.Dest = string(fullAddr)
  154. }
  155. } else {
  156. if _, err := strconv.Atoi(fb.Dest); err == nil {
  157. fb.Dest = "localhost:" + fb.Dest
  158. }
  159. if _, _, err := net.SplitHostPort(fb.Dest); err == nil {
  160. fb.Type = "tcp"
  161. }
  162. }
  163. }
  164. if fb.Type == "" {
  165. return nil, errors.New(`Trojan fallbacks: please fill in a valid value for every "dest"`)
  166. }
  167. if fb.Xver > 2 {
  168. return nil, errors.New(`Trojan fallbacks: invalid PROXY protocol version, "xver" only accepts 0, 1, 2`)
  169. }
  170. }
  171. return config, nil
  172. }