diff --git a/wrp.go b/wrp.go index df95360..5c97cba 100644 --- a/wrp.go +++ b/wrp.go @@ -23,96 +23,122 @@ import ( "time" "github.com/chromedp/cdproto/emulation" - "github.com/chromedp/cdproto/runtime" - "github.com/chromedp/cdproto/cdp" - "github.com/chromedp/cdproto/dom" "github.com/chromedp/chromedp" "github.com/ericpauley/go-quantize/quantize" ) -// Ismap for server side processing -type Ismap struct { - xmin int64 - ymin int64 - xmax int64 - ymax int64 - url string -} - var ( - version = "3.0" + version = "4.0" srv http.Server ctx context.Context cancel context.CancelFunc gifmap = make(map[string]bytes.Buffer) - ismap = make(map[string][]Ismap) + ismap = make(map[string]wrpReq) ) -func pageServer(out http.ResponseWriter, req *http.Request) { +type wrpReq struct { + U string // url + W int64 // width + H int64 // height + S float64 // scale + C int64 // #colors + X int64 // mouseX + Y int64 // mouseY + K string // keys to send + B bool // history back +} + +func (w *wrpReq) parseForm(req *http.Request) { req.ParseForm() - u := req.FormValue("url") - var istr string - var ion string - var i bool - if req.FormValue("i") == "on" { - istr = "CHECKED" - i = true - ion = "&i=on" - } else { - istr = "" - i = false + w.U = req.FormValue("url") + 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)) } - p, _ := strconv.ParseInt(req.FormValue("p"), 10, 64) - if req.FormValue("pg") == "Dn" { - p++ - } else if req.FormValue("pg") == "Up" { - p-- - } else { - p = 0 + w.W, _ = strconv.ParseInt(req.FormValue("w"), 10, 64) + if w.W < 10 { + w.W = 1152 } - w, _ := strconv.ParseInt(req.FormValue("w"), 10, 64) - if w < 10 { - w = 1024 + w.H, _ = strconv.ParseInt(req.FormValue("h"), 10, 64) + if w.H < 10 { + w.H = 600 } - h, _ := strconv.ParseInt(req.FormValue("h"), 10, 64) - if h < 10 { - h = 768 + w.S, _ = strconv.ParseFloat(req.FormValue("s"), 64) + if w.S < 0.1 { + w.S = 1.0 } - s, _ := strconv.ParseFloat(req.FormValue("s"), 64) - if s < 0.1 { - s = 1.0 + w.C, _ = strconv.ParseInt(req.FormValue("c"), 10, 64) + if w.C < 2 || w.C > 256 { + w.C = 256 } - c, _ := strconv.ParseInt(req.FormValue("c"), 10, 64) - if c < 2 || c > 256 { - c = 256 + w.K = req.FormValue("k") + if w.K == "\\b" { + w.K = "\b" + } else if w.K == "\\r" { + w.K = "\r" } - log.Printf("%s Page Request for url=\"%s\" [%s]\n", req.RemoteAddr, u, req.URL.Path) + if req.FormValue("hist") == "Back" { + w.B = true + } + log.Printf("WrpReq from Form: %+v\n", w) +} + +func (w wrpReq) printPage(out http.ResponseWriter) { out.Header().Set("Content-Type", "text/html") fmt.Fprintf(out, "\n", version) - fmt.Fprintf(out, "\n
"+ + "Web Rendering Proxy Version %s | Shutdown WRP\n\n", w.W, w.H, w.S, w.C, version) +} + +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) + var w wrpReq + w.parseForm(req) + + if len(w.U) > 4 { + w.capture(req.RemoteAddr, out) } else { - fmt.Fprintf(out, "No URL or search query specified") + w.printPage(out) + w.printFooter(out) + } +} + +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) + w, ok := ismap[req.URL.Path] + if !ok { + fmt.Fprintf(out, "Unable to find map %s\n", req.URL.Path) + log.Printf("Unable to find map %s\n", req.URL.Path) + return + } + defer delete(ismap, req.URL.Path) + n, err := fmt.Sscanf(req.URL.RawQuery, "%d,%d", &w.X, &w.Y) + if err != nil || n != 2 { + fmt.Fprintf(out, "n=%d, err=%s\n", n, err) + log.Printf("%s ISMAP n=%d, err=%s\n", req.RemoteAddr, n, err) + return + } + log.Printf("%s WrpReq from ISMAP: %+v\n", req.RemoteAddr, w) + if len(w.U) > 4 { + w.capture(req.RemoteAddr, out) + } else { + w.printPage(out) + w.printFooter(out) } - fmt.Fprintf(out, "\n
Web Rendering Proxy Version %s | Shutdown WRP\n\n", w, h, s, c, ion, version)
}
func imgServer(out http.ResponseWriter, req *http.Request) {
@@ -130,55 +156,33 @@ func imgServer(out http.ResponseWriter, req *http.Request) {
out.(http.Flusher).Flush()
}
-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)
- var loc string
- var x, y int64
- n, err := fmt.Sscanf(req.URL.RawQuery, "%d,%d", &x, &y)
- if err != nil || n != 2 {
- fmt.Fprintf(out, "n=%d, err=%s\n", n, err)
- log.Printf("%s ISMAP n=%d, err=%s\n", req.RemoteAddr, n, err)
- return
- }
- is, ok := ismap[req.URL.Path]
- if !ok || is == nil {
- fmt.Fprintf(out, "Unable to find map %s\n", req.URL.Path)
- log.Printf("Unable to find map %s\n", req.URL.Path)
- return
- }
- defer delete(ismap, req.URL.Path)
- for _, i := range is {
- if x >= i.xmin && x <= i.xmax && y >= i.ymin && y <= i.ymax {
- loc = i.url
- }
- }
- if len(loc) < 1 {
- loc = is[0].url
- }
- log.Printf("%s ISMAP Redirect to: http://%s%s\n", req.RemoteAddr, req.Context().Value(http.LocalAddrContextKey), loc)
- http.Redirect(out, req, fmt.Sprintf("http://%s%s", req.Context().Value(http.LocalAddrContextKey), loc), 301)
-}
-
-func capture(gourl string, w int64, h int64, s float64, co int, p int64, i bool, c string, out http.ResponseWriter) {
- var nodes []*cdp.Node
- ctxx := chromedp.FromContext(ctx)
+func (w wrpReq) capture(c string, out http.ResponseWriter) {
var pngbuf []byte
var gifbuf bytes.Buffer
- var loc string
- var res *runtime.RemoteObject
- is := make([]Ismap, 0)
- var ion string
+ var err error
- log.Printf("%s Processing Capture Request for %s\n", c, gourl)
-
- // Run ChromeDP Magic
- err := chromedp.Run(ctx,
- emulation.SetDeviceMetricsOverride(int64(float64(w)/s), int64(float64(h)/s), s, false),
- chromedp.Navigate(gourl),
- chromedp.Evaluate(fmt.Sprintf("window.scrollTo(0, %d);", p*int64(float64(h)*float64(0.9))), &res),
- chromedp.Sleep(time.Second*1),
- chromedp.CaptureScreenshot(&pngbuf),
- chromedp.Location(&loc))
+ if w.X > 0 && w.Y > 0 {
+ log.Printf("%s Mouse Click %d,%d\n", c, w.X, w.Y)
+ chromedp.Run(ctx,
+ chromedp.MouseClickXY(int64(float64(w.X)/w.S), int64(float64(w.Y)/w.S)),
+ )
+ } else if w.B {
+ log.Printf("%s History Back\n", c)
+ chromedp.Run(ctx,
+ chromedp.NavigateBack(),
+ )
+ } else if len(w.K) > 0 {
+ log.Printf("%s Sending Keys: %#v\n", c, w.K)
+ err = chromedp.Run(ctx,
+ chromedp.KeyEvent(w.K),
+ )
+ } else {
+ log.Printf("%s Processing Capture Request for %s\n", c, w.U)
+ err = chromedp.Run(ctx,
+ emulation.SetDeviceMetricsOverride(int64(float64(w.W)/w.S), int64(float64(w.H)/w.S), w.S, false),
+ chromedp.Navigate(w.U),
+ )
+ }
if err != nil {
if err.Error() == "context canceled" {
@@ -192,9 +196,20 @@ func capture(gourl string, w int64, h int64, s float64, co int, p int64, i bool,
return
}
- log.Printf("%s Landed on: %s, Nodes: %d\n", c, loc, len(nodes))
+ chromedp.Run(
+ ctx, chromedp.Sleep(time.Second*3),
+ chromedp.Location(&w.U),
+ )
+ log.Printf("%s Landed on: %s\n", c, w.U)
+ w.printPage(out)
// Process Screenshot Image
+ err = chromedp.Run(ctx, chromedp.CaptureScreenshot(&pngbuf))
+ if err != nil {
+ log.Printf("%s Failed to capture screenshot: %s\n", c, err)
+ fmt.Fprintf(out, "
Unable to capture screenshot:
%s
\n", err)
+ return
+ }
bytes.NewReader(pngbuf).Seek(0, 0)
img, err := png.Decode(bytes.NewReader(pngbuf))
if err != nil {
@@ -203,63 +218,23 @@ func capture(gourl string, w int64, h int64, s float64, co int, p int64, i bool,
return
}
gifbuf.Reset()
- err = gif.Encode(&gifbuf, img, &gif.Options{NumColors: co, Quantizer: quantize.MedianCutQuantizer{}})
+ err = gif.Encode(&gifbuf, img, &gif.Options{NumColors: int(w.C), Quantizer: quantize.MedianCutQuantizer{}})
if err != nil {
log.Printf("%s Failed to encode GIF: %s\n", c, err)
fmt.Fprintf(out, "
Unable to encode GIF:
%s
\n", err)
return
}
+
+ // Compose map and gif
seq := rand.Intn(9999)
imgpath := fmt.Sprintf("/img/%04d.gif", seq)
mappath := fmt.Sprintf("/map/%04d.map", seq)
- log.Printf("%s Encoded GIF image: %s, Size: %dKB, Colors: %d\n", c, imgpath, len(gifbuf.Bytes())/1024, co)
gifmap[imgpath] = gifbuf
-
- // Process Nodes
- tctx, cancel := context.WithTimeout(ctx, time.Second*2) // a context with timeout is needed for pages without nodes
- defer cancel()
- chromedp.Run(tctx, chromedp.Nodes("a", &nodes, chromedp.ByQueryAll))
- base, _ := url.Parse(loc)
- if i {
- fmt.Fprintf(out, "", mappath, imgpath)
- is = append(is, Ismap{xmin: -1, xmax: -1, ymin: -1, ymax: -1, url: fmt.Sprintf("/?url=%s&w=%d&h=%d&s=%1.2f&c=%d&i=on", loc, w, h, s, co)})
- ion = "&i=on"
- } else {
- fmt.Fprintf(out, "\n\n")
- }
- ismap[mappath] = is
- out.(http.Flusher).Flush()
- log.Printf("%s Done with caputure for %s\n", c, gourl)
+ ismap[mappath] = w
+ log.Printf("%s Encoded GIF image: %s, Size: %dKB, Colors: %d\n", c, imgpath, len(gifbuf.Bytes())/1024, w.C)
+ fmt.Fprintf(out, "", mappath, imgpath)
+ w.printFooter(out)
+ log.Printf("%s Done with caputure for %s\n", c, w.U)
}
func haltServer(out http.ResponseWriter, req *http.Request) {
@@ -283,6 +258,7 @@ func main() {
}
opts := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.Flag("headless", headless),
+ chromedp.Flag("hide-scrollbars", false),
)
actx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
@@ -294,8 +270,8 @@ func main() {
defer cancel()
rand.Seed(time.Now().UnixNano())
http.HandleFunc("/", pageServer)
- http.HandleFunc("/img/", imgServer)
http.HandleFunc("/map/", mapServer)
+ http.HandleFunc("/img/", imgServer)
http.HandleFunc("/shutdown/", haltServer)
http.HandleFunc("/favicon.ico", http.NotFound)
log.Printf("Web Rendering Proxy Version %s\n", version)