-
Notifications
You must be signed in to change notification settings - Fork 1
/
bugzillatoatom.go
600 lines (490 loc) · 18 KB
/
bugzillatoatom.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
package main
import "strings"
import "strconv"
import "fmt"
import "errors"
import "net"
import "net/http"
import "net/url"
import "context"
import "time"
import "html"
import "log"
import "flag"
import "regexp"
import "sort"
import "encoding/xml"
import "encoding/hex"
import "crypto/sha256"
import "github.com/kaueraal/bugzillatoatom/throttling"
import "golang.org/x/tools/blog/atom"
const bugzillaDateFormat = "2006-01-02 15:04:05 -0700"
const userAgentName = "bugzillatoatom"
// Networks which are by default blocked
var blockedLocalNetworks []string = []string{
"127.0.0.0/8",
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16",
"169.254.0.0/16",
"::1/128",
"fc00::/7",
"fe80::/10",
"fec0::/10", // deprecated, but just be sure...
}
// Maximum size a request for a bug-xml is read in byte.
var maxBugRequestRead int64
// Maximum number of requests per second. Set to something negative to disable
var maxRequestsPerSecond int
// throttler to block on during too many requests in a second
var throttler *throttling.Throttler
// Requests the bug from given url
func doRequest(target *url.URL) (string, error) {
request := http.Request{
Method: http.MethodGet,
URL: target,
Header: http.Header{
"User-Agent": {userAgentName + "/" + getVersion()},
},
}
resp, err := http.DefaultClient.Do(&request)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", errors.New(fmt.Sprintf("Request returned status code %d (%s).", resp.StatusCode, http.StatusText(resp.StatusCode)))
}
// Read until "<attachment". If this is found close the tags. In any case the string
// is only read if in the first 1024 bytes the word "bugzilla" appears.
return readUntilString(resp.Body, "<attachment", "</bug></bugzilla>", "bugzilla", 1024)
}
// Regex for detection of bug numbers in comments. Used in convertXmlToAtom.
// The pre region is used so we don't recognize escaped html as Ӓ as bug number.
var bugNumberRegex *regexp.Regexp = regexp.MustCompile(`(?P<pre>\A|[^&])(?P<all>(?:[Bb]ug |#)(?P<num>\d+))`)
// Regex for detection of attachment uploads in comments. Used in convertXmlToAtom.
var attachmentUploadRegex *regexp.Regexp = regexp.MustCompile(`(?P<pre>\ACreated )(?P<all>attachment (?P<num>\d+))`)
// Somewhat crude regex for detection of most urls in comments. Used in convertXmlToAtom.
var urlRegex *regexp.Regexp = regexp.MustCompile(`\b(?P<all>\w+://\S+)`)
// converts the given xml string into an atom feed
func convertXmlToAtom(inXml string) (string, error) {
type Who struct {
Name string `xml:",chardata"`
RealName string `xml:"name,attr"`
}
getFormatedName := func(w Who) string {
if w.RealName == "" {
return w.Name
} else {
return w.RealName + " (" + w.Name + ")"
}
}
type Comment struct {
CommentId int `xml:"commentid"`
CommentCount *int `xml:"comment_count"`
AttachmentID int `xml:"attachid"`
Who Who `xml:"who"`
When string `xml:"bug_when"`
Text string `xml:"thetext"`
}
type InResult struct {
Urlbase string `xml:"urlbase,attr"`
BugId int `xml:"bug>bug_id"`
Description string `xml:"bug>short_desc"`
Comments []Comment `xml:"bug>long_desc"`
}
inResult := InResult{}
err := xml.Unmarshal([]byte(inXml), &inResult)
if err != nil {
return "", errors.New(fmt.Sprintf("Error during unmarshalling the xml: %s", err))
} else if len(inResult.Comments) == 0 {
// One comment, the initial one, should always be available
err := errors.New("Zero comments in bug. There should be at least the initial one.")
return "", err
}
updateTime, err := time.Parse(bugzillaDateFormat, inResult.Comments[len(inResult.Comments)-1].When)
if err != nil {
return "", errors.New(fmt.Sprintf("Couldn't parse updateTime in initial comment: %s", err))
}
inUrl := fmt.Sprintf("%sshow_bug.cgi?id=%d", inResult.Urlbase, inResult.BugId)
bugUrl := fmt.Sprintf("%sshow_bug.cgi?id=", inResult.Urlbase)
attachmentUrl := fmt.Sprintf("%sattachment.cgi?id=", inResult.Urlbase)
feed := &atom.Feed{
Title: inResult.Description,
ID: inUrl,
Link: []atom.Link{atom.Link{Href: inUrl, Rel: "alternate"}},
Updated: atom.Time(updateTime),
Author: &atom.Person{Name: getFormatedName(inResult.Comments[0].Who)},
Entry: make([]*atom.Entry, 0, len(inResult.Comments)),
}
for i, comment := range inResult.Comments {
creationTime, err := time.Parse(bugzillaDateFormat, comment.When)
if err != nil {
return "", errors.New(fmt.Sprintf("Couldn't parse updateTime in comment %d: %s", i, err))
}
// Some xmls do not contain comment_count for whatever reason
if comment.CommentCount == nil {
comment.CommentCount = new(int)
*comment.CommentCount = i
}
links := []atom.Link{atom.Link{Href: inUrl + "#c" + strconv.Itoa(*comment.CommentCount), Rel: "alternate"}}
if comment.AttachmentID != 0 {
links = append(links, atom.Link{Href: attachmentUrl + strconv.Itoa(comment.AttachmentID), Rel: "enclosure", Title: "Attachment"})
}
body := html.EscapeString(comment.Text)
body = urlRegex.ReplaceAllString(body, `<a href="${all}">${all}</a>`)
body = attachmentUploadRegex.ReplaceAllString(body, `${pre}<a href="`+attachmentUrl+`${num}">${all}</a>`)
body = bugNumberRegex.ReplaceAllString(body, `${pre}<a href="`+bugUrl+`${num}">${all}</a>`)
body = `<pre style="white-space: pre-wrap">` + body + "</pre>"
// We don't limit the title here too much, because comment.Who can be too
// large as well. Just to be sure: It needs to be least 3 for the dots.
maxTitleLength := 100
title := getFormatedName(comment.Who) + ": " + comment.Text[:min(maxTitleLength, len(comment.Text))]
if len(title) > maxTitleLength {
// Find latest space starting three positions before maxTitleLength and cut off
// If the space is too far away simply cut off without a nice cut.
// Find lastSpace with maxTitleLength-2, because if this position is a
// space it is cut out, as title[:lastspace] always cuts at least one char.
lastSpace := strings.LastIndex(title[:maxTitleLength-2], " ")
if lastSpace < maxTitleLength*3/4 {
title = title[:maxTitleLength-3] + "..."
} else {
title = title[:lastSpace] + "..."
}
}
entry := &atom.Entry{
Title: title,
ID: inUrl + "#c" + strconv.Itoa(*comment.CommentCount),
Link: links,
Published: atom.Time(creationTime),
Updated: atom.Time(creationTime),
Author: &atom.Person{Name: getFormatedName(comment.Who)},
Content: &atom.Text{Type: "html", Body: body},
}
feed.Entry = append(feed.Entry, entry)
}
atom, err := xml.MarshalIndent(feed, "", "\t")
if err != nil {
return "", errors.New(fmt.Sprintf("Error during creating the atom feed: %s", err))
}
return xml.Header + string(atom), nil
}
// Checks for not allowed targets, defined by the given networks.
// Returns a list of IPs if target is allowed, otherwise
// throws an error
func targetAllowedIPs(target string, forbiddenNetworks []*net.IPNet) ([]net.IP, error) {
ips, err := net.LookupIP(target)
if forbiddenNetworks == nil {
return ips, nil
}
if err != nil {
return []net.IP{}, err
}
for _, ip := range ips {
for _, ipnet := range forbiddenNetworks {
if ipnet.Contains(ip) {
log.Printf("Blocked target \"%s\" since it's IP %s is contained in blocked network %s.\n", target, ip, ipnet)
errStr := fmt.Sprintf("Target \"%s\" blocked.", target)
return []net.IP{}, errors.New(errStr)
}
}
}
return ips, nil
}
func handleConvert(w http.ResponseWriter, r *http.Request) {
// Check for a possible recursive call
if r.Header != nil {
for _, agent := range r.Header["User-Agent"] {
if strings.Contains(agent, userAgentName) {
log.Printf("Blocked request by %s due to User-Agent \"%s\".\n", r.RemoteAddr, agent)
errStr := fmt.Sprintf("User-Agent \"%s\" blocked.", r.Header["User-Agent"])
http.Error(w, errStr, http.StatusForbidden)
return
}
}
}
formValueUrl := strings.Trim(r.FormValue("url"), " \t")
// if the user didn't give a protocol simply assume http
if !(strings.HasPrefix(formValueUrl, "http://") || strings.HasPrefix(formValueUrl, "https://")) {
formValueUrl = "http://" + formValueUrl
}
target, err := url.Parse(formValueUrl)
if err != nil {
log.Printf("Error occurred during parsing the url \"%s\": %s.\n", r.FormValue("url"), err.Error())
errStr := fmt.Sprintf("Error occurred during parsing the url \"%s\": %s\nAre you sure the url is correct?", r.FormValue("url"), err.Error())
http.Error(w, errStr, http.StatusBadRequest)
return
}
parsedQuery := target.Query()
parsedQuery.Set("ctype", "xml")
target.RawQuery = parsedQuery.Encode()
target.Fragment = ""
// Block during too many requests in the last second.
// Try to handle if a client disconnects while we block.
if maxRequestsPerSecond >= 0 {
// The conversion should never fail, unless the go developers break their API
closeChan := w.(http.CloseNotifier).CloseNotify()
throttler.RequestTicket()
select {
case <-closeChan:
throttler.ReturnUnusedTicket()
return
default:
throttler.UseTicket()
}
}
inXml, err := doRequest(target)
if err != nil {
log.Printf("Error occurred during fetching the url \"%s\": %s\n", target.String(), err.Error())
errStr := fmt.Sprintf("Error occurred during fetching the url \"%s\": %s\nAre you sure the url is correct?", target.String(), err.Error())
http.Error(w, errStr, http.StatusInternalServerError)
return
}
atom, err := convertXmlToAtom(inXml)
if err != nil {
log.Printf("Error occurred during conversion of the url \"%s\" to atom: %s\n", target.String(), err.Error())
errStr := fmt.Sprintf("Error occurred during conversion of the url \"%s\" to atom: %s\nAre you sure the url is correct?", target.String(), err.Error())
http.Error(w, errStr, http.StatusInternalServerError)
return
}
// Generate and check Etag
etagHash := sha256.Sum256([]byte(atom))
etag := "\"" + hex.EncodeToString(etagHash[:]) + "\""
if r.Header.Get("If-None-Match") == etag {
w.WriteHeader(http.StatusNotModified)
return
}
header := w.Header()
header.Set("Etag", etag)
header.Set("Content-Type", "application/atom+xml; charset=utf-8")
fmt.Fprintf(w, "%s", atom)
}
func handleMain(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
errStr := fmt.Sprintf("Error %d: Path \"%s\" not found.", http.StatusNotFound, r.URL.Path)
http.Error(w, errStr, http.StatusNotFound)
return
}
fmt.Fprintf(w, "%s", `
<html>
<head>
<title>bugzillatoatom</title>
</head>
<body bgcolor="#FFFFFF">
<form action=convert>
Convert a Bugzilla bug entry into an Atom feed. Enter an url:
<input type="text" name="url">
<input type="submit" value="convert">
</form>
<div style="position: absolute; right: 0px; bottom: 0px; margin: 8px">via <a href="https://github.com/kaueraal/bugzillatoatom">bugzillatoatom</a></div>
</body>
</html>
`)
}
// Parses a given IP or CIDR into a CIDR. IPs are treated as CIDRs with full bitmask
func parseIPOrCIDR(str string) (*net.IPNet, error) {
if !strings.Contains(str, "/") {
if strings.Contains(str, ":") {
str = str + "/128"
} else {
str = str + "/32"
}
}
_, ipnet, err := net.ParseCIDR(str)
return ipnet, err
}
// To allow forbiddenNetworks to be parsed as argument
type CIDRList []*net.IPNet
func (xs CIDRList) Len() int {
return len(xs)
}
func (xs CIDRList) Swap(i int, j int) {
xs[i], xs[j] = xs[j], xs[i]
}
func (xs CIDRList) Less(i int, j int) bool {
if len(xs[i].IP) != len(xs[j].IP) {
return len(xs[i].IP) < len(xs[j].IP)
}
for k := 0; k < len(xs[i].IP); k++ {
if xs[i].Mask[k] != xs[j].Mask[k] {
return xs[i].Mask[k] < xs[j].Mask[k]
}
}
for k := 0; k < len(xs[i].IP); k++ {
if xs[i].IP[k] != xs[j].IP[k] {
return xs[i].IP[k] < xs[j].IP[k]
}
}
return false
}
// Is quite dump. Does not detect IPv4 embedding in IPv6, etc.
func networkContainsOther(network *net.IPNet, other *net.IPNet) bool {
if len(network.IP) != len(other.IP) {
return false
}
for i := 0; i < len(network.IP); i++ {
// other might have more bits in the mask set, but network not.
if network.Mask[i]|other.Mask[i] != other.Mask[i] {
return false
}
maskedNetwork := network.IP[i] & network.Mask[i]
maskedOther := other.IP[i] & network.Mask[i]
if maskedNetwork != maskedOther {
return false
}
}
return true
}
// This function assumes that no network in forbiddenNetworks contains another.
// This function makes sure that this is true after adding the new network.
func (forbiddenNetworks *CIDRList) addNetwork(network *net.IPNet) {
// Try to find out, whether some network contains the new one or other way round
newForbiddenNetworks := CIDRList{}
for _, n := range *forbiddenNetworks {
if networkContainsOther(n, network) {
// No change needed
return
} else if !networkContainsOther(network, n) {
newForbiddenNetworks = append(newForbiddenNetworks, n)
}
}
newForbiddenNetworks = append(newForbiddenNetworks, network)
*forbiddenNetworks = newForbiddenNetworks
}
func (forbiddenNetworks *CIDRList) String() string {
strs := []string{}
for _, ipnet := range *forbiddenNetworks {
strs = append(strs, ipnet.String())
}
return strings.Join(strs, ", ")
}
func (forbiddenNetworks *CIDRList) Set(value string) error {
ipnet, err := parseIPOrCIDR(value)
if err == nil {
forbiddenNetworks.addNetwork(ipnet)
}
return err
}
// Blocks known local networks and all networks currently known to all interfaces.
// This is only somewhat effective, as the known networks can change later on.
func (forbiddenNetworks *CIDRList) blockLocalNetworks() {
interfaceAddrs, err := net.InterfaceAddrs()
if err != nil {
log.Fatalln("Getting of used IPs failed!")
}
for _, blockedNetwork := range interfaceAddrs {
ipnet, err := parseIPOrCIDR(blockedNetwork.String())
if err != nil {
log.Fatalln("Parsing of local network/IP " + blockedNetwork.String() + " failed. That should never happen.")
}
forbiddenNetworks.addNetwork(ipnet)
}
// We still block networks from a list, since the user might add networks
// after bugzillatoatom is started
for _, blockedNetwork := range blockedLocalNetworks {
ipnet, err := parseIPOrCIDR(blockedNetwork)
if err != nil {
log.Fatalln("Parsing of integrated network " + blockedNetwork + " failed. That should never happen.")
}
forbiddenNetworks.addNetwork(ipnet)
}
}
// Sets the http.DefaultClient to a client with timeout which
// blocks connections to a host with an IP in forbiddenNetworks
func setHttpDefaultClient(forbiddenNetworks []*net.IPNet) {
// Copy the original DefaultTransport and add our dialer
httpDialer := &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}
httpTransport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
httpTransport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
host := addr
port := ""
portPosition := strings.LastIndex(addr, ":")
bracketPosition := strings.LastIndex(addr, "]")
// Strip port and brackets, targetAllowedIps/net.LookupIP needs it that way.
// This comparison also works if one or both are not found
if portPosition > bracketPosition {
if bracketPosition == -1 {
port = addr[portPosition:]
host = addr[:portPosition]
} else {
port = addr[portPosition:]
host = addr[1 : portPosition-1]
}
} else {
if bracketPosition != -1 {
host = addr[1 : len(addr)-2]
}
}
ips, err := targetAllowedIPs(host, forbiddenNetworks)
if err != nil {
return nil, err
}
for _, ip := range ips {
var ipstr string
if len(ip.To4()) == net.IPv4len {
ipstr = ip.String() + port
} else {
ipstr = "[" + ip.String() + "]" + port
}
conn, err := httpDialer.DialContext(ctx, network, ipstr)
if err == nil {
return conn, nil
}
}
return nil, errors.New("Unable to connect to " + addr)
}
// Add a timeout for the default http.Get() in case something goes wrong
// on the other side.
http.DefaultClient = &http.Client{
Timeout: time.Second * 30,
Transport: httpTransport,
}
}
func main() {
version := flag.Bool("version", false, "Print the current version and exit.")
port := flag.Uint64("p", 33916, "Port to bind to.")
maxBugRequestReadFlag := flag.Uint64("requestsize", 1*1024*1024, "Maximum number of bytes to read during a request to another server.") // 1MiB per default
flag.IntVar(&maxRequestsPerSecond, "persecond", 5, "Maximum number of requests to another server per second. Set to -1 to disable.")
forbiddenNetworks := CIDRList{}
flag.Var(&forbiddenNetworks, "b", "IP or network in CIDR format to block. If a host is available under any blocked IP it will be blocked. Can be given multiple times.\n\tYou probably want to exclude localhost or local networks both on IPv4 and IPv6. These are blocked by default unless you give -nolocalblock.")
nolocalblock := flag.Bool("nolocalblock", false, "Disables default blocking of local networks and IPs. If this flag is given you can add those again via -b.\n\tThis includes all IPs and networks of local interfaces and the networks "+strings.Join(blockedLocalNetworks, ", ")+".")
flag.Parse()
if *version {
log.Fatalln(getVersion())
}
if *maxBugRequestReadFlag&(1<<63) != 0 {
log.Fatalln("Too large requestsize")
} else {
maxBugRequestRead = int64(*maxBugRequestReadFlag)
}
// Block local networks + IPs
if !(*nolocalblock) {
forbiddenNetworks.blockLocalNetworks()
}
sort.Sort(forbiddenNetworks)
for _, ipnet := range forbiddenNetworks {
log.Printf("Network %s blocked\n", ipnet)
}
setHttpDefaultClient(forbiddenNetworks)
if maxRequestsPerSecond >= 0 {
throttler = throttling.NewThrottler(uint(maxRequestsPerSecond))
}
server := http.Server{
ReadTimeout: 30 * time.Second,
Addr: ":" + strconv.FormatUint(*port, 10),
}
// Disable keepalives, as they can interfere with the CloseNotifier interface (as of 2017-02-09)
server.SetKeepAlivesEnabled(false)
http.HandleFunc("/convert", handleConvert)
http.HandleFunc("/", handleMain)
log.Fatal(server.ListenAndServe())
}