trojan.go 4.9 KB

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