2019-05-30 01:53:05 +00:00
|
|
|
//
|
|
|
|
// WRP - Web Rendering Proxy
|
|
|
|
//
|
|
|
|
// Copyright (c) 2013-2018 Antoni Sawicki
|
2020-04-23 10:27:16 +00:00
|
|
|
// Copyright (c) 2019-2020 Google LLC
|
2019-05-30 01:53:05 +00:00
|
|
|
//
|
|
|
|
|
2019-05-29 08:29:01 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-05-30 06:49:39 +00:00
|
|
|
"bytes"
|
2019-05-29 08:52:28 +00:00
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2019-11-13 08:22:07 +00:00
|
|
|
"image"
|
2019-05-30 06:49:39 +00:00
|
|
|
"image/gif"
|
|
|
|
"image/png"
|
2019-05-29 08:52:28 +00:00
|
|
|
"log"
|
2019-11-03 23:01:05 +00:00
|
|
|
"math"
|
2019-05-30 09:03:17 +00:00
|
|
|
"math/rand"
|
2019-05-29 08:52:28 +00:00
|
|
|
"net/http"
|
2019-05-30 06:49:39 +00:00
|
|
|
"net/url"
|
2019-07-17 05:29:35 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2019-05-29 08:52:28 +00:00
|
|
|
"strconv"
|
2019-05-31 07:36:53 +00:00
|
|
|
"strings"
|
2019-07-17 05:29:35 +00:00
|
|
|
"syscall"
|
2019-05-29 08:52:28 +00:00
|
|
|
"time"
|
2019-05-29 08:29:01 +00:00
|
|
|
|
2019-08-13 06:35:29 +00:00
|
|
|
"github.com/chromedp/cdproto/css"
|
2019-05-29 09:39:06 +00:00
|
|
|
"github.com/chromedp/cdproto/emulation"
|
2019-11-03 23:01:05 +00:00
|
|
|
"github.com/chromedp/cdproto/page"
|
2019-05-29 08:52:28 +00:00
|
|
|
"github.com/chromedp/chromedp"
|
2019-06-06 08:06:36 +00:00
|
|
|
"github.com/ericpauley/go-quantize/quantize"
|
2019-05-29 08:29:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-11-03 23:01:05 +00:00
|
|
|
version = "4.5"
|
2019-06-18 06:53:22 +00:00
|
|
|
srv http.Server
|
2019-06-04 08:23:46 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2019-11-03 07:56:04 +00:00
|
|
|
img = make(map[string]bytes.Buffer)
|
2019-07-11 06:58:40 +00:00
|
|
|
ismap = make(map[string]wrpReq)
|
2019-08-08 09:31:23 +00:00
|
|
|
nodel bool
|
2019-11-04 00:58:38 +00:00
|
|
|
deftype string
|
2019-11-03 23:38:26 +00:00
|
|
|
defgeom geom
|
2019-05-29 08:29:01 +00:00
|
|
|
)
|
|
|
|
|
2019-11-03 23:38:26 +00:00
|
|
|
type geom struct {
|
|
|
|
w int64
|
|
|
|
h int64
|
|
|
|
c int64
|
|
|
|
}
|
|
|
|
|
2020-04-24 10:06:21 +00:00
|
|
|
// WRP Request
|
2019-07-11 06:58:40 +00:00
|
|
|
type wrpReq struct {
|
2020-04-26 06:56:14 +00:00
|
|
|
url string // url
|
|
|
|
width int64 // width
|
|
|
|
height int64 // height
|
|
|
|
scale float64 // scale
|
|
|
|
colors int64 // #colors
|
|
|
|
mouseX int64 // mouseX
|
|
|
|
mouseY int64 // mouseY
|
|
|
|
keys string // keys to send
|
|
|
|
buttons string // Fn buttons
|
|
|
|
imgType string // imgtype
|
|
|
|
out http.ResponseWriter
|
|
|
|
req *http.Request
|
2019-06-26 00:07:43 +00:00
|
|
|
}
|
2019-06-24 07:40:34 +00:00
|
|
|
|
2020-04-24 10:06:21 +00:00
|
|
|
// Parse HTML Form, Process Input Boxes, Etc.
|
2020-04-24 07:47:11 +00:00
|
|
|
func (w *wrpReq) parseForm() {
|
2020-04-26 06:56:14 +00:00
|
|
|
w.req.ParseForm()
|
|
|
|
w.url = w.req.FormValue("url")
|
|
|
|
if len(w.url) > 1 && !strings.HasPrefix(w.url, "http") {
|
|
|
|
w.url = fmt.Sprintf("http://www.google.com/search?q=%s", url.QueryEscape(w.url))
|
2019-06-26 00:07:43 +00:00
|
|
|
}
|
2020-04-26 06:56:14 +00:00
|
|
|
w.width, _ = strconv.ParseInt(w.req.FormValue("w"), 10, 64)
|
|
|
|
w.height, _ = strconv.ParseInt(w.req.FormValue("h"), 10, 64)
|
|
|
|
if w.width < 10 && w.height < 10 {
|
|
|
|
w.width = defgeom.w
|
|
|
|
w.height = defgeom.h
|
2019-05-29 08:52:28 +00:00
|
|
|
}
|
2020-04-26 06:56:14 +00:00
|
|
|
w.scale, _ = strconv.ParseFloat(w.req.FormValue("s"), 64)
|
|
|
|
if w.scale < 0.1 {
|
|
|
|
w.scale = 1.0
|
2019-05-31 01:08:48 +00:00
|
|
|
}
|
2020-04-26 06:56:14 +00:00
|
|
|
w.colors, _ = strconv.ParseInt(w.req.FormValue("c"), 10, 64)
|
|
|
|
if w.colors < 2 || w.colors > 256 {
|
|
|
|
w.colors = defgeom.c
|
2019-06-03 00:06:41 +00:00
|
|
|
}
|
2020-04-26 06:56:14 +00:00
|
|
|
w.keys = w.req.FormValue("k")
|
|
|
|
w.buttons = w.req.FormValue("Fn")
|
|
|
|
w.imgType = w.req.FormValue("t")
|
|
|
|
if w.imgType != "gif" && w.imgType != "png" {
|
|
|
|
w.imgType = deftype
|
2019-11-04 00:58:38 +00:00
|
|
|
}
|
2020-04-26 08:33:20 +00:00
|
|
|
log.Printf("%s WrpReq from UI Form: %+v\n", w.req.RemoteAddr, w)
|
2019-06-26 00:07:43 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 10:06:21 +00:00
|
|
|
// Display WP UI
|
|
|
|
// TODO: make this in to an external template
|
2020-04-24 07:47:11 +00:00
|
|
|
func (w wrpReq) printPage(bgcolor string) {
|
2019-11-04 01:53:20 +00:00
|
|
|
var s string
|
2020-04-26 06:56:14 +00:00
|
|
|
w.out.Header().Set("Cache-Control", "max-age=0")
|
|
|
|
w.out.Header().Set("Expires", "-1")
|
|
|
|
w.out.Header().Set("Pragma", "no-cache")
|
|
|
|
w.out.Header().Set("Content-Type", "text/html")
|
|
|
|
fmt.Fprintf(w.out, "<!-- Web Rendering Proxy Version %s -->\n", version)
|
|
|
|
fmt.Fprintf(w.out, "<HTML>\n<HEAD><TITLE>WRP %s</TITLE></HEAD>\n<BODY BGCOLOR=\"%s\">\n", w.url, bgcolor)
|
|
|
|
fmt.Fprintf(w.out, "<FORM ACTION=\"/\" METHOD=\"POST\">\n")
|
|
|
|
fmt.Fprintf(w.out, "<INPUT TYPE=\"TEXT\" NAME=\"url\" VALUE=\"%s\" SIZE=\"20\">", w.url)
|
|
|
|
fmt.Fprintf(w.out, "<INPUT TYPE=\"SUBMIT\" VALUE=\"Go\">\n")
|
|
|
|
fmt.Fprintf(w.out, "<INPUT TYPE=\"SUBMIT\" NAME=\"Fn\" VALUE=\"Bk\">\n")
|
|
|
|
fmt.Fprintf(w.out, "W <INPUT TYPE=\"TEXT\" NAME=\"w\" VALUE=\"%d\" SIZE=\"4\"> \n", w.width)
|
|
|
|
fmt.Fprintf(w.out, "H <INPUT TYPE=\"TEXT\" NAME=\"h\" VALUE=\"%d\" SIZE=\"4\"> \n", w.height)
|
|
|
|
fmt.Fprintf(w.out, "S <SELECT NAME=\"s\">\n")
|
2019-11-04 01:53:20 +00:00
|
|
|
for _, v := range []float64{0.65, 0.75, 0.85, 0.95, 1.0, 1.05, 1.15, 1.25} {
|
2020-04-26 06:56:14 +00:00
|
|
|
if v == w.scale {
|
2019-11-04 01:53:20 +00:00
|
|
|
s = "SELECTED"
|
|
|
|
} else {
|
|
|
|
s = ""
|
|
|
|
}
|
2020-04-26 06:56:14 +00:00
|
|
|
fmt.Fprintf(w.out, "<OPTION VALUE=\"%1.2f\" %s>%1.2f</OPTION>\n", v, s, v)
|
2019-11-04 01:53:20 +00:00
|
|
|
}
|
2020-04-26 06:56:14 +00:00
|
|
|
fmt.Fprintf(w.out, "</SELECT>\n")
|
|
|
|
fmt.Fprintf(w.out, "T <SELECT NAME=\"t\">\n")
|
2019-11-04 00:58:38 +00:00
|
|
|
for _, v := range []string{"gif", "png"} {
|
2020-04-26 06:56:14 +00:00
|
|
|
if v == w.imgType {
|
2019-11-04 00:58:38 +00:00
|
|
|
s = "SELECTED"
|
|
|
|
} else {
|
|
|
|
s = ""
|
|
|
|
}
|
2020-04-26 06:56:14 +00:00
|
|
|
fmt.Fprintf(w.out, "<OPTION VALUE=\"%s\" %s>%s</OPTION>\n", v, s, strings.ToUpper(v))
|
2019-11-04 00:58:38 +00:00
|
|
|
}
|
2020-04-26 06:56:14 +00:00
|
|
|
fmt.Fprintf(w.out, "</SELECT>\n")
|
|
|
|
fmt.Fprintf(w.out, "C <INPUT TYPE=\"TEXT\" NAME=\"c\" VALUE=\"%d\" SIZE=\"3\">\n", w.colors)
|
|
|
|
fmt.Fprintf(w.out, "K <INPUT TYPE=\"TEXT\" NAME=\"k\" VALUE=\"\" SIZE=\"4\"> \n")
|
|
|
|
fmt.Fprintf(w.out, "<INPUT TYPE=\"SUBMIT\" NAME=\"Fn\" VALUE=\"Bs\">\n")
|
|
|
|
fmt.Fprintf(w.out, "<INPUT TYPE=\"SUBMIT\" NAME=\"Fn\" VALUE=\"Rt\">\n")
|
|
|
|
fmt.Fprintf(w.out, "<INPUT TYPE=\"SUBMIT\" NAME=\"Fn\" VALUE=\"<\">\n")
|
|
|
|
fmt.Fprintf(w.out, "<INPUT TYPE=\"SUBMIT\" NAME=\"Fn\" VALUE=\"^\">\n")
|
|
|
|
fmt.Fprintf(w.out, "<INPUT TYPE=\"SUBMIT\" NAME=\"Fn\" VALUE=\"v\">\n")
|
|
|
|
fmt.Fprintf(w.out, "<INPUT TYPE=\"SUBMIT\" NAME=\"Fn\" VALUE=\">\" SIZE=\"1\">\n")
|
|
|
|
fmt.Fprintf(w.out, "</FORM><BR>\n")
|
2019-06-26 08:07:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 10:06:21 +00:00
|
|
|
// Status bar below captured image
|
2020-04-24 07:47:11 +00:00
|
|
|
func (w wrpReq) printFooter(h string, s string) {
|
2020-04-26 08:32:08 +00:00
|
|
|
fmt.Fprintf(w.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\">"+
|
|
|
|
"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.width,
|
|
|
|
w.height,
|
|
|
|
w.scale,
|
|
|
|
w.colors,
|
|
|
|
w.imgType,
|
|
|
|
version,
|
|
|
|
h,
|
|
|
|
s)
|
2019-06-26 08:07:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 10:06:21 +00:00
|
|
|
// Process HTTP requests to WRP '/' url
|
2019-06-26 08:07:13 +00:00
|
|
|
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)
|
2019-07-11 06:58:40 +00:00
|
|
|
var w wrpReq
|
2020-04-26 06:56:14 +00:00
|
|
|
w.req = req
|
|
|
|
w.out = out
|
2020-04-24 07:47:11 +00:00
|
|
|
w.parseForm()
|
2020-04-26 06:56:14 +00:00
|
|
|
if len(w.url) > 4 {
|
2020-04-24 10:09:20 +00:00
|
|
|
w.navigate()
|
2020-04-24 07:47:11 +00:00
|
|
|
w.capture()
|
2019-07-11 01:09:20 +00:00
|
|
|
} else {
|
2020-04-24 07:47:11 +00:00
|
|
|
w.printPage("#FFFFFF")
|
|
|
|
w.printFooter("", "")
|
2019-06-26 08:07:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 10:06:21 +00:00
|
|
|
// Process HTTP requests to ISMAP '/map/' url
|
2019-06-26 08:07:13 +00:00
|
|
|
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)
|
2019-07-11 06:58:40 +00:00
|
|
|
w, ok := ismap[req.URL.Path]
|
2020-04-26 06:56:14 +00:00
|
|
|
w.req = req
|
|
|
|
w.out = out
|
2019-06-26 08:07:13 +00:00
|
|
|
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
|
|
|
|
}
|
2019-08-08 09:31:23 +00:00
|
|
|
if !nodel {
|
|
|
|
defer delete(ismap, req.URL.Path)
|
|
|
|
}
|
2020-04-26 06:56:14 +00:00
|
|
|
n, err := fmt.Sscanf(req.URL.RawQuery, "%d,%d", &w.mouseX, &w.mouseY)
|
2019-07-10 08:01:40 +00:00
|
|
|
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
|
|
|
|
}
|
2019-07-11 06:58:40 +00:00
|
|
|
log.Printf("%s WrpReq from ISMAP: %+v\n", req.RemoteAddr, w)
|
2020-04-26 06:56:14 +00:00
|
|
|
if len(w.url) > 4 {
|
2020-04-24 10:09:20 +00:00
|
|
|
w.navigate()
|
2020-04-24 07:47:11 +00:00
|
|
|
w.capture()
|
2019-07-11 01:09:20 +00:00
|
|
|
} else {
|
2020-04-24 07:47:11 +00:00
|
|
|
w.printPage("#FFFFFF")
|
|
|
|
w.printFooter("", "")
|
2019-06-26 08:07:13 +00:00
|
|
|
}
|
2019-05-29 08:29:01 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 10:06:21 +00:00
|
|
|
// Process HTTP requests for images '/img/' url
|
2019-05-29 08:29:01 +00:00
|
|
|
func imgServer(out http.ResponseWriter, req *http.Request) {
|
2019-06-01 01:20:55 +00:00
|
|
|
log.Printf("%s IMG Request for %s\n", req.RemoteAddr, req.URL.Path)
|
2019-11-03 07:56:04 +00:00
|
|
|
imgbuf, ok := img[req.URL.Path]
|
2019-08-22 07:38:36 +00:00
|
|
|
if !ok || imgbuf.Bytes() == nil {
|
2019-06-03 05:23:41 +00:00
|
|
|
fmt.Fprintf(out, "Unable to find image %s\n", req.URL.Path)
|
2019-08-22 07:38:36 +00:00
|
|
|
log.Printf("%s Unable to find image %s\n", req.RemoteAddr, req.URL.Path)
|
2019-06-03 05:23:41 +00:00
|
|
|
return
|
|
|
|
}
|
2019-08-08 09:31:23 +00:00
|
|
|
if !nodel {
|
2019-11-03 07:56:04 +00:00
|
|
|
defer delete(img, req.URL.Path)
|
2019-08-08 09:31:23 +00:00
|
|
|
}
|
2019-08-22 07:38:36 +00:00
|
|
|
if strings.HasPrefix(req.URL.Path, ".gif") {
|
|
|
|
out.Header().Set("Content-Type", "image/gif")
|
|
|
|
} else if strings.HasPrefix(req.URL.Path, ".png") {
|
|
|
|
out.Header().Set("Content-Type", "image/png")
|
|
|
|
}
|
|
|
|
out.Header().Set("Content-Length", strconv.Itoa(len(imgbuf.Bytes())))
|
2019-07-17 05:47:23 +00:00
|
|
|
out.Header().Set("Cache-Control", "max-age=0")
|
|
|
|
out.Header().Set("Expires", "-1")
|
|
|
|
out.Header().Set("Pragma", "no-cache")
|
2019-08-22 07:38:36 +00:00
|
|
|
out.Write(imgbuf.Bytes())
|
2019-05-30 09:03:17 +00:00
|
|
|
out.(http.Flusher).Flush()
|
2019-05-29 08:29:01 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 10:06:21 +00:00
|
|
|
// Process HTTP requests for Shutdown via '/shutdown/' url
|
|
|
|
func haltServer(out http.ResponseWriter, req *http.Request) {
|
|
|
|
log.Printf("%s Shutdown Request for %s\n", req.RemoteAddr, req.URL.Path)
|
|
|
|
out.Header().Set("Cache-Control", "max-age=0")
|
|
|
|
out.Header().Set("Expires", "-1")
|
|
|
|
out.Header().Set("Pragma", "no-cache")
|
|
|
|
out.Header().Set("Content-Type", "text/plain")
|
|
|
|
fmt.Fprintf(out, "Shutting down WRP...\n")
|
|
|
|
out.(http.Flusher).Flush()
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
cancel()
|
|
|
|
srv.Shutdown(context.Background())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2020-04-24 10:09:20 +00:00
|
|
|
// Process Keyboard and Mouse events or Navigate to the desired URL.
|
|
|
|
func (w wrpReq) navigate() {
|
2019-07-10 08:01:40 +00:00
|
|
|
var err error
|
2020-04-24 10:09:20 +00:00
|
|
|
// Mouse Click
|
2020-04-26 06:56:14 +00:00
|
|
|
if w.mouseX > 0 && w.mouseY > 0 {
|
|
|
|
log.Printf("%s Mouse Click %d,%d\n", w.req.RemoteAddr, w.mouseX, w.mouseY)
|
|
|
|
err = chromedp.Run(ctx, chromedp.MouseClickXY(float64(w.mouseX)/float64(w.scale), float64(w.mouseY)/float64(w.scale)))
|
2020-04-24 10:09:20 +00:00
|
|
|
// Buttons
|
2020-04-26 06:56:14 +00:00
|
|
|
} else if len(w.buttons) > 0 {
|
|
|
|
log.Printf("%s Button %v\n", w.req.RemoteAddr, w.buttons)
|
|
|
|
switch w.buttons {
|
2019-07-13 07:40:56 +00:00
|
|
|
case "Bk":
|
|
|
|
err = chromedp.Run(ctx, chromedp.NavigateBack())
|
|
|
|
case "Bs":
|
|
|
|
err = chromedp.Run(ctx, chromedp.KeyEvent("\b"))
|
|
|
|
case "Rt":
|
|
|
|
err = chromedp.Run(ctx, chromedp.KeyEvent("\r"))
|
|
|
|
case "<":
|
|
|
|
err = chromedp.Run(ctx, chromedp.KeyEvent("\u0302"))
|
|
|
|
case "^":
|
|
|
|
err = chromedp.Run(ctx, chromedp.KeyEvent("\u0304"))
|
|
|
|
case "v":
|
|
|
|
err = chromedp.Run(ctx, chromedp.KeyEvent("\u0301"))
|
|
|
|
case ">":
|
|
|
|
err = chromedp.Run(ctx, chromedp.KeyEvent("\u0303"))
|
|
|
|
}
|
2020-04-24 10:09:20 +00:00
|
|
|
// Keys
|
2020-04-26 06:56:14 +00:00
|
|
|
} else if len(w.keys) > 0 {
|
|
|
|
log.Printf("%s Sending Keys: %#v\n", w.req.RemoteAddr, w.keys)
|
|
|
|
err = chromedp.Run(ctx, chromedp.KeyEvent(w.keys))
|
2020-04-24 10:09:20 +00:00
|
|
|
// Navigate to URL
|
2019-07-10 08:01:40 +00:00
|
|
|
} else {
|
2020-04-26 06:56:14 +00:00
|
|
|
log.Printf("%s Processing Capture Request for %s\n", w.req.RemoteAddr, w.url)
|
|
|
|
err = chromedp.Run(ctx, chromedp.Navigate(w.url))
|
2019-07-10 08:01:40 +00:00
|
|
|
}
|
2019-05-31 07:41:46 +00:00
|
|
|
if err != nil {
|
2019-06-09 00:32:35 +00:00
|
|
|
if err.Error() == "context canceled" {
|
2020-04-26 06:56:14 +00:00
|
|
|
log.Printf("%s Contex cancelled, try again", w.req.RemoteAddr)
|
|
|
|
fmt.Fprintf(w.out, "<BR>%s<BR> -- restarting, try again", err)
|
2019-06-09 00:32:35 +00:00
|
|
|
ctx, cancel = chromedp.NewContext(context.Background())
|
|
|
|
} else {
|
2020-04-26 06:56:14 +00:00
|
|
|
log.Printf("%s %s", w.req.RemoteAddr, err)
|
|
|
|
fmt.Fprintf(w.out, "<BR>%s<BR>", err)
|
2019-06-09 00:32:35 +00:00
|
|
|
}
|
2019-06-17 07:21:32 +00:00
|
|
|
return
|
2019-05-31 07:41:46 +00:00
|
|
|
}
|
2020-04-24 09:45:34 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 10:06:21 +00:00
|
|
|
// Capture currently rendered web page to an image and fake ISMAP
|
2020-04-24 09:45:34 +00:00
|
|
|
func (w wrpReq) capture() {
|
|
|
|
var err error
|
2020-04-23 10:25:39 +00:00
|
|
|
var styles []*css.ComputedStyleProperty
|
2019-08-22 07:38:36 +00:00
|
|
|
var r, g, b int
|
2019-11-03 23:01:05 +00:00
|
|
|
var h int64
|
|
|
|
var pngcap []byte
|
2019-08-13 06:35:29 +00:00
|
|
|
chromedp.Run(ctx,
|
2020-04-26 06:56:14 +00:00
|
|
|
emulation.SetDeviceMetricsOverride(int64(float64(w.width)/w.scale), 10, w.scale, false),
|
2019-08-13 06:35:29 +00:00
|
|
|
chromedp.Sleep(time.Second*2),
|
2020-04-26 06:56:14 +00:00
|
|
|
chromedp.Location(&w.url),
|
2019-11-03 23:01:05 +00:00
|
|
|
chromedp.ComputedStyle("body", &styles, chromedp.ByQuery),
|
|
|
|
chromedp.ActionFunc(func(ctx context.Context) error {
|
|
|
|
_, _, s, err := page.GetLayoutMetrics().Do(ctx)
|
|
|
|
if err == nil {
|
|
|
|
h = int64(math.Ceil(s.Height))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}),
|
2019-07-12 08:51:23 +00:00
|
|
|
)
|
2019-08-13 06:35:29 +00:00
|
|
|
for _, style := range styles {
|
|
|
|
if style.Name == "background-color" {
|
|
|
|
fmt.Sscanf(style.Value, "rgb(%d,%d,%d)", &r, &g, &b)
|
|
|
|
}
|
|
|
|
}
|
2020-04-26 06:56:14 +00:00
|
|
|
log.Printf("%s Landed on: %s, Height: %v\n", w.req.RemoteAddr, w.url, h)
|
2020-04-24 07:47:11 +00:00
|
|
|
w.printPage(fmt.Sprintf("#%02X%02X%02X", r, g, b))
|
2020-04-26 06:56:14 +00:00
|
|
|
if w.height == 0 && h > 0 {
|
|
|
|
chromedp.Run(ctx, emulation.SetDeviceMetricsOverride(int64(float64(w.width)/w.scale), h+30, w.scale, false))
|
2019-11-03 23:01:05 +00:00
|
|
|
} else {
|
2020-04-26 06:56:14 +00:00
|
|
|
chromedp.Run(ctx, emulation.SetDeviceMetricsOverride(int64(float64(w.width)/w.scale), int64(float64(w.height)/w.scale), w.scale, false))
|
2019-11-03 23:01:05 +00:00
|
|
|
}
|
2020-04-26 07:23:31 +00:00
|
|
|
// Capture screenshot...
|
2019-08-22 07:38:36 +00:00
|
|
|
err = chromedp.Run(ctx, chromedp.CaptureScreenshot(&pngcap))
|
2019-06-24 07:40:34 +00:00
|
|
|
if err != nil {
|
2020-04-26 07:23:31 +00:00
|
|
|
if err.Error() == "context canceled" {
|
|
|
|
log.Printf("%s Contex cancelled, try again", w.req.RemoteAddr)
|
|
|
|
fmt.Fprintf(w.out, "<BR>%s<BR> -- restarting, try again", err)
|
|
|
|
ctx, cancel = chromedp.NewContext(context.Background())
|
|
|
|
} else {
|
|
|
|
log.Printf("%s Failed to capture screenshot: %s\n", w.req.RemoteAddr, err)
|
|
|
|
fmt.Fprintf(w.out, "<BR>Unable to capture screenshot:<BR>%s<BR>\n", err)
|
|
|
|
}
|
2019-06-24 07:40:34 +00:00
|
|
|
return
|
|
|
|
}
|
2019-06-26 08:07:13 +00:00
|
|
|
seq := rand.Intn(9999)
|
2020-04-26 06:56:14 +00:00
|
|
|
imgpath := fmt.Sprintf("/img/%04d.%s", seq, w.imgType)
|
2019-06-26 08:07:13 +00:00
|
|
|
mappath := fmt.Sprintf("/map/%04d.map", seq)
|
2019-07-11 06:58:40 +00:00
|
|
|
ismap[mappath] = w
|
2019-11-04 01:24:18 +00:00
|
|
|
var ssize string
|
2019-11-13 08:22:07 +00:00
|
|
|
var sw, sh int
|
2020-04-26 06:56:14 +00:00
|
|
|
if w.imgType == "gif" {
|
2019-11-03 07:56:04 +00:00
|
|
|
i, err := png.Decode(bytes.NewReader(pngcap))
|
2019-08-22 07:38:36 +00:00
|
|
|
if err != nil {
|
2020-04-26 06:56:14 +00:00
|
|
|
log.Printf("%s Failed to decode screenshot: %s\n", w.req.RemoteAddr, err)
|
|
|
|
fmt.Fprintf(w.out, "<BR>Unable to decode page screenshot:<BR>%s<BR>\n", err)
|
2019-08-22 07:38:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var gifbuf bytes.Buffer
|
2020-04-26 06:56:14 +00:00
|
|
|
err = gif.Encode(&gifbuf, i, &gif.Options{NumColors: int(w.colors), Quantizer: quantize.MedianCutQuantizer{}})
|
2019-08-22 07:38:36 +00:00
|
|
|
if err != nil {
|
2020-04-26 06:56:14 +00:00
|
|
|
log.Printf("%s Failed to encode GIF: %s\n", w.req.RemoteAddr, err)
|
|
|
|
fmt.Fprintf(w.out, "<BR>Unable to encode GIF:<BR>%s<BR>\n", err)
|
2019-08-22 07:38:36 +00:00
|
|
|
return
|
|
|
|
}
|
2019-11-03 07:56:04 +00:00
|
|
|
img[imgpath] = gifbuf
|
2020-04-26 08:25:28 +00:00
|
|
|
ssize = fmt.Sprintf("%.0f KB", float32(len(gifbuf.Bytes()))/1024.0)
|
2019-11-13 08:22:07 +00:00
|
|
|
sw = i.Bounds().Max.X
|
|
|
|
sh = i.Bounds().Max.Y
|
2020-04-26 06:56:14 +00:00
|
|
|
log.Printf("%s Encoded GIF image: %s, Size: %s, Colors: %d, %dx%d\n", w.req.RemoteAddr, imgpath, ssize, w.colors, sw, sh)
|
|
|
|
} else if w.imgType == "png" {
|
2019-08-22 07:38:36 +00:00
|
|
|
pngbuf := bytes.NewBuffer(pngcap)
|
2019-11-03 07:56:04 +00:00
|
|
|
img[imgpath] = *pngbuf
|
2019-11-13 08:22:07 +00:00
|
|
|
cfg, _, _ := image.DecodeConfig(pngbuf)
|
2020-04-26 08:25:28 +00:00
|
|
|
ssize = fmt.Sprintf("%.0f KB", float32(len(pngbuf.Bytes()))/1024.0)
|
2019-11-13 08:22:07 +00:00
|
|
|
sw = cfg.Width
|
|
|
|
sh = cfg.Height
|
2020-04-26 06:56:14 +00:00
|
|
|
log.Printf("%s Got PNG image: %s, Size: %s, %dx%d\n", w.req.RemoteAddr, imgpath, ssize, sw, sh)
|
2019-08-22 07:38:36 +00:00
|
|
|
}
|
2020-04-26 06:56:14 +00:00
|
|
|
fmt.Fprintf(w.out, "<A HREF=\"%s\"><IMG SRC=\"%s\" BORDER=\"0\" ALT=\"Url: %s, Size: %s\" WIDTH=\"%d\" HEIGHT=\"%d\" ISMAP></A>", mappath, imgpath, w.url, ssize, sw, sh)
|
2020-04-24 07:47:11 +00:00
|
|
|
w.printFooter(fmt.Sprintf("%d PX", h), ssize)
|
2020-04-26 06:56:14 +00:00
|
|
|
log.Printf("%s Done with caputure for %s\n", w.req.RemoteAddr, w.url)
|
2019-05-29 08:29:01 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 10:06:21 +00:00
|
|
|
// Main...
|
2019-05-29 08:29:01 +00:00
|
|
|
func main() {
|
2019-11-03 23:38:26 +00:00
|
|
|
var addr, fgeom string
|
2019-06-04 07:58:45 +00:00
|
|
|
var head, headless bool
|
2019-06-04 00:50:16 +00:00
|
|
|
var debug bool
|
2019-11-03 23:38:26 +00:00
|
|
|
var err error
|
2019-05-29 08:52:28 +00:00
|
|
|
flag.StringVar(&addr, "l", ":8080", "Listen address:port, default :8080")
|
2019-06-04 00:50:16 +00:00
|
|
|
flag.BoolVar(&head, "h", false, "Headed mode - display browser window")
|
|
|
|
flag.BoolVar(&debug, "d", false, "Debug ChromeDP")
|
2019-08-22 07:38:36 +00:00
|
|
|
flag.BoolVar(&nodel, "n", false, "Do not free maps and images after use")
|
2019-11-04 00:58:38 +00:00
|
|
|
flag.StringVar(&deftype, "t", "gif", "Image type: gif|png")
|
2019-11-03 23:38:26 +00:00
|
|
|
flag.StringVar(&fgeom, "g", "1152x600x256", "Geometry: width x height x colors, height can be 0 for unlimited")
|
2019-05-29 08:52:28 +00:00
|
|
|
flag.Parse()
|
2019-06-04 00:50:16 +00:00
|
|
|
if head {
|
|
|
|
headless = false
|
|
|
|
} else {
|
|
|
|
headless = true
|
|
|
|
}
|
2019-11-03 23:38:26 +00:00
|
|
|
n, err := fmt.Sscanf(fgeom, "%dx%dx%d", &defgeom.w, &defgeom.h, &defgeom.c)
|
|
|
|
if err != nil || n != 3 {
|
|
|
|
log.Fatalf("Unable to parse -g geometry flag / %s", err)
|
|
|
|
}
|
2019-06-04 00:50:16 +00:00
|
|
|
opts := append(chromedp.DefaultExecAllocatorOptions[:],
|
2019-06-04 07:58:45 +00:00
|
|
|
chromedp.Flag("headless", headless),
|
2019-07-11 06:58:40 +00:00
|
|
|
chromedp.Flag("hide-scrollbars", false),
|
2019-06-04 00:50:16 +00:00
|
|
|
)
|
2019-07-17 05:29:35 +00:00
|
|
|
actx, acancel := chromedp.NewExecAllocator(context.Background(), opts...)
|
|
|
|
defer acancel()
|
2019-06-04 00:50:16 +00:00
|
|
|
if debug {
|
2019-06-04 08:23:46 +00:00
|
|
|
ctx, cancel = chromedp.NewContext(actx, chromedp.WithDebugf(log.Printf))
|
2019-06-04 00:50:16 +00:00
|
|
|
} else {
|
2019-06-04 08:23:46 +00:00
|
|
|
ctx, cancel = chromedp.NewContext(actx)
|
2019-06-04 00:50:16 +00:00
|
|
|
}
|
|
|
|
defer cancel()
|
2019-05-30 09:03:17 +00:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
2019-07-17 05:29:35 +00:00
|
|
|
c := make(chan os.Signal)
|
|
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
<-c
|
|
|
|
log.Printf("Interrupt - shutting down.")
|
|
|
|
cancel()
|
|
|
|
srv.Shutdown(context.Background())
|
|
|
|
os.Exit(1)
|
|
|
|
}()
|
2019-05-29 08:52:28 +00:00
|
|
|
http.HandleFunc("/", pageServer)
|
2019-06-26 08:07:13 +00:00
|
|
|
http.HandleFunc("/map/", mapServer)
|
2019-05-30 09:03:17 +00:00
|
|
|
http.HandleFunc("/img/", imgServer)
|
2019-06-18 06:53:22 +00:00
|
|
|
http.HandleFunc("/shutdown/", haltServer)
|
2019-05-30 01:47:03 +00:00
|
|
|
http.HandleFunc("/favicon.ico", http.NotFound)
|
2019-06-02 23:24:46 +00:00
|
|
|
log.Printf("Web Rendering Proxy Version %s\n", version)
|
2019-05-31 06:40:43 +00:00
|
|
|
log.Printf("Starting WRP http server on %s\n", addr)
|
2019-06-18 06:53:22 +00:00
|
|
|
srv.Addr = addr
|
2019-11-03 23:38:26 +00:00
|
|
|
err = srv.ListenAndServe()
|
2019-06-18 06:53:22 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-05-29 08:29:01 +00:00
|
|
|
}
|