trojan.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package conf
  2. import (
  3. "encoding/json"
  4. "runtime"
  5. "strconv"
  6. "syscall"
  7. "github.com/golang/protobuf/proto"
  8. "v2ray.com/core/common/net"
  9. "v2ray.com/core/common/protocol"
  10. "v2ray.com/core/common/serial"
  11. "v2ray.com/core/proxy/trojan"
  12. )
  13. // TrojanServerTarget is configuration of a single trojan server
  14. type TrojanServerTarget struct {
  15. Address *Address `json:"address"`
  16. Port uint16 `json:"port"`
  17. Password string `json:"password"`
  18. Email string `json:"email"`
  19. Level byte `json:"level"`
  20. Flow string `json:"flow"`
  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. Flow: rec.Flow,
  46. }
  47. trojan := &protocol.ServerEndpoint{
  48. Address: rec.Address.Build(),
  49. Port: uint32(rec.Port),
  50. User: []*protocol.User{
  51. {
  52. Level: uint32(rec.Level),
  53. Email: rec.Email,
  54. Account: serial.ToTypedMessage(account),
  55. },
  56. },
  57. }
  58. serverSpecs[idx] = trojan
  59. }
  60. config.Server = serverSpecs
  61. return config, nil
  62. }
  63. // TrojanInboundFallback is fallback configuration
  64. type TrojanInboundFallback struct {
  65. Alpn string `json:"alpn"`
  66. Path string `json:"path"`
  67. Type string `json:"type"`
  68. Dest json.RawMessage `json:"dest"`
  69. Xver uint64 `json:"xver"`
  70. }
  71. // TrojanUserConfig is user configuration
  72. type TrojanUserConfig struct {
  73. Password string `json:"password"`
  74. Level byte `json:"level"`
  75. Email string `json:"email"`
  76. Flow string `json:"flow"`
  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. Flow: rawUser.Flow,
  93. }
  94. user.Email = rawUser.Email
  95. user.Level = uint32(rawUser.Level)
  96. user.Account = serial.ToTypedMessage(account)
  97. config.Users[idx] = user
  98. }
  99. if c.Fallback != nil {
  100. return nil, newError(`Trojan settings: please use "fallbacks":[{}] instead of "fallback":{}`)
  101. }
  102. for _, fb := range c.Fallbacks {
  103. var i uint16
  104. var s string
  105. if err := json.Unmarshal(fb.Dest, &i); err == nil {
  106. s = strconv.Itoa(int(i))
  107. } else {
  108. _ = json.Unmarshal(fb.Dest, &s)
  109. }
  110. config.Fallbacks = append(config.Fallbacks, &trojan.Fallback{
  111. Alpn: fb.Alpn,
  112. Path: fb.Path,
  113. Type: fb.Type,
  114. Dest: s,
  115. Xver: fb.Xver,
  116. })
  117. }
  118. for _, fb := range config.Fallbacks {
  119. /*
  120. if fb.Alpn == "h2" && fb.Path != "" {
  121. return nil, newError(`Trojan fallbacks: "alpn":"h2" doesn't support "path"`)
  122. }
  123. */
  124. if fb.Path != "" && fb.Path[0] != '/' {
  125. return nil, newError(`Trojan fallbacks: "path" must be empty or start with "/"`)
  126. }
  127. if fb.Type == "" && fb.Dest != "" {
  128. if fb.Dest == "serve-ws-none" {
  129. fb.Type = "serve"
  130. } else {
  131. switch fb.Dest[0] {
  132. case '@', '/':
  133. fb.Type = "unix"
  134. if fb.Dest[0] == '@' && len(fb.Dest) > 1 && fb.Dest[1] == '@' && runtime.GOOS == "linux" {
  135. fullAddr := make([]byte, len(syscall.RawSockaddrUnix{}.Path)) // may need padding to work with haproxy
  136. copy(fullAddr, fb.Dest[1:])
  137. fb.Dest = string(fullAddr)
  138. }
  139. default:
  140. if _, err := strconv.Atoi(fb.Dest); err == nil {
  141. fb.Dest = "127.0.0.1:" + fb.Dest
  142. }
  143. if _, _, err := net.SplitHostPort(fb.Dest); err == nil {
  144. fb.Type = "tcp"
  145. }
  146. }
  147. }
  148. }
  149. if fb.Type == "" {
  150. return nil, newError(`Trojan fallbacks: please fill in a valid value for every "dest"`)
  151. }
  152. if fb.Xver > 2 {
  153. return nil, newError(`Trojan fallbacks: invalid PROXY protocol version, "xver" only accepts 0, 1, 2`)
  154. }
  155. }
  156. return config, nil
  157. }