-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
195 lines (160 loc) · 5.18 KB
/
http.go
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
184
185
186
187
188
189
190
191
192
193
194
195
package directadmin
import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/goccy/go-json"
"github.com/spf13/cast"
)
type httpDebug struct {
Body string
Code int
Endpoint string
Start time.Time
}
// makeRequest is the underlying function for HTTP requests. It handles debugging statements, and simple error handling
func (c *UserContext) makeRequest(req *http.Request) ([]byte, error) {
debug := httpDebug{
Endpoint: req.URL.Path,
Start: time.Now(),
}
defer c.api.printDebugHTTP(&debug)
resp, err := c.api.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if c.api.debug {
debug.Code = resp.StatusCode
}
// exists solely for user session switching when logging in as a user under a reseller
for _, cookie := range resp.Cookies() {
if cookie.Name == "session" {
c.sessionID = cookie.Value
break
}
}
var responseBytes []byte
if resp.Body != nil {
responseBytes, err = io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %w", err)
}
if c.api.debug {
if len(responseBytes) > 32768 {
debug.Body = "body too long for debug: " + cast.ToString(len(responseBytes))
} else {
debug.Body = string(responseBytes)
}
}
}
if resp.StatusCode/100 != 2 {
return responseBytes, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return responseBytes, nil
}
// makeRequestNew supports DirectAdmin's new API
func (c *UserContext) makeRequestNew(method string, endpoint string, body any, object any) ([]byte, error) {
var bodyBytes []byte
if body != nil {
var err error
bodyBytes, err = json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("error serializing body: %w", err)
}
}
req, err := http.NewRequest(strings.ToUpper(method), c.api.url+"/api/"+endpoint, bytes.NewBuffer(bodyBytes))
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}
query := req.URL.Query()
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Referer", c.api.url)
req.URL.RawQuery = query.Encode()
if c.sessionID != "" {
req.AddCookie(&http.Cookie{Name: "session", Value: c.sessionID})
} else {
req.SetBasicAuth(c.credentials.username, c.credentials.passkey)
}
resp, err := c.makeRequest(req)
if err != nil {
return nil, fmt.Errorf("error making request: %w", err)
}
if resp != nil && object != nil {
if err = json.Unmarshal(resp, &object); err != nil {
return nil, fmt.Errorf("error unmarshalling response: %w", err)
}
}
return resp, nil
}
// makeRequestOld supports DirectAdmin's old API
func (c *UserContext) makeRequestOld(method string, endpoint string, body url.Values, object any) ([]byte, error) {
req, err := http.NewRequest(strings.ToUpper(method), c.api.url+"/CMD_"+endpoint, strings.NewReader(body.Encode()))
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}
query := req.URL.Query()
query.Add("json", "yes")
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Referer", c.api.url)
req.URL.RawQuery = query.Encode()
if c.sessionID != "" {
req.AddCookie(&http.Cookie{Name: "session", Value: c.sessionID})
} else {
req.SetBasicAuth(c.credentials.username, c.credentials.passkey)
}
var genericResponse apiGenericResponse
resp, err := c.makeRequest(req)
if err != nil {
jsonErr := json.Unmarshal(resp, &genericResponse)
if jsonErr != nil {
return nil, fmt.Errorf("error making request: %w", err)
}
return nil, errors.New(genericResponse.Error + ": " + genericResponse.Result)
}
if resp != nil {
if object != nil {
if err = json.Unmarshal(resp, &object); err != nil {
return nil, fmt.Errorf("error unmarshalling response: %w", err)
}
} else if err = json.Unmarshal(resp, &genericResponse); err == nil && genericResponse.Error != "" {
return nil, errors.New(genericResponse.Error + ": " + genericResponse.Result)
}
}
return resp, nil
}
// uploadFile functions for either the old or new DA API
func (c *UserContext) uploadFile(method string, endpoint string, data []byte, object any, contentType string) ([]byte, error) {
req, err := http.NewRequest(strings.ToUpper(method), c.api.url+endpoint, bytes.NewBuffer(data))
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}
query := req.URL.Query()
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", contentType)
req.Header.Set("Referer", c.api.url)
req.SetBasicAuth(c.credentials.username, c.credentials.passkey)
req.URL.RawQuery = query.Encode()
resp, err := c.makeRequest(req)
if err != nil {
return nil, fmt.Errorf("error making request: %w", err)
}
if resp != nil && len(resp) > 0 && object != nil {
if err = json.Unmarshal(resp, &object); err != nil {
return nil, fmt.Errorf("error unmarshalling response: %w", err)
}
}
return resp, nil
}
func (a *API) printDebugHTTP(debug *httpDebug) {
if a.debug {
fmt.Printf("\nENDPOINT: %v\nSTATUS CODE: %v\nTIME STARTED: %v\nTIME ENDED: %v\nTIME TAKEN: %v\nBODY: %v\n", debug.Endpoint, debug.Code, debug.Start, time.Now(), time.Since(debug.Start), debug.Body)
}
}