-
Notifications
You must be signed in to change notification settings - Fork 5
/
api.go
executable file
·156 lines (136 loc) · 3.79 KB
/
api.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"encoding/json"
"net"
"net/http"
"strconv"
)
// Handle requests to /all/ by returning all stats
func allStatsHandler(w http.ResponseWriter, r *http.Request) {
// First check to see if we should allow this request.
auth, err := SystemConfig.Access.JSONApi.Authentication.method(r)
if err != nil {
http.Error(w, "500 Server Error", http.StatusInternalServerError)
l.Errln(err)
return
}
if !auth {
http.Error(w, "401 Unauthorized", http.StatusUnauthorized)
return
}
err, httpStatusCode := updateCjdnsStats()
if err != nil {
http.Error(w, "500 Server Error", httpStatusCode)
l.Errln(err)
return
}
// Render the json and send it
err = sendJSON(w, r, Data)
if err != nil {
l.Errln(err)
return
}
}
// Handle requests to /node/ by returning just cjdns stats
func nodeStatsHandler(w http.ResponseWriter, r *http.Request) {
// First check to see if we should allow this request.
auth, err := SystemConfig.Access.JSONApi.Authentication.method(r)
if err != nil {
http.Error(w, "500 Server Error", http.StatusInternalServerError)
l.Errln(err)
return
}
if !auth {
http.Error(w, "401 Unauthorized", http.StatusUnauthorized)
return
}
err, httpStatusCode := updateCjdnsStats()
if err != nil {
http.Error(w, "500 Server Error", httpStatusCode)
l.Errln(err)
return
}
err = sendJSON(w, r, Data.Node)
if err != nil {
http.Error(w, "500 Server Error", http.StatusInternalServerError)
l.Errln(err)
return
}
}
// Handle requests to /peers/ by returning just peer stats
func peerStatsHandler(w http.ResponseWriter, r *http.Request) {
l.Debugln("Received request for peer data")
// First check to see if we should allow this request.
auth, err := SystemConfig.Access.JSONApi.Authentication.method(r)
if err != nil {
http.Error(w, "500 Server Error", http.StatusInternalServerError)
l.Errln(err)
return
}
if !auth {
http.Error(w, "401 Unauthorized", http.StatusUnauthorized)
return
}
// Render the json and send it
err = sendJSON(w, r, Data.Peers)
if err != nil {
l.Errln(err)
return
}
}
func sendJSON(w http.ResponseWriter, r *http.Request, v interface{}) (err error) {
// Render the json and send it
jsonOut, err := json.MarshalIndent(v, "", "\t")
if err != nil {
return
}
r.ParseForm()
callback := []byte(r.Form.Get("callback"))
cb := []byte(r.Form.Get("cb"))
if SystemConfig.Access.JSONApi.EnableJSCallbacks {
if validIP.MatchString(r.Header.Get("Referer")) {
l.Infoln("Successful JS access attempt from ", r.Header.Get("Referer"))
if(len(callback) > 0) {
callback := append(callback, []byte("(")...)
jsonOut = append(callback, jsonOut...)
jsonOut = append(jsonOut, []byte(")")...)
} else if(len(cb) > 0) {
cb := append(cb, []byte("(")...)
jsonOut = append(cb, jsonOut...)
jsonOut = append(jsonOut, []byte(")")...)
}
} else {
http.Error(w, "401 Unauthorized", http.StatusUnauthorized)
}
}
w.Header().Set("Content-Length", strconv.Itoa(len(jsonOut)))
w.Header().Set("Content-Type", "Text/JavaScript")
w.Write(jsonOut)
return
}
// Always allows access, effectively disabling authentication.
func nullAuth(r *http.Request) (authorized bool, err error) {
host, port, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
l.Errln(err)
return
}
l.Infoln("Successful access attempt from", host, port)
return true, nil
}
// Only allows access from specific IP addresses.
func IPAuth(r *http.Request) (authorized bool, err error) {
host, port, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
l.Errln(err)
return
}
for _, ip := range SystemConfig.Access.JSONApi.Authentication.IP.Authorized {
if host == ip {
l.Infoln("Successful access attempt from", host, port)
return true, nil
}
}
l.Infoln("Failed access attempt from", host)
return
}