-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
60 lines (48 loc) · 1.15 KB
/
server.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
package main
import (
"fmt"
"net/http"
"os/exec"
"github.com/gorilla/mux"
)
const (
addr = ":9091"
whoisRoute = "/whois/plain/{domain}"
)
// main runs server on fiven ip and port
func main() {
// setup routing
router := mux.NewRouter()
router.HandleFunc(whoisRoute, getWhoisPlain)
http.Handle("/", router)
err := http.ListenAndServe(addr, nil)
if err != nil {
fmt.Println("\n\n", err)
}
}
// getWhoisPlain outputs whois information by given domain name and port.
// Example: /whois/plain/google.com?port=9999
func getWhoisPlain(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
fmt.Fprintf(w, "Only GET requests are allowed")
return
}
vars := mux.Vars(r)
var err error
var out []byte
port := r.URL.Query().Get("port")
if port != "" {
out, err = exec.Command("whois", "-H", vars["domain"], "-p", port).Output()
} else {
out, err = exec.Command("whois", "-H", vars["domain"]).Output()
}
checkError(err, "Whois failed. Err:")
// send data to client side
fmt.Fprintf(w, "%s", string(out))
}
// checkError check if error and output it
func checkError(err error, mes string) {
if err != nil {
fmt.Println(mes, err)
}
}