trojan.go 4.7 KB

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