diff --git a/ismap.go b/ismap.go index 0402e15..e880580 100644 --- a/ismap.go +++ b/ismap.go @@ -246,6 +246,9 @@ func (rq *wrpReq) captureScreenshot() { return nil }), ) + if rq.proxy { + rq.url = strings.Replace(rq.url, "https://", "http://", 1) + } log.Printf("%s Landed on: %s, Height: %v\n", rq.r.RemoteAddr, rq.url, h) height := int64(float64(rq.height) / rq.zoom) if rq.height == 0 && h > 0 { @@ -341,9 +344,9 @@ func (rq *wrpReq) captureScreenshot() { } if rq.proxy { rq.w.Header().Set("Content-Type", "text/html") - fmt.Fprintf(rq.w, "%s"+ + fmt.Fprintf(rq.w, "%s%s"+ ""+ - "", rq.url, *bgColor, mapPath, imgPath, iW, iH) + "", rq.baseTag(), rq.url, *bgColor, mapPath, imgPath, iW, iH) } else { rq.printUI(uiParams{ bgColor: *bgColor, diff --git a/shtml.go b/shtml.go index 803494a..452d482 100644 --- a/shtml.go +++ b/shtml.go @@ -371,8 +371,8 @@ func (rq *wrpReq) captureMarkdown() { if rq.proxy { rq.w.Header().Set("Content-Type", "text/html") - fmt.Fprintf(rq.w, "%s%s", - rq.url, *bgColor, string(asciify([]byte(simplified)))) + fmt.Fprintf(rq.w, "%s%s%s", + rq.baseTag(), rq.url, *bgColor, string(asciify([]byte(simplified)))) return } rq.printUI(uiParams{ diff --git a/wrp.go b/wrp.go index 631becb..25401a6 100644 --- a/wrp.go +++ b/wrp.go @@ -122,6 +122,16 @@ type wrpReq struct { r *http.Request } +func (rq *wrpReq) baseTag() string { + if rq.r.Method != "CONNECT" { + return "" + } + if addr := rq.r.Context().Value(http.LocalAddrContextKey); addr != nil { + return fmt.Sprintf(``, addr) + } + return "" +} + func (rq *wrpReq) parseForm() { rq.r.ParseForm() rq.wrpMode = rq.r.FormValue("m") @@ -201,13 +211,14 @@ func (rq *wrpReq) printUI(p uiParams) { } func proxyServer(w http.ResponseWriter, r *http.Request) { - if r.Method == "CONNECT" { - http.Error(w, "CONNECT not supported, use http:// URLs", http.StatusMethodNotAllowed) - return - } - purl := r.URL.String() - if r.URL.Scheme == "" { + var purl string + switch { + case r.Method == "CONNECT": + purl = "https://" + r.Host + case r.URL.Scheme == "": purl = "http://" + r.Host + r.URL.RequestURI() + default: + purl = r.URL.String() } log.Printf("%s Proxy Request for %s\n", r.RemoteAddr, purl) rq := wrpReq{ @@ -227,9 +238,14 @@ func proxyServer(w http.ResponseWriter, r *http.Request) { var currentURL string chromedp.Run(ctx, chromedp.Location(¤tURL)) currentURL = strings.Replace(currentURL, "https://", "http://", 1) - if currentURL != rq.url { + if currentURL != strings.Replace(rq.url, "https://", "http://", 1) { rq.navigate() } + rq.url = strings.Replace(rq.url, "https://", "http://", 1) + if r.Method == "CONNECT" { + w.Header().Set("Content-Type", "text/html") + w.WriteHeader(http.StatusBadGateway) + } if rq.wrpMode == "html" { rq.captureMarkdown() return @@ -242,6 +258,7 @@ func isProxyRequest(r *http.Request) bool { } func pageServer(w http.ResponseWriter, r *http.Request) { + log.Printf("%s %s %s [%+v]\n", r.RemoteAddr, r.Method, r.URL, r.Host) if isProxyRequest(r) { proxyServer(w, r) return @@ -267,8 +284,6 @@ func pageServer(w http.ResponseWriter, r *http.Request) { func pacServer(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/x-ns-proxy-autoconfig") fmt.Fprintf(w, `function FindProxyForURL(url, host) { - if (url.substring(0, 6) == "https:") - return "DIRECT"; if (isPlainHostName(host) || host == "localhost" || isInNet(host, "127.0.0.0", "255.0.0.0") || @@ -375,6 +390,13 @@ func main() { log.Print("Starting WRP http server") srv.Addr = *addr + srv.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == "CONNECT" { + pageServer(w, r) + return + } + http.DefaultServeMux.ServeHTTP(w, r) + }) err = srv.ListenAndServe() if err != nil { log.Fatal(err)