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-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-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"
|
|
|
|
)
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Structs
|
|
|
|
///
|
|
|
|
|
2019-05-17 14:23:05 +00:00
|
|
|
type ClientConfig struct {
|
|
|
|
Version uint64
|
2019-03-29 23:05:11 +00:00
|
|
|
AutoUpdateFrequency time.Duration
|
|
|
|
MetricsUpdateFrequency time.Duration
|
|
|
|
HeartbeatInterval time.Duration
|
|
|
|
MaxFailedHeartbeats uint64
|
|
|
|
GracePeriod time.Duration
|
|
|
|
DoHProxyConfigs []*DoHProxyConfig
|
|
|
|
ReverseProxyConfigs []*ReverseProxyConfig
|
2019-05-17 14:23:05 +00:00
|
|
|
NumHAConnections uint8
|
2019-03-29 23:05:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type UseConfigurationResult struct {
|
|
|
|
Success bool
|
|
|
|
ErrorMessage string
|
|
|
|
}
|
|
|
|
|
|
|
|
type DoHProxyConfig struct {
|
|
|
|
ListenHost string
|
|
|
|
ListenPort uint16
|
|
|
|
Upstreams []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ReverseProxyConfig struct {
|
2019-05-17 14:23:05 +00:00
|
|
|
TunnelHostname string
|
2019-03-29 23:05:11 +00:00
|
|
|
Origin OriginConfig
|
|
|
|
Retries uint64
|
|
|
|
ConnectionTimeout time.Duration
|
|
|
|
CompressionQuality uint64
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return nil, fmt.Errorf("NewReverseProxyConfig: originConfig was null")
|
|
|
|
}
|
|
|
|
return &ReverseProxyConfig{
|
|
|
|
TunnelHostname: tunnelHostname,
|
|
|
|
Origin: originConfig,
|
|
|
|
Retries: retries,
|
|
|
|
ConnectionTimeout: connectionTimeout,
|
|
|
|
CompressionQuality: compressionQuality,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
type HTTPOriginConfig struct {
|
2019-05-30 20:45:46 +00:00
|
|
|
URL OriginAddr `capnp:"url"`
|
2019-03-29 23:05:11 +00:00
|
|
|
TCPKeepAlive time.Duration `capnp:"tcpKeepAlive"`
|
|
|
|
DialDualStack bool
|
|
|
|
TLSHandshakeTimeout time.Duration `capnp:"tlsHandshakeTimeout"`
|
|
|
|
TLSVerify bool `capnp:"tlsVerify"`
|
|
|
|
OriginCAPool string
|
|
|
|
OriginServerName string
|
|
|
|
MaxIdleConnections uint64
|
|
|
|
IdleConnectionTimeout time.Duration
|
2019-05-30 20:45:46 +00:00
|
|
|
ProxyConnectTimeout time.Duration
|
|
|
|
ExpectContinueTimeout time.Duration
|
|
|
|
ChunkedEncoding bool
|
2019-03-29 23:05:11 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 20:45:46 +00:00
|
|
|
type OriginAddr interface {
|
|
|
|
Addr() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type HTTPURL struct {
|
|
|
|
URL *url.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ha *HTTPURL) Addr() string {
|
|
|
|
return ha.URL.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ha *HTTPURL) capnpHTTPURL() *CapnpHTTPURL {
|
|
|
|
return &CapnpHTTPURL{
|
|
|
|
URL: ha.URL.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// URL for a HTTP origin, capnp doesn't have native support for URL, so represent it as string
|
|
|
|
type CapnpHTTPURL struct {
|
|
|
|
URL string `capnp:"url"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type UnixPath struct {
|
2019-03-29 23:05:11 +00:00
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
2019-05-30 20:45:46 +00:00
|
|
|
func (up *UnixPath) Addr() string {
|
|
|
|
return up.Path
|
|
|
|
}
|
2019-03-29 23:05:11 +00:00
|
|
|
|
2019-05-28 20:53:35 +00:00
|
|
|
func (hc *HTTPOriginConfig) Service() (originservice.OriginService, error) {
|
|
|
|
rootCAs, err := tlsconfig.LoadCustomCertPool(hc.OriginCAPool)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dialContext := (&net.Dialer{
|
|
|
|
Timeout: hc.ProxyConnectTimeout,
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
if unixPath, ok := hc.URL.(*UnixPath); ok {
|
|
|
|
transport.DialContext = func(ctx context.Context, _, _ string) (net.Conn, error) {
|
|
|
|
return dialContext(ctx, "unix", unixPath.Addr())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return originservice.NewHTTPService(transport, hc.URL.Addr(), hc.ChunkedEncoding), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (_ *HTTPOriginConfig) isOriginConfig() {}
|
|
|
|
|
2019-03-29 23:05:11 +00:00
|
|
|
type WebSocketOriginConfig struct {
|
2019-05-30 20:45:46 +00:00
|
|
|
URL string `capnp:"url"`
|
|
|
|
TLSVerify bool `capnp:"tlsVerify"`
|
|
|
|
OriginCAPool string
|
|
|
|
OriginServerName string
|
2019-03-29 23:05:11 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 20:53:35 +00:00
|
|
|
func (wsc *WebSocketOriginConfig) Service() (originservice.OriginService, error) {
|
|
|
|
rootCAs, err := tlsconfig.LoadCustomCertPool(wsc.OriginCAPool)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
RootCAs: rootCAs,
|
|
|
|
ServerName: wsc.OriginServerName,
|
|
|
|
InsecureSkipVerify: wsc.TLSVerify,
|
|
|
|
}
|
|
|
|
return originservice.NewWebSocketService(tlsConfig, wsc.URL)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
s.SetVersion(p.Version)
|
2019-03-29 23:05:11 +00:00
|
|
|
s.SetAutoUpdateFrequency(p.AutoUpdateFrequency.Nanoseconds())
|
|
|
|
s.SetMetricsUpdateFrequency(p.MetricsUpdateFrequency.Nanoseconds())
|
|
|
|
s.SetHeartbeatInterval(p.HeartbeatInterval.Nanoseconds())
|
|
|
|
s.SetMaxFailedHeartbeats(p.MaxFailedHeartbeats)
|
|
|
|
s.SetGracePeriod(p.GracePeriod.Nanoseconds())
|
2019-05-17 14:23:05 +00:00
|
|
|
s.SetNumHAConnections(p.NumHAConnections)
|
2019-03-29 23:05:11 +00:00
|
|
|
err := marshalDoHProxyConfigs(s, p.DoHProxyConfigs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-17 14:23:05 +00:00
|
|
|
return marshalReverseProxyConfigs(s, p.ReverseProxyConfigs)
|
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)
|
|
|
|
p.Version = s.Version()
|
2019-03-29 23:05:11 +00:00
|
|
|
p.AutoUpdateFrequency = time.Duration(s.AutoUpdateFrequency())
|
|
|
|
p.MetricsUpdateFrequency = time.Duration(s.MetricsUpdateFrequency())
|
|
|
|
p.HeartbeatInterval = time.Duration(s.HeartbeatInterval())
|
|
|
|
p.MaxFailedHeartbeats = s.MaxFailedHeartbeats()
|
|
|
|
p.GracePeriod = time.Duration(s.GracePeriod())
|
2019-05-17 14:23:05 +00:00
|
|
|
p.NumHAConnections = s.NumHAConnections()
|
2019-03-29 23:05:11 +00:00
|
|
|
dohProxyConfigs, err := unmarshalDoHProxyConfigs(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.DoHProxyConfigs = dohProxyConfigs
|
|
|
|
reverseProxyConfigs, err := unmarshalReverseProxyConfigs(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.ReverseProxyConfigs = reverseProxyConfigs
|
|
|
|
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 {
|
|
|
|
return pogs.Insert(tunnelrpc.UseConfigurationResult_TypeID, s.Struct, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnmarshalUseConfigurationResult(s tunnelrpc.UseConfigurationResult) (*UseConfigurationResult, error) {
|
|
|
|
p := new(UseConfigurationResult)
|
|
|
|
err := pogs.Extract(p, tunnelrpc.UseConfigurationResult_TypeID, s.Struct)
|
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-17 14:23:05 +00:00
|
|
|
s.SetTunnelHostname(p.TunnelHostname)
|
2019-03-29 23:05:11 +00:00
|
|
|
switch config := p.Origin.(type) {
|
|
|
|
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-05-17 14:23:05 +00:00
|
|
|
p.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
|
|
|
|
}
|
|
|
|
p.Origin = config
|
|
|
|
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
|
|
|
|
}
|
|
|
|
p.Origin = config
|
|
|
|
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
|
|
|
|
}
|
|
|
|
p.Origin = config
|
|
|
|
}
|
|
|
|
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-05-30 20:45:46 +00:00
|
|
|
switch originAddr := p.URL.(type) {
|
|
|
|
case *HTTPURL:
|
|
|
|
ss, err := s.OriginAddr().NewHttp()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := MarshalHTTPURL(ss, originAddr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *UnixPath:
|
|
|
|
ss, err := s.OriginAddr().NewUnix()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := MarshalUnixPath(ss, originAddr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unknown type for OriginAddr: %T", originAddr)
|
|
|
|
}
|
|
|
|
s.SetTcpKeepAlive(p.TCPKeepAlive.Nanoseconds())
|
|
|
|
s.SetDialDualStack(p.DialDualStack)
|
|
|
|
s.SetTlsHandshakeTimeout(p.TLSHandshakeTimeout.Nanoseconds())
|
|
|
|
s.SetTlsVerify(p.TLSVerify)
|
|
|
|
s.SetOriginCAPool(p.OriginCAPool)
|
|
|
|
s.SetOriginServerName(p.OriginServerName)
|
|
|
|
s.SetMaxIdleConnections(p.MaxIdleConnections)
|
|
|
|
s.SetIdleConnectionTimeout(p.IdleConnectionTimeout.Nanoseconds())
|
|
|
|
s.SetProxyConnectionTimeout(p.ProxyConnectTimeout.Nanoseconds())
|
|
|
|
s.SetExpectContinueTimeout(p.ExpectContinueTimeout.Nanoseconds())
|
|
|
|
s.SetChunkedEncoding(p.ChunkedEncoding)
|
|
|
|
return nil
|
2019-03-29 23:05:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func UnmarshalHTTPOriginConfig(s tunnelrpc.HTTPOriginConfig) (*HTTPOriginConfig, error) {
|
|
|
|
p := new(HTTPOriginConfig)
|
2019-05-30 20:45:46 +00:00
|
|
|
switch s.OriginAddr().Which() {
|
|
|
|
case tunnelrpc.HTTPOriginConfig_originAddr_Which_http:
|
|
|
|
ss, err := s.OriginAddr().Http()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
originAddr, err := UnmarshalCapnpHTTPURL(ss)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.URL = originAddr
|
|
|
|
case tunnelrpc.HTTPOriginConfig_originAddr_Which_unix:
|
|
|
|
ss, err := s.OriginAddr().Unix()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
originAddr, err := UnmarshalUnixPath(ss)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.URL = originAddr
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unknown type for OriginAddr: %T", s.OriginAddr().Which())
|
|
|
|
}
|
|
|
|
p.TCPKeepAlive = time.Duration(s.TcpKeepAlive())
|
|
|
|
p.DialDualStack = s.DialDualStack()
|
|
|
|
p.TLSHandshakeTimeout = time.Duration(s.TlsHandshakeTimeout())
|
|
|
|
p.TLSVerify = s.TlsVerify()
|
|
|
|
originCAPool, err := s.OriginCAPool()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.OriginCAPool = originCAPool
|
|
|
|
originServerName, err := s.OriginServerName()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.OriginServerName = originServerName
|
|
|
|
p.MaxIdleConnections = s.MaxIdleConnections()
|
|
|
|
p.IdleConnectionTimeout = time.Duration(s.IdleConnectionTimeout())
|
|
|
|
p.ProxyConnectTimeout = time.Duration(s.ProxyConnectionTimeout())
|
|
|
|
p.ExpectContinueTimeout = time.Duration(s.ExpectContinueTimeout())
|
|
|
|
p.ChunkedEncoding = s.ChunkedEncoding()
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func MarshalHTTPURL(s tunnelrpc.CapnpHTTPURL, p *HTTPURL) error {
|
|
|
|
return pogs.Insert(tunnelrpc.CapnpHTTPURL_TypeID, s.Struct, p.capnpHTTPURL())
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnmarshalCapnpHTTPURL(s tunnelrpc.CapnpHTTPURL) (*HTTPURL, error) {
|
|
|
|
p := new(CapnpHTTPURL)
|
|
|
|
err := pogs.Extract(p, tunnelrpc.CapnpHTTPURL_TypeID, s.Struct)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
url, err := url.Parse(p.URL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &HTTPURL{
|
|
|
|
URL: url,
|
|
|
|
}, nil
|
2019-03-29 23:05:11 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 20:45:46 +00:00
|
|
|
func MarshalUnixPath(s tunnelrpc.UnixPath, p *UnixPath) error {
|
|
|
|
err := pogs.Insert(tunnelrpc.UnixPath_TypeID, s.Struct, p)
|
|
|
|
return err
|
2019-03-29 23:05:11 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 20:45:46 +00:00
|
|
|
func UnmarshalUnixPath(s tunnelrpc.UnixPath) (*UnixPath, error) {
|
|
|
|
p := new(UnixPath)
|
|
|
|
err := pogs.Extract(p, tunnelrpc.UnixPath_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)
|
|
|
|
}
|