-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.go
More file actions
183 lines (158 loc) · 4.39 KB
/
client.go
File metadata and controls
183 lines (158 loc) · 4.39 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package csclient
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
// Client is the Cobalt Strike API client
type Client struct {
baseURL string
httpClient *http.Client
token string
maxRetries int
retryDelay time.Duration
}
// NewClient creates a new Cobalt Strike API client
func NewClient(host string, port int) *Client {
return &Client{
baseURL: fmt.Sprintf("https://%s:%d", host, port),
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
maxRetries: 3,
retryDelay: 2 * time.Second,
}
}
// SetHTTPClient allows setting a custom HTTP client (e.g., for custom TLS config)
func (c *Client) SetHTTPClient(client *http.Client) {
c.httpClient = client
}
// SetRetryPolicy sets the retry policy for failed requests
func (c *Client) SetRetryPolicy(maxRetries int, retryDelay time.Duration) {
c.maxRetries = maxRetries
c.retryDelay = retryDelay
}
// Login authenticates with the Cobalt Strike server
func (c *Client) Login(ctx context.Context, username, password string, durationMs int) (*AuthDto, error) {
req := LoginRequest{
Username: username,
Password: password,
DurationMs: durationMs,
}
var auth AuthDto
if err := c.doRequest(ctx, "POST", "/api/auth/login", req, &auth, false); err != nil {
return nil, fmt.Errorf("login failed: %w", err)
}
c.token = auth.AccessToken
return &auth, nil
}
// doRequest performs an HTTP request with retry logic
func (c *Client) doRequest(ctx context.Context, method, path string, body interface{}, result interface{}, requireAuth bool) error {
var lastErr error
for attempt := 0; attempt <= c.maxRetries; attempt++ {
if attempt > 0 {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(c.retryDelay):
}
}
err := c.doRequestOnce(ctx, method, path, body, result, requireAuth)
if err == nil {
return nil
}
lastErr = err
// Don't retry on certain errors
if isNonRetryableError(err) {
return lastErr
}
// Don't retry if context is cancelled
if ctx.Err() != nil {
return ctx.Err()
}
}
return fmt.Errorf("request failed after %d attempts: %w", c.maxRetries+1, lastErr)
}
// doRequestOnce performs a single HTTP request
func (c *Client) doRequestOnce(ctx context.Context, method, path string, body interface{}, result interface{}, requireAuth bool) error {
var reqBody io.Reader
if body != nil {
jsonData, err := json.Marshal(body)
if err != nil {
return &APIError{
StatusCode: 0,
Message: fmt.Sprintf("failed to marshal request: %v", err),
Retryable: false,
}
}
reqBody = bytes.NewBuffer(jsonData)
}
req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, reqBody)
if err != nil {
return &APIError{
StatusCode: 0,
Message: fmt.Sprintf("failed to create request: %v", err),
Retryable: false,
}
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
if requireAuth && c.token != "" {
req.Header.Set("Authorization", "Bearer "+c.token)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return &APIError{
StatusCode: 0,
Message: fmt.Sprintf("request failed: %v", err),
Retryable: true,
}
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return &APIError{
StatusCode: resp.StatusCode,
Message: fmt.Sprintf("failed to read response: %v", err),
Retryable: true,
}
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
retryable := resp.StatusCode >= 500 || resp.StatusCode == 429 // Retry on server errors and rate limits
msg := string(respBody)
if msg == "" {
msg = fmt.Sprintf("HTTP %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
return &APIError{
StatusCode: resp.StatusCode,
Message: msg,
Retryable: retryable,
}
}
if result != nil && len(respBody) > 0 {
// If the caller passed a *string, store the raw response body as-is
// (some API endpoints return plain text, not JSON)
if strPtr, ok := result.(*string); ok {
*strPtr = string(respBody)
} else if err := json.Unmarshal(respBody, result); err != nil {
return &APIError{
StatusCode: resp.StatusCode,
Message: fmt.Sprintf("failed to unmarshal response: %v", err),
Retryable: false,
}
}
}
return nil
}
// isNonRetryableError checks if an error should not be retried
func isNonRetryableError(err error) bool {
if apiErr, ok := err.(*APIError); ok {
return !apiErr.Retryable
}
return false
}