2019-03-29 23:05:11 +00:00
|
|
|
package pogs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-05-28 20:53:35 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2019-03-29 23:05:11 +00:00
|
|
|
"fmt"
|
2019-05-28 20:53:35 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2019-05-30 20:45:46 +00:00
|
|
|
"net/url"
|
2019-03-29 23:05:11 +00:00
|
|
|
"time"
|
|
|
|
|
2019-06-12 15:07:24 +00:00
|
|
|
"github.com/cloudflare/cloudflared/h2mux"
|
2019-05-28 20:53:35 +00:00
|
|
|
"github.com/cloudflare/cloudflared/originservice"
|
|
|
|
"github.com/cloudflare/cloudflared/tlsconfig"
|
2019-03-29 23:05:11 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tunnelrpc"
|
2019-07-31 19:01:23 +00:00
|
|
|
|
2019-05-28 20:53:35 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-03-29 23:05:11 +00:00
|
|
|
capnp "zombiezen.com/go/capnproto2"
|
|
|
|
"zombiezen.com/go/capnproto2/pogs"
|
|
|
|
"zombiezen.com/go/capnproto2/rpc"
|
2019-06-12 15:07:24 +00:00
|
|
|
"zombiezen.com/go/capnproto2/server"
|
2019-03-29 23:05:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Structs
|
|
|
|
///
|
|
|
|
|
2019-06-12 15:07:24 +00:00
|
|
|
// ClientConfig is a collection of FallibleConfig that determines how cloudflared should function
|
2019-05-17 14:23:05 +00:00
|
|
|
type ClientConfig struct {
|
2019-07-31 19:01:23 +00:00
|
|
|
Version Version `json:"version"`
|
|
|
|
SupervisorConfig *SupervisorConfig `json:"supervisor_config"`
|
|
|
|
EdgeConnectionConfig *EdgeConnectionConfig `json:"edge_connection_config"`
|
|
|
|
DoHProxyConfigs []*DoHProxyConfig `json:"doh_proxy_configs"`
|
|
|
|
ReverseProxyConfigs []*ReverseProxyConfig `json:"reverse_proxy_configs"`
|
2019-06-12 15:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Version type models the version of a ClientConfig
|
|
|
|
type Version uint64
|
|
|
|
|
|
|
|
func InitVersion() Version {
|
|
|
|
return Version(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Version) IsNewerOrEqual(comparedVersion Version) bool {
|
|
|
|
return v >= comparedVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Version) String() string {
|
|
|
|
return fmt.Sprintf("Version: %d", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FallibleConfig is an interface implemented by configs that cloudflared might not be able to apply
|
|
|
|
type FallibleConfig interface {
|
|
|
|
FailReason(err error) string
|
|
|
|
}
|
|
|
|
|
|
|
|
// SupervisorConfig specifies config of components managed by Supervisor other than ConnectionManager
|
|
|
|
type SupervisorConfig struct {
|
2019-07-31 19:01:23 +00:00
|
|
|
AutoUpdateFrequency time.Duration `json:"auto_update_frequency"`
|
|
|
|
MetricsUpdateFrequency time.Duration `json:"metrics_update_frequency"`
|
|
|
|
GracePeriod time.Duration `json:"grace_period"`
|
2019-03-29 23:05:11 +00:00
|
|
|
}
|
|
|
|
|
2019-06-12 15:07:24 +00:00
|
|
|
// FailReason impelents FallibleConfig interface for SupervisorConfig
|
|
|
|
func (sc *SupervisorConfig) FailReason(err error) string {
|
|
|
|
return fmt.Sprintf("Cannot apply SupervisorConfig, err: %v", err)
|
2019-03-29 23:05:11 +00:00
|
|
|
}
|
|
|
|
|
2019-06-12 15:07:24 +00:00
|
|
|
// EdgeConnectionConfig specifies what parameters and how may connections should ConnectionManager establish with edge
|
|
|
|
type EdgeConnectionConfig struct {
|
2019-07-31 19:01:23 +00:00
|
|
|
NumHAConnections uint8 `json:"num_ha_connections"`
|
|
|
|
HeartbeatInterval time.Duration `json:"heartbeat_interval"`
|
|
|
|
Timeout time.Duration `json:"timeout"`
|
|
|
|
MaxFailedHeartbeats uint64 `json:"max_failed_heartbeats"`
|
|
|
|
UserCredentialPath string `json:"user_credential_path"`
|
2019-06-12 15:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FailReason impelents FallibleConfig interface for EdgeConnectionConfig
|
|
|
|
func (cmc *EdgeConnectionConfig) FailReason(err error) string {
|
|
|
|
return fmt.Sprintf("Cannot apply EdgeConnectionConfig, err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DoHProxyConfig is configuration for DNS over HTTPS service
|
2019-03-29 23:05:11 +00:00
|
|
|
type DoHProxyConfig struct {
|
2019-07-31 19:01:23 +00:00
|
|
|
ListenHost string `json:"listen_host"`
|
|
|
|
ListenPort uint16 `json:"listen_port"`
|
|
|
|
Upstreams []string `json:"upstreams"`
|
2019-03-29 23:05:11 +00:00
|
|
|
}
|
|
|
|
|
2019-06-12 15:07:24 +00:00
|
|
|
// FailReason impelents FallibleConfig interface for DoHProxyConfig
|
|
|
|
func (dpc *DoHProxyConfig) FailReason(err error) string {
|
|
|
|
return fmt.Sprintf("Cannot apply DoHProxyConfig, err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReverseProxyConfig how and for what hostnames can this cloudflared proxy
|
2019-03-29 23:05:11 +00:00
|
|
|
type ReverseProxyConfig struct {
|
2019-07-31 19:01:23 +00:00
|
|
|
TunnelHostname h2mux.TunnelHostname `json:"tunnel_hostname"`
|
|
|
|
OriginConfigUnmarshaler *OriginConfigUnmarshaler `json:"origin_config"`
|
|
|
|
Retries uint64 `json:"retries"`
|
|
|
|
ConnectionTimeout time.Duration `json:"connection_timeout"`
|
|
|
|
CompressionQuality uint64 `json:"compression_quality"`
|
2019-03-29 23:05:11 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 14:23:05 +00:00
|
|
|
func NewReverseProxyConfig(
|
|
|
|
tunnelHostname string,
|
|
|
|
originConfig OriginConfig,
|
|
|
|
retries uint64,
|
|
|
|
connectionTimeout time.Duration,
|
|
|
|
compressionQuality uint64,
|
|
|
|
) (*ReverseProxyConfig, error) {
|
|
|
|
if originConfig == nil {
|
2019-07-31 19:01:23 +00:00
|
|
|
return nil, fmt.Errorf("NewReverseProxyConfig: originConfigUnmarshaler was null")
|
2019-05-17 14:23:05 +00:00
|
|
|
}
|
|
|
|
return &ReverseProxyConfig{
|
2019-07-31 19:01:23 +00:00
|
|
|
TunnelHostname: h2mux.TunnelHostname(tunnelHostname),
|
|
|
|
OriginConfigUnmarshaler: &OriginConfigUnmarshaler{originConfig},
|
|
|
|
Retries: retries,
|
|
|
|
ConnectionTimeout: connectionTimeout,
|
|
|
|
CompressionQuality: compressionQuality,
|
2019-05-17 14:23:05 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-06-12 15:07:24 +00:00
|
|
|
// FailReason impelents FallibleConfig interface for ReverseProxyConfig
|
|
|
|
func (rpc *ReverseProxyConfig) FailReason(err error) string {
|
|
|
|
return fmt.Sprintf("Cannot apply ReverseProxyConfig, err: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-03-29 23:05:11 +00:00
|
|
|
//go-sumtype:decl OriginConfig
|
|
|
|
type OriginConfig interface {
|
2019-05-28 20:53:35 +00:00
|
|
|
// Service returns a OriginService used to proxy to the origin
|
|
|
|
Service() (originservice.OriginService, error)
|
|
|
|
// go-sumtype requires at least one unexported method, otherwise it will complain that interface is not sealed
|
2019-03-29 23:05:11 +00:00
|
|
|
isOriginConfig()
|
|
|
|
}
|
|
|
|
|
2019-07-31 19:01:23 +00:00
|
|
|
type originType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
httpType originType = iota
|
|
|
|
wsType
|
|
|
|
helloWorldType
|
|
|
|
)
|
|
|
|
|
|
|
|
func (ot originType) String() string {
|
|
|
|
switch ot {
|
|
|
|
case httpType:
|
|
|
|
return "Http"
|
|
|
|
case wsType:
|
|
|
|
return "WebSocket"
|
|
|
|
case helloWorldType:
|
|
|
|
return "HelloWorld"
|
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 23:05:11 +00:00
|
|
|
type HTTPOriginConfig struct {
|
2019-07-31 19:01:23 +00:00
|
|
|
URLString string `capnp:"urlString" mapstructure:"url_string"`
|
|
|
|
TCPKeepAlive time.Duration `capnp:"tcpKeepAlive" mapstructure:"tcp_keep_alive"`
|
|
|
|
DialDualStack bool `mapstructure:"dial_dual_stack"`
|
|
|
|
TLSHandshakeTimeout time.Duration `capnp:"tlsHandshakeTimeout" mapstructure:"tls_handshake_timeout"`
|
|
|
|
TLSVerify bool `capnp:"tlsVerify" mapstructure:"tls_verify"`
|
|
|
|
OriginCAPool string `mapstructure:"origin_ca_pool"`
|
|
|
|
OriginServerName string `mapstructure:"origin_server_name"`
|
|
|
|
MaxIdleConnections uint64 `mapstructure:"max_idle_connections"`
|
|
|
|
IdleConnectionTimeout time.Duration `mapstructure:"idle_connection_timeout"`
|
|
|
|
ProxyConnectionTimeout time.Duration `mapstructure:"proxy_connection_timeout"`
|
|
|
|
ExpectContinueTimeout time.Duration `mapstructure:"expect_continue_timeout"`
|
|
|
|
ChunkedEncoding bool `mapstructure:"chunked_encoding"`
|
2019-05-30 20:45:46 +00:00
|
|
|
}
|
2019-03-29 23:05:11 +00:00
|
|
|
|
2019-05-28 20:53:35 +00:00
|
|
|
func (hc *HTTPOriginConfig) Service() (originservice.OriginService, error) {
|
2019-06-21 00:29:18 +00:00
|
|
|
rootCAs, err := tlsconfig.LoadCustomOriginCA(hc.OriginCAPool)
|
2019-05-28 20:53:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-20 16:18:59 +00:00
|
|
|
|
2019-05-28 20:53:35 +00:00
|
|
|
dialContext := (&net.Dialer{
|
2019-06-20 16:18:59 +00:00
|
|
|
Timeout: hc.ProxyConnectionTimeout,
|
2019-05-28 20:53:35 +00:00
|
|
|
KeepAlive: hc.TCPKeepAlive,
|
|
|
|
DualStack: hc.DialDualStack,
|
|
|
|
}).DialContext
|
|
|
|
transport := &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
DialContext: dialContext,
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
RootCAs: rootCAs,
|
|
|
|
ServerName: hc.OriginServerName,
|
|
|
|
InsecureSkipVerify: hc.TLSVerify,
|
|
|
|
},
|
|
|
|
TLSHandshakeTimeout: hc.TLSHandshakeTimeout,
|
|
|
|
MaxIdleConns: int(hc.MaxIdleConnections),
|
|
|
|
IdleConnTimeout: hc.IdleConnectionTimeout,
|
|
|
|
ExpectContinueTimeout: hc.ExpectContinueTimeout,
|
|
|
|
}
|
2019-06-20 16:18:59 +00:00
|
|
|
url, err := url.Parse(hc.URLString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "%s is not a valid URL", hc.URLString)
|
|
|
|
}
|
|
|
|
if url.Scheme == "unix" {
|
2019-05-28 20:53:35 +00:00
|
|
|
transport.DialContext = func(ctx context.Context, _, _ string) (net.Conn, error) {
|
2019-06-20 16:18:59 +00:00
|
|
|
return dialContext(ctx, "unix", url.Host)
|
2019-05-28 20:53:35 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-20 16:18:59 +00:00
|
|
|
return originservice.NewHTTPService(transport, url, hc.ChunkedEncoding), nil
|
2019-05-28 20:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (_ *HTTPOriginConfig) isOriginConfig() {}
|
|
|
|
|
2019-03-29 23:05:11 +00:00
|
|
|
type WebSocketOriginConfig struct {
|
2019-07-31 19:01:23 +00:00
|
|
|
URLString string `capnp:"urlString" mapstructure:"url_string"`
|
|
|
|
TLSVerify bool `capnp:"tlsVerify" mapstructure:"tls_verify"`
|
|
|
|
OriginCAPool string `mapstructure:"origin_ca_pool"`
|
|
|
|
OriginServerName string `mapstructure:"origin_server_name"`
|
2019-03-29 23:05:11 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 20:53:35 +00:00
|
|
|
func (wsc *WebSocketOriginConfig) Service() (originservice.OriginService, error) {
|
2019-06-21 00:29:18 +00:00
|
|
|
rootCAs, err := tlsconfig.LoadCustomOriginCA(wsc.OriginCAPool)
|
2019-05-28 20:53:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
RootCAs: rootCAs,
|
|
|
|
ServerName: wsc.OriginServerName,
|
|
|
|
InsecureSkipVerify: wsc.TLSVerify,
|
|
|
|
}
|
2019-06-20 16:18:59 +00:00
|
|
|
|
|
|
|
url, err := url.Parse(wsc.URLString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "%s is not a valid URL", wsc.URLString)
|
|
|
|
}
|
|
|
|
return originservice.NewWebSocketService(tlsConfig, url)
|
2019-05-28 20:53:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 23:05:11 +00:00
|
|
|
func (_ *WebSocketOriginConfig) isOriginConfig() {}
|
|
|
|
|
|
|
|
type HelloWorldOriginConfig struct{}
|
|
|
|
|
2019-05-28 20:53:35 +00:00
|
|
|
func (_ *HelloWorldOriginConfig) Service() (originservice.OriginService, error) {
|
|
|
|
helloCert, err := tlsconfig.GetHelloCertificateX509()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Cannot get Hello World server certificate")
|
|
|
|
}
|
|
|
|
rootCAs := x509.NewCertPool()
|
|
|
|
rootCAs.AddCert(helloCert)
|
|
|
|
transport := &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
DialContext: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
DualStack: true,
|
|
|
|
}).DialContext,
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
RootCAs: rootCAs,
|
|
|
|
},
|
|
|
|
MaxIdleConns: 100,
|
|
|
|
IdleConnTimeout: 90 * time.Second,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
ExpectContinueTimeout: 1 * time.Second,
|
|
|
|
}
|
|
|
|
return originservice.NewHelloWorldService(transport)
|
|
|
|
}
|
|
|
|
|
2019-03-29 23:05:11 +00:00
|
|
|
func (_ *HelloWorldOriginConfig) isOriginConfig() {}
|
|
|
|
|
2019-05-17 14:23:05 +00:00
|
|
|
/*
|
|
|
|
* Boilerplate to convert between these structs and the primitive structs
|
|
|
|
* generated by capnp-go.
|
|
|
|
* Mnemonics for variable names in this section:
|
|
|
|
* - `p` is for POGS (plain old Go struct)
|
|
|
|
* - `s` (and `ss`) is for "capnp.Struct", which is the fundamental type
|
|
|
|
* underlying the capnp-go data structures.
|
|
|
|
*/
|
|
|
|
|
|
|
|
func MarshalClientConfig(s tunnelrpc.ClientConfig, p *ClientConfig) error {
|
2019-06-12 15:07:24 +00:00
|
|
|
s.SetVersion(uint64(p.Version))
|
|
|
|
|
|
|
|
supervisorConfig, err := s.NewSupervisorConfig()
|
2019-03-29 23:05:11 +00:00
|
|
|
if err != nil {
|
2019-06-12 15:07:24 +00:00
|
|
|
return errors.Wrap(err, "failed to get SupervisorConfig")
|
|
|
|
}
|
|
|
|
if err = MarshalSupervisorConfig(supervisorConfig, p.SupervisorConfig); err != nil {
|
|
|
|
return errors.Wrap(err, "MarshalSupervisorConfig error")
|
|
|
|
}
|
|
|
|
|
|
|
|
edgeConnectionConfig, err := s.NewEdgeConnectionConfig()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to get EdgeConnectionConfig")
|
|
|
|
}
|
|
|
|
if err := MarshalEdgeConnectionConfig(edgeConnectionConfig, p.EdgeConnectionConfig); err != nil {
|
|
|
|
return errors.Wrap(err, "MarshalEdgeConnectionConfig error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := marshalDoHProxyConfigs(s, p.DoHProxyConfigs); err != nil {
|
|
|
|
return errors.Wrap(err, "marshalDoHProxyConfigs error")
|
|
|
|
}
|
|
|
|
if err := marshalReverseProxyConfigs(s, p.ReverseProxyConfigs); err != nil {
|
|
|
|
return errors.Wrap(err, "marshalReverseProxyConfigs error")
|
2019-03-29 23:05:11 +00:00
|
|
|
}
|
2019-06-12 15:07:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func MarshalSupervisorConfig(s tunnelrpc.SupervisorConfig, p *SupervisorConfig) error {
|
|
|
|
if err := pogs.Insert(tunnelrpc.SupervisorConfig_TypeID, s.Struct, p); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to insert SupervisorConfig")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func MarshalEdgeConnectionConfig(s tunnelrpc.EdgeConnectionConfig, p *EdgeConnectionConfig) error {
|
|
|
|
if err := pogs.Insert(tunnelrpc.EdgeConnectionConfig_TypeID, s.Struct, p); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to insert EdgeConnectionConfig")
|
|
|
|
}
|
|
|
|
return nil
|
2019-03-29 23:05:11 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 14:23:05 +00:00
|
|
|
func marshalDoHProxyConfigs(s tunnelrpc.ClientConfig, dohProxyConfigs []*DoHProxyConfig) error {
|
2019-03-29 23:05:11 +00:00
|
|
|
capnpList, err := s.NewDohProxyConfigs(int32(len(dohProxyConfigs)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i, unmarshalledConfig := range dohProxyConfigs {
|
|
|
|
err := MarshalDoHProxyConfig(capnpList.At(i), unmarshalledConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-17 14:23:05 +00:00
|
|
|
func marshalReverseProxyConfigs(s tunnelrpc.ClientConfig, reverseProxyConfigs []*ReverseProxyConfig) error {
|
2019-03-29 23:05:11 +00:00
|
|
|
capnpList, err := s.NewReverseProxyConfigs(int32(len(reverseProxyConfigs)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i, unmarshalledConfig := range reverseProxyConfigs {
|
|
|
|
err := MarshalReverseProxyConfig(capnpList.At(i), unmarshalledConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-17 14:23:05 +00:00
|
|
|
func UnmarshalClientConfig(s tunnelrpc.ClientConfig) (*ClientConfig, error) {
|
|
|
|
p := new(ClientConfig)
|
2019-06-12 15:07:24 +00:00
|
|
|
p.Version = Version(s.Version())
|
|
|
|
|
|
|
|
supervisorConfig, err := s.SupervisorConfig()
|
2019-03-29 23:05:11 +00:00
|
|
|
if err != nil {
|
2019-06-12 15:07:24 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to get SupervisorConfig")
|
2019-03-29 23:05:11 +00:00
|
|
|
}
|
2019-06-12 15:07:24 +00:00
|
|
|
p.SupervisorConfig, err = UnmarshalSupervisorConfig(supervisorConfig)
|
2019-03-29 23:05:11 +00:00
|
|
|
if err != nil {
|
2019-06-12 15:07:24 +00:00
|
|
|
return nil, errors.Wrap(err, "UnmarshalSupervisorConfig error")
|
|
|
|
}
|
|
|
|
|
|
|
|
edgeConnectionConfig, err := s.EdgeConnectionConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to get ConnectionManagerConfig")
|
|
|
|
}
|
|
|
|
p.EdgeConnectionConfig, err = UnmarshalEdgeConnectionConfig(edgeConnectionConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "UnmarshalConnectionManagerConfig error")
|
2019-03-29 23:05:11 +00:00
|
|
|
}
|
2019-06-12 15:07:24 +00:00
|
|
|
|
|
|
|
p.DoHProxyConfigs, err = unmarshalDoHProxyConfigs(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "unmarshalDoHProxyConfigs error")
|
|
|
|
}
|
|
|
|
|
|
|
|
p.ReverseProxyConfigs, err = unmarshalReverseProxyConfigs(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "unmarshalReverseProxyConfigs error")
|
|
|
|
}
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnmarshalSupervisorConfig(s tunnelrpc.SupervisorConfig) (*SupervisorConfig, error) {
|
|
|
|
p := new(SupervisorConfig)
|
|
|
|
err := pogs.Extract(p, tunnelrpc.SupervisorConfig_TypeID, s.Struct)
|
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnmarshalEdgeConnectionConfig(s tunnelrpc.EdgeConnectionConfig) (*EdgeConnectionConfig, error) {
|
|
|
|
p := new(EdgeConnectionConfig)
|
|
|
|
err := pogs.Extract(p, tunnelrpc.EdgeConnectionConfig_TypeID, s.Struct)
|
2019-03-29 23:05:11 +00:00
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
2019-05-17 14:23:05 +00:00
|
|
|
func unmarshalDoHProxyConfigs(s tunnelrpc.ClientConfig) ([]*DoHProxyConfig, error) {
|
2019-03-29 23:05:11 +00:00
|
|
|
var result []*DoHProxyConfig
|
|
|
|
marshalledDoHProxyConfigs, err := s.DohProxyConfigs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for i := 0; i < marshalledDoHProxyConfigs.Len(); i++ {
|
|
|
|
ss := marshalledDoHProxyConfigs.At(i)
|
|
|
|
dohProxyConfig, err := UnmarshalDoHProxyConfig(ss)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, dohProxyConfig)
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2019-05-17 14:23:05 +00:00
|
|
|
func unmarshalReverseProxyConfigs(s tunnelrpc.ClientConfig) ([]*ReverseProxyConfig, error) {
|
2019-03-29 23:05:11 +00:00
|
|
|
var result []*ReverseProxyConfig
|
|
|
|
marshalledReverseProxyConfigs, err := s.ReverseProxyConfigs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for i := 0; i < marshalledReverseProxyConfigs.Len(); i++ {
|
|
|
|
ss := marshalledReverseProxyConfigs.At(i)
|
|
|
|
reverseProxyConfig, err := UnmarshalReverseProxyConfig(ss)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, reverseProxyConfig)
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func MarshalUseConfigurationResult(s tunnelrpc.UseConfigurationResult, p *UseConfigurationResult) error {
|
2019-06-12 15:07:24 +00:00
|
|
|
capnpList, err := s.NewFailedConfigs(int32(len(p.FailedConfigs)))
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Cannot create new FailedConfigs")
|
|
|
|
}
|
|
|
|
for i, unmarshalledFailedConfig := range p.FailedConfigs {
|
|
|
|
err := MarshalFailedConfig(capnpList.At(i), unmarshalledFailedConfig)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "Cannot MarshalFailedConfig at index %d", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.SetSuccess(p.Success)
|
|
|
|
return nil
|
2019-03-29 23:05:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func UnmarshalUseConfigurationResult(s tunnelrpc.UseConfigurationResult) (*UseConfigurationResult, error) {
|
|
|
|
p := new(UseConfigurationResult)
|
2019-06-12 15:07:24 +00:00
|
|
|
var failedConfigs []*FailedConfig
|
|
|
|
marshalledFailedConfigs, err := s.FailedConfigs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Cannot get FailedConfigs")
|
|
|
|
}
|
|
|
|
for i := 0; i < marshalledFailedConfigs.Len(); i++ {
|
|
|
|
ss := marshalledFailedConfigs.At(i)
|
|
|
|
failedConfig, err := UnmarshalFailedConfig(ss)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "Cannot UnmarshalFailedConfig at index %d", i)
|
|
|
|
}
|
|
|
|
failedConfigs = append(failedConfigs, failedConfig)
|
|
|
|
}
|
|
|
|
p.FailedConfigs = failedConfigs
|
|
|
|
p.Success = s.Success()
|
|
|
|
return p, nil
|
2019-03-29 23:05:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func MarshalDoHProxyConfig(s tunnelrpc.DoHProxyConfig, p *DoHProxyConfig) error {
|
|
|
|
return pogs.Insert(tunnelrpc.DoHProxyConfig_TypeID, s.Struct, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnmarshalDoHProxyConfig(s tunnelrpc.DoHProxyConfig) (*DoHProxyConfig, error) {
|
|
|
|
p := new(DoHProxyConfig)
|
|
|
|
err := pogs.Extract(p, tunnelrpc.DoHProxyConfig_TypeID, s.Struct)
|
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func MarshalReverseProxyConfig(s tunnelrpc.ReverseProxyConfig, p *ReverseProxyConfig) error {
|
2019-06-12 15:07:24 +00:00
|
|
|
s.SetTunnelHostname(p.TunnelHostname.String())
|
2019-07-31 19:01:23 +00:00
|
|
|
switch config := p.OriginConfigUnmarshaler.OriginConfig.(type) {
|
2019-03-29 23:05:11 +00:00
|
|
|
case *HTTPOriginConfig:
|
|
|
|
ss, err := s.Origin().NewHttp()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-30 20:45:46 +00:00
|
|
|
if err := MarshalHTTPOriginConfig(ss, config); err != nil {
|
2019-03-29 23:05:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *WebSocketOriginConfig:
|
|
|
|
ss, err := s.Origin().NewWebsocket()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-30 20:45:46 +00:00
|
|
|
if err := MarshalWebSocketOriginConfig(ss, config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-29 23:05:11 +00:00
|
|
|
case *HelloWorldOriginConfig:
|
|
|
|
ss, err := s.Origin().NewHelloWorld()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-30 20:45:46 +00:00
|
|
|
if err := MarshalHelloWorldOriginConfig(ss, config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-29 23:05:11 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unknown type for config: %T", config)
|
|
|
|
}
|
|
|
|
s.SetRetries(p.Retries)
|
|
|
|
s.SetConnectionTimeout(p.ConnectionTimeout.Nanoseconds())
|
|
|
|
s.SetCompressionQuality(p.CompressionQuality)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnmarshalReverseProxyConfig(s tunnelrpc.ReverseProxyConfig) (*ReverseProxyConfig, error) {
|
|
|
|
p := new(ReverseProxyConfig)
|
2019-05-17 14:23:05 +00:00
|
|
|
tunnelHostname, err := s.TunnelHostname()
|
2019-03-29 23:05:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-12 15:07:24 +00:00
|
|
|
p.TunnelHostname = h2mux.TunnelHostname(tunnelHostname)
|
2019-03-29 23:05:11 +00:00
|
|
|
switch s.Origin().Which() {
|
|
|
|
case tunnelrpc.ReverseProxyConfig_origin_Which_http:
|
|
|
|
ss, err := s.Origin().Http()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config, err := UnmarshalHTTPOriginConfig(ss)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-31 19:01:23 +00:00
|
|
|
p.OriginConfigUnmarshaler = &OriginConfigUnmarshaler{config}
|
2019-03-29 23:05:11 +00:00
|
|
|
case tunnelrpc.ReverseProxyConfig_origin_Which_websocket:
|
|
|
|
ss, err := s.Origin().Websocket()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config, err := UnmarshalWebSocketOriginConfig(ss)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-31 19:01:23 +00:00
|
|
|
p.OriginConfigUnmarshaler = &OriginConfigUnmarshaler{config}
|
2019-03-29 23:05:11 +00:00
|
|
|
case tunnelrpc.ReverseProxyConfig_origin_Which_helloWorld:
|
|
|
|
ss, err := s.Origin().HelloWorld()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config, err := UnmarshalHelloWorldOriginConfig(ss)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-31 19:01:23 +00:00
|
|
|
p.OriginConfigUnmarshaler = &OriginConfigUnmarshaler{config}
|
2019-03-29 23:05:11 +00:00
|
|
|
}
|
|
|
|
p.Retries = s.Retries()
|
|
|
|
p.ConnectionTimeout = time.Duration(s.ConnectionTimeout())
|
|
|
|
p.CompressionQuality = s.CompressionQuality()
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func MarshalHTTPOriginConfig(s tunnelrpc.HTTPOriginConfig, p *HTTPOriginConfig) error {
|
2019-06-20 16:18:59 +00:00
|
|
|
return pogs.Insert(tunnelrpc.HTTPOriginConfig_TypeID, s.Struct, p)
|
2019-03-29 23:05:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func UnmarshalHTTPOriginConfig(s tunnelrpc.HTTPOriginConfig) (*HTTPOriginConfig, error) {
|
|
|
|
p := new(HTTPOriginConfig)
|
2019-06-20 16:18:59 +00:00
|
|
|
err := pogs.Extract(p, tunnelrpc.HTTPOriginConfig_TypeID, s.Struct)
|
2019-03-29 23:05:11 +00:00
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func MarshalWebSocketOriginConfig(s tunnelrpc.WebSocketOriginConfig, p *WebSocketOriginConfig) error {
|
|
|
|
return pogs.Insert(tunnelrpc.WebSocketOriginConfig_TypeID, s.Struct, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnmarshalWebSocketOriginConfig(s tunnelrpc.WebSocketOriginConfig) (*WebSocketOriginConfig, error) {
|
|
|
|
p := new(WebSocketOriginConfig)
|
|
|
|
err := pogs.Extract(p, tunnelrpc.WebSocketOriginConfig_TypeID, s.Struct)
|
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func MarshalHelloWorldOriginConfig(s tunnelrpc.HelloWorldOriginConfig, p *HelloWorldOriginConfig) error {
|
|
|
|
return pogs.Insert(tunnelrpc.HelloWorldOriginConfig_TypeID, s.Struct, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnmarshalHelloWorldOriginConfig(s tunnelrpc.HelloWorldOriginConfig) (*HelloWorldOriginConfig, error) {
|
|
|
|
p := new(HelloWorldOriginConfig)
|
|
|
|
err := pogs.Extract(p, tunnelrpc.HelloWorldOriginConfig_TypeID, s.Struct)
|
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
2019-05-17 14:23:05 +00:00
|
|
|
type ClientService interface {
|
2019-05-30 20:45:46 +00:00
|
|
|
UseConfiguration(ctx context.Context, config *ClientConfig) (*UseConfigurationResult, error)
|
2019-03-29 23:05:11 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 14:23:05 +00:00
|
|
|
type ClientService_PogsClient struct {
|
2019-03-29 23:05:11 +00:00
|
|
|
Client capnp.Client
|
|
|
|
Conn *rpc.Conn
|
|
|
|
}
|
|
|
|
|
2019-05-17 14:23:05 +00:00
|
|
|
func (c *ClientService_PogsClient) Close() error {
|
2019-03-29 23:05:11 +00:00
|
|
|
return c.Conn.Close()
|
|
|
|
}
|
|
|
|
|
2019-05-17 14:23:05 +00:00
|
|
|
func (c *ClientService_PogsClient) UseConfiguration(
|
2019-03-29 23:05:11 +00:00
|
|
|
ctx context.Context,
|
2019-05-17 14:23:05 +00:00
|
|
|
config *ClientConfig,
|
2019-03-29 23:05:11 +00:00
|
|
|
) (*UseConfigurationResult, error) {
|
2019-05-17 14:23:05 +00:00
|
|
|
client := tunnelrpc.ClientService{Client: c.Client}
|
|
|
|
promise := client.UseConfiguration(ctx, func(p tunnelrpc.ClientService_useConfiguration_Params) error {
|
2019-05-30 20:45:46 +00:00
|
|
|
clientServiceConfig, err := p.NewClientServiceConfig()
|
2019-03-29 23:05:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-17 14:23:05 +00:00
|
|
|
return MarshalClientConfig(clientServiceConfig, config)
|
2019-03-29 23:05:11 +00:00
|
|
|
})
|
|
|
|
retval, err := promise.Result().Struct()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return UnmarshalUseConfigurationResult(retval)
|
|
|
|
}
|
2019-06-12 15:07:24 +00:00
|
|
|
|
|
|
|
func ClientService_ServerToClient(s ClientService) tunnelrpc.ClientService {
|
|
|
|
return tunnelrpc.ClientService_ServerToClient(ClientService_PogsImpl{s})
|
|
|
|
}
|
|
|
|
|
|
|
|
type ClientService_PogsImpl struct {
|
|
|
|
impl ClientService
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i ClientService_PogsImpl) UseConfiguration(p tunnelrpc.ClientService_useConfiguration) error {
|
|
|
|
config, err := p.Params.ClientServiceConfig()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Cannot get CloudflaredConfig parameter")
|
|
|
|
}
|
|
|
|
pogsConfig, err := UnmarshalClientConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Cannot unmarshal tunnelrpc.CloudflaredConfig to *CloudflaredConfig")
|
|
|
|
}
|
|
|
|
server.Ack(p.Options)
|
|
|
|
userConfigResult, err := i.impl.UseConfiguration(p.Ctx, pogsConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
result, err := p.Results.NewResult()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return MarshalUseConfigurationResult(result, userConfigResult)
|
|
|
|
}
|
|
|
|
|
|
|
|
type UseConfigurationResult struct {
|
|
|
|
Success bool
|
|
|
|
FailedConfigs []*FailedConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
type FailedConfig struct {
|
|
|
|
Config FallibleConfig
|
|
|
|
Reason string
|
|
|
|
}
|
|
|
|
|
|
|
|
func MarshalFailedConfig(s tunnelrpc.FailedConfig, p *FailedConfig) error {
|
|
|
|
switch config := p.Config.(type) {
|
|
|
|
case *SupervisorConfig:
|
|
|
|
ss, err := s.Config().NewSupervisor()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = MarshalSupervisorConfig(ss, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *EdgeConnectionConfig:
|
|
|
|
ss, err := s.Config().EdgeConnection()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = MarshalEdgeConnectionConfig(ss, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *DoHProxyConfig:
|
|
|
|
ss, err := s.Config().NewDoh()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = MarshalDoHProxyConfig(ss, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *ReverseProxyConfig:
|
|
|
|
ss, err := s.Config().NewReverseProxy()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = MarshalReverseProxyConfig(ss, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unknown type for Config: %T", config)
|
|
|
|
}
|
|
|
|
s.SetReason(p.Reason)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnmarshalFailedConfig(s tunnelrpc.FailedConfig) (*FailedConfig, error) {
|
|
|
|
p := new(FailedConfig)
|
|
|
|
switch s.Config().Which() {
|
|
|
|
case tunnelrpc.FailedConfig_config_Which_supervisor:
|
|
|
|
ss, err := s.Config().Supervisor()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Cannot get SupervisorConfig from Config")
|
|
|
|
}
|
|
|
|
config, err := UnmarshalSupervisorConfig(ss)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Cannot UnmarshalSupervisorConfig")
|
|
|
|
}
|
|
|
|
p.Config = config
|
|
|
|
case tunnelrpc.FailedConfig_config_Which_edgeConnection:
|
|
|
|
ss, err := s.Config().EdgeConnection()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Cannot get ConnectionManager from Config")
|
|
|
|
}
|
|
|
|
config, err := UnmarshalEdgeConnectionConfig(ss)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Cannot UnmarshalConnectionManagerConfig")
|
|
|
|
}
|
|
|
|
p.Config = config
|
|
|
|
case tunnelrpc.FailedConfig_config_Which_doh:
|
|
|
|
ss, err := s.Config().Doh()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Cannot get Doh from Config")
|
|
|
|
}
|
|
|
|
config, err := UnmarshalDoHProxyConfig(ss)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Cannot UnmarshalDoHProxyConfig")
|
|
|
|
}
|
|
|
|
p.Config = config
|
|
|
|
case tunnelrpc.FailedConfig_config_Which_reverseProxy:
|
|
|
|
ss, err := s.Config().ReverseProxy()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Cannot get ReverseProxy from Config")
|
|
|
|
}
|
|
|
|
config, err := UnmarshalReverseProxyConfig(ss)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Cannot UnmarshalReverseProxyConfig")
|
|
|
|
}
|
|
|
|
p.Config = config
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unknown type for FailedConfig: %v", s.Config().Which())
|
|
|
|
}
|
|
|
|
reason, err := s.Reason()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Cannot get Reason")
|
|
|
|
}
|
|
|
|
p.Reason = reason
|
|
|
|
return p, nil
|
|
|
|
}
|