-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
220 lines (185 loc) · 5.15 KB
/
utils.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
package main
import (
"DemaeDominos/dominos"
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"github.com/WiiLink24/nwc24"
"github.com/getsentry/sentry-go"
"github.com/logrusorgru/aurora/v4"
"log"
"net/http"
"os"
"strconv"
)
func NewResponse(r *http.Request, w *http.ResponseWriter, xmlType XMLType) *Response {
wiiNumber, err := strconv.ParseUint(r.Header.Get("X-WiiNo"), 10, 64)
if err != nil {
// Failed to parse Wii Number or invalid integer
(*w).WriteHeader(http.StatusBadRequest)
return nil
}
number := nwc24.LoadWiiNumber(wiiNumber)
if !number.CheckWiiNumber() {
// Bad Wii Number
(*w).WriteHeader(http.StatusBadRequest)
return nil
}
return &Response{
ResponseFields: KVFieldWChildren{
XMLName: xml.Name{Local: "response"},
Value: nil,
},
wiiNumber: number,
request: r,
writer: w,
isMultipleRootNodes: xmlType == 1,
}
}
func (r *Response) GetHollywoodId() string {
return strconv.Itoa(int(r.wiiNumber.GetHollywoodID()))
}
// AddCustomType adds a given key by name to a specified structure.
func (r *Response) AddCustomType(customType any) {
k, ok := r.ResponseFields.(KVFieldWChildren)
if ok {
k.Value = append(k.Value, customType)
r.ResponseFields = k
return
}
// Now check if the fields is an array of any.
array, ok := r.ResponseFields.([]any)
if ok {
r.ResponseFields = append(r.ResponseFields.([]any), array)
}
}
// AddKVNode adds a given key by name to a specified value, such as <key>value</key>.
func (r *Response) AddKVNode(key string, value string) {
k, ok := r.ResponseFields.(KVFieldWChildren)
if !ok {
return
}
k.Value = append(k.Value, KVField{
XMLName: xml.Name{Local: key},
Value: value,
})
r.ResponseFields = k
}
// AddKVWChildNode adds a given key by name to a specified value, such as <key><child>...</child></key>.
func (r *Response) AddKVWChildNode(key string, value any) {
k, ok := r.ResponseFields.(KVFieldWChildren)
if !ok {
return
}
k.Value = append(k.Value, KVFieldWChildren{
XMLName: xml.Name{Local: key},
Value: []any{value},
})
r.ResponseFields = k
}
func (r *Response) toXML() (string, error) {
var contents string
if r.isMultipleRootNodes {
var temp []byte
var err error
array, ok := r.ResponseFields.([]any)
if ok {
for _, a := range array {
temp, err = xml.MarshalIndent(a, "", " ")
if err != nil {
return "", err
}
contents += string(temp) + "\n"
}
} else {
temp, err = xml.MarshalIndent(r.ResponseFields, "", " ")
if err != nil {
return "", err
}
contents += string(temp) + "\n"
}
// Now the version and API tags
version, apiStatus := GenerateVersionAndAPIStatus()
temp, err = xml.MarshalIndent(version, "", " ")
if err != nil {
return "", err
}
contents += string(temp) + "\n"
temp, err = xml.MarshalIndent(apiStatus, "", " ")
if err != nil {
return "", err
}
contents += string(temp)
} else {
version, apiStatus := GenerateVersionAndAPIStatus()
r.AddCustomType(version)
r.AddCustomType(apiStatus)
temp, err := xml.MarshalIndent(r.ResponseFields, "", " ")
if err != nil {
return "", err
}
contents += string(temp)
}
return contents, nil
}
func GenerateVersionAndAPIStatus() (*KVField, *KVFieldWChildren) {
version := KVField{
XMLName: xml.Name{Local: "version"},
Value: "1",
}
apiStatus := KVFieldWChildren{
XMLName: xml.Name{Local: "apiStatus"},
Value: []any{
KVField{
XMLName: xml.Name{Local: "code"},
Value: "0",
},
},
}
return &version, &apiStatus
}
// BoolToInt converts a boolean value to an integer.
// This is needed because Nintendo wants the integer, not the string literal.
func BoolToInt(b bool) int {
if b {
return 1
}
return 0
}
func PostDiscordWebhook(title, message, url string, color int) {
theMap := map[string]any{
"content": nil,
"embeds": []map[string]any{
{
"title": title,
"description": message,
"color": color,
},
},
}
jsonData, _ := json.Marshal(theMap)
_, _ = http.Post(url, "application/json", bytes.NewBuffer(jsonData))
}
// ReportError helps make errors nicer. First it logs the error to Sentry,
// then writes a response for the server to send.
func (r *Response) ReportError(err error) {
if !errors.Is(err, dominos.InvalidCountry) && r.dominos != nil {
// Write the JSON Dominos sent us to the system.
_ = os.WriteFile(fmt.Sprintf("errors/%s_%s.json", r.request.URL.Path, r.request.Header.Get("X-WiiNo")), r.dominos.GetResponse(), 0664)
}
sentry.WithScope(func(s *sentry.Scope) {
s.SetTag("Wii ID", r.GetHollywoodId())
sentry.CaptureException(err)
})
log.Printf("An error has occurred: %s", aurora.Red(err.Error()))
errorString := fmt.Sprintf("%s\nWii ID: %s\nWii Number: %s", err.Error(), r.GetHollywoodId(), r.request.Header.Get("X-WiiNo"))
PostDiscordWebhook("An error has occurred in Demae Domino's!", errorString, config.ErrorWebhook, 16711711)
// With the new patches I created, we can now send the error to the channel.
r.AddKVNode("error", err.Error())
}
func printError(w http.ResponseWriter, reason string, code int) {
http.Error(w, reason, code)
log.Print("Failed to handle request: ", aurora.Red(reason))
}