mirror of
https://github.com/zhigang1992/cow.git
synced 2026-01-12 17:12:57 +08:00
Only provide add direct option if domain not in direct DS.
This commit is contained in:
11
domainset.go
11
domainset.go
@@ -107,6 +107,17 @@ func isHostBlocked(host string) bool {
|
||||
return blockedDs.has(dm)
|
||||
}
|
||||
|
||||
func isHostDirect(host string) bool {
|
||||
dm := host2Domain(host)
|
||||
if alwaysDirectDs[dm] {
|
||||
return true
|
||||
}
|
||||
if alwaysBlockedDs[dm] {
|
||||
return false
|
||||
}
|
||||
return directDs.has(dm)
|
||||
}
|
||||
|
||||
func isHostInChouDs(host string) bool {
|
||||
return chouDs[host2Domain(host)]
|
||||
}
|
||||
|
||||
22
error.go
22
error.go
@@ -23,13 +23,15 @@ var errPageRawTmpl = `<!DOCTYPE html>
|
||||
`
|
||||
|
||||
var blockedFormRawTmpl = `<p></p>
|
||||
Refresh to retry or add <b>{{.Domain}}</b> to
|
||||
Add <b>{{.Domain}}</b> to
|
||||
<form action="http://{{.ListenAddr}}/blocked" method="get">
|
||||
<input type="hidden" name="host" value={{.Host}}>
|
||||
<b>blocked sites</b>
|
||||
<input type="submit" name="submit" value="blocked">
|
||||
</form>
|
||||
<form action="http://{{.ListenAddr}}/direct" method="get">
|
||||
`
|
||||
|
||||
var directFormRawTmpl = `<form action="http://{{.ListenAddr}}/direct" method="get">
|
||||
<input type="hidden" name="host" value={{.Host}}>
|
||||
<b>direct accessible sites</b>
|
||||
<input type="submit" name="submit" value="direct">
|
||||
@@ -44,7 +46,7 @@ var headRawTmpl = "HTTP/1.1 {{.CodeReason}}\r\n" +
|
||||
"Content-Type: text/html\r\n" +
|
||||
"Content-Length: {{.Length}}\r\n"
|
||||
|
||||
var errPageTmpl, headTmpl, blockedFormTmpl *template.Template
|
||||
var errPageTmpl, headTmpl, blockedFormTmpl, directFormTmpl *template.Template
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
@@ -60,6 +62,10 @@ func init() {
|
||||
fmt.Println("Internal error on generating blocked form template")
|
||||
os.Exit(1)
|
||||
}
|
||||
if directFormTmpl, err = template.New("directForm").Parse(directFormRawTmpl); err != nil {
|
||||
fmt.Println("Internal error on generating direct form template")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func genErrorPage(h1, msg, form string) (string, error) {
|
||||
@@ -117,10 +123,10 @@ func sendRedirectPage(w *bufio.Writer, location string) {
|
||||
}
|
||||
|
||||
func sendBlockedErrorPage(w *bufio.Writer, codeReason, h1, msg string, r *Request) {
|
||||
// If host is IP, we can't add it to blocked or direct domain list. Just
|
||||
// If host is IP or in always DS, we can't add it to blocked or direct domain list. Just
|
||||
// return ordinary error page.
|
||||
h, _ := splitHostPort(r.URL.Host)
|
||||
if hostIsIP(h) {
|
||||
if hostIsIP(r.URL.Host) || inAlwaysDs(host2Domain(r.URL.Host)) {
|
||||
sendErrorPage(w, codeReason, h1, msg)
|
||||
return
|
||||
}
|
||||
@@ -139,5 +145,11 @@ func sendBlockedErrorPage(w *bufio.Writer, codeReason, h1, msg string, r *Reques
|
||||
errl.Println("Error generating blocked form:", err)
|
||||
return
|
||||
}
|
||||
if !isHostDirect(h) {
|
||||
if err := directFormTmpl.Execute(buf, data); err != nil {
|
||||
errl.Println("Error generating direct form:", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
sendPageGeneric(w, codeReason, "[Error] "+h1, msg, buf.String(), "")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user