github.com/igoogolx/clash@v1.19.8/listener/http/utils.go (about) 1 package http 2 3 import ( 4 "encoding/base64" 5 "errors" 6 "net" 7 "net/http" 8 "strings" 9 ) 10 11 // removeHopByHopHeaders remove Proxy-* headers 12 func removeProxyHeaders(header http.Header) { 13 header.Del("Proxy-Connection") 14 header.Del("Proxy-Authenticate") 15 header.Del("Proxy-Authorization") 16 } 17 18 // removeHopByHopHeaders remove hop-by-hop header 19 func removeHopByHopHeaders(header http.Header) { 20 // Strip hop-by-hop header based on RFC: 21 // http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.5.1 22 // https://www.mnot.net/blog/2011/07/11/what_proxies_must_do 23 24 removeProxyHeaders(header) 25 26 header.Del("TE") 27 header.Del("Trailers") 28 header.Del("Transfer-Encoding") 29 header.Del("Upgrade") 30 31 connections := header.Get("Connection") 32 header.Del("Connection") 33 if len(connections) == 0 { 34 return 35 } 36 for _, h := range strings.Split(connections, ",") { 37 header.Del(strings.TrimSpace(h)) 38 } 39 } 40 41 // removeExtraHTTPHostPort remove extra host port (example.com:80 --> example.com) 42 // It resolves the behavior of some HTTP servers that do not handle host:80 (e.g. baidu.com) 43 func removeExtraHTTPHostPort(req *http.Request) { 44 host := req.Host 45 if host == "" { 46 host = req.URL.Host 47 } 48 49 if pHost, port, err := net.SplitHostPort(host); err == nil && port == "80" { 50 host = pHost 51 } 52 53 req.Host = host 54 req.URL.Host = host 55 } 56 57 // parseBasicProxyAuthorization parse header Proxy-Authorization and return base64-encoded credential 58 func parseBasicProxyAuthorization(request *http.Request) string { 59 value := request.Header.Get("Proxy-Authorization") 60 if !strings.HasPrefix(value, "Basic ") { 61 return "" 62 } 63 64 return value[6:] // value[len("Basic "):] 65 } 66 67 // decodeBasicProxyAuthorization decode base64-encoded credential 68 func decodeBasicProxyAuthorization(credential string) (string, string, error) { 69 plain, err := base64.StdEncoding.DecodeString(credential) 70 if err != nil { 71 return "", "", err 72 } 73 74 user, pass, found := strings.Cut(string(plain), ":") 75 if !found { 76 return "", "", errors.New("invalid login") 77 } 78 79 return user, pass, nil 80 }