mirror of
https://github.com/wweir/sower.git
synced 2024-04-21 12:42:15 +00:00
57 lines
1.0 KiB
Go
57 lines
1.0 KiB
Go
package quic
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"net"
|
|
"time"
|
|
|
|
quic "github.com/lucas-clemente/quic-go"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type client struct {
|
|
conf *quic.Config
|
|
sess quic.Session
|
|
}
|
|
|
|
func NewClient() *client {
|
|
return &client{
|
|
conf: &quic.Config{
|
|
HandshakeTimeout: 5 * time.Second,
|
|
MaxIncomingStreams: 1024,
|
|
KeepAlive: true,
|
|
IdleTimeout: 5 * time.Minute,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (c *client) Dial(server string) (net.Conn, error) {
|
|
if c.sess == nil {
|
|
if sess, err := quic.DialAddr(server, &tls.Config{InsecureSkipVerify: true}, c.conf); err != nil {
|
|
return nil, errors.Wrap(err, "session")
|
|
} else {
|
|
go func() {
|
|
<-sess.Context().Done()
|
|
sess.Close()
|
|
c.sess = nil
|
|
}()
|
|
c.sess = sess
|
|
}
|
|
}
|
|
|
|
var stream quic.Stream
|
|
if err := WithTimeout(func() (err error) {
|
|
if stream, err = c.sess.OpenStream(); err != nil {
|
|
c.sess = nil
|
|
}
|
|
return
|
|
}, time.Second); err != nil {
|
|
return nil, errors.Wrap(err, "stream")
|
|
}
|
|
|
|
return &streamConn{
|
|
Stream: stream,
|
|
sess: c.sess,
|
|
}, nil
|
|
}
|