2020-05-21 20:36:49 +00:00
|
|
|
package tunnelstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2020-08-05 10:49:53 +00:00
|
|
|
"net/url"
|
|
|
|
"path"
|
2020-05-21 20:36:49 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-06-11 23:44:39 +00:00
|
|
|
"github.com/google/uuid"
|
2020-05-21 20:36:49 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-07-21 21:15:48 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/logger"
|
2020-05-21 20:36:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultTimeout = 15 * time.Second
|
|
|
|
jsonContentType = "application/json"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrTunnelNameConflict = errors.New("tunnel with name already exists")
|
|
|
|
ErrUnauthorized = errors.New("unauthorized")
|
|
|
|
ErrBadRequest = errors.New("incorrect request parameters")
|
|
|
|
ErrNotFound = errors.New("not found")
|
|
|
|
)
|
|
|
|
|
|
|
|
type Tunnel struct {
|
2020-07-06 08:01:48 +00:00
|
|
|
ID uuid.UUID `json:"id"`
|
2020-06-11 23:44:39 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
CreatedAt time.Time `json:"created_at"`
|
2020-06-16 22:55:33 +00:00
|
|
|
DeletedAt time.Time `json:"deleted_at"`
|
2020-06-11 23:44:39 +00:00
|
|
|
Connections []Connection `json:"connections"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Connection struct {
|
2020-08-05 20:43:56 +00:00
|
|
|
ColoName string `json:"colo_name"`
|
|
|
|
ID uuid.UUID `json:"uuid"`
|
|
|
|
IsPendingReconnect bool `json:"is_pending_reconnect"`
|
2020-05-21 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 08:01:48 +00:00
|
|
|
// Route represents a record type that can route to a tunnel
|
|
|
|
type Route interface {
|
|
|
|
json.Marshaler
|
|
|
|
RecordType() string
|
2020-07-30 14:33:10 +00:00
|
|
|
// SuccessSummary explains what will route to this tunnel when it's provisioned successfully
|
|
|
|
SuccessSummary() string
|
2020-07-06 08:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DNSRoute struct {
|
|
|
|
userHostname string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDNSRoute(userHostname string) Route {
|
|
|
|
return &DNSRoute{
|
|
|
|
userHostname: userHostname,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dr *DNSRoute) MarshalJSON() ([]byte, error) {
|
|
|
|
s := struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
UserHostname string `json:"user_hostname"`
|
|
|
|
}{
|
|
|
|
Type: dr.RecordType(),
|
|
|
|
UserHostname: dr.userHostname,
|
|
|
|
}
|
|
|
|
return json.Marshal(&s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dr *DNSRoute) RecordType() string {
|
|
|
|
return "dns"
|
|
|
|
}
|
|
|
|
|
2020-07-30 14:33:10 +00:00
|
|
|
func (dr *DNSRoute) SuccessSummary() string {
|
|
|
|
return fmt.Sprintf("%s will route to your tunnel", dr.userHostname)
|
|
|
|
}
|
|
|
|
|
2020-07-06 08:01:48 +00:00
|
|
|
type LBRoute struct {
|
|
|
|
lbName string
|
|
|
|
lbPool string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLBRoute(lbName, lbPool string) Route {
|
|
|
|
return &LBRoute{
|
|
|
|
lbName: lbName,
|
|
|
|
lbPool: lbPool,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lr *LBRoute) MarshalJSON() ([]byte, error) {
|
|
|
|
s := struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
LBName string `json:"lb_name"`
|
|
|
|
LBPool string `json:"lb_pool"`
|
|
|
|
}{
|
|
|
|
Type: lr.RecordType(),
|
|
|
|
LBName: lr.lbName,
|
|
|
|
LBPool: lr.lbPool,
|
|
|
|
}
|
|
|
|
return json.Marshal(&s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lr *LBRoute) RecordType() string {
|
|
|
|
return "lb"
|
|
|
|
}
|
|
|
|
|
2020-07-30 14:33:10 +00:00
|
|
|
func (lr *LBRoute) SuccessSummary() string {
|
|
|
|
return fmt.Sprintf("Load balancer %s will route to this tunnel through pool %s", lr.lbName, lr.lbPool)
|
|
|
|
}
|
|
|
|
|
2020-05-21 20:36:49 +00:00
|
|
|
type Client interface {
|
2020-06-15 18:33:41 +00:00
|
|
|
CreateTunnel(name string, tunnelSecret []byte) (*Tunnel, error)
|
2020-07-06 08:01:48 +00:00
|
|
|
GetTunnel(tunnelID uuid.UUID) (*Tunnel, error)
|
|
|
|
DeleteTunnel(tunnelID uuid.UUID) error
|
2020-08-07 12:29:53 +00:00
|
|
|
ListTunnels(filter *Filter) ([]*Tunnel, error)
|
2020-07-06 08:01:48 +00:00
|
|
|
CleanupConnections(tunnelID uuid.UUID) error
|
|
|
|
RouteTunnel(tunnelID uuid.UUID, route Route) error
|
2020-05-21 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RESTClient struct {
|
2020-07-06 08:01:48 +00:00
|
|
|
baseEndpoints *baseEndpoints
|
|
|
|
authToken string
|
|
|
|
client http.Client
|
|
|
|
logger logger.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
type baseEndpoints struct {
|
2020-08-05 10:49:53 +00:00
|
|
|
accountLevel url.URL
|
|
|
|
zoneLevel url.URL
|
2020-05-21 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Client = (*RESTClient)(nil)
|
|
|
|
|
2020-08-05 10:49:53 +00:00
|
|
|
func NewRESTClient(baseURL string, accountTag, zoneTag string, authToken string, logger logger.Service) (*RESTClient, error) {
|
2020-05-21 20:36:49 +00:00
|
|
|
if strings.HasSuffix(baseURL, "/") {
|
|
|
|
baseURL = baseURL[:len(baseURL)-1]
|
|
|
|
}
|
2020-08-05 10:49:53 +00:00
|
|
|
accountLevelEndpoint, err := url.Parse(fmt.Sprintf("%s/accounts/%s/tunnels", baseURL, accountTag))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create account level endpoint")
|
|
|
|
}
|
|
|
|
zoneLevelEndpoint, err := url.Parse(fmt.Sprintf("%s/zones/%s/tunnels", baseURL, zoneTag))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create account level endpoint")
|
|
|
|
}
|
2020-05-21 20:36:49 +00:00
|
|
|
return &RESTClient{
|
2020-07-06 08:01:48 +00:00
|
|
|
baseEndpoints: &baseEndpoints{
|
2020-08-05 10:49:53 +00:00
|
|
|
accountLevel: *accountLevelEndpoint,
|
|
|
|
zoneLevel: *zoneLevelEndpoint,
|
2020-07-06 08:01:48 +00:00
|
|
|
},
|
2020-05-21 20:36:49 +00:00
|
|
|
authToken: authToken,
|
|
|
|
client: http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSHandshakeTimeout: defaultTimeout,
|
|
|
|
ResponseHeaderTimeout: defaultTimeout,
|
|
|
|
},
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
},
|
2020-04-29 20:51:32 +00:00
|
|
|
logger: logger,
|
2020-08-05 10:49:53 +00:00
|
|
|
}, nil
|
2020-05-21 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type newTunnel struct {
|
2020-06-15 18:33:41 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
TunnelSecret []byte `json:"tunnel_secret"`
|
2020-05-21 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 18:33:41 +00:00
|
|
|
func (r *RESTClient) CreateTunnel(name string, tunnelSecret []byte) (*Tunnel, error) {
|
2020-05-21 20:36:49 +00:00
|
|
|
if name == "" {
|
|
|
|
return nil, errors.New("tunnel name required")
|
|
|
|
}
|
2020-08-18 21:54:05 +00:00
|
|
|
if _, err := uuid.Parse(name); err == nil {
|
|
|
|
return nil, errors.New("you cannot use UUIDs as tunnel names")
|
|
|
|
}
|
2020-07-21 21:15:48 +00:00
|
|
|
body := &newTunnel{
|
2020-06-15 18:33:41 +00:00
|
|
|
Name: name,
|
|
|
|
TunnelSecret: tunnelSecret,
|
2020-05-21 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 21:15:48 +00:00
|
|
|
resp, err := r.sendRequest("POST", r.baseEndpoints.accountLevel, body)
|
2020-05-21 20:36:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "REST request failed")
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusOK:
|
|
|
|
return unmarshalTunnel(resp.Body)
|
|
|
|
case http.StatusConflict:
|
|
|
|
return nil, ErrTunnelNameConflict
|
|
|
|
}
|
|
|
|
|
2020-07-21 21:15:48 +00:00
|
|
|
return nil, r.statusCodeToError("create tunnel", resp)
|
2020-05-21 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 08:01:48 +00:00
|
|
|
func (r *RESTClient) GetTunnel(tunnelID uuid.UUID) (*Tunnel, error) {
|
2020-08-05 10:49:53 +00:00
|
|
|
endpoint := r.baseEndpoints.accountLevel
|
|
|
|
endpoint.Path = path.Join(endpoint.Path, fmt.Sprintf("%v", tunnelID))
|
2020-07-06 08:01:48 +00:00
|
|
|
resp, err := r.sendRequest("GET", endpoint, nil)
|
2020-05-21 20:36:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "REST request failed")
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusOK {
|
|
|
|
return unmarshalTunnel(resp.Body)
|
|
|
|
}
|
|
|
|
|
2020-07-21 21:15:48 +00:00
|
|
|
return nil, r.statusCodeToError("get tunnel", resp)
|
2020-05-21 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 08:01:48 +00:00
|
|
|
func (r *RESTClient) DeleteTunnel(tunnelID uuid.UUID) error {
|
2020-08-05 10:49:53 +00:00
|
|
|
endpoint := r.baseEndpoints.accountLevel
|
|
|
|
endpoint.Path = path.Join(endpoint.Path, fmt.Sprintf("%v", tunnelID))
|
2020-07-06 08:01:48 +00:00
|
|
|
resp, err := r.sendRequest("DELETE", endpoint, nil)
|
2020-05-21 20:36:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "REST request failed")
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2020-07-21 21:15:48 +00:00
|
|
|
return r.statusCodeToError("delete tunnel", resp)
|
2020-05-21 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 12:29:53 +00:00
|
|
|
func (r *RESTClient) ListTunnels(filter *Filter) ([]*Tunnel, error) {
|
2020-08-05 10:49:53 +00:00
|
|
|
endpoint := r.baseEndpoints.accountLevel
|
|
|
|
endpoint.RawQuery = filter.encode()
|
|
|
|
resp, err := r.sendRequest("GET", endpoint, nil)
|
2020-05-21 20:36:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "REST request failed")
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusOK {
|
2020-08-07 12:29:53 +00:00
|
|
|
var tunnels []*Tunnel
|
2020-05-21 20:36:49 +00:00
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&tunnels); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to decode response")
|
|
|
|
}
|
|
|
|
return tunnels, nil
|
|
|
|
}
|
|
|
|
|
2020-07-21 21:15:48 +00:00
|
|
|
return nil, r.statusCodeToError("list tunnels", resp)
|
2020-07-03 08:55:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 08:01:48 +00:00
|
|
|
func (r *RESTClient) CleanupConnections(tunnelID uuid.UUID) error {
|
2020-08-05 10:49:53 +00:00
|
|
|
endpoint := r.baseEndpoints.accountLevel
|
|
|
|
endpoint.Path = path.Join(endpoint.Path, fmt.Sprintf("%v/connections", tunnelID))
|
2020-07-06 08:01:48 +00:00
|
|
|
resp, err := r.sendRequest("DELETE", endpoint, nil)
|
2020-07-03 08:55:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "REST request failed")
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2020-07-21 21:15:48 +00:00
|
|
|
return r.statusCodeToError("cleanup connections", resp)
|
2020-05-21 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 08:01:48 +00:00
|
|
|
func (r *RESTClient) RouteTunnel(tunnelID uuid.UUID, route Route) error {
|
2020-08-05 10:49:53 +00:00
|
|
|
endpoint := r.baseEndpoints.zoneLevel
|
|
|
|
endpoint.Path = path.Join(endpoint.Path, fmt.Sprintf("%v/routes", tunnelID))
|
2020-07-21 21:15:48 +00:00
|
|
|
resp, err := r.sendRequest("PUT", endpoint, route)
|
2020-07-06 08:01:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "REST request failed")
|
2020-05-21 20:36:49 +00:00
|
|
|
}
|
2020-07-06 08:01:48 +00:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2020-07-21 21:15:48 +00:00
|
|
|
return r.statusCodeToError("add route", resp)
|
2020-05-21 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 10:49:53 +00:00
|
|
|
func (r *RESTClient) sendRequest(method string, url url.URL, body interface{}) (*http.Response, error) {
|
2020-07-21 21:15:48 +00:00
|
|
|
var bodyReader io.Reader
|
|
|
|
if body != nil {
|
|
|
|
if bodyBytes, err := json.Marshal(body); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to serialize json body")
|
|
|
|
} else {
|
|
|
|
bodyReader = bytes.NewBuffer(bodyBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 10:49:53 +00:00
|
|
|
req, err := http.NewRequest(method, url.String(), bodyReader)
|
2020-05-21 20:36:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "can't create %s request", method)
|
|
|
|
}
|
2020-07-21 21:15:48 +00:00
|
|
|
if bodyReader != nil {
|
2020-05-21 20:36:49 +00:00
|
|
|
req.Header.Set("Content-Type", jsonContentType)
|
|
|
|
}
|
|
|
|
req.Header.Add("X-Auth-User-Service-Key", r.authToken)
|
|
|
|
return r.client.Do(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalTunnel(reader io.Reader) (*Tunnel, error) {
|
|
|
|
var tunnel Tunnel
|
|
|
|
if err := json.NewDecoder(reader).Decode(&tunnel); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to decode response")
|
|
|
|
}
|
|
|
|
return &tunnel, nil
|
|
|
|
}
|
|
|
|
|
2020-07-21 21:15:48 +00:00
|
|
|
func (r *RESTClient) statusCodeToError(op string, resp *http.Response) error {
|
2020-05-21 20:36:49 +00:00
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusOK:
|
|
|
|
return nil
|
|
|
|
case http.StatusBadRequest:
|
|
|
|
return ErrBadRequest
|
|
|
|
case http.StatusUnauthorized, http.StatusForbidden:
|
|
|
|
return ErrUnauthorized
|
|
|
|
case http.StatusNotFound:
|
|
|
|
return ErrNotFound
|
|
|
|
}
|
2020-07-03 08:55:11 +00:00
|
|
|
return errors.Errorf("API call to %s failed with status %d: %s", op,
|
2020-05-21 20:36:49 +00:00
|
|
|
resp.StatusCode, http.StatusText(resp.StatusCode))
|
|
|
|
}
|