2019-03-18 23:14:47 +00:00
|
|
|
package connection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-12-13 23:05:21 +00:00
|
|
|
"net"
|
2019-03-18 23:14:47 +00:00
|
|
|
"time"
|
|
|
|
|
2019-06-17 21:18:47 +00:00
|
|
|
"github.com/google/uuid"
|
2019-03-18 23:14:47 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2019-11-21 17:03:13 +00:00
|
|
|
"github.com/cloudflare/cloudflared/h2mux"
|
|
|
|
tunnelpogs "github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
2019-03-18 23:14:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-04-02 23:12:09 +00:00
|
|
|
openStreamTimeout = 30 * time.Second
|
2019-03-18 23:14:47 +00:00
|
|
|
)
|
|
|
|
|
2019-06-17 21:18:47 +00:00
|
|
|
type Connection struct {
|
2020-02-06 00:55:26 +00:00
|
|
|
id uuid.UUID
|
|
|
|
muxer *h2mux.Muxer
|
|
|
|
addr *net.TCPAddr
|
|
|
|
isLongLived bool
|
|
|
|
longLivedID int
|
2019-03-18 23:14:47 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 23:05:21 +00:00
|
|
|
func newConnection(muxer *h2mux.Muxer, addr *net.TCPAddr) (*Connection, error) {
|
2019-06-17 21:18:47 +00:00
|
|
|
id, err := uuid.NewRandom()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Connection{
|
|
|
|
id: id,
|
|
|
|
muxer: muxer,
|
2019-12-13 23:05:21 +00:00
|
|
|
addr: addr,
|
2019-06-17 21:18:47 +00:00
|
|
|
}, nil
|
2019-03-18 23:14:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 21:18:47 +00:00
|
|
|
func (c *Connection) Serve(ctx context.Context) error {
|
2019-03-18 23:14:47 +00:00
|
|
|
// Serve doesn't return until h2mux is shutdown
|
2019-06-17 21:18:47 +00:00
|
|
|
return c.muxer.Serve(ctx)
|
2019-03-18 23:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Connect is used to establish connections with cloudflare's edge network
|
2019-11-21 17:03:13 +00:00
|
|
|
func (c *Connection) Connect(ctx context.Context, parameters *tunnelpogs.ConnectParameters, logger *logrus.Entry) (tunnelpogs.ConnectResult, error) {
|
|
|
|
tsClient, err := NewRPCClient(ctx, c.muxer, logger.WithField("rpc", "connect"), openStreamTimeout)
|
2019-03-18 23:14:47 +00:00
|
|
|
if err != nil {
|
2019-06-17 21:18:47 +00:00
|
|
|
return nil, errors.Wrap(err, "cannot create new RPC connection")
|
2019-03-18 23:14:47 +00:00
|
|
|
}
|
2019-11-21 17:03:13 +00:00
|
|
|
defer tsClient.Close()
|
2019-03-18 23:14:47 +00:00
|
|
|
return tsClient.Connect(ctx, parameters)
|
|
|
|
}
|
|
|
|
|
2019-06-17 21:18:47 +00:00
|
|
|
func (c *Connection) Shutdown() {
|
|
|
|
c.muxer.Shutdown()
|
2019-03-18 23:14:47 +00:00
|
|
|
}
|