client.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package quic
  2. import (
  3. "context"
  4. tls "github.com/Psiphon-Labs/psiphon-tls"
  5. "errors"
  6. "net"
  7. "github.com/Psiphon-Labs/quic-go/internal/protocol"
  8. )
  9. // make it possible to mock connection ID for initial generation in the tests
  10. var generateConnectionIDForInitial = protocol.GenerateConnectionIDForInitial
  11. // DialAddr establishes a new QUIC connection to a server.
  12. // It resolves the address, and then creates a new UDP connection to dial the QUIC server.
  13. // When the QUIC connection is closed, this UDP connection is closed.
  14. // See Dial for more details.
  15. func DialAddr(ctx context.Context, addr string, tlsConf *tls.Config, conf *Config) (Connection, error) {
  16. udpConn, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4zero, Port: 0})
  17. if err != nil {
  18. return nil, err
  19. }
  20. udpAddr, err := net.ResolveUDPAddr("udp", addr)
  21. if err != nil {
  22. return nil, err
  23. }
  24. tr, err := setupTransport(udpConn, tlsConf, true)
  25. if err != nil {
  26. return nil, err
  27. }
  28. return tr.dial(ctx, udpAddr, addr, tlsConf, conf, false)
  29. }
  30. // DialAddrEarly establishes a new 0-RTT QUIC connection to a server.
  31. // See DialAddr for more details.
  32. func DialAddrEarly(ctx context.Context, addr string, tlsConf *tls.Config, conf *Config) (EarlyConnection, error) {
  33. udpConn, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4zero, Port: 0})
  34. if err != nil {
  35. return nil, err
  36. }
  37. udpAddr, err := net.ResolveUDPAddr("udp", addr)
  38. if err != nil {
  39. return nil, err
  40. }
  41. tr, err := setupTransport(udpConn, tlsConf, true)
  42. if err != nil {
  43. return nil, err
  44. }
  45. conn, err := tr.dial(ctx, udpAddr, addr, tlsConf, conf, true)
  46. if err != nil {
  47. tr.Close()
  48. return nil, err
  49. }
  50. return conn, nil
  51. }
  52. // DialEarly establishes a new 0-RTT QUIC connection to a server using a net.PacketConn.
  53. // See Dial for more details.
  54. func DialEarly(ctx context.Context, c net.PacketConn, addr net.Addr, tlsConf *tls.Config, conf *Config) (EarlyConnection, error) {
  55. dl, err := setupTransport(c, tlsConf, false)
  56. if err != nil {
  57. return nil, err
  58. }
  59. conn, err := dl.DialEarly(ctx, addr, tlsConf, conf)
  60. if err != nil {
  61. dl.Close()
  62. return nil, err
  63. }
  64. return conn, nil
  65. }
  66. // Dial establishes a new QUIC connection to a server using a net.PacketConn.
  67. // If the PacketConn satisfies the OOBCapablePacketConn interface (as a net.UDPConn does),
  68. // ECN and packet info support will be enabled. In this case, ReadMsgUDP and WriteMsgUDP
  69. // will be used instead of ReadFrom and WriteTo to read/write packets.
  70. // The tls.Config must define an application protocol (using NextProtos).
  71. //
  72. // This is a convenience function. More advanced use cases should instantiate a Transport,
  73. // which offers configuration options for a more fine-grained control of the connection establishment,
  74. // including reusing the underlying UDP socket for multiple QUIC connections.
  75. func Dial(ctx context.Context, c net.PacketConn, addr net.Addr, tlsConf *tls.Config, conf *Config) (Connection, error) {
  76. dl, err := setupTransport(c, tlsConf, false)
  77. if err != nil {
  78. return nil, err
  79. }
  80. conn, err := dl.Dial(ctx, addr, tlsConf, conf)
  81. if err != nil {
  82. dl.Close()
  83. return nil, err
  84. }
  85. return conn, nil
  86. }
  87. func setupTransport(c net.PacketConn, tlsConf *tls.Config, createdPacketConn bool) (*Transport, error) {
  88. if tlsConf == nil {
  89. return nil, errors.New("quic: tls.Config not set")
  90. }
  91. return &Transport{
  92. Conn: c,
  93. createdConn: createdPacketConn,
  94. isSingleUse: true,
  95. }, nil
  96. }