-
Notifications
You must be signed in to change notification settings - Fork 8
/
web.go
248 lines (210 loc) · 6.46 KB
/
web.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
// Copyright (C) 2013 Alexander Bauer, Luke Evers, Daniel Supernault,
// Dylan Whichard, and contributors; (GPLv3) see LICENSE or doc.go
import (
"encoding/xml"
"errors"
"github.com/baliw/moverss"
"github.com/dchest/captcha"
"github.com/russross/blackfriday"
"html/template"
"net"
"net/http"
"os"
"path"
"strings"
"time"
)
var (
NodeRSS *moverss.Channel
NodeXMLName xml.Name
)
var (
listener net.Listener
captchaServer = captcha.Server(captcha.StdWidth, captcha.StdHeight)
)
var (
InvalidBindAddress = errors.New("invalid address to bind to")
InvalidCAPTCHAFormat = errors.New("CAPTCHA format invalid")
IncorrectCAPTCHA = errors.New("CAPTCHA ID or solution is incorrect")
)
// StartServer is a simple helper function to register any handlers
// (such as the API) and start the HTTP server on the configured
// address (Conf.Web.Addr).
//
// If Conf.Web.Prefix or Conf.Web.DeproxyHeaderFields has a length
// greater than zero, it wraps its http.ServeMux with a Deproxier.
//
// On crash, it returns the error.
func StartServer() (err error) {
// Register any handlers.
RegisterAPI(Conf.Web.Prefix)
l.Debug("Registered API handler\n")
err = RegisterTemplates()
if err != nil {
return
}
// Parse the address and create an appropriate net.Listener. The
// Web.Addr will be of the form "protocol://address:port".
parts := strings.Split(Conf.Web.Addr, "://")
if len(parts) != 2 {
return InvalidBindAddress
}
listener, err = net.Listen(parts[0], parts[1])
if err != nil {
return
}
// Create a custom http.Server, so that we can have better control
// over certain behaviors.
s := &http.Server{}
// If either the Prefix or DeproxyHeaderFields are set, then we
// need to wrap the default Handler with a Deproxier. Otherwise,
// we just use our Handler.
if len(Conf.Web.Prefix) > 0 || len(Conf.Web.DeproxyHeaderFields) > 0 {
s.Handler = &Deproxier{http.DefaultServeMux}
} else {
s.Handler = &Handler{http.DefaultServeMux}
}
// We need to set the database tile store.
captcha.SetCustomStore(CAPTCHAStore{})
http.HandleFunc("/", HandleStatic)
http.HandleFunc("/node/", HandleMap)
http.HandleFunc("/verify/", HandleMap)
http.Handle("/captcha/", captchaServer)
// Start the HTTP server and return any errors if it crashes.
l.Infof("Starting HTTP server on %q\n", Conf.Web.Addr)
return s.Serve(listener)
}
// HandleStatic serves files directly from <StaticDir>/web using
// http.ServeFile().
func HandleStatic(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, path.Join(StaticDir, "web", req.URL.Path))
}
// HandleMap always serves <StaticDir>/web/index.html.
func HandleMap(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, path.Join(StaticDir, "web", "index.html"))
}
// Handler acts is a simple http.Handler which performs some cleanup
// on the Request before passing it on to its underlying
// http.ServeMux.
type Handler struct {
Mux *http.ServeMux
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.RemoteAddr, _, _ = net.SplitHostPort(r.RemoteAddr)
h.Mux.ServeHTTP(w, r)
}
// Deproxier implements the http.Handler interface by setting the
// http.Request.RemoteAddr to the appropriate header field, if
// set, then passing the request on to its underlying http.ServeMux.
//
// It interprets the following header fields as a real remote address,
// in this order.
// X-Proxied-For
// X-Real-Ip
type Deproxier struct {
Mux *http.ServeMux
}
func (d *Deproxier) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Check the acceptable header fields in order, checking if each
// one is present. If so, set the r.RemoteAddr to its first value
// and break out of the loop.
for _, fieldname := range Conf.Web.DeproxyHeaderFields {
if realip, ok := r.Header[fieldname]; ok {
r.RemoteAddr = realip[0]
break
}
}
// Finally, pass the request on to the underlying http.ServeMux.
d.Mux.ServeHTTP(w, r)
}
// RegisterTemplates loads templates from <StaticDir>/email/*.txt into
// the global variable t.
func RegisterTemplates() (err error) {
t = template.New("")
t.Funcs(template.FuncMap{
"markdownify": func(s string) template.HTML {
return template.HTML(
string(blackfriday.MarkdownBasic([]byte(s))))
},
})
t, err = t.ParseGlob(path.Join(StaticDir, "email/*.txt"))
return
}
// VerifyCAPTCHA accepts a *http.Request and verifies that the given
// 'captcha' form is valid. This is a string of the form
// "id:solution". It will return IncorrectCAPTCHAError if the solution
// or ID is invalid.
func VerifyCAPTCHA(req *http.Request) error {
// Get the "captcha" form value.
solution := req.FormValue("captcha")
// Find the point to split the form value at. If it's not found in
// the string, return the InvalidCAPTCHAFormat error.
index := strings.Index(solution, ":")
if index < 0 {
return InvalidCAPTCHAFormat
}
// If that was successful, try to verify it. If it returns false,
// the ID or solution was invalid.
if !captcha.VerifyString(solution[:index], solution[index+1:]) {
return IncorrectCAPTCHA
}
// If we get to this point, then it was successfully validated and
// we can return nil.
return nil
}
// GenerateNodeRSS creates an RSS feed of nodes from the database and
// returns any errors.
func GenerateNodeRSS() (err error) {
NodeRSS = moverss.ChannelFactory(
Conf.Name+" NodeAtlas",
Conf.Web.Hostname,
"New local node feed")
NodeXMLName = xml.Name{
Space: Conf.Web.Hostname,
Local: "nodes",
}
// We need to use some custom SQL here in order to retrieve the
// timestamps.
rows, err := Db.Query(`
SELECT updated,address,owner
FROM nodes;`)
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
// Initialize the variables.
var updated int64
node := new(Node)
// Scan all of the values into them.
err = rows.Scan(&updated, &node.Addr, &node.OwnerName)
if err != nil {
return
}
// Add the Node to the RSS feed.
in := node.Item()
in.SetPubDate(time.Unix(updated, 0))
NodeRSS.AddItem(in)
}
// Write the feed to the file, and log any errors.
WriteNodeRSS()
return
}
// AddNodeToRSS adds a Node to the existing NodeRSS channel with the
// given time and invokes WriteNodeRSS to write it to a file and log
// any errors.
func AddNodeToRSS(n *Node, t time.Time) {
in := n.Item()
in.SetPubDate(t)
NodeRSS.AddItem(in)
WriteNodeRSS()
}
func WriteNodeRSS() {
f, err := os.Create(StaticDir + "/web/index.rss")
if err != nil {
l.Errf("Error writing NodeRSS feed: %s", err)
}
f.Write(NodeRSS.Publish())
f.Close()
}