dialer.go 2.6 KB

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