2017-07-11 17:12:12 +00:00
|
|
|
package grpchijack
|
|
|
|
|
|
|
|
import (
|
2018-01-16 22:30:10 +00:00
|
|
|
"context"
|
2018-01-06 16:54:10 +00:00
|
|
|
"io"
|
2017-07-11 17:12:12 +00:00
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
controlapi "github.com/moby/buildkit/api/services/control"
|
|
|
|
"github.com/moby/buildkit/session"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Dialer(api controlapi.ControlClient) session.Dialer {
|
|
|
|
return func(ctx context.Context, proto string, meta map[string][]string) (net.Conn, error) {
|
|
|
|
|
|
|
|
meta = lowerHeaders(meta)
|
|
|
|
|
|
|
|
md := metadata.MD(meta)
|
|
|
|
|
2018-01-06 16:54:10 +00:00
|
|
|
ctx = metadata.NewOutgoingContext(ctx, md)
|
2017-07-11 17:12:12 +00:00
|
|
|
|
|
|
|
stream, err := api.Session(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-01-06 16:54:10 +00:00
|
|
|
c, _ := streamToConn(stream)
|
|
|
|
return c, nil
|
2017-07-11 17:12:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-06 16:54:10 +00:00
|
|
|
func streamToConn(stream grpc.Stream) (net.Conn, <-chan struct{}) {
|
|
|
|
closeCh := make(chan struct{})
|
|
|
|
c := &conn{stream: stream, buf: make([]byte, 32*1<<10), closeCh: closeCh}
|
|
|
|
return c, closeCh
|
2017-07-11 17:12:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type conn struct {
|
|
|
|
stream grpc.Stream
|
|
|
|
buf []byte
|
|
|
|
lastBuf []byte
|
2018-01-06 16:54:10 +00:00
|
|
|
|
|
|
|
closedOnce sync.Once
|
|
|
|
readMu sync.Mutex
|
|
|
|
err error
|
|
|
|
closeCh chan struct{}
|
2017-07-11 17:12:12 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 16:54:10 +00:00
|
|
|
func (c *conn) Read(b []byte) (n int, err error) {
|
|
|
|
c.readMu.Lock()
|
|
|
|
defer c.readMu.Unlock()
|
|
|
|
|
2017-07-11 17:12:12 +00:00
|
|
|
if c.lastBuf != nil {
|
|
|
|
n := copy(b, c.lastBuf)
|
|
|
|
c.lastBuf = c.lastBuf[n:]
|
|
|
|
if len(c.lastBuf) == 0 {
|
|
|
|
c.lastBuf = nil
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
m := new(controlapi.BytesMessage)
|
|
|
|
m.Data = c.buf
|
|
|
|
|
|
|
|
if err := c.stream.RecvMsg(m); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
c.buf = m.Data[:cap(m.Data)]
|
|
|
|
|
2018-01-06 16:54:10 +00:00
|
|
|
n = copy(b, m.Data)
|
2017-07-11 17:12:12 +00:00
|
|
|
if n < len(m.Data) {
|
|
|
|
c.lastBuf = m.Data[n:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *conn) Write(b []byte) (int, error) {
|
|
|
|
m := &controlapi.BytesMessage{Data: b}
|
|
|
|
if err := c.stream.SendMsg(m); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
|
2018-01-06 16:54:10 +00:00
|
|
|
func (c *conn) Close() (err error) {
|
|
|
|
c.closedOnce.Do(func() {
|
|
|
|
defer func() {
|
|
|
|
close(c.closeCh)
|
|
|
|
}()
|
|
|
|
|
|
|
|
if cs, ok := c.stream.(grpc.ClientStream); ok {
|
|
|
|
err = cs.CloseSend()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.readMu.Lock()
|
|
|
|
for {
|
|
|
|
m := new(controlapi.BytesMessage)
|
|
|
|
m.Data = c.buf
|
|
|
|
err = c.stream.RecvMsg(m)
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = nil
|
|
|
|
break
|
|
|
|
}
|
|
|
|
c.buf = m.Data[:cap(m.Data)]
|
|
|
|
c.lastBuf = append(c.lastBuf, c.buf...)
|
|
|
|
}
|
|
|
|
c.readMu.Unlock()
|
|
|
|
|
|
|
|
})
|
2017-07-11 17:12:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:35 +00:00
|
|
|
func (c *conn) LocalAddr() net.Addr {
|
2017-07-11 17:12:12 +00:00
|
|
|
return dummyAddr{}
|
|
|
|
}
|
2017-11-17 02:09:35 +00:00
|
|
|
func (c *conn) RemoteAddr() net.Addr {
|
2017-07-11 17:12:12 +00:00
|
|
|
return dummyAddr{}
|
|
|
|
}
|
2017-11-17 02:09:35 +00:00
|
|
|
func (c *conn) SetDeadline(t time.Time) error {
|
2017-07-11 17:12:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-11-17 02:09:35 +00:00
|
|
|
func (c *conn) SetReadDeadline(t time.Time) error {
|
2017-07-11 17:12:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-11-17 02:09:35 +00:00
|
|
|
func (c *conn) SetWriteDeadline(t time.Time) error {
|
2017-07-11 17:12:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type dummyAddr struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d dummyAddr) Network() string {
|
|
|
|
return "tcp"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d dummyAddr) String() string {
|
|
|
|
return "localhost"
|
|
|
|
}
|
|
|
|
|
|
|
|
func lowerHeaders(in map[string][]string) map[string][]string {
|
|
|
|
out := map[string][]string{}
|
|
|
|
for k := range in {
|
|
|
|
out[strings.ToLower(k)] = in[k]
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|