This repository has been archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (107 loc) · 3 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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"math/rand"
"net"
"net/http"
"strconv"
"time"
"github.com/HackDalton/homemade-http/router"
"github.com/mbndr/figlet4go"
)
var asciiRenderer *figlet4go.AsciiRender
var asciiOptions *figlet4go.RenderOptions
var rtr router.Router
func main() {
rand.Seed(time.Now().UTC().UnixNano())
asciiRenderer = figlet4go.NewAsciiRender()
asciiOptions = figlet4go.NewRenderOptions()
asciiOptions.FontColor = []figlet4go.Color{
figlet4go.ColorCyan,
figlet4go.ColorRed,
}
rtr = router.CreateRouter()
rtr.AddRoute(router.POST("/login", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("content-type", "text/plain")
if req.FormValue("username") == "" || req.FormValue("password") == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Missing parameters."))
return
} else if req.FormValue("username") != "admin" || req.FormValue("password") != "secret" {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Invalid login."))
return
}
flag, err := ioutil.ReadFile("./flag.txt")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Header().Add("content-type", "text/plain")
w.Write([]byte("Couldn't read flag.txt."))
fmt.Println(err)
return
}
w.WriteHeader(http.StatusOK)
w.Write(flag)
})))
rtr.AddRoute(router.GET("/", http.FileServer(http.Dir("./static"))))
ln, err := net.Listen("tcp", ":8080")
if err != nil {
panic(err)
}
for {
conn, err := ln.Accept()
if err != nil {
fmt.Println(err)
}
go handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
captcha1 := rand.Intn(10)
captcha2 := rand.Intn(10)
captchaAnswer := captcha1 + captcha2
captcha, _ := asciiRenderer.RenderOpts(strconv.Itoa(captcha1)+"+"+strconv.Itoa(captcha2), asciiOptions)
conn.Write([]byte("Solve this captcha to connect:\n"))
conn.Write([]byte(captcha))
buff := bufio.NewReader(conn)
captchaAnswerBytes, _, err := buff.ReadLine()
if err != nil {
conn.Write([]byte("An error occured reading your response\n"))
conn.Close()
return
}
submittedAnswer, err := strconv.Atoi(string(captchaAnswerBytes))
if err != nil {
conn.Write([]byte("You must submit a positive integer as your captcha answer.\n"))
conn.Close()
return
}
if submittedAnswer != captchaAnswer {
conn.Write([]byte("That answer was not correct.\n"))
conn.Close()
return
}
conn.Write([]byte("Now accepting HTTP/1.1 requests.\n\n"))
for true {
req, err := http.ReadRequest(buff)
if err != nil {
conn.Write([]byte("An error occured processing your request:\n"))
conn.Write([]byte(err.Error()))
conn.Close()
return
} else if req.ProtoAtLeast(2, 0) {
conn.Write([]byte("This server only supports HTTP/1.1 and below."))
conn.Close()
return
} else if req.Host != "problems.hackdalton.com" {
conn.Write([]byte("This server can only serve connections to problems.hackdalton.com."))
conn.Close()
return
}
resp := rtr.Handle(*req)
resp.Write(conn)
conn.Write([]byte("\n\n"))
}
}