mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-27 19:19:57 +00:00
TUN-1828: Update declarative tunnel config struct
This commit is contained in:
@@ -15,8 +15,8 @@ import (
|
||||
/// Structs
|
||||
///
|
||||
|
||||
type CloudflaredConfig struct {
|
||||
Timestamp time.Time
|
||||
type ClientConfig struct {
|
||||
Version uint64
|
||||
AutoUpdateFrequency time.Duration
|
||||
MetricsUpdateFrequency time.Duration
|
||||
HeartbeatInterval time.Duration
|
||||
@@ -24,6 +24,7 @@ type CloudflaredConfig struct {
|
||||
GracePeriod time.Duration
|
||||
DoHProxyConfigs []*DoHProxyConfig
|
||||
ReverseProxyConfigs []*ReverseProxyConfig
|
||||
NumHAConnections uint8
|
||||
}
|
||||
|
||||
type UseConfigurationResult struct {
|
||||
@@ -38,7 +39,7 @@ type DoHProxyConfig struct {
|
||||
}
|
||||
|
||||
type ReverseProxyConfig struct {
|
||||
TunnelID string
|
||||
TunnelHostname string
|
||||
Origin OriginConfig
|
||||
Retries uint64
|
||||
ConnectionTimeout time.Duration
|
||||
@@ -46,6 +47,27 @@ type ReverseProxyConfig struct {
|
||||
CompressionQuality uint64
|
||||
}
|
||||
|
||||
func NewReverseProxyConfig(
|
||||
tunnelHostname string,
|
||||
originConfig OriginConfig,
|
||||
retries uint64,
|
||||
connectionTimeout time.Duration,
|
||||
chunkedEncoding bool,
|
||||
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,
|
||||
ChunkedEncoding: chunkedEncoding,
|
||||
CompressionQuality: compressionQuality,
|
||||
}, nil
|
||||
}
|
||||
|
||||
//go-sumtype:decl OriginConfig
|
||||
type OriginConfig interface {
|
||||
isOriginConfig()
|
||||
@@ -81,29 +103,31 @@ type HelloWorldOriginConfig struct{}
|
||||
|
||||
func (_ *HelloWorldOriginConfig) isOriginConfig() {}
|
||||
|
||||
///
|
||||
/// Boilerplate to convert between these structs and the primitive structs generated by capnp-go
|
||||
///
|
||||
/*
|
||||
* 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 MarshalCloudflaredConfig(s tunnelrpc.CloudflaredConfig, p *CloudflaredConfig) error {
|
||||
s.SetTimestamp(p.Timestamp.UnixNano())
|
||||
func MarshalClientConfig(s tunnelrpc.ClientConfig, p *ClientConfig) error {
|
||||
s.SetVersion(p.Version)
|
||||
s.SetAutoUpdateFrequency(p.AutoUpdateFrequency.Nanoseconds())
|
||||
s.SetMetricsUpdateFrequency(p.MetricsUpdateFrequency.Nanoseconds())
|
||||
s.SetHeartbeatInterval(p.HeartbeatInterval.Nanoseconds())
|
||||
s.SetMaxFailedHeartbeats(p.MaxFailedHeartbeats)
|
||||
s.SetGracePeriod(p.GracePeriod.Nanoseconds())
|
||||
s.SetNumHAConnections(p.NumHAConnections)
|
||||
err := marshalDoHProxyConfigs(s, p.DoHProxyConfigs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = marshalReverseProxyConfigs(s, p.ReverseProxyConfigs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
return marshalReverseProxyConfigs(s, p.ReverseProxyConfigs)
|
||||
}
|
||||
|
||||
func marshalDoHProxyConfigs(s tunnelrpc.CloudflaredConfig, dohProxyConfigs []*DoHProxyConfig) error {
|
||||
func marshalDoHProxyConfigs(s tunnelrpc.ClientConfig, dohProxyConfigs []*DoHProxyConfig) error {
|
||||
capnpList, err := s.NewDohProxyConfigs(int32(len(dohProxyConfigs)))
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -117,7 +141,7 @@ func marshalDoHProxyConfigs(s tunnelrpc.CloudflaredConfig, dohProxyConfigs []*Do
|
||||
return nil
|
||||
}
|
||||
|
||||
func marshalReverseProxyConfigs(s tunnelrpc.CloudflaredConfig, reverseProxyConfigs []*ReverseProxyConfig) error {
|
||||
func marshalReverseProxyConfigs(s tunnelrpc.ClientConfig, reverseProxyConfigs []*ReverseProxyConfig) error {
|
||||
capnpList, err := s.NewReverseProxyConfigs(int32(len(reverseProxyConfigs)))
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -131,14 +155,15 @@ func marshalReverseProxyConfigs(s tunnelrpc.CloudflaredConfig, reverseProxyConfi
|
||||
return nil
|
||||
}
|
||||
|
||||
func UnmarshalCloudflaredConfig(s tunnelrpc.CloudflaredConfig) (*CloudflaredConfig, error) {
|
||||
p := new(CloudflaredConfig)
|
||||
p.Timestamp = time.Unix(0, s.Timestamp()).UTC()
|
||||
func UnmarshalClientConfig(s tunnelrpc.ClientConfig) (*ClientConfig, error) {
|
||||
p := new(ClientConfig)
|
||||
p.Version = s.Version()
|
||||
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())
|
||||
p.NumHAConnections = s.NumHAConnections()
|
||||
dohProxyConfigs, err := unmarshalDoHProxyConfigs(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -152,7 +177,7 @@ func UnmarshalCloudflaredConfig(s tunnelrpc.CloudflaredConfig) (*CloudflaredConf
|
||||
return p, err
|
||||
}
|
||||
|
||||
func unmarshalDoHProxyConfigs(s tunnelrpc.CloudflaredConfig) ([]*DoHProxyConfig, error) {
|
||||
func unmarshalDoHProxyConfigs(s tunnelrpc.ClientConfig) ([]*DoHProxyConfig, error) {
|
||||
var result []*DoHProxyConfig
|
||||
marshalledDoHProxyConfigs, err := s.DohProxyConfigs()
|
||||
if err != nil {
|
||||
@@ -169,7 +194,7 @@ func unmarshalDoHProxyConfigs(s tunnelrpc.CloudflaredConfig) ([]*DoHProxyConfig,
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func unmarshalReverseProxyConfigs(s tunnelrpc.CloudflaredConfig) ([]*ReverseProxyConfig, error) {
|
||||
func unmarshalReverseProxyConfigs(s tunnelrpc.ClientConfig) ([]*ReverseProxyConfig, error) {
|
||||
var result []*ReverseProxyConfig
|
||||
marshalledReverseProxyConfigs, err := s.ReverseProxyConfigs()
|
||||
if err != nil {
|
||||
@@ -207,7 +232,7 @@ func UnmarshalDoHProxyConfig(s tunnelrpc.DoHProxyConfig) (*DoHProxyConfig, error
|
||||
}
|
||||
|
||||
func MarshalReverseProxyConfig(s tunnelrpc.ReverseProxyConfig, p *ReverseProxyConfig) error {
|
||||
s.SetTunnelID(p.TunnelID)
|
||||
s.SetTunnelHostname(p.TunnelHostname)
|
||||
switch config := p.Origin.(type) {
|
||||
case *HTTPOriginConfig:
|
||||
ss, err := s.Origin().NewHttp()
|
||||
@@ -245,11 +270,11 @@ func MarshalReverseProxyConfig(s tunnelrpc.ReverseProxyConfig, p *ReverseProxyCo
|
||||
|
||||
func UnmarshalReverseProxyConfig(s tunnelrpc.ReverseProxyConfig) (*ReverseProxyConfig, error) {
|
||||
p := new(ReverseProxyConfig)
|
||||
tunnelID, err := s.TunnelID()
|
||||
tunnelHostname, err := s.TunnelHostname()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.TunnelID = tunnelID
|
||||
p.TunnelHostname = tunnelHostname
|
||||
switch s.Origin().Which() {
|
||||
case tunnelrpc.ReverseProxyConfig_origin_Which_http:
|
||||
ss, err := s.Origin().Http()
|
||||
@@ -339,31 +364,30 @@ func UnmarshalHelloWorldOriginConfig(s tunnelrpc.HelloWorldOriginConfig) (*Hello
|
||||
return p, err
|
||||
}
|
||||
|
||||
type CloudflaredServer interface {
|
||||
UseConfiguration(ctx context.Context, config *CloudflaredConfig) (*CloudflaredConfig, error)
|
||||
GetConfiguration(ctx context.Context) (*CloudflaredConfig, error)
|
||||
type ClientService interface {
|
||||
UseConfiguration(ctx context.Context, config *ClientConfig) (*ClientConfig, error)
|
||||
}
|
||||
|
||||
type CloudflaredServer_PogsClient struct {
|
||||
type ClientService_PogsClient struct {
|
||||
Client capnp.Client
|
||||
Conn *rpc.Conn
|
||||
}
|
||||
|
||||
func (c *CloudflaredServer_PogsClient) Close() error {
|
||||
func (c *ClientService_PogsClient) Close() error {
|
||||
return c.Conn.Close()
|
||||
}
|
||||
|
||||
func (c *CloudflaredServer_PogsClient) UseConfiguration(
|
||||
func (c *ClientService_PogsClient) UseConfiguration(
|
||||
ctx context.Context,
|
||||
config *CloudflaredConfig,
|
||||
config *ClientConfig,
|
||||
) (*UseConfigurationResult, error) {
|
||||
client := tunnelrpc.CloudflaredServer{Client: c.Client}
|
||||
promise := client.UseConfiguration(ctx, func(p tunnelrpc.CloudflaredServer_useConfiguration_Params) error {
|
||||
cloudflaredConfig, err := p.NewCloudflaredConfig()
|
||||
client := tunnelrpc.ClientService{Client: c.Client}
|
||||
promise := client.UseConfiguration(ctx, func(p tunnelrpc.ClientService_useConfiguration_Params) error {
|
||||
clientServiceConfig, err := p.NewClientConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return MarshalCloudflaredConfig(cloudflaredConfig, config)
|
||||
return MarshalClientConfig(clientServiceConfig, config)
|
||||
})
|
||||
retval, err := promise.Result().Struct()
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user