transport_internet.go 6.6 KB

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