forked from arlolra/check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
173 lines (144 loc) · 3.91 KB
/
handlers.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/samuel/go-gettext/gettext"
"html/template"
"io"
"log"
"net"
"net/http"
"regexp"
"strconv"
"time"
)
// page model
type Page struct {
IsTor bool
NotUpToDate bool
Small bool
NotTBB bool
Fingerprint string
OnOff string
Lang string
IP string
Locales map[string]string
}
func RootHandler(Layout *template.Template, Exits *Exits, domain *gettext.Domain, Phttp *http.ServeMux, Locales map[string]string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// serve public files
if len(r.URL.Path) > 1 {
Phttp.ServeHTTP(w, r)
return
}
var (
err error
isTor bool
host string
onOff string
fingerprint string
)
if host, err = GetHost(r); err == nil {
fingerprint, isTor = Exits.IsTor(host)
}
// short circuit for torbutton
if IsParamSet(r, "TorButton") {
WriteHTMLBuf(w, r, Layout, domain, "torbutton.html", Page{IsTor: isTor})
return
}
// try to determine if it's TBB
notTBB := !LikelyTBB(r.UserAgent())
// users shouldn't be relying on check
// to determine the TBB is up-to-date
// always return false to this param
notUpToDate := IsParamSet(r, "uptodate")
// string used for classes and such
// in the template
if isTor {
if notTBB || notUpToDate {
onOff = "not"
} else {
onOff = "on"
}
} else {
onOff = "off"
}
// instance of your page model
p := Page{
isTor,
notUpToDate,
IsParamSet(r, "small"),
notTBB,
fingerprint,
onOff,
Lang(r),
host,
Locales,
}
// render the template
WriteHTMLBuf(w, r, Layout, domain, "index.html", p)
}
}
type IPResp struct {
IsTor bool
IP string
}
func APIHandler(Exits *Exits) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var (
err error
isTor bool
host string
)
if host, err = GetHost(r); err == nil {
_, isTor = Exits.IsTor(host)
}
ip, _ := json.Marshal(IPResp{isTor, host})
w.Write(ip)
}
}
func BulkHandler(Layout *template.Template, Exits *Exits, domain *gettext.Domain) http.HandlerFunc {
ApiPath := regexp.MustCompile("^/api/")
return func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
ip := q.Get("ip")
if net.ParseIP(ip) == nil {
WriteHTMLBuf(w, r, Layout, domain, "bulk.html", Page{Lang: "en"})
return
}
port, port_str := GetQS(q, "port", 80)
n, n_str := GetQS(q, "n", 16)
w.Header().Set("Last-Modified", Exits.UpdateTime.UTC().Format(http.TimeFormat))
if q.Get("format") == "json" || ApiPath.MatchString(r.URL.Path) {
w.Header().Set("Content-Type", "application/json")
Exits.DumpJSON(w, n, ip, port)
} else {
str := fmt.Sprintf("# This is a list of all Tor exit nodes from the past %d hours that can contact %s on port %d #\n", n, ip, port)
str += fmt.Sprintf("# You can update this list by visiting https://check.torproject.org/cgi-bin/TorBulkExitList.py?ip=%s%s%s #\n", ip, port_str, n_str)
str += fmt.Sprintf("# This file was generated on %v #\n", Exits.UpdateTime.UTC().Format(time.UnixDate))
fmt.Fprintf(w, str)
Exits.Dump(w, n, ip, port)
}
}
}
func WriteHTMLBuf(w http.ResponseWriter, r *http.Request, Layout *template.Template, domain *gettext.Domain, tmp string, p Page) {
buf := new(bytes.Buffer)
// render template
if err := Layout.ExecuteTemplate(buf, tmp, p); err != nil {
log.Printf("Layout.ExecuteTemplate: %v", err)
http.Error(w, domain.GetText(p.Lang, "Sorry, your query failed or an unexpected response was received."), http.StatusInternalServerError)
return
}
// set some headers
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if r.Method == "HEAD" {
w.Header().Set("Content-Length", strconv.Itoa(buf.Len()))
return
}
// write buf
if _, err := io.Copy(w, buf); err != nil {
log.Printf("io.Copy: %v", err)
}
}