dialer.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package websocket
  2. import (
  3. "io/ioutil"
  4. "net"
  5. "github.com/gorilla/websocket"
  6. "v2ray.com/core/common"
  7. "v2ray.com/core/common/log"
  8. v2net "v2ray.com/core/common/net"
  9. "v2ray.com/core/transport/internet"
  10. "v2ray.com/core/transport/internet/internal"
  11. v2tls "v2ray.com/core/transport/internet/tls"
  12. )
  13. var (
  14. globalCache = internal.NewConnectionPool()
  15. )
  16. func Dial(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (internet.Connection, error) {
  17. log.Info("WebSocket|Dailer: Creating connection to ", dest)
  18. if src == nil {
  19. src = v2net.AnyIP
  20. }
  21. networkSettings, err := options.Stream.GetEffectiveTransportSettings()
  22. if err != nil {
  23. return nil, err
  24. }
  25. wsSettings := networkSettings.(*Config)
  26. id := internal.NewConnectionID(src, dest)
  27. var conn *wsconn
  28. if dest.Network == v2net.Network_TCP && wsSettings.IsConnectionReuse() {
  29. connt := globalCache.Get(id)
  30. if connt != nil {
  31. conn = connt.(*wsconn)
  32. }
  33. }
  34. if conn == nil {
  35. var err error
  36. conn, err = wsDial(src, dest, options)
  37. if err != nil {
  38. log.Warning("WebSocket|Dialer: Dial failed: ", err)
  39. return nil, err
  40. }
  41. }
  42. return internal.NewConnection(id, conn, globalCache, internal.ReuseConnection(wsSettings.IsConnectionReuse())), nil
  43. }
  44. func init() {
  45. common.Must(internet.RegisterTransportDialer(internet.TransportProtocol_WebSocket, Dial))
  46. }
  47. func wsDial(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (*wsconn, error) {
  48. networkSettings, err := options.Stream.GetEffectiveTransportSettings()
  49. if err != nil {
  50. return nil, err
  51. }
  52. wsSettings := networkSettings.(*Config)
  53. commonDial := func(network, addr string) (net.Conn, error) {
  54. return internet.DialSystem(src, dest)
  55. }
  56. dialer := websocket.Dialer{
  57. NetDial: commonDial,
  58. ReadBufferSize: 65536,
  59. WriteBufferSize: 65536,
  60. }
  61. protocol := "ws"
  62. if options.Stream != nil && options.Stream.HasSecuritySettings() {
  63. protocol = "wss"
  64. securitySettings, err := options.Stream.GetEffectiveSecuritySettings()
  65. if err != nil {
  66. log.Error("WebSocket: Failed to create security settings: ", err)
  67. return nil, err
  68. }
  69. tlsConfig, ok := securitySettings.(*v2tls.Config)
  70. if ok {
  71. dialer.TLSClientConfig = tlsConfig.GetTLSConfig()
  72. if dest.Address.Family().IsDomain() {
  73. dialer.TLSClientConfig.ServerName = dest.Address.Domain()
  74. }
  75. }
  76. }
  77. host := dest.NetAddr()
  78. if (protocol == "ws" && dest.Port == 80) || (protocol == "wss" && dest.Port == 443) {
  79. host = dest.Address.String()
  80. }
  81. uri := protocol + "://" + host + "/" + wsSettings.Path
  82. conn, resp, err := dialer.Dial(uri, nil)
  83. if err != nil {
  84. if resp != nil {
  85. reason, reasonerr := ioutil.ReadAll(resp.Body)
  86. log.Info(string(reason), reasonerr)
  87. }
  88. return nil, err
  89. }
  90. return func() internet.Connection {
  91. connv2ray := &wsconn{
  92. wsc: conn,
  93. connClosing: false,
  94. config: wsSettings,
  95. }
  96. connv2ray.setup()
  97. return connv2ray
  98. }().(*wsconn), nil
  99. }