Signed-off-by: arraykeys@gmail.com <arraykeys@gmail.com>

This commit is contained in:
arraykeys@gmail.com
2018-03-05 11:15:30 +08:00
parent 2c675f2cbe
commit 5436a95430

View File

@ -5,6 +5,7 @@ import (
"crypto/tls" "crypto/tls"
"encoding/base64" "encoding/base64"
"encoding/binary" "encoding/binary"
"errors"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -54,7 +55,7 @@ func NewChecker(timeout int, interval int64, blockedFile, directFile string) Che
if !ch.directMap.IsEmpty() { if !ch.directMap.IsEmpty() {
log.Printf("direct file loaded , domains : %d", ch.directMap.Count()) log.Printf("direct file loaded , domains : %d", ch.directMap.Count())
} }
if interval>0{ if interval > 0 {
ch.start() ch.start()
} }
@ -364,8 +365,7 @@ func (req *HTTPRequest) HTTP() (err error) {
return return
} }
} }
req.URL, err = req.getHTTPURL() req.URL = req.getHTTPURL()
if err == nil {
var u *url.URL var u *url.URL
u, err = url.Parse(req.URL) u, err = url.Parse(req.URL)
if err != nil { if err != nil {
@ -373,7 +373,6 @@ func (req *HTTPRequest) HTTP() (err error) {
} }
req.Host = u.Host req.Host = u.Host
req.addPortIfNot() req.addPortIfNot()
}
return return
} }
func (req *HTTPRequest) HTTPS() (err error) { func (req *HTTPRequest) HTTPS() (err error) {
@ -385,7 +384,6 @@ func (req *HTTPRequest) HTTPS() (err error) {
} }
req.Host = req.hostOrURL req.Host = req.hostOrURL
req.addPortIfNot() req.addPortIfNot()
//_, err = fmt.Fprint(*req.conn, "HTTP/1.1 200 Connection established\r\n\r\n")
return return
} }
func (req *HTTPRequest) HTTPSReply() (err error) { func (req *HTTPRequest) HTTPSReply() (err error) {
@ -398,24 +396,20 @@ func (req *HTTPRequest) IsHTTPS() bool {
func (req *HTTPRequest) BasicAuth() (err error) { func (req *HTTPRequest) BasicAuth() (err error) {
//log.Printf("request :%s", string(b[:n]))authorization // log.Printf("request :%s", string(req.HeadBuf))
isProxyAuthorization := false code := "407"
authorization, err := req.getHeader("Authorization") authorization := req.getHeader("Proxy-Authorization")
if err != nil { // if authorization == "" {
fmt.Fprint((*req.conn), "HTTP/1.1 401 Unauthorized\r\nWWW-Authenticate: Basic realm=\"\"\r\n\r\nUnauthorized") // authorization = req.getHeader("Authorization")
CloseConn(req.conn) // code = "401"
return // }
}
if authorization == "" { if authorization == "" {
authorization, err = req.getHeader("Proxy-Authorization") fmt.Fprintf((*req.conn), "HTTP/1.1 %s Unauthorized\r\nWWW-Authenticate: Basic realm=\"\"\r\n\r\nUnauthorized", code)
if err != nil {
fmt.Fprint((*req.conn), "HTTP/1.1 407 Unauthorized\r\nWWW-Authenticate: Basic realm=\"\"\r\n\r\nUnauthorized")
CloseConn(req.conn) CloseConn(req.conn)
err = errors.New("require auth header data")
return return
} }
isProxyAuthorization = true //log.Printf("Authorization:%authorization = req.getHeader("Authorization")
}
//log.Printf("Authorization:%s", authorization)
basic := strings.Fields(authorization) basic := strings.Fields(authorization)
if len(basic) != 2 { if len(basic) != 2 {
err = fmt.Errorf("authorization data error,ERR:%s", authorization) err = fmt.Errorf("authorization data error,ERR:%s", authorization)
@ -433,15 +427,11 @@ func (req *HTTPRequest) BasicAuth() (err error) {
if req.IsHTTPS() { if req.IsHTTPS() {
URL = "https://" + req.Host URL = "https://" + req.Host
} else { } else {
URL, _ = req.getHTTPURL() URL = req.getHTTPURL()
} }
authOk := (*req.basicAuth).Check(string(user), addr[0], URL) authOk := (*req.basicAuth).Check(string(user), addr[0], URL)
//log.Printf("auth %s,%v", string(user), authOk) //log.Printf("auth %s,%v", string(user), authOk)
if !authOk { if !authOk {
code := "401"
if isProxyAuthorization {
code = "407"
}
fmt.Fprintf((*req.conn), "HTTP/1.1 %s Unauthorized\r\n\r\nUnauthorized", code) fmt.Fprintf((*req.conn), "HTTP/1.1 %s Unauthorized\r\n\r\nUnauthorized", code)
CloseConn(req.conn) CloseConn(req.conn)
err = fmt.Errorf("basic auth fail") err = fmt.Errorf("basic auth fail")
@ -449,18 +439,18 @@ func (req *HTTPRequest) BasicAuth() (err error) {
} }
return return
} }
func (req *HTTPRequest) getHTTPURL() (URL string, err error) { func (req *HTTPRequest) getHTTPURL() (URL string) {
if !strings.HasPrefix(req.hostOrURL, "/") { if !strings.HasPrefix(req.hostOrURL, "/") {
return req.hostOrURL, nil return req.hostOrURL
} }
_host, err := req.getHeader("host") _host := req.getHeader("host")
if err != nil { if _host == "" {
return return
} }
URL = fmt.Sprintf("http://%s%s", _host, req.hostOrURL) URL = fmt.Sprintf("http://%s%s", _host, req.hostOrURL)
return return
} }
func (req *HTTPRequest) getHeader(key string) (val string, err error) { func (req *HTTPRequest) getHeader(key string) (val string) {
key = strings.ToUpper(key) key = strings.ToUpper(key)
lines := strings.Split(string(req.HeadBuf), "\r\n") lines := strings.Split(string(req.HeadBuf), "\r\n")
//log.Println(lines) //log.Println(lines)