transport_internet.go 6.0 KB

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