-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
handle_whois.go
42 lines (37 loc) · 893 Bytes
/
handle_whois.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
package main
import (
"fmt"
"log"
"net"
"net/http"
)
func (s *server) handleWhois() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ip := net.ParseIP(r.RemoteAddr)
if ip != nil {
w.Header().Set("Location", "/"+ip.String()+"/whois")
w.WriteHeader(http.StatusFound)
return
}
http.NotFound(w, r)
}
}
func (s *server) handleIPWhois() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if s.whois == nil {
log.Println("whois is nil")
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
ip := r.PathValue("ip")
// todo: check with regex
whoisRaw, err := s.whois.Query(ip)
if err != nil {
log.Println(err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/plain")
fmt.Fprint(w, whoisRaw)
}
}