-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
129 lines (117 loc) · 3.32 KB
/
report.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"math/rand"
"net"
"net/http"
"strings"
"time"
"github.com/kaginawa/kaginawa-server"
)
// reply defines all of reply message attributes
type reply struct {
Reboot bool `json:"reboot,omitempty"` // Reboot requested from the server
SSHServerHost string `json:"ssh_host,omitempty"`
SSHServerPort int `json:"ssh_port,omitempty"`
SSHServerUser string `json:"ssh_user,omitempty"`
SSHKey string `json:"ssh_key,omitempty"`
SSHPassword string `json:"ssh_password,omitempty"`
}
// handleReport handles report submits.
//
// - Method: POST
// - Client: Kaginawa
// - Access: Normal
// - Response: JSON
func handleReport(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
if !validateAPIKey(r, false) {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("failed to read request: %v", err)
http.Error(w, "Response read error", http.StatusInternalServerError)
return
}
defer safeClose(r.Body, "Report body")
var report kaginawa.Report
if err := json.Unmarshal(body, &report); err != nil {
http.Error(w, "Response unmarshal error", http.StatusBadRequest)
return
}
report.ServerTime = time.Now().UTC().Unix()
report.APIKey = extractAPIKey(r)
log.Printf("REPORT from %s %s %d", report.ID, report.CustomID, report.SSHRemotePort)
// Pick global IP
forwardedFor := r.Header.Get("X-Forwarded-For")
if len(forwardedFor) > 0 {
list := strings.Split(forwardedFor, ",")
report.GlobalIP = list[len(list)-1]
}
if len(report.GlobalIP) == 0 {
report.GlobalIP = trimPort(r.RemoteAddr)
}
// Reverse lookup
if len(report.GlobalIP) > 0 {
name, err := reverseLookup(report.GlobalIP)
if err != nil {
report.GlobalHost = report.GlobalIP
}
report.GlobalHost = name
}
if len(report.GlobalHost) == 0 {
report.GlobalHost = report.GlobalIP
}
if err := db.PutReport(report); err != nil {
log.Printf("failed to put Report (id=%s): %v", report.ID, err)
http.Error(w, "Failed to put database", http.StatusInternalServerError)
return
}
// TODO: implement reboot request feature
var msg reply
if len(kaginawa.SSHServers) > 0 {
i := rand.Int() % len(kaginawa.SSHServers)
msg = reply{
Reboot: false,
SSHServerHost: kaginawa.SSHServers[i].Host,
SSHServerPort: kaginawa.SSHServers[i].Port,
SSHServerUser: kaginawa.SSHServers[i].User,
SSHKey: kaginawa.SSHServers[i].Key,
SSHPassword: kaginawa.SSHServers[i].Password,
}
}
rawReply, err := json.MarshalIndent(msg, "", " ")
if err != nil {
http.Error(w, "Response marshal error", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
if _, err := w.Write(rawReply); err != nil {
log.Printf("failed to write response: %v", err)
}
// TODO: reset reboot request
}
func reverseLookup(globalIP string) (string, error) {
names, err := net.LookupAddr(globalIP)
if err != nil {
return "", err
}
if len(names) == 0 {
return globalIP, nil // no lookup address
}
return strings.TrimRight(names[0], "."), nil
}
func trimPort(addr string) string {
i := strings.LastIndex(addr, ":")
if i > 0 {
return addr[0:i]
}
return addr
}