/
main.go
181 lines (152 loc) · 4.12 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
package main
import (
"context"
"encoding/binary"
"encoding/hex"
"errors"
"github.com/cockroachdb/pebble"
"log"
"math/rand"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
)
const dbPath = "/var/lib/hibp/pebble"
var db *pebble.DB
func main() {
var err error
db, err = pebble.Open(dbPath, &pebble.Options{})
if err != nil {
log.Fatalf("Can't open database %v", err)
}
defer db.Close()
mux := http.NewServeMux()
mux.HandleFunc("/", hipbPasswordFunc)
server := &http.Server{
Addr: ":8080",
Handler: mux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
}
go func() {
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
s := <-quit
log.Printf("Caught signal: %s\n", s.String())
log.Println("Shutting down")
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
err := server.Shutdown(ctx)
if err != nil {
log.Fatalf("Shutting down server failed %v", err)
}
}()
err = server.ListenAndServe()
if !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("Shutting down server failed %v", err)
}
}
func hipbPasswordFunc(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method not supported", http.StatusInternalServerError)
return
}
if !strings.HasPrefix(r.URL.Path, "/range/") {
http.Error(w, "invalid query", http.StatusNotFound)
return
}
hashPrefix := strings.TrimPrefix(r.URL.Path, "/range/")
if !isValidHashPrefix(hashPrefix) {
http.Error(w, "The hash prefix was not in a valid format", http.StatusBadRequest)
return
}
hashPrefix = strings.ToUpper(hashPrefix)
lower, err := hex.DecodeString(hashPrefix + "00000000000000000000000000000000000")
if err != nil {
http.Error(w, "decoding failed", http.StatusInternalServerError)
return
}
upper, err := hex.DecodeString(hashPrefix + "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
if err != nil {
http.Error(w, "decoding failed", http.StatusInternalServerError)
return
}
iter := db.NewIter(&pebble.IterOptions{
LowerBound: lower,
UpperBound: upper,
})
counter := 0
for iter.First(); iter.Valid(); iter.Next() {
key := iter.Key()
value, err := iter.ValueAndErr()
if err != nil {
http.Error(w, "getting value failed", http.StatusInternalServerError)
}
if writeResult(w, key, value, hashPrefix) {
return
}
counter++
}
// upper bound is not included in the iterator (exclusive)
appearedBytes, closer, err := db.Get(upper)
if err != nil && err != pebble.ErrNotFound {
http.Error(w, "getting upper bound value failed", http.StatusInternalServerError)
return
}
if err == nil {
defer closer.Close()
writeResult(w, upper, appearedBytes, hashPrefix)
}
// add-padding header
addPaddingHeader := strings.ToLower(r.Header.Get("add-padding"))
if addPaddingHeader == "true" {
minimum := 800 - counter
random := rand.Intn(200)
for i := 0; i < minimum+random; i++ {
rh, err := randomHex(18)
if err != nil {
http.Error(w, "randomHex failed", http.StatusInternalServerError)
return
}
_, err = w.Write([]byte(rh[:35] + ":0\n"))
if err != nil {
http.Error(w, "writing response failed", http.StatusInternalServerError)
return
}
}
}
}
func writeResult(w http.ResponseWriter, key, value []byte, hashPrefix string) bool {
hashStr := strings.TrimPrefix(strings.ToUpper(hex.EncodeToString(key)), hashPrefix)
appeared, _ := binary.Uvarint(value)
response := hashStr + ":" + strconv.FormatUint(appeared, 10) + "\n"
_, err := w.Write([]byte(response))
if err != nil {
http.Error(w, "writing response failed", http.StatusInternalServerError)
return true
}
return false
}
func isValidHashPrefix(hashPrefix string) bool {
if len(hashPrefix) != 5 {
return false
}
for _, c := range hashPrefix {
if !(c >= '0' && c <= '9') && !(c >= 'A' && c <= 'F') && !(c >= 'a' && c <= 'f') {
return false
}
}
return true
}
func randomHex(n int) (string, error) {
bytes := make([]byte, n)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return strings.ToUpper(hex.EncodeToString(bytes)), nil
}