Skip to content

Commit d3b0581

Browse files
Custom headers (#61)
* Add: a CustomHeaders field of type map[string]string to RemoteQueryConfiguration for custom headers in remote requests. * Add: custom header processing * Add: custom header configuration to the remote client * WithCustomHeader -> WithCustomHeaders * use criticalHeaders --------- Co-authored-by: shiro8613 <psp1070@gmail.com>
1 parent a4de1af commit d3b0581

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ func rootCmdRun(cmd *cobra.Command, _ []string) {
153153
pclient := remote.New(
154154
config.Get().PanelLocation,
155155
remote.WithCredentials(t.ID, t.Token),
156+
remote.WithCustomHeaders(config.Get().RemoteQuery.CustomHeaders),
156157
remote.WithHttpClient(&http.Client{
157158
Timeout: time.Second * time.Duration(config.Get().RemoteQuery.Timeout),
158159
}),

config/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ type RemoteQueryConfiguration struct {
126126
// 50 servers is likely just as quick as two for 100 or one for 400, and will certainly
127127
// be less likely to cause performance issues on the Panel.
128128
BootServersPerPage int `default:"50" yaml:"boot_servers_per_page"`
129+
130+
//When using services like Cloudflare Access to manage access to
131+
//a specific system via an external authentication system,
132+
//it is possible to add special headers to bypass authentication.
133+
//The mentioned headers can be appended to queries sent from Wings to the panel.
134+
CustomHeaders map[string]string `yaml:"custom_headers"`
129135
}
130136

131137
// SystemConfiguration defines basic system configuration settings.

remote/http.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type client struct {
4242
tokenId string
4343
token string
4444
maxAttempts int
45+
customHeaders map[string]string
4546
}
4647

4748
// New returns a new HTTP request client that is used for making authenticated
@@ -69,6 +70,13 @@ func WithCredentials(id, token string) ClientOption {
6970
}
7071
}
7172

73+
// WithCustomHeaders sets custom headers to be used when making remote requests.
74+
func WithCustomHeaders(headers map[string]string) ClientOption {
75+
return func(c *client) {
76+
c.customHeaders = headers
77+
}
78+
}
79+
7280
// WithHttpClient sets the underlying HTTP client instance to use when making
7381
// requests to the Panel API.
7482
func WithHttpClient(httpClient *http.Client) ClientOption {
@@ -110,6 +118,19 @@ func (c *client) requestOnce(ctx context.Context, method, path string, body io.R
110118
req.Header.Set("Accept", "application/json")
111119
req.Header.Set("Content-Type", "application/json")
112120
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s.%s", c.tokenId, c.token))
121+
122+
// Apply custom headers, but prevent overriding critical headers
123+
criticalHeaders := map[string]bool{
124+
"Authorization": true,
125+
"User-Agent": true,
126+
"Accept": true,
127+
"Content-Type": true,
128+
}
129+
for key, value := range c.customHeaders {
130+
if !criticalHeaders[key] {
131+
req.Header.Set(key, value)
132+
}
133+
}
113134

114135
// Call all opts functions to allow modifying the request
115136
for _, o := range opts {

0 commit comments

Comments
 (0)