-
Notifications
You must be signed in to change notification settings - Fork 13
/
cpanelgo.go
250 lines (218 loc) · 4.72 KB
/
cpanelgo.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package cpanelgo
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"strconv"
"strings"
)
const (
megabyte = 1 * 1024 * 1024
ErrorUnknown = "Unknown"
)
var (
ResponseSizeLimit = (20 * megabyte) + 1337
)
func init() {
if respSizeLimitOverride := os.Getenv("CPANELGO_RESPONSE_SIZE_LIMIT"); respSizeLimitOverride != "" {
conv, err := strconv.Atoi(respSizeLimitOverride)
if err != nil {
return
}
if conv >= (5*megabyte)+1337 {
ResponseSizeLimit = conv
}
}
}
type BaseResult struct {
ErrorString string `json:"error"`
}
func (r BaseResult) Error() error {
if r.ErrorString == "" {
return nil
}
return errors.New(r.ErrorString)
}
type UAPIResult struct {
BaseResult
Result json.RawMessage `json:"result"`
}
type API2Result struct {
BaseResult
Result json.RawMessage `json:"cpanelresult"`
}
type BaseUAPIResponse struct {
BaseResult
StatusCode int `json:"status"`
Errors []string `json:"errors"`
Messages []string `json:"messages"`
}
func (r BaseUAPIResponse) Error() error {
if r.StatusCode == 1 {
return nil
}
if err := r.BaseResult.Error(); err != nil {
return err
}
if len(r.Errors) == 0 {
return errors.New(ErrorUnknown)
}
return errors.New(strings.Join(r.Errors, "\n"))
}
func (r BaseUAPIResponse) Message() string {
if r.Messages == nil || len(r.Messages) == 0 {
return ""
}
return strings.Join(r.Messages, "\n")
}
type BaseAPI2Response struct {
BaseResult
Event struct {
Result int `json:"result"`
Reason string `json:"reason"`
} `json:"event"`
}
func (r BaseAPI2Response) Error() error {
if r.Event.Result == 1 {
return nil
}
err := r.BaseResult.Error()
if err != nil {
return err
}
if len(r.Event.Reason) == 0 {
return errors.New("Unknown")
}
return errors.New(r.Event.Reason)
}
type BaseAPI1Response struct {
// other stuff here "apiversion":"1","type":"event","module":"Serverinfo","func":"servicestatus","source":"module"
Data struct {
Result string `json:"result"`
} `json:"data"`
ErrorString string `json:"error"`
// "event":{"result":1,"reason":"blah blah"}}
Event struct {
Result int `json:"result"`
Reason string `json:"reason"`
} `json:"event"`
}
func (r BaseAPI1Response) Error() error {
if r.ErrorString != "" {
return errors.New(r.ErrorString)
}
if r.Event.Result != 1 {
// if the result != 1 the reason usually present in error ^ so kinda redundant to check, but check just in case
if len(r.Event.Reason) == 0 {
return errors.New("Unknown")
}
return errors.New(r.Event.Reason)
}
return nil
}
type Args map[string]interface{}
func (a Args) Values(apiVersion string) url.Values {
vals := url.Values{}
for k, v := range a {
if apiVersion == "1" {
kv := strings.SplitN(k, "=", 2)
if len(kv) == 1 {
vals.Add(kv[0], "")
} else if len(kv) == 2 {
vals.Add(kv[0], kv[1])
}
} else {
vals.Add(k, fmt.Sprintf("%v", v))
}
}
return vals
}
type ApiGateway interface {
UAPI(module, function string, arguments Args, out interface{}) error
API2(module, function string, arguments Args, out interface{}) error
API1(module, function string, arguments []string, out interface{}) error
Close() error
}
type Api struct {
Gateway ApiGateway
}
func NewApi(gw ApiGateway) Api {
return Api{
Gateway: gw,
}
}
func (a Api) Close() error {
if a.Gateway != nil {
return a.Gateway.Close()
} else {
return nil
}
}
type MaybeInt64 int64
func (m *MaybeInt64) MarshalJSON() ([]byte, error) {
return json.Marshal(int64(*m))
}
func (m *MaybeInt64) UnmarshalJSON(buf []byte) error {
var out interface{}
if err := json.Unmarshal(buf, &out); err != nil {
return err
}
switch v := out.(type) {
case string:
if len(v) == 0 {
*m = 0
break
}
f, err := strconv.ParseFloat(v, 64)
if err != nil {
return err
}
*m = MaybeInt64(f)
case float64:
*m = MaybeInt64(v)
case nil:
*m = 0
default:
return errors.New("Not a string or int64")
}
return nil
}
/*
"subject.commonName" : {
"commonName" : "mail.l33t.website"
},
or
"subject.commonName" : "mail.l33t.website",
*/
type MaybeCommonNameString string
func (m *MaybeCommonNameString) MarshalJSON() ([]byte, error) {
return json.Marshal(string(*m))
}
func (m *MaybeCommonNameString) UnmarshalJSON(buf []byte) error {
var out interface{}
if err := json.Unmarshal(buf, &out); err != nil {
return err
}
switch v := out.(type) {
case string:
*m = MaybeCommonNameString(v)
case map[string]interface{}:
vv, ok := v["commonName"]
if !ok {
*m = ""
return nil
}
vvv, ok := vv.(string)
if !ok {
return errors.New("subject.commonName: commonName entry not a string")
}
*m = MaybeCommonNameString(vvv)
case nil:
*m = ""
default:
return errors.New("subject.commonName: not a string or map[string]interface{}")
}
return nil
}