trojan.go 4.5 KB

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