include http req and out in wrpReq sturct

This commit is contained in:
Antoni Sawicki 2020-04-24 00:47:11 -07:00
parent a258f603b3
commit f69a6e5219

146
wrp.go
View File

@ -63,84 +63,86 @@ type wrpReq struct {
K string // keys to send K string // keys to send
F string // Fn buttons F string // Fn buttons
T string // imgtype T string // imgtype
o http.ResponseWriter
r *http.Request
} }
func (w *wrpReq) parseForm(req *http.Request) { func (w *wrpReq) parseForm() {
req.ParseForm() w.r.ParseForm()
w.U = req.FormValue("url") w.U = w.r.FormValue("url")
if len(w.U) > 1 && !strings.HasPrefix(w.U, "http") { if len(w.U) > 1 && !strings.HasPrefix(w.U, "http") {
w.U = fmt.Sprintf("http://www.google.com/search?q=%s", url.QueryEscape(w.U)) w.U = fmt.Sprintf("http://www.google.com/search?q=%s", url.QueryEscape(w.U))
} }
w.W, _ = strconv.ParseInt(req.FormValue("w"), 10, 64) w.W, _ = strconv.ParseInt(w.r.FormValue("w"), 10, 64)
w.H, _ = strconv.ParseInt(req.FormValue("h"), 10, 64) w.H, _ = strconv.ParseInt(w.r.FormValue("h"), 10, 64)
if w.W < 10 && w.H < 10 { if w.W < 10 && w.H < 10 {
w.W = defgeom.w w.W = defgeom.w
w.H = defgeom.h w.H = defgeom.h
} }
w.S, _ = strconv.ParseFloat(req.FormValue("s"), 64) w.S, _ = strconv.ParseFloat(w.r.FormValue("s"), 64)
if w.S < 0.1 { if w.S < 0.1 {
w.S = 1.0 w.S = 1.0
} }
w.C, _ = strconv.ParseInt(req.FormValue("c"), 10, 64) w.C, _ = strconv.ParseInt(w.r.FormValue("c"), 10, 64)
if w.C < 2 || w.C > 256 { if w.C < 2 || w.C > 256 {
w.C = defgeom.c w.C = defgeom.c
} }
w.K = req.FormValue("k") w.K = w.r.FormValue("k")
w.F = req.FormValue("Fn") w.F = w.r.FormValue("Fn")
w.T = req.FormValue("t") w.T = w.r.FormValue("t")
if w.T != "gif" && w.T != "png" { if w.T != "gif" && w.T != "png" {
w.T = deftype w.T = deftype
} }
log.Printf("%s WrpReq from Form: %+v\n", req.RemoteAddr, w) log.Printf("%s WrpReq from Form: %+v\n", w.r.RemoteAddr, w)
} }
func (w wrpReq) printPage(out http.ResponseWriter, bgcolor string) { func (w wrpReq) printPage(bgcolor string) {
var s string var s string
out.Header().Set("Cache-Control", "max-age=0") w.o.Header().Set("Cache-Control", "max-age=0")
out.Header().Set("Expires", "-1") w.o.Header().Set("Expires", "-1")
out.Header().Set("Pragma", "no-cache") w.o.Header().Set("Pragma", "no-cache")
out.Header().Set("Content-Type", "text/html") w.o.Header().Set("Content-Type", "text/html")
fmt.Fprintf(out, "<!-- Web Rendering Proxy Version %s -->\n", version) fmt.Fprintf(w.o, "<!-- Web Rendering Proxy Version %s -->\n", version)
fmt.Fprintf(out, "<HTML>\n<HEAD><TITLE>WRP %s</TITLE></HEAD>\n<BODY BGCOLOR=\"%s\">\n", w.U, bgcolor) fmt.Fprintf(w.o, "<HTML>\n<HEAD><TITLE>WRP %s</TITLE></HEAD>\n<BODY BGCOLOR=\"%s\">\n", w.U, bgcolor)
fmt.Fprintf(out, "<FORM ACTION=\"/\" METHOD=\"POST\">\n") fmt.Fprintf(w.o, "<FORM ACTION=\"/\" METHOD=\"POST\">\n")
fmt.Fprintf(out, "<INPUT TYPE=\"TEXT\" NAME=\"url\" VALUE=\"%s\" SIZE=\"20\">", w.U) fmt.Fprintf(w.o, "<INPUT TYPE=\"TEXT\" NAME=\"url\" VALUE=\"%s\" SIZE=\"20\">", w.U)
fmt.Fprintf(out, "<INPUT TYPE=\"SUBMIT\" VALUE=\"Go\">\n") fmt.Fprintf(w.o, "<INPUT TYPE=\"SUBMIT\" VALUE=\"Go\">\n")
fmt.Fprintf(out, "<INPUT TYPE=\"SUBMIT\" NAME=\"Fn\" VALUE=\"Bk\">\n") fmt.Fprintf(w.o, "<INPUT TYPE=\"SUBMIT\" NAME=\"Fn\" VALUE=\"Bk\">\n")
fmt.Fprintf(out, "W <INPUT TYPE=\"TEXT\" NAME=\"w\" VALUE=\"%d\" SIZE=\"4\"> \n", w.W) fmt.Fprintf(w.o, "W <INPUT TYPE=\"TEXT\" NAME=\"w\" VALUE=\"%d\" SIZE=\"4\"> \n", w.W)
fmt.Fprintf(out, "H <INPUT TYPE=\"TEXT\" NAME=\"h\" VALUE=\"%d\" SIZE=\"4\"> \n", w.H) fmt.Fprintf(w.o, "H <INPUT TYPE=\"TEXT\" NAME=\"h\" VALUE=\"%d\" SIZE=\"4\"> \n", w.H)
fmt.Fprintf(out, "S <SELECT NAME=\"s\">\n") fmt.Fprintf(w.o, "S <SELECT NAME=\"s\">\n")
for _, v := range []float64{0.65, 0.75, 0.85, 0.95, 1.0, 1.05, 1.15, 1.25} { for _, v := range []float64{0.65, 0.75, 0.85, 0.95, 1.0, 1.05, 1.15, 1.25} {
if v == w.S { if v == w.S {
s = "SELECTED" s = "SELECTED"
} else { } else {
s = "" s = ""
} }
fmt.Fprintf(out, "<OPTION VALUE=\"%1.2f\" %s>%1.2f</OPTION>\n", v, s, v) fmt.Fprintf(w.o, "<OPTION VALUE=\"%1.2f\" %s>%1.2f</OPTION>\n", v, s, v)
} }
fmt.Fprintf(out, "</SELECT>\n") fmt.Fprintf(w.o, "</SELECT>\n")
fmt.Fprintf(out, "T <SELECT NAME=\"t\">\n") fmt.Fprintf(w.o, "T <SELECT NAME=\"t\">\n")
for _, v := range []string{"gif", "png"} { for _, v := range []string{"gif", "png"} {
if v == w.T { if v == w.T {
s = "SELECTED" s = "SELECTED"
} else { } else {
s = "" s = ""
} }
fmt.Fprintf(out, "<OPTION VALUE=\"%s\" %s>%s</OPTION>\n", v, s, strings.ToUpper(v)) fmt.Fprintf(w.o, "<OPTION VALUE=\"%s\" %s>%s</OPTION>\n", v, s, strings.ToUpper(v))
} }
fmt.Fprintf(out, "</SELECT>\n") fmt.Fprintf(w.o, "</SELECT>\n")
fmt.Fprintf(out, "C <INPUT TYPE=\"TEXT\" NAME=\"c\" VALUE=\"%d\" SIZE=\"3\">\n", w.C) fmt.Fprintf(w.o, "C <INPUT TYPE=\"TEXT\" NAME=\"c\" VALUE=\"%d\" SIZE=\"3\">\n", w.C)
fmt.Fprintf(out, "K <INPUT TYPE=\"TEXT\" NAME=\"k\" VALUE=\"\" SIZE=\"4\"> \n") fmt.Fprintf(w.o, "K <INPUT TYPE=\"TEXT\" NAME=\"k\" VALUE=\"\" SIZE=\"4\"> \n")
fmt.Fprintf(out, "<INPUT TYPE=\"SUBMIT\" NAME=\"Fn\" VALUE=\"Bs\">\n") fmt.Fprintf(w.o, "<INPUT TYPE=\"SUBMIT\" NAME=\"Fn\" VALUE=\"Bs\">\n")
fmt.Fprintf(out, "<INPUT TYPE=\"SUBMIT\" NAME=\"Fn\" VALUE=\"Rt\">\n") fmt.Fprintf(w.o, "<INPUT TYPE=\"SUBMIT\" NAME=\"Fn\" VALUE=\"Rt\">\n")
fmt.Fprintf(out, "<INPUT TYPE=\"SUBMIT\" NAME=\"Fn\" VALUE=\"&lt;\">\n") fmt.Fprintf(w.o, "<INPUT TYPE=\"SUBMIT\" NAME=\"Fn\" VALUE=\"&lt;\">\n")
fmt.Fprintf(out, "<INPUT TYPE=\"SUBMIT\" NAME=\"Fn\" VALUE=\"^\">\n") fmt.Fprintf(w.o, "<INPUT TYPE=\"SUBMIT\" NAME=\"Fn\" VALUE=\"^\">\n")
fmt.Fprintf(out, "<INPUT TYPE=\"SUBMIT\" NAME=\"Fn\" VALUE=\"v\">\n") fmt.Fprintf(w.o, "<INPUT TYPE=\"SUBMIT\" NAME=\"Fn\" VALUE=\"v\">\n")
fmt.Fprintf(out, "<INPUT TYPE=\"SUBMIT\" NAME=\"Fn\" VALUE=\"&gt;\" SIZE=\"1\">\n") fmt.Fprintf(w.o, "<INPUT TYPE=\"SUBMIT\" NAME=\"Fn\" VALUE=\"&gt;\" SIZE=\"1\">\n")
fmt.Fprintf(out, "</FORM><BR>\n") fmt.Fprintf(w.o, "</FORM><BR>\n")
} }
func (w wrpReq) printFooter(out http.ResponseWriter, h string, s string) { func (w wrpReq) printFooter(h string, s string) {
fmt.Fprintf(out, "\n<P><FONT SIZE=\"-2\"><A HREF=\"/?url=https://github.com/tenox7/wrp/&w=%d&h=%d&s=%1.2f&c=%d&t=%s\">"+ fmt.Fprintf(w.o, "\n<P><FONT SIZE=\"-2\"><A HREF=\"/?url=https://github.com/tenox7/wrp/&w=%d&h=%d&s=%1.2f&c=%d&t=%s\">"+
"Web Rendering Proxy Version %s</A> | <A HREF=\"/shutdown/\">Shutdown WRP</A> | "+ "Web Rendering Proxy Version %s</A> | <A HREF=\"/shutdown/\">Shutdown WRP</A> | "+
"<A HREF=\"/\">Page Height: %s</A> | <A HREF=\"/\">Img Size: %s</A></FONT></BODY>\n</HTML>\n", w.W, w.H, w.S, w.C, w.T, version, h, s) "<A HREF=\"/\">Page Height: %s</A> | <A HREF=\"/\">Img Size: %s</A></FONT></BODY>\n</HTML>\n", w.W, w.H, w.S, w.C, w.T, version, h, s)
} }
@ -148,18 +150,22 @@ func (w wrpReq) printFooter(out http.ResponseWriter, h string, s string) {
func pageServer(out http.ResponseWriter, req *http.Request) { func pageServer(out http.ResponseWriter, req *http.Request) {
log.Printf("%s Page Request for %s [%+v]\n", req.RemoteAddr, req.URL.Path, req.URL.RawQuery) log.Printf("%s Page Request for %s [%+v]\n", req.RemoteAddr, req.URL.Path, req.URL.RawQuery)
var w wrpReq var w wrpReq
w.parseForm(req) w.r = req
w.o = out
w.parseForm()
if len(w.U) > 4 { if len(w.U) > 4 {
w.capture(req.RemoteAddr, out) w.capture()
} else { } else {
w.printPage(out, "#FFFFFF") w.printPage("#FFFFFF")
w.printFooter(out, "", "") w.printFooter("", "")
} }
} }
func mapServer(out http.ResponseWriter, req *http.Request) { func mapServer(out http.ResponseWriter, req *http.Request) {
log.Printf("%s ISMAP Request for %s [%+v]\n", req.RemoteAddr, req.URL.Path, req.URL.RawQuery) log.Printf("%s ISMAP Request for %s [%+v]\n", req.RemoteAddr, req.URL.Path, req.URL.RawQuery)
w, ok := ismap[req.URL.Path] w, ok := ismap[req.URL.Path]
w.r = req
w.o = out
if !ok { if !ok {
fmt.Fprintf(out, "Unable to find map %s\n", req.URL.Path) fmt.Fprintf(out, "Unable to find map %s\n", req.URL.Path)
log.Printf("Unable to find map %s\n", req.URL.Path) log.Printf("Unable to find map %s\n", req.URL.Path)
@ -176,10 +182,10 @@ func mapServer(out http.ResponseWriter, req *http.Request) {
} }
log.Printf("%s WrpReq from ISMAP: %+v\n", req.RemoteAddr, w) log.Printf("%s WrpReq from ISMAP: %+v\n", req.RemoteAddr, w)
if len(w.U) > 4 { if len(w.U) > 4 {
w.capture(req.RemoteAddr, out) w.capture()
} else { } else {
w.printPage(out, "#FFFFFF") w.printPage("#FFFFFF")
w.printFooter(out, "", "") w.printFooter("", "")
} }
} }
@ -207,13 +213,13 @@ func imgServer(out http.ResponseWriter, req *http.Request) {
out.(http.Flusher).Flush() out.(http.Flusher).Flush()
} }
func (w wrpReq) capture(c string, out http.ResponseWriter) { func (w wrpReq) capture() {
var err error var err error
if w.X > 0 && w.Y > 0 { if w.X > 0 && w.Y > 0 {
log.Printf("%s Mouse Click %d,%d\n", c, w.X, w.Y) log.Printf("%s Mouse Click %d,%d\n", w.r.RemoteAddr, w.X, w.Y)
err = chromedp.Run(ctx, chromedp.MouseClickXY(float64(w.X)/float64(w.S), float64(w.Y)/float64(w.S))) err = chromedp.Run(ctx, chromedp.MouseClickXY(float64(w.X)/float64(w.S), float64(w.Y)/float64(w.S)))
} else if len(w.F) > 0 { } else if len(w.F) > 0 {
log.Printf("%s Button %v\n", c, w.F) log.Printf("%s Button %v\n", w.r.RemoteAddr, w.F)
switch w.F { switch w.F {
case "Bk": case "Bk":
err = chromedp.Run(ctx, chromedp.NavigateBack()) err = chromedp.Run(ctx, chromedp.NavigateBack())
@ -231,20 +237,20 @@ func (w wrpReq) capture(c string, out http.ResponseWriter) {
err = chromedp.Run(ctx, chromedp.KeyEvent("\u0303")) err = chromedp.Run(ctx, chromedp.KeyEvent("\u0303"))
} }
} else if len(w.K) > 0 { } else if len(w.K) > 0 {
log.Printf("%s Sending Keys: %#v\n", c, w.K) log.Printf("%s Sending Keys: %#v\n", w.r.RemoteAddr, w.K)
err = chromedp.Run(ctx, chromedp.KeyEvent(w.K)) err = chromedp.Run(ctx, chromedp.KeyEvent(w.K))
} else { } else {
log.Printf("%s Processing Capture Request for %s\n", c, w.U) log.Printf("%s Processing Capture Request for %s\n", w.r.RemoteAddr, w.U)
err = chromedp.Run(ctx, chromedp.Navigate(w.U)) err = chromedp.Run(ctx, chromedp.Navigate(w.U))
} }
if err != nil { if err != nil {
if err.Error() == "context canceled" { if err.Error() == "context canceled" {
log.Printf("%s Contex cancelled, try again", c) log.Printf("%s Contex cancelled, try again", w.r.RemoteAddr)
fmt.Fprintf(out, "<BR>%s<BR> -- restarting, try again", err) fmt.Fprintf(w.o, "<BR>%s<BR> -- restarting, try again", err)
ctx, cancel = chromedp.NewContext(context.Background()) ctx, cancel = chromedp.NewContext(context.Background())
} else { } else {
log.Printf("%s %s", c, err) log.Printf("%s %s", w.r.RemoteAddr, err)
fmt.Fprintf(out, "<BR>%s<BR>", err) fmt.Fprintf(w.o, "<BR>%s<BR>", err)
} }
return return
} }
@ -270,8 +276,8 @@ func (w wrpReq) capture(c string, out http.ResponseWriter) {
fmt.Sscanf(style.Value, "rgb(%d,%d,%d)", &r, &g, &b) fmt.Sscanf(style.Value, "rgb(%d,%d,%d)", &r, &g, &b)
} }
} }
log.Printf("%s Landed on: %s, Height: %v\n", c, w.U, h) log.Printf("%s Landed on: %s, Height: %v\n", w.r.RemoteAddr, w.U, h)
w.printPage(out, fmt.Sprintf("#%02X%02X%02X", r, g, b)) w.printPage(fmt.Sprintf("#%02X%02X%02X", r, g, b))
if w.H == 0 && h > 0 { if w.H == 0 && h > 0 {
chromedp.Run(ctx, emulation.SetDeviceMetricsOverride(int64(float64(w.W)/w.S), h+30, w.S, false)) chromedp.Run(ctx, emulation.SetDeviceMetricsOverride(int64(float64(w.W)/w.S), h+30, w.S, false))
} else { } else {
@ -279,8 +285,8 @@ func (w wrpReq) capture(c string, out http.ResponseWriter) {
} }
err = chromedp.Run(ctx, chromedp.CaptureScreenshot(&pngcap)) err = chromedp.Run(ctx, chromedp.CaptureScreenshot(&pngcap))
if err != nil { if err != nil {
log.Printf("%s Failed to capture screenshot: %s\n", c, err) log.Printf("%s Failed to capture screenshot: %s\n", w.r.RemoteAddr, err)
fmt.Fprintf(out, "<BR>Unable to capture screenshot:<BR>%s<BR>\n", err) fmt.Fprintf(w.o, "<BR>Unable to capture screenshot:<BR>%s<BR>\n", err)
return return
} }
seq := rand.Intn(9999) seq := rand.Intn(9999)
@ -292,22 +298,22 @@ func (w wrpReq) capture(c string, out http.ResponseWriter) {
if w.T == "gif" { if w.T == "gif" {
i, err := png.Decode(bytes.NewReader(pngcap)) i, err := png.Decode(bytes.NewReader(pngcap))
if err != nil { if err != nil {
log.Printf("%s Failed to decode screenshot: %s\n", c, err) log.Printf("%s Failed to decode screenshot: %s\n", w.r.RemoteAddr, err)
fmt.Fprintf(out, "<BR>Unable to decode page screenshot:<BR>%s<BR>\n", err) fmt.Fprintf(w.o, "<BR>Unable to decode page screenshot:<BR>%s<BR>\n", err)
return return
} }
var gifbuf bytes.Buffer var gifbuf bytes.Buffer
err = gif.Encode(&gifbuf, i, &gif.Options{NumColors: int(w.C), Quantizer: quantize.MedianCutQuantizer{}}) err = gif.Encode(&gifbuf, i, &gif.Options{NumColors: int(w.C), Quantizer: quantize.MedianCutQuantizer{}})
if err != nil { if err != nil {
log.Printf("%s Failed to encode GIF: %s\n", c, err) log.Printf("%s Failed to encode GIF: %s\n", w.r.RemoteAddr, err)
fmt.Fprintf(out, "<BR>Unable to encode GIF:<BR>%s<BR>\n", err) fmt.Fprintf(w.o, "<BR>Unable to encode GIF:<BR>%s<BR>\n", err)
return return
} }
img[imgpath] = gifbuf img[imgpath] = gifbuf
ssize = fmt.Sprintf("%.1f MB", float32(len(gifbuf.Bytes()))/1024.0/1024.0) ssize = fmt.Sprintf("%.1f MB", float32(len(gifbuf.Bytes()))/1024.0/1024.0)
sw = i.Bounds().Max.X sw = i.Bounds().Max.X
sh = i.Bounds().Max.Y sh = i.Bounds().Max.Y
log.Printf("%s Encoded GIF image: %s, Size: %s, Colors: %d, %dx%d\n", c, imgpath, ssize, w.C, sw, sh) log.Printf("%s Encoded GIF image: %s, Size: %s, Colors: %d, %dx%d\n", w.r.RemoteAddr, imgpath, ssize, w.C, sw, sh)
} else if w.T == "png" { } else if w.T == "png" {
pngbuf := bytes.NewBuffer(pngcap) pngbuf := bytes.NewBuffer(pngcap)
img[imgpath] = *pngbuf img[imgpath] = *pngbuf
@ -315,11 +321,11 @@ func (w wrpReq) capture(c string, out http.ResponseWriter) {
ssize = fmt.Sprintf("%.1f MB", float32(len(pngbuf.Bytes()))/1024.0/1024.0) ssize = fmt.Sprintf("%.1f MB", float32(len(pngbuf.Bytes()))/1024.0/1024.0)
sw = cfg.Width sw = cfg.Width
sh = cfg.Height sh = cfg.Height
log.Printf("%s Got PNG image: %s, Size: %s, %dx%d\n", c, imgpath, ssize, sw, sh) log.Printf("%s Got PNG image: %s, Size: %s, %dx%d\n", w.r.RemoteAddr, imgpath, ssize, sw, sh)
} }
fmt.Fprintf(out, "<A HREF=\"%s\"><IMG SRC=\"%s\" BORDER=\"0\" ALT=\"Url: %s, Size: %s\" WIDTH=\"%d\" HEIGHT=\"%d\" ISMAP></A>", mappath, imgpath, w.U, ssize, sw, sh) fmt.Fprintf(w.o, "<A HREF=\"%s\"><IMG SRC=\"%s\" BORDER=\"0\" ALT=\"Url: %s, Size: %s\" WIDTH=\"%d\" HEIGHT=\"%d\" ISMAP></A>", mappath, imgpath, w.U, ssize, sw, sh)
w.printFooter(out, fmt.Sprintf("%d PX", h), ssize) w.printFooter(fmt.Sprintf("%d PX", h), ssize)
log.Printf("%s Done with caputure for %s\n", c, w.U) log.Printf("%s Done with caputure for %s\n", w.r.RemoteAddr, w.U)
} }
func haltServer(out http.ResponseWriter, req *http.Request) { func haltServer(out http.ResponseWriter, req *http.Request) {