-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
140 lines (130 loc) · 3.59 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
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"compress/gzip"
"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 {
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
}
reader := r.Body
defer safeClose(reader, "Report body")
if r.Header.Get("Content-Encoding") == "gzip" {
r, err := gzip.NewReader(r.Body)
if err != nil {
log.Printf("failed to read gzipped request body: %v", err)
http.Error(w, "Response read error", http.StatusInternalServerError)
return
}
reader = r
}
body, err := ioutil.ReadAll(reader)
if err != nil {
log.Printf("failed to read request body: %v", err)
http.Error(w, "Response read error", http.StatusInternalServerError)
return
}
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
report.GlobalIP = remoteIP(r)
// 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
}
var msg reply
if len(kaginawa.SSHServers) > 0 {
i := rand.Int() % len(kaginawa.SSHServers)
msg = reply{
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.Marshal(msg)
if err != nil {
http.Error(w, "Response marshal error", http.StatusInternalServerError)
return
}
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Set("Content-Encoding", "gzip")
w.WriteHeader(http.StatusCreated)
gz := gzip.NewWriter(w)
defer safeClose(gz, "gzipped response")
if _, err := gz.Write(rawReply); err != nil {
log.Printf("failed to write response: %v", err)
}
} else {
w.WriteHeader(http.StatusCreated)
if _, err := w.Write(rawReply); err != nil {
log.Printf("failed to write response: %v", err)
}
}
}
func reverseLookup(globalIP string) (string, error) {
if globalIP == "[::1]" {
return "", nil
}
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
}