2020-12-29 17:49:26 +02:00
|
|
|
package transport
|
2019-05-24 19:39:26 +02:00
|
|
|
|
|
|
|
import (
|
2019-07-11 11:47:02 +02:00
|
|
|
"context"
|
2019-05-24 19:39:26 +02:00
|
|
|
"crypto/tls"
|
|
|
|
"encoding/gob"
|
2021-10-12 13:55:53 +02:00
|
|
|
"go-micro.dev/v4/cmd"
|
2019-09-13 01:22:43 +02:00
|
|
|
"time"
|
2019-05-24 19:39:26 +02:00
|
|
|
|
2021-10-12 13:55:53 +02:00
|
|
|
"go-micro.dev/v4/transport"
|
|
|
|
utls "go-micro.dev/v4/util/tls"
|
2019-12-03 21:59:44 +02:00
|
|
|
quic "github.com/lucas-clemente/quic-go"
|
2019-05-24 19:39:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type quicSocket struct {
|
|
|
|
s quic.Session
|
|
|
|
st quic.Stream
|
|
|
|
enc *gob.Encoder
|
|
|
|
dec *gob.Decoder
|
|
|
|
}
|
|
|
|
|
|
|
|
type quicTransport struct {
|
2021-02-26 10:13:12 +02:00
|
|
|
opts transport.Options
|
2019-05-24 19:39:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type quicClient struct {
|
|
|
|
*quicSocket
|
|
|
|
t *quicTransport
|
2021-02-26 10:13:12 +02:00
|
|
|
opts transport.DialOptions
|
2019-05-24 19:39:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type quicListener struct {
|
|
|
|
l quic.Listener
|
|
|
|
t *quicTransport
|
2021-02-26 10:13:12 +02:00
|
|
|
opts transport.ListenOptions
|
2019-05-24 19:39:26 +02:00
|
|
|
}
|
|
|
|
|
2021-09-02 19:10:24 +02:00
|
|
|
func init() {
|
|
|
|
cmd.DefaultTransports["quic"] = NewTransport
|
|
|
|
}
|
|
|
|
|
2019-05-24 19:39:26 +02:00
|
|
|
func (q *quicClient) Close() error {
|
|
|
|
return q.quicSocket.st.Close()
|
|
|
|
}
|
|
|
|
|
2021-02-26 10:13:12 +02:00
|
|
|
func (q *quicSocket) Recv(m *transport.Message) error {
|
2019-05-24 19:39:26 +02:00
|
|
|
return q.dec.Decode(&m)
|
|
|
|
}
|
|
|
|
|
2021-02-26 10:13:12 +02:00
|
|
|
func (q *quicSocket) Send(m *transport.Message) error {
|
2019-09-13 01:22:43 +02:00
|
|
|
// set the write deadline
|
|
|
|
q.st.SetWriteDeadline(time.Now().Add(time.Second * 10))
|
|
|
|
// send the data
|
2019-05-24 19:39:26 +02:00
|
|
|
return q.enc.Encode(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *quicSocket) Close() error {
|
2020-12-12 20:59:40 +02:00
|
|
|
return q.s.CloseWithError(0, "EOF")
|
2019-05-24 19:39:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *quicSocket) Local() string {
|
|
|
|
return q.s.LocalAddr().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *quicSocket) Remote() string {
|
|
|
|
return q.s.RemoteAddr().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *quicListener) Addr() string {
|
|
|
|
return q.l.Addr().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *quicListener) Close() error {
|
|
|
|
return q.l.Close()
|
|
|
|
}
|
|
|
|
|
2021-02-26 10:13:12 +02:00
|
|
|
func (q *quicListener) Accept(fn func(transport.Socket)) error {
|
2019-05-24 19:39:26 +02:00
|
|
|
for {
|
2019-07-11 11:47:02 +02:00
|
|
|
s, err := q.l.Accept(context.TODO())
|
2019-05-24 19:39:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-11 11:47:02 +02:00
|
|
|
stream, err := s.AcceptStream(context.TODO())
|
2019-05-24 19:39:26 +02:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
fn(&quicSocket{
|
|
|
|
s: s,
|
|
|
|
st: stream,
|
|
|
|
enc: gob.NewEncoder(stream),
|
|
|
|
dec: gob.NewDecoder(stream),
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 10:13:12 +02:00
|
|
|
func (q *quicTransport) Init(opts ...transport.Option) error {
|
2019-05-24 19:39:26 +02:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&q.opts)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-26 10:13:12 +02:00
|
|
|
func (q *quicTransport) Options() transport.Options {
|
2019-05-24 19:39:26 +02:00
|
|
|
return q.opts
|
|
|
|
}
|
|
|
|
|
2021-02-26 10:13:12 +02:00
|
|
|
func (q *quicTransport) Dial(addr string, opts ...transport.DialOption) (transport.Client, error) {
|
|
|
|
var options transport.DialOptions
|
2019-05-24 19:39:26 +02:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := q.opts.TLSConfig
|
|
|
|
if config == nil {
|
|
|
|
config = &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
2019-07-11 10:38:20 +02:00
|
|
|
NextProtos: []string{"http/1.1"},
|
2019-05-24 19:39:26 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-13 01:22:43 +02:00
|
|
|
s, err := quic.DialAddr(addr, config, &quic.Config{
|
2020-12-12 20:59:40 +02:00
|
|
|
MaxIdleTimeout: time.Minute * 2,
|
2020-12-12 21:04:19 +02:00
|
|
|
KeepAlive: true,
|
2019-09-13 01:22:43 +02:00
|
|
|
})
|
2019-05-24 19:39:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-11 11:47:02 +02:00
|
|
|
st, err := s.OpenStreamSync(context.TODO())
|
2019-05-24 19:39:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
enc := gob.NewEncoder(st)
|
|
|
|
dec := gob.NewDecoder(st)
|
|
|
|
|
|
|
|
return &quicClient{
|
|
|
|
&quicSocket{
|
|
|
|
s: s,
|
|
|
|
st: st,
|
|
|
|
enc: enc,
|
|
|
|
dec: dec,
|
|
|
|
},
|
|
|
|
q,
|
|
|
|
options,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-02-26 10:13:12 +02:00
|
|
|
func (q *quicTransport) Listen(addr string, opts ...transport.ListenOption) (transport.Listener, error) {
|
|
|
|
var options transport.ListenOptions
|
2019-05-24 19:39:26 +02:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := q.opts.TLSConfig
|
|
|
|
if config == nil {
|
|
|
|
cfg, err := utls.Certificate(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config = &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{cfg},
|
2019-07-11 10:38:20 +02:00
|
|
|
NextProtos: []string{"http/1.1"},
|
2019-05-24 19:39:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-11 17:12:31 +02:00
|
|
|
l, err := quic.ListenAddr(addr, config, &quic.Config{KeepAlive: true})
|
2019-05-24 19:39:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &quicListener{
|
|
|
|
l: l,
|
|
|
|
t: q,
|
|
|
|
opts: options,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *quicTransport) String() string {
|
|
|
|
return "quic"
|
|
|
|
}
|
|
|
|
|
2021-02-26 10:13:12 +02:00
|
|
|
func NewTransport(opts ...transport.Option) transport.Transport {
|
|
|
|
options := transport.Options{}
|
2019-05-24 19:39:26 +02:00
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &quicTransport{
|
|
|
|
opts: options,
|
|
|
|
}
|
|
|
|
}
|