-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_ping.go
executable file
·58 lines (51 loc) · 1.36 KB
/
handler_ping.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
package main
import (
"encoding/json"
"fmt"
"net"
"net/http"
"os"
)
func pingHandler(w http.ResponseWriter, r *http.Request) {
var err error
var reqJSON map[string]interface{}
// Read JSON request.
dec := json.NewDecoder(http.MaxBytesReader(w, r.Body, 8192))
err = dec.Decode(&reqJSON)
if err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
// Get IP address from addr (IP:port).
addr, _, err := net.SplitHostPort(reqJSON["addr"].(string))
if err != nil {
http.Error(w, "Invalid remote address", http.StatusBadRequest)
return
}
// Get information about addr from IXC.
info, err := getInfoForAddr(addr)
if err != nil {
http.Error(w, "", http.StatusInternalServerError)
fmt.Fprintf(os.Stderr, "getInfoForAddr: %v\n", err)
return
}
// Add information from the speedtest site.
if _, ok := reqJSON["start"]; ok {
info += "--\n"
info += "Iniciado\n"
}
if _, ok := reqJSON["download"]; ok {
info += "--\n"
info += fmt.Sprintf("Down: %v\n", reqJSON["download"])
info += fmt.Sprintf("Up: %v\n", reqJSON["upload"])
info += fmt.Sprintf("Ping: %v\n", reqJSON["ping"])
info += fmt.Sprintf("Jitter: %v\n", reqJSON["jitter"])
}
// Send everything above to Telegram.
err = sendMessage(info)
if err != nil {
http.Error(w, "", http.StatusInternalServerError)
fmt.Fprintf(os.Stderr, "sendMessage: %v\n", err)
return
}
}