-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
106 lines (87 loc) · 2.06 KB
/
main.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 (
"ctf_cup/models"
"encoding/json"
"flag"
"github.com/golang/glog"
"github.com/google/uuid"
"io"
"net/http"
"strings"
"time"
)
const register = "http://localhost:8080/register"
const putNotePath = "http://localhost:8080/putNote"
const getNotePath = "http://localhost:8080/getNote"
const contentType = "application/json"
func main() {
flag.Parse()
newUser := models.Credentials{
Login: uuid.New().String(),
Password: uuid.New().String(),
}
existUser := models.Credentials{
Login: uuid.New().String(),
Password: uuid.New().String(),
}
RegisterUser(existUser)
registered := make(chan bool)
go func() {
for {
select {
case <-registered:
return
default:
getNote(newUser, "secretNote")
}
}
}()
go func() {
for range time.Tick(500 * time.Millisecond) {
putNote(existUser, uuid.New().String(), "another shitty day gone")
}
}()
RegisterUser(newUser)
registered <- true
}
func RegisterUser(creds models.Credentials) {
credsStr, _ := json.Marshal(creds)
post, err := http.Post(register, contentType, strings.NewReader(string(credsStr)))
if err != nil {
glog.Fatal(err.Error())
return
}
all, _ := io.ReadAll(post.Body)
glog.Infof("%d : %s", post.StatusCode, string(all))
}
func putNote(creds models.Credentials, name string, text string) {
req := models.PutNoteRequest{
Credentials: creds,
Note: models.Note{
Name: name,
Text: text,
},
}
reqStr, _ := json.Marshal(req)
post, err := http.Post(putNotePath, contentType, strings.NewReader(string(reqStr)))
if err != nil {
glog.Fatal(err.Error())
return
}
all, _ := io.ReadAll(post.Body)
glog.Infof("%d : %s", post.StatusCode, string(all))
}
func getNote(creds models.Credentials, name string) {
req := models.GetNoteRequest{
Credentials: creds,
Name: name,
}
reqStr, _ := json.Marshal(req)
post, err := http.Post(getNotePath, contentType, strings.NewReader(string(reqStr)))
if err != nil {
glog.Fatal(err.Error())
return
}
all, _ := io.ReadAll(post.Body)
glog.Infof("%d : %s", post.StatusCode, string(all))
}