fixup connect mode

This commit is contained in:
Antoni Sawicki
2026-03-14 20:59:27 -07:00
parent 90dc64a625
commit 61af9046ae
3 changed files with 38 additions and 13 deletions

View File

@@ -246,6 +246,9 @@ func (rq *wrpReq) captureScreenshot() {
return nil 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) log.Printf("%s Landed on: %s, Height: %v\n", rq.r.RemoteAddr, rq.url, h)
height := int64(float64(rq.height) / rq.zoom) height := int64(float64(rq.height) / rq.zoom)
if rq.height == 0 && h > 0 { if rq.height == 0 && h > 0 {
@@ -341,9 +344,9 @@ func (rq *wrpReq) captureScreenshot() {
} }
if rq.proxy { if rq.proxy {
rq.w.Header().Set("Content-Type", "text/html") rq.w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(rq.w, "<HTML><HEAD><TITLE>%s</TITLE></HEAD><BODY BGCOLOR=\"%s\">"+ fmt.Fprintf(rq.w, "<HTML><HEAD>%s<TITLE>%s</TITLE></HEAD><BODY BGCOLOR=\"%s\">"+
"<A HREF=\"%s\"><IMG SRC=\"%s\" BORDER=\"0\" WIDTH=\"%d\" HEIGHT=\"%d\" ISMAP></A>"+ "<A HREF=\"%s\"><IMG SRC=\"%s\" BORDER=\"0\" WIDTH=\"%d\" HEIGHT=\"%d\" ISMAP></A>"+
"</BODY></HTML>", rq.url, *bgColor, mapPath, imgPath, iW, iH) "</BODY></HTML>", rq.baseTag(), rq.url, *bgColor, mapPath, imgPath, iW, iH)
} else { } else {
rq.printUI(uiParams{ rq.printUI(uiParams{
bgColor: *bgColor, bgColor: *bgColor,

View File

@@ -371,8 +371,8 @@ func (rq *wrpReq) captureMarkdown() {
if rq.proxy { if rq.proxy {
rq.w.Header().Set("Content-Type", "text/html") rq.w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(rq.w, "<HTML><HEAD><TITLE>%s</TITLE></HEAD><BODY BGCOLOR=\"%s\">%s</BODY></HTML>", fmt.Fprintf(rq.w, "<HTML><HEAD>%s<TITLE>%s</TITLE></HEAD><BODY BGCOLOR=\"%s\">%s</BODY></HTML>",
rq.url, *bgColor, string(asciify([]byte(simplified)))) rq.baseTag(), rq.url, *bgColor, string(asciify([]byte(simplified))))
return return
} }
rq.printUI(uiParams{ rq.printUI(uiParams{

40
wrp.go
View File

@@ -122,6 +122,16 @@ type wrpReq struct {
r *http.Request 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(`<BASE HREF="http://%v/">`, addr)
}
return ""
}
func (rq *wrpReq) parseForm() { func (rq *wrpReq) parseForm() {
rq.r.ParseForm() rq.r.ParseForm()
rq.wrpMode = rq.r.FormValue("m") rq.wrpMode = rq.r.FormValue("m")
@@ -201,13 +211,14 @@ func (rq *wrpReq) printUI(p uiParams) {
} }
func proxyServer(w http.ResponseWriter, r *http.Request) { func proxyServer(w http.ResponseWriter, r *http.Request) {
if r.Method == "CONNECT" { var purl string
http.Error(w, "CONNECT not supported, use http:// URLs", http.StatusMethodNotAllowed) switch {
return case r.Method == "CONNECT":
} purl = "https://" + r.Host
purl := r.URL.String() case r.URL.Scheme == "":
if r.URL.Scheme == "" {
purl = "http://" + r.Host + r.URL.RequestURI() purl = "http://" + r.Host + r.URL.RequestURI()
default:
purl = r.URL.String()
} }
log.Printf("%s Proxy Request for %s\n", r.RemoteAddr, purl) log.Printf("%s Proxy Request for %s\n", r.RemoteAddr, purl)
rq := wrpReq{ rq := wrpReq{
@@ -227,9 +238,14 @@ func proxyServer(w http.ResponseWriter, r *http.Request) {
var currentURL string var currentURL string
chromedp.Run(ctx, chromedp.Location(&currentURL)) chromedp.Run(ctx, chromedp.Location(&currentURL))
currentURL = strings.Replace(currentURL, "https://", "http://", 1) currentURL = strings.Replace(currentURL, "https://", "http://", 1)
if currentURL != rq.url { if currentURL != strings.Replace(rq.url, "https://", "http://", 1) {
rq.navigate() 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" { if rq.wrpMode == "html" {
rq.captureMarkdown() rq.captureMarkdown()
return return
@@ -242,6 +258,7 @@ func isProxyRequest(r *http.Request) bool {
} }
func pageServer(w http.ResponseWriter, r *http.Request) { 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) { if isProxyRequest(r) {
proxyServer(w, r) proxyServer(w, r)
return return
@@ -267,8 +284,6 @@ func pageServer(w http.ResponseWriter, r *http.Request) {
func pacServer(w http.ResponseWriter, r *http.Request) { func pacServer(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/x-ns-proxy-autoconfig") w.Header().Set("Content-Type", "application/x-ns-proxy-autoconfig")
fmt.Fprintf(w, `function FindProxyForURL(url, host) { fmt.Fprintf(w, `function FindProxyForURL(url, host) {
if (url.substring(0, 6) == "https:")
return "DIRECT";
if (isPlainHostName(host) || if (isPlainHostName(host) ||
host == "localhost" || host == "localhost" ||
isInNet(host, "127.0.0.0", "255.0.0.0") || isInNet(host, "127.0.0.0", "255.0.0.0") ||
@@ -375,6 +390,13 @@ func main() {
log.Print("Starting WRP http server") log.Print("Starting WRP http server")
srv.Addr = *addr 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() err = srv.ListenAndServe()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)