-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
127 lines (109 loc) · 2.68 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
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
package main
import (
"bufio"
"fmt"
"log"
"net/http"
"net/url"
"os"
"strings"
)
type EzproxyComponents struct {
Urls map[string]bool
Hosts map[string]bool
Domains map[string]bool
}
var e EzproxyComponents
func init() {
var err error
e.Urls, err = populateMap("config/urls.txt")
if err != nil {
log.Fatal("Unable to load urls")
}
e.Hosts, err = populateMap("config/hosts.txt")
if err != nil {
log.Fatal("Unable to load hosts")
}
e.Domains, err = populateMap("config/domains.txt")
if err != nil {
log.Fatal("Unable to load domains")
}
}
func main() {
http.HandleFunc("/proxyUrl", checkUrl)
listen := os.Getenv("LISTEN")
if listen == "" {
listen = ":8888"
}
log.Printf("Server listening on %s\n", listen)
if err := http.ListenAndServe(listen, nil); err != nil {
panic(err)
}
}
func populateMap(f string) (map[string]bool, error) {
m := make(map[string]bool)
file, err := os.Open(f)
if err != nil {
log.Printf("Error opening file: %v", err)
return nil, err
}
defer file.Close()
log.Println("Loading EZProxy config", f)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
m[line] = true
}
if err := scanner.Err(); err != nil {
log.Printf("Error reading file: %v", err)
return nil, err
}
return m, nil
}
func checkUrl(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
u := r.URL.Query().Get("url")
if u == "" {
http.Error(w, "url parameter not provided.", http.StatusBadRequest)
return
}
p, err := url.Parse(u)
if err != nil {
log.Printf("Error parsing URL for %s: %v\n", u, err)
http.Error(w, "url parameter not valid.", http.StatusBadRequest)
return
}
httpStatus := http.StatusNotFound
// normalize the URL so we are always dealing with a domain
domain := p.Hostname()
// but also handle just passing the domain to the endpoint
if p.Scheme == "" || p.Host == "" {
domain = u
}
if e.Hosts[domain] || e.Urls[domain] {
httpStatus = http.StatusOK
} else {
// check for wildcard domains set in EZProxy
domainParts := strings.Split(domain, ".")
for i := len(domainParts); i >= 2; i-- {
subdomain := strings.Join(domainParts[len(domainParts)-i:], ".")
if e.Domains[subdomain] {
httpStatus = http.StatusOK
break
}
}
}
log.Printf("GET /proxyUrl?url=%s HTTP/1.1 %d\n", u, httpStatus)
w.WriteHeader(httpStatus)
w.Header().Set("Content-Type", "text/plain")
if httpStatus == http.StatusOK {
fmt.Fprint(w, "1")
} else {
fmt.Fprint(w, "0")
}
default:
log.Printf("%s /proxyUrl HTTP/1.1 %d\n", r.Method, http.StatusMethodNotAllowed)
http.Error(w, "Method not allowed.", http.StatusMethodNotAllowed)
}
}