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
/
account.go
executable file
·93 lines (78 loc) · 2.32 KB
/
account.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
package main
import (
"crypto/sha512"
"database/sql"
"encoding/hex"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/logrusorgru/aurora/v3"
"log"
"net/http"
"strconv"
)
func initAccountDB() {
var err error
createAccountStmt, err = db.Prepare("INSERT IGNORE INTO `accounts` (`mlid`,`passwd`, `mlchkid` ) VALUES (?, ?, ?)")
if err != nil {
LogError("Unable to prepare account statement", err)
panic(err)
}
}
var createAccountStmt *sql.Stmt
func Account(w http.ResponseWriter, r *http.Request) {
var is string
// Check if we should use `=` for a Wii or
// `:` for the Homebrew patcher.
if r.URL.Path == "/cgi-bin/account.cgi" {
is = "="
} else {
is = ":"
}
wiiID := r.Form.Get("mlid")
if !friendCodeIsValid(wiiID) {
fmt.Fprint(w, GenAccountErrorCode(610, is, "Invalid Wii Friend Code."))
return
} else if wiiID == "" {
fmt.Fprint(w, GenNormalErrorCode(310, "Unable to parse parameters."))
return
}
w.Header().Add("Content-Type", "text/plain;charset=utf-8")
passwd := RandStringBytesMaskImprSrc(16)
passwdByte := sha512.Sum512(append(salt, []byte(passwd)...))
passwdHash := hex.EncodeToString(passwdByte[:])
mlchkid := RandStringBytesMaskImprSrc(32)
mlchkidByte := sha512.Sum512(append(salt, []byte(mlchkid)...))
mlchkidHash := hex.EncodeToString(mlchkidByte[:])
result, err := createAccountStmt.Exec(wiiID, passwdHash, mlchkidHash)
if err != nil {
fmt.Fprint(w, GenAccountErrorCode(410, is, "Database error."))
LogError("Unable to execute statement", err)
return
}
affected, err := result.RowsAffected()
if err != nil {
fmt.Fprint(w, GenAccountErrorCode(410, is, "Database error."))
LogError("Unable to get rows affected", err)
return
}
if affected == 0 {
fmt.Fprint(w, GenAccountErrorCode(211, is, "Duplicate registration."))
return
}
if global.Datadog {
err = dataDogClient.Incr("mail.accounts_registered", nil, 1)
if err != nil {
LogError("Unable to update accounts_registered.", err)
}
}
fmt.Fprint(w, GenSuccessResponseTyped(is),
"mlid", is, wiiID, "\n",
"passwd", is, passwd, "\n",
"mlchkid", is, mlchkid, "\n")
}
func GenAccountErrorCode(error int, is string, reason string) string {
log.Println(aurora.Red("[Warning]"), "Encountered error", error, "with reason", reason)
return fmt.Sprint(
"cd", is, strconv.Itoa(error), "\n",
"msg", is, reason, "\n")
}