-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddbotsrc.go
84 lines (68 loc) · 1.49 KB
/
addbotsrc.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
package main
import (
"fmt"
"log"
"net/http"
"encoding/base64"
"strings"
)
// global 'Database' of URL's
var uMap map[string]int
// number of times to return an url after each hit
var increment = 10
// number of urls to transmit in each retr
var sendcount = 20
func vers(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("1"))
}
func retr(w http.ResponseWriter, r *http.Request) {
if len(uMap) == 0 {
w.WriteHeader(http.StatusNoContent)
return
}
i := sendcount
for k,v := range uMap {
w.Write([]byte(k))
w.Write([]byte("\n"))
if (v == 1) {
delete(uMap,k)
fmt.Println(len(uMap))
} else {
uMap[k]=v-1
}
i--
if i==0 {
return
}
}
}
func push(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
// check if expected data is there...
if len(r.Header["U"]) == 0 {
return;
}
// U is a space sepperated list contaning the urls base64 encoded
// extract add add to uMap
data := r.Header["U"][0]
for _, f := range strings.Fields(data) {
if decoded, errD := base64.StdEncoding.DecodeString(f); errD == nil {
decodedStr := string(decoded)
uMap[decodedStr] = uMap[decodedStr] + increment
} else {
fmt.Print(errD)
}
}
w.WriteHeader(http.StatusNoContent)
fmt.Println(len(uMap))
} else {
w.WriteHeader(http.StatusNotFound)
}
}
func main() {
uMap = make(map[string]int)
http.HandleFunc("/v", vers)
http.HandleFunc("/p", push)
http.HandleFunc("/r", retr)
log.Fatal(http.ListenAndServe(":8080", nil))
}