This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.go
executable file
·118 lines (100 loc) · 3.44 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
package main
import (
"fmt"
"github.com/RiiConnect24/wiino/golang"
"github.com/getsentry/sentry-go"
_ "github.com/go-sql-driver/mysql"
"github.com/logrusorgru/aurora/v3"
"log"
"math/rand"
"regexp"
"strconv"
"time"
)
// https://stackoverflow.com/a/31832326/3874884
var src = rand.NewSource(time.Now().UnixNano())
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
var mailRegex = regexp.MustCompile(`w\d{16}`)
// RandStringBytesMaskImprSrc makes a random string with the specified size.
func RandStringBytesMaskImprSrc(n int) string {
b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}
// GenMailErrorCode formulates a proper response needed for mail-specific errors.
func GenMailErrorCode(mailNumber string, error int, reason string) string {
if error != 100 {
log.Println(aurora.Red("[Warning]"), "Encountered error", error, "with reason", reason)
}
return fmt.Sprint(
"cd", mailNumber[1:], "=", strconv.Itoa(error), "\n",
"msg", mailNumber[1:], "=", reason, "\n")
}
// GenNormalErrorCode formulates a proper response for overall errors.
func GenNormalErrorCode(error int, reason string) string {
switch error {
case 220:
break
default:
log.Println(aurora.Red("[Warning]"), "Encountered error", error, "with reason", reason)
}
return fmt.Sprint(
"cd=", strconv.Itoa(error), "\n",
"msg=", reason, "\n")
}
// GenSuccessResponse returns a successful message, using = as the divider between characters.
func GenSuccessResponse() string {
return GenSuccessResponseTyped("=")
}
// GenSuccessResponseTyped returns a successful message, using the specified character as a divider.
func GenSuccessResponseTyped(divider string) string {
return fmt.Sprint(
"cd", divider, "100\n",
"msg", divider, "Success.\n")
}
// friendCodeIsValid determines if a friend code is valid by
// checking not empty, is 17 in length, and starts with w.
// It then checks the numerical validity of the friend code.
func friendCodeIsValid(friendCode string) bool {
// An empty or invalid length mlid is automatically false.
if friendCode == "" || len(friendCode) != 17 {
return false
}
// Ensure the provided mlid is the correct format.
if !mailRegex.MatchString(friendCode) {
return false
}
// We verified previously that the last 16 characters are digits. This should not fail.
// However, should it, we do not want to hint to the user any error occurred and return false.
wiiId, err := strconv.Atoi(friendCode[1:])
if err != nil {
return false
}
return wiino.NWC24CheckUserID(uint64(wiiId)) == 0
}
// GenerateBoundary returns a string with the format Nintendo used for boundaries.
func GenerateBoundary() string {
return fmt.Sprint(time.Now().Format("200601021504"), "/", random(1000000, 9999999))
}
func LogError(reason string, err error) {
// Log to console
log.Printf("%s: %v", reason, err)
// and if it's available, Sentry.
sentry.CaptureException(err)
}