/
main.go
83 lines (78 loc) · 1.59 KB
/
main.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
package main
import (
"bufio"
"fmt"
"io"
"log"
"net/http"
"os"
"regexp"
"strings"
)
type redirect struct {
from, shortTo, longTo string
}
func findRedirects(filename string) (result []*redirect, err error) {
var (
f *os.File
re *regexp.Regexp
line []byte
isPrefix bool
)
if f, err = os.Open(filename); err != nil {
return
}
defer f.Close()
r := bufio.NewReader(f)
if re, err = regexp.Compile("^::1 (.*) #redirects-to (.*)$"); err != nil {
return
}
result = []*redirect{}
for {
if line, isPrefix, err = r.ReadLine(); err == io.EOF {
break
}
if isPrefix {
err = fmt.Errorf("line too long: %s...", line)
break
}
if match := re.FindSubmatch(line); match != nil {
var shortTo, longTo string
allRedirects := strings.Split(string(match[2]), " ")
shortTo = allRedirects[0]
if len(allRedirects) > 1 {
longTo = allRedirects[1]
}
result = append(result, &redirect{
from: string(match[1]),
shortTo: shortTo,
longTo: longTo})
}
}
if err == io.EOF {
err = nil
}
return
}
func addRedirect(r *redirect) {
http.HandleFunc(r.from+"/", func(w http.ResponseWriter, req *http.Request) {
path := req.URL.Path[1:]
if r.longTo != "" && len(req.URL.Path) > 1 {
http.Redirect(w, req, r.longTo+path, http.StatusSeeOther)
} else {
http.Redirect(w, req, r.shortTo, http.StatusSeeOther)
}
})
}
func main() {
redirects, err := findRedirects("/etc/hosts")
if err != nil {
log.Fatal(err)
}
for _, r := range redirects {
addRedirect(r)
}
if err = http.ListenAndServe(":80", nil); err != nil {
log.Fatal(err)
}
}