transport_internet.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package conf
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "strings"
  8. "v2ray.com/core/common/loader"
  9. v2net "v2ray.com/core/common/net"
  10. "v2ray.com/core/transport/internet"
  11. "v2ray.com/core/transport/internet/kcp"
  12. "v2ray.com/core/transport/internet/tcp"
  13. "v2ray.com/core/transport/internet/tls"
  14. "v2ray.com/core/transport/internet/ws"
  15. )
  16. var (
  17. kcpHeaderLoader = NewJSONConfigLoader(ConfigCreatorCache{
  18. "none": func() interface{} { return new(NoOpAuthenticator) },
  19. "srtp": func() interface{} { return new(SRTPAuthenticator) },
  20. "utp": func() interface{} { return new(UTPAuthenticator) },
  21. }, "type", "")
  22. tcpHeaderLoader = NewJSONConfigLoader(ConfigCreatorCache{
  23. "none": func() interface{} { return new(NoOpConnectionAuthenticator) },
  24. "http": func() interface{} { return new(HTTPAuthenticator) },
  25. }, "type", "")
  26. )
  27. type KCPConfig struct {
  28. Mtu *uint32 `json:"mtu"`
  29. Tti *uint32 `json:"tti"`
  30. UpCap *uint32 `json:"uplinkCapacity"`
  31. DownCap *uint32 `json:"downlinkCapacity"`
  32. Congestion *bool `json:"congestion"`
  33. ReadBufferSize *uint32 `json:"readBufferSize"`
  34. WriteBufferSize *uint32 `json:"writeBufferSize"`
  35. HeaderConfig json.RawMessage `json:"header"`
  36. }
  37. func (this *KCPConfig) Build() (*loader.TypedSettings, error) {
  38. config := new(kcp.Config)
  39. if this.Mtu != nil {
  40. mtu := *this.Mtu
  41. if mtu < 576 || mtu > 1460 {
  42. return nil, fmt.Errorf("KCP|Config: Invalid MTU size: %d", mtu)
  43. }
  44. config.Mtu = &kcp.MTU{Value: mtu}
  45. }
  46. if this.Tti != nil {
  47. tti := *this.Tti
  48. if tti < 10 || tti > 100 {
  49. return nil, fmt.Errorf("KCP|Config: Invalid TTI: %d", tti)
  50. }
  51. config.Tti = &kcp.TTI{Value: tti}
  52. }
  53. if this.UpCap != nil {
  54. config.UplinkCapacity = &kcp.UplinkCapacity{Value: *this.UpCap}
  55. }
  56. if this.DownCap != nil {
  57. config.DownlinkCapacity = &kcp.DownlinkCapacity{Value: *this.DownCap}
  58. }
  59. if this.Congestion != nil {
  60. config.Congestion = *this.Congestion
  61. }
  62. if this.ReadBufferSize != nil {
  63. size := *this.ReadBufferSize
  64. if size > 0 {
  65. config.ReadBuffer = &kcp.ReadBuffer{Size: size * 1024 * 1024}
  66. } else {
  67. config.ReadBuffer = &kcp.ReadBuffer{Size: 512 * 1024}
  68. }
  69. }
  70. if this.WriteBufferSize != nil {
  71. size := *this.WriteBufferSize
  72. if size > 0 {
  73. config.WriteBuffer = &kcp.WriteBuffer{Size: size * 1024 * 1024}
  74. } else {
  75. config.WriteBuffer = &kcp.WriteBuffer{Size: 512 * 1024}
  76. }
  77. }
  78. if len(this.HeaderConfig) > 0 {
  79. headerConfig, _, err := kcpHeaderLoader.Load(this.HeaderConfig)
  80. if err != nil {
  81. return nil, errors.New("KCP|Config: Failed to parse header config: " + err.Error())
  82. }
  83. ts, err := headerConfig.(Buildable).Build()
  84. if err != nil {
  85. return nil, errors.New("Failed to get KCP authenticator config: " + err.Error())
  86. }
  87. config.HeaderConfig = ts
  88. }
  89. return loader.NewTypedSettings(config), nil
  90. }
  91. type TCPConfig struct {
  92. ConnectionReuse *bool `json:"connectionReuse"`
  93. HeaderConfig json.RawMessage `json:"header"`
  94. }
  95. func (this *TCPConfig) Build() (*loader.TypedSettings, error) {
  96. config := new(tcp.Config)
  97. if this.ConnectionReuse != nil {
  98. config.ConnectionReuse = &tcp.ConnectionReuse{
  99. Enable: *this.ConnectionReuse,
  100. }
  101. }
  102. if len(this.HeaderConfig) > 0 {
  103. headerConfig, _, err := tcpHeaderLoader.Load(this.HeaderConfig)
  104. if err != nil {
  105. return nil, errors.New("TCP|Config: Failed to parse header config: " + err.Error())
  106. }
  107. ts, err := headerConfig.(Buildable).Build()
  108. if err != nil {
  109. return nil, errors.New("Failed to get TCP authenticator config: " + err.Error())
  110. }
  111. config.HeaderSettings = ts
  112. }
  113. return loader.NewTypedSettings(config), nil
  114. }
  115. type WebSocketConfig struct {
  116. ConnectionReuse *bool `json:"connectionReuse"`
  117. Path string `json:"Path"`
  118. }
  119. func (this *WebSocketConfig) Build() (*loader.TypedSettings, error) {
  120. config := &ws.Config{
  121. Path: this.Path,
  122. }
  123. if this.ConnectionReuse != nil {
  124. config.ConnectionReuse = &ws.ConnectionReuse{
  125. Enable: *this.ConnectionReuse,
  126. }
  127. }
  128. return loader.NewTypedSettings(config), nil
  129. }
  130. type TLSCertConfig struct {
  131. CertFile string `json:"certificateFile"`
  132. KeyFile string `json:"keyFile"`
  133. }
  134. type TLSConfig struct {
  135. Insecure bool `json:"allowInsecure"`
  136. Certs []*TLSCertConfig `json:"certificates"`
  137. }
  138. func (this *TLSConfig) Build() (*loader.TypedSettings, error) {
  139. config := new(tls.Config)
  140. config.Certificate = make([]*tls.Certificate, len(this.Certs))
  141. for idx, certConf := range this.Certs {
  142. cert, err := ioutil.ReadFile(certConf.CertFile)
  143. if err != nil {
  144. return nil, errors.New("TLS: Failed to load certificate file: " + err.Error())
  145. }
  146. key, err := ioutil.ReadFile(certConf.KeyFile)
  147. if err != nil {
  148. return nil, errors.New("TLS: Failed to load key file: " + err.Error())
  149. }
  150. config.Certificate[idx] = &tls.Certificate{
  151. Key: key,
  152. Certificate: cert,
  153. }
  154. }
  155. config.AllowInsecure = this.Insecure
  156. return loader.NewTypedSettings(config), nil
  157. }
  158. type StreamConfig struct {
  159. Network *Network `json:"network"`
  160. Security string `json:"security"`
  161. TLSSettings *TLSConfig `json:"tlsSettings"`
  162. TCPSettings *TCPConfig `json:"tcpSettings"`
  163. KCPSettings *KCPConfig `json:"kcpSettings"`
  164. WSSettings *WebSocketConfig `json:"wsSettings"`
  165. }
  166. func (this *StreamConfig) Build() (*internet.StreamConfig, error) {
  167. config := &internet.StreamConfig{
  168. Network: v2net.Network_RawTCP,
  169. }
  170. if this.Network != nil {
  171. config.Network = (*this.Network).Build()
  172. }
  173. if strings.ToLower(this.Security) == "tls" {
  174. tlsSettings := this.TLSSettings
  175. if tlsSettings == nil {
  176. tlsSettings = &TLSConfig{}
  177. }
  178. ts, err := tlsSettings.Build()
  179. if err != nil {
  180. return nil, errors.New("Failed to build TLS config: " + err.Error())
  181. }
  182. config.SecuritySettings = append(config.SecuritySettings, ts)
  183. }
  184. if this.TCPSettings != nil {
  185. ts, err := this.TCPSettings.Build()
  186. if err != nil {
  187. return nil, errors.New("Failed to build TCP config: " + err.Error())
  188. }
  189. config.NetworkSettings = append(config.NetworkSettings, &internet.NetworkSettings{
  190. Network: v2net.Network_TCP,
  191. Settings: ts,
  192. })
  193. }
  194. if this.KCPSettings != nil {
  195. ts, err := this.KCPSettings.Build()
  196. if err != nil {
  197. return nil, errors.New("Failed to build KCP config: " + err.Error())
  198. }
  199. config.NetworkSettings = append(config.NetworkSettings, &internet.NetworkSettings{
  200. Network: v2net.Network_KCP,
  201. Settings: ts,
  202. })
  203. }
  204. if this.WSSettings != nil {
  205. ts, err := this.WSSettings.Build()
  206. if err != nil {
  207. return nil, errors.New("Failed to build WebSocket config: " + err.Error())
  208. }
  209. config.NetworkSettings = append(config.NetworkSettings, &internet.NetworkSettings{
  210. Network: v2net.Network_WebSocket,
  211. Settings: ts,
  212. })
  213. }
  214. return config, nil
  215. }