trojan.go 4.6 KB

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