add experimental proxy mode

This commit is contained in:
Antoni Sawicki
2026-03-14 00:38:47 -07:00
parent 64c2a16085
commit a70f1a9ebf
3 changed files with 74 additions and 14 deletions

View File

@@ -284,15 +284,22 @@ func (rq *wrpReq) captureScreenshot() {
iH = i.Bounds().Max.Y
log.Printf("%s Encoded JPG image: %s, Size: %s, Quality: %d, Res: %dx%d, Time: %vms\n", rq.r.RemoteAddr, imgPath, sSize, *defJpgQual, iW, iH, time.Since(st).Milliseconds())
}
rq.printUI(uiParams{
bgColor: "#FFFFFF",
pageHeight: fmt.Sprintf("%d PX", h),
imgSize: sSize,
imgURL: imgPath,
mapURL: mapPath,
imgWidth: iW,
imgHeight: iH,
})
if rq.proxy {
rq.w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(rq.w, "<HTML><HEAD><TITLE>%s</TITLE></HEAD><BODY BGCOLOR=\"%s\">"+
"<A HREF=\"%s\"><IMG SRC=\"%s\" BORDER=\"0\" WIDTH=\"%d\" HEIGHT=\"%d\" ISMAP></A>"+
"</BODY></HTML>", rq.url, *bgColor, mapPath, imgPath, iW, iH)
} else {
rq.printUI(uiParams{
bgColor: *bgColor,
pageHeight: fmt.Sprintf("%d PX", h),
imgSize: sSize,
imgURL: imgPath,
mapURL: mapPath,
imgWidth: iW,
imgHeight: iH,
})
}
log.Printf("%s Done with capture for %s\n", rq.r.RemoteAddr, rq.url)
}
@@ -317,7 +324,7 @@ func mapServer(w http.ResponseWriter, r *http.Request) {
}
log.Printf("%s WrpReq from ISMAP: %+v\n", r.RemoteAddr, rq)
if len(rq.url) < 4 {
rq.printUI(uiParams{bgColor: "#FFFFFF"})
rq.printUI(uiParams{})
return
}
rq.navigate() // TODO: if error from navigate do not capture

View File

@@ -277,7 +277,12 @@ func simplifyDOM(doc *goquery.Document, rq *wrpReq) int {
return
}
abs := resolveURL(href, baseURL)
s.SetAttr("href", "/?"+wrpParams+"&url="+url.QueryEscape(abs))
if rq.proxy {
abs = strings.Replace(abs, "https://", "http://", 1)
s.SetAttr("href", abs)
} else {
s.SetAttr("href", "/?"+wrpParams+"&url="+url.QueryEscape(abs))
}
})
type imgJob struct {
@@ -357,9 +362,15 @@ func (rq *wrpReq) captureMarkdown() {
}
log.Printf("Simplified to %v bytes html for %v", len(simplified), rq.url)
if rq.proxy {
rq.w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(rq.w, "<HTML><HEAD><TITLE>%s</TITLE></HEAD><BODY BGCOLOR=\"%s\">%s</BODY></HTML>",
rq.url, *bgColor, string(asciify([]byte(simplified))))
return
}
rq.printUI(uiParams{
text: string(asciify([]byte(simplified))),
bgColor: "#FFFFFF",
bgColor: *bgColor,
imgSize: fmt.Sprintf("%.0f KB", float32(totSize)/1024.0),
})
}

46
wrp.go
View File

@@ -45,6 +45,7 @@ var (
browserPath = flag.String("b", "", "browser executable path (e.g., /Applications/Brave Browser.app/Contents/MacOS/Brave Browser)")
searchEng = flag.String("se", "https://duckduckgo.com/search?q=", "Search engine string")
userDataDir = flag.String("profile", "", "Chrome user data dir for persistent cookies/sessions")
bgColor = flag.String("bgcolor", "#F0F0F0", "Background color for WRP UI")
)
var (
@@ -118,6 +119,7 @@ type wrpReq struct {
imgType string
wrpMode string
maxSize int64
proxy bool
w http.ResponseWriter
r *http.Request
}
@@ -172,7 +174,7 @@ func (rq *wrpReq) printUI(p uiParams) {
rq.w.Header().Set("Pragma", "no-cache")
rq.w.Header().Set("Content-Type", "text/html")
if p.bgColor == "" {
p.bgColor = "#FFFFFF"
p.bgColor = *bgColor
}
data := uiData{
Version: version,
@@ -200,7 +202,47 @@ 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 == "" {
purl = "http://" + r.Host + r.URL.RequestURI()
}
log.Printf("%s Proxy Request for %s\n", r.RemoteAddr, purl)
rq := wrpReq{
r: r,
w: w,
url: purl,
width: defGeom.w,
height: defGeom.h,
nColors: defGeom.c,
zoom: 1.0,
imgType: *defType,
wrpMode: *wrpMode,
maxSize: *defImgSize,
jQual: *defJpgQual,
proxy: true,
}
rq.navigate()
if rq.wrpMode == "html" {
rq.captureMarkdown()
return
}
rq.captureScreenshot()
}
func isProxyRequest(r *http.Request) bool {
return r.URL.IsAbs() || r.Method == "CONNECT"
}
func pageServer(w http.ResponseWriter, r *http.Request) {
if isProxyRequest(r) {
proxyServer(w, r)
return
}
log.Printf("%s Page Request for %s [%+v]\n", r.RemoteAddr, r.URL.Path, r.URL.RawQuery)
rq := wrpReq{
r: r,
@@ -208,7 +250,7 @@ func pageServer(w http.ResponseWriter, r *http.Request) {
}
rq.parseForm()
if len(rq.url) < 4 {
rq.printUI(uiParams{bgColor: "#FFFFFF"})
rq.printUI(uiParams{})
return
}
rq.navigate() // TODO: if error from navigate do not capture