From e1578e93c78de4df7c69456f948534e6a6c1ee62 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Fri, 24 May 2019 18:39:26 +0100 Subject: [PATCH 1/2] add quic transport --- transport/quic/quic.go | 190 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 transport/quic/quic.go diff --git a/transport/quic/quic.go b/transport/quic/quic.go new file mode 100644 index 00000000..853d8865 --- /dev/null +++ b/transport/quic/quic.go @@ -0,0 +1,190 @@ +// Package quic provides a QUIC based transport +package quic + +import ( + "crypto/tls" + "encoding/gob" + + "github.com/micro/go-micro/cmd" + "github.com/lucas-clemente/quic-go" + "github.com/micro/go-micro/transport" + utls "github.com/micro/util/go/lib/tls" +) + +type quicSocket struct { + s quic.Session + st quic.Stream + enc *gob.Encoder + dec *gob.Decoder +} + +type quicTransport struct { + opts transport.Options +} + +type quicClient struct { + *quicSocket + t *quicTransport + opts transport.DialOptions +} + +type quicListener struct { + l quic.Listener + t *quicTransport + opts transport.ListenOptions +} + +func init() { + cmd.DefaultTransports["quic"] = NewTransport +} + +func (q *quicClient) Close() error { + return q.quicSocket.st.Close() +} + +func (q *quicSocket) Recv(m *transport.Message) error { + return q.dec.Decode(&m) +} + +func (q *quicSocket) Send(m *transport.Message) error { + return q.enc.Encode(m) +} + +func (q *quicSocket) Close() error { + return q.s.Close() +} + +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() +} + +func (q *quicListener) Accept(fn func(transport.Socket)) error { + for { + s, err := q.l.Accept() + if err != nil { + return err + } + + stream, err := s.AcceptStream() + if err != nil { + continue + } + +// buf := bufio.NewWriter(stream) + + go func() { + fn(&quicSocket{ + s: s, + st: stream, + enc: gob.NewEncoder(stream), + dec: gob.NewDecoder(stream), + }) + }() + } +} + +func (q *quicTransport) Init(opts ...transport.Option) error { + for _, o := range opts { + o(&q.opts) + } + return nil +} + +func (q *quicTransport) Options() transport.Options { + return q.opts +} + +func (q *quicTransport) Dial(addr string, opts ...transport.DialOption) (transport.Client, error) { + var options transport.DialOptions + for _, o := range opts { + o(&options) + } + + config := q.opts.TLSConfig + if config == nil { + config = &tls.Config{ + InsecureSkipVerify: true, + } + } + s, err := quic.DialAddr(addr, config, nil) + if err != nil { + return nil, err + } + + st, err := s.OpenStreamSync() + if err != nil { + return nil, err + } + + //buf := bufio.NewWriter(st) + enc := gob.NewEncoder(st) + dec := gob.NewDecoder(st) + + return &quicClient{ + &quicSocket{ + s: s, + st: st, + enc: enc, + dec: dec, + }, + q, + options, + }, nil +} + +func (q *quicTransport) Listen(addr string, opts ...transport.ListenOption) (transport.Listener, error) { + var options transport.ListenOptions + 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}, + } + } + + l, err := quic.ListenAddr(addr, config, nil) + if err != nil { + return nil, err + } + + return &quicListener{ + l: l, + t: q, + opts: options, + }, nil +} + +func (q *quicTransport) String() string { + return "quic" +} + +func NewTransport(opts ...transport.Option) transport.Transport { + options := transport.Options{} + + for _, o := range opts { + o(&options) + } + + return &quicTransport{ + opts: options, + } +} From ff1c325391ff12e94e3abca51f74f41863ebb576 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Fri, 24 May 2019 18:44:57 +0100 Subject: [PATCH 2/2] remove commented lines --- transport/quic/quic.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/transport/quic/quic.go b/transport/quic/quic.go index 853d8865..fd72db54 100644 --- a/transport/quic/quic.go +++ b/transport/quic/quic.go @@ -82,8 +82,6 @@ func (q *quicListener) Accept(fn func(transport.Socket)) error { continue } -// buf := bufio.NewWriter(stream) - go func() { fn(&quicSocket{ s: s, @@ -128,7 +126,6 @@ func (q *quicTransport) Dial(addr string, opts ...transport.DialOption) (transpo return nil, err } - //buf := bufio.NewWriter(st) enc := gob.NewEncoder(st) dec := gob.NewDecoder(st)