-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
191 lines (161 loc) · 3.75 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
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
package main
import (
"context"
"crypto/rand"
"errors"
"fmt"
"log"
"math/big"
"net"
"os"
"os/signal"
"sync"
"time"
)
type Server struct {
ln net.Listener
quit chan struct{}
wg sync.WaitGroup
}
func NewServer(ln net.Listener) *Server {
return &Server{ln: ln, quit: make(chan struct{})}
}
func (s *Server) Serve() {
s.wg.Add(1)
go s.serve()
}
func Run(ctx context.Context) error {
ln, err := net.Listen("tcp4", ":1337")
if err != nil {
return err
}
s := NewServer(ln)
s.Serve()
fmt.Println("Listening on :1337")
<-ctx.Done()
s.Close()
return nil
}
func (s *Server) Close() {
close(s.quit)
s.ln.Close()
s.wg.Wait()
}
func (s *Server) serve() {
defer s.wg.Done()
for {
conn, err := s.ln.Accept()
if err != nil {
select {
case <-s.quit:
return
default:
log.Println("accept error", err)
}
} else {
s.wg.Add(1)
conn.SetDeadline(time.Now().Add(time.Second * 5))
go func() {
if err := s.handleConnection(conn); errors.Is(err, os.ErrDeadlineExceeded) {
conn.SetDeadline(time.Now().Add(time.Second * 3))
if _, err := fmt.Fprintln(conn, "Time's up! Please retry another time."); err != nil {
log.Println(err)
}
} else if err != nil {
log.Println(err)
}
conn.Close()
s.wg.Done()
}()
}
}
}
func generateBigInt() (*big.Int, error) {
// Max value, a 130-bits integer, i.e 2^130 - 1
var max *big.Int = big.NewInt(0).Exp(big.NewInt(2), big.NewInt(230), nil)
// Generate cryptographically strong pseudo-random between [0, max)
n, err := rand.Int(rand.Reader, max)
if err != nil {
return nil, err
}
return n, nil
}
func randomOperation() string {
ops := []string{"+", "-", "*"}
return ops[time.Now().UnixMicro()%3]
}
func (s *Server) handleConnection(c net.Conn) error {
_, err := fmt.Fprintln(c, `
$$\
\__|
$$$$$$\ $$\ $$\ $$\ $$$$$$$$\
$$ __$$\ $$ | $$ |$$ |\____$$ |
$$ / $$ |$$ | $$ |$$ | $$$$ _/
$$ | $$ |$$ | $$ |$$ | $$ _/
\$$$$$$$ |\$$$$$$ |$$ |$$$$$$$$\
\____$$ | \______/ \__|\________|
$$ |
$$ |
\__|
`)
if err != nil {
return err
}
_, err = fmt.Fprintln(c, `Hello wanderer! Welcome to the havce quiz.
You will be asked 50 questions on basic math.
You must answer in less than 5 seconds to win a juicy prize.
Let's start!`)
if err != nil {
return err
}
for i := 0; i < 50; i++ {
a, err := generateBigInt()
if err != nil {
fmt.Fprintln(c, "ooooopsie, there was an error generating your numbers. quitting.")
return err
}
b, err := generateBigInt()
if err != nil {
fmt.Fprintln(c, "ooooopsie, there was an error generating your numbers. quitting.")
return err
}
op := randomOperation()
var result *big.Int
switch op {
case "+":
result = big.NewInt(0).Add(a, b)
case "-":
result = big.NewInt(0).Sub(a, b)
case "*":
result = big.NewInt(0).Mul(a, b)
}
if _, err := fmt.Fprintf(c, "Please submit %v %s %v\n> ", a, op, b); err != nil {
return err
}
var user string
if _, err := fmt.Fscanln(c, &user); err != nil {
return err
}
i := big.NewInt(0)
i.SetString(user, 10)
if i.Cmp(result) != 0 {
fmt.Fprintf(c, "Nope, you lost! The correct answer was: %v. Please retry another time.\n", result)
return fmt.Errorf("user lost")
}
if _, err := fmt.Fprintln(c, "Good job!"); err != nil {
return err
}
}
flag := os.Getenv("FLAG")
if _, err := fmt.Fprintf(c, "You won! Congrats, here's your flag: %s.", flag); err != nil {
return err
}
return nil
}
func main() {
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt)
if err := Run(ctx); err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err)
os.Exit(1)
}
}