diff --git a/domainset.go b/domainset.go index f0e16f6..f702d48 100644 --- a/domainset.go +++ b/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)] } diff --git a/error.go b/error.go index ac54d68..b84b654 100644 --- a/error.go +++ b/error.go @@ -23,13 +23,15 @@ var errPageRawTmpl = ` ` var blockedFormRawTmpl = `

- Refresh to retry or add {{.Domain}} to + Add {{.Domain}} to
blocked sites
-
+` + +var directFormRawTmpl = ` direct accessible sites @@ -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(), "") }