dialer.go 2.6 KB

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