File size: 9,763 Bytes
ea775bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package api

import (
	"net/http"
	"sync/atomic"

	"github.com/Resinat/Resin/internal/config"
	"github.com/Resinat/Resin/internal/service"
)

type systemEnvConfigResponse struct {
	CacheDir                                        string          `json:"cache_dir"`
	StateDir                                        string          `json:"state_dir"`
	LogDir                                          string          `json:"log_dir"`
	ListenAddress                                   string          `json:"listen_address"`
	ResinPort                                       int             `json:"resin_port"`
	APIMaxBodyBytes                                 int             `json:"api_max_body_bytes"`
	MaxLatencyTableEntries                          int             `json:"max_latency_table_entries"`
	ProbeConcurrency                                int             `json:"probe_concurrency"`
	GeoIPUpdateSchedule                             string          `json:"geoip_update_schedule"`
	DefaultPlatformStickyTTL                        config.Duration `json:"default_platform_sticky_ttl"`
	DefaultPlatformRegexFilters                     []string        `json:"default_platform_regex_filters"`
	DefaultPlatformRegionFilters                    []string        `json:"default_platform_region_filters"`
	DefaultPlatformReverseProxyMissAction           string          `json:"default_platform_reverse_proxy_miss_action"`
	DefaultPlatformReverseProxyEmptyAccountBehavior string          `json:"default_platform_reverse_proxy_empty_account_behavior"`
	DefaultPlatformReverseProxyFixedAccountHeader   string          `json:"default_platform_reverse_proxy_fixed_account_header"`
	DefaultPlatformAllocationPolicy                 string          `json:"default_platform_allocation_policy"`
	ProbeTimeout                                    config.Duration `json:"probe_timeout"`
	ResourceFetchTimeout                            config.Duration `json:"resource_fetch_timeout"`
	NodeDNSUpstreams                                []string        `json:"node_dns_upstreams"`
	ProxyTransportMaxIdleConns                      int             `json:"proxy_transport_max_idle_conns"`
	ProxyTransportMaxIdleConnsPerHost               int             `json:"proxy_transport_max_idle_conns_per_host"`
	ProxyTransportIdleConnTimeout                   config.Duration `json:"proxy_transport_idle_conn_timeout"`
	ProxyBypassRules                                []string        `json:"proxy_bypass_rules"`
	RequestLogQueueSize                             int             `json:"request_log_queue_size"`
	RequestLogQueueFlushBatchSize                   int             `json:"request_log_queue_flush_batch_size"`
	RequestLogQueueFlushInterval                    config.Duration `json:"request_log_queue_flush_interval"`
	RequestLogDBMaxMB                               int             `json:"request_log_db_max_mb"`
	RequestLogDBRetainCount                         int             `json:"request_log_db_retain_count"`
	MetricThroughputIntervalSeconds                 int             `json:"metric_throughput_interval_seconds"`
	MetricThroughputRetentionSeconds                int             `json:"metric_throughput_retention_seconds"`
	MetricBucketSeconds                             int             `json:"metric_bucket_seconds"`
	MetricConnectionsIntervalSeconds                int             `json:"metric_connections_interval_seconds"`
	MetricConnectionsRetentionSeconds               int             `json:"metric_connections_retention_seconds"`
	MetricLeasesIntervalSeconds                     int             `json:"metric_leases_interval_seconds"`
	MetricLeasesRetentionSeconds                    int             `json:"metric_leases_retention_seconds"`
	MetricLatencyBinWidthMS                         int             `json:"metric_latency_bin_width_ms"`
	MetricLatencyBinOverflowMS                      int             `json:"metric_latency_bin_overflow_ms"`
	AdminTokenSet                                   bool            `json:"admin_token_set"`
	ProxyTokenSet                                   bool            `json:"proxy_token_set"`
	AdminTokenWeak                                  bool            `json:"admin_token_weak"`
	ProxyTokenWeak                                  bool            `json:"proxy_token_weak"`
	AuthVersion                                     string          `json:"auth_version"`
}

// HandleSystemInfo returns a handler for GET /api/v1/system/info.
func HandleSystemInfo(info service.SystemInfo) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		WriteJSON(w, http.StatusOK, info)
	}
}

// HandleSystemConfig returns a handler for GET /api/v1/system/config.
func HandleSystemConfig(runtimeCfg *atomic.Pointer[config.RuntimeConfig]) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		if runtimeCfg == nil {
			WriteJSON(w, http.StatusOK, nil)
			return
		}
		WriteJSON(w, http.StatusOK, runtimeCfg.Load())
	}
}

// HandleSystemDefaultConfig returns a handler for GET /api/v1/system/config/default.
func HandleSystemDefaultConfig() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		WriteJSON(w, http.StatusOK, config.NewDefaultRuntimeConfig())
	}
}

// HandleSystemEnvConfig returns a handler for GET /api/v1/system/config/env.
func HandleSystemEnvConfig(envCfg *config.EnvConfig) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		WriteJSON(w, http.StatusOK, systemEnvConfigSnapshot(envCfg))
	}
}

// HandlePatchSystemConfig returns a handler for PATCH /api/v1/system/config.
func HandlePatchSystemConfig(cp *service.ControlPlaneService) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		body, ok := readRawBodyOrWriteInvalid(w, r)
		if !ok {
			return
		}
		result, err := cp.PatchRuntimeConfig(body)
		if err != nil {
			writeServiceError(w, err)
			return
		}
		WriteJSON(w, http.StatusOK, result)
	}
}

func systemEnvConfigSnapshot(envCfg *config.EnvConfig) *systemEnvConfigResponse {
	if envCfg == nil {
		return nil
	}
	adminTokenSet := envCfg.AdminToken != ""
	proxyTokenSet := envCfg.ProxyToken != ""
	return &systemEnvConfigResponse{
		CacheDir:                              envCfg.CacheDir,
		StateDir:                              envCfg.StateDir,
		LogDir:                                envCfg.LogDir,
		ListenAddress:                         envCfg.ListenAddress,
		ResinPort:                             envCfg.ResinPort,
		APIMaxBodyBytes:                       envCfg.APIMaxBodyBytes,
		MaxLatencyTableEntries:                envCfg.MaxLatencyTableEntries,
		ProbeConcurrency:                      envCfg.ProbeConcurrency,
		GeoIPUpdateSchedule:                   envCfg.GeoIPUpdateSchedule,
		DefaultPlatformStickyTTL:              config.Duration(envCfg.DefaultPlatformStickyTTL),
		DefaultPlatformRegexFilters:           append([]string(nil), envCfg.DefaultPlatformRegexFilters...),
		DefaultPlatformRegionFilters:          append([]string(nil), envCfg.DefaultPlatformRegionFilters...),
		DefaultPlatformReverseProxyMissAction: envCfg.DefaultPlatformReverseProxyMissAction,
		DefaultPlatformReverseProxyEmptyAccountBehavior: envCfg.DefaultPlatformReverseProxyEmptyAccountBehavior,
		DefaultPlatformReverseProxyFixedAccountHeader:   envCfg.DefaultPlatformReverseProxyFixedAccountHeader,
		DefaultPlatformAllocationPolicy:                 envCfg.DefaultPlatformAllocationPolicy,
		ProbeTimeout:                                    config.Duration(envCfg.ProbeTimeout),
		ResourceFetchTimeout:                            config.Duration(envCfg.ResourceFetchTimeout),
		NodeDNSUpstreams:                                append([]string(nil), envCfg.NodeDNSUpstreams...),
		ProxyTransportMaxIdleConns:                      envCfg.ProxyTransportMaxIdleConns,
		ProxyTransportMaxIdleConnsPerHost:               envCfg.ProxyTransportMaxIdleConnsPerHost,
		ProxyTransportIdleConnTimeout:                   config.Duration(envCfg.ProxyTransportIdleConnTimeout),
		ProxyBypassRules:                                append([]string(nil), envCfg.ProxyBypassRules...),
		RequestLogQueueSize:                             envCfg.RequestLogQueueSize,
		RequestLogQueueFlushBatchSize:                   envCfg.RequestLogQueueFlushBatchSize,
		RequestLogQueueFlushInterval:                    config.Duration(envCfg.RequestLogQueueFlushInterval),
		RequestLogDBMaxMB:                               envCfg.RequestLogDBMaxMB,
		RequestLogDBRetainCount:                         envCfg.RequestLogDBRetainCount,
		MetricThroughputIntervalSeconds:                 envCfg.MetricThroughputIntervalSeconds,
		MetricThroughputRetentionSeconds:                envCfg.MetricThroughputRetentionSeconds,
		MetricBucketSeconds:                             envCfg.MetricBucketSeconds,
		MetricConnectionsIntervalSeconds:                envCfg.MetricConnectionsIntervalSeconds,
		MetricConnectionsRetentionSeconds:               envCfg.MetricConnectionsRetentionSeconds,
		MetricLeasesIntervalSeconds:                     envCfg.MetricLeasesIntervalSeconds,
		MetricLeasesRetentionSeconds:                    envCfg.MetricLeasesRetentionSeconds,
		MetricLatencyBinWidthMS:                         envCfg.MetricLatencyBinWidthMS,
		MetricLatencyBinOverflowMS:                      envCfg.MetricLatencyBinOverflowMS,
		AdminTokenSet:                                   adminTokenSet,
		ProxyTokenSet:                                   proxyTokenSet,
		AdminTokenWeak:                                  adminTokenSet && config.IsWeakToken(envCfg.AdminToken),
		ProxyTokenWeak:                                  proxyTokenSet && config.IsWeakToken(envCfg.ProxyToken),
		AuthVersion:                                     string(envCfg.AuthVersion),
	}
}