trojan.go 4.6 KB

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