wrp/wrp.go

502 lines
13 KiB
Go
Raw Normal View History

2019-05-30 01:53:05 +00:00
//
// WRP - Web Rendering Proxy
//
// Copyright (c) 2013-2018 Antoni Sawicki
2021-03-08 12:26:09 +00:00
// Copyright (c) 2019-2021 Google LLC
2019-05-30 01:53:05 +00:00
//
2019-05-29 08:29:01 +00:00
package main
import (
"bytes"
2019-05-29 08:52:28 +00:00
"context"
2022-03-17 02:27:34 +00:00
"embed"
2019-05-29 08:52:28 +00:00
"flag"
"fmt"
"html/template"
"image"
"image/gif"
"image/png"
2020-10-29 14:16:14 +00:00
"io/ioutil"
2019-05-29 08:52:28 +00:00
"log"
"math"
2019-05-30 09:03:17 +00:00
"math/rand"
2019-05-29 08:52:28 +00:00
"net/http"
"net/url"
2019-07-17 05:29:35 +00:00
"os"
"os/signal"
2019-05-29 08:52:28 +00:00
"strconv"
"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
2020-09-28 19:19:27 +00:00
"github.com/MaxHalford/halfgone"
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"
"github.com/chromedp/cdproto/page"
2019-05-29 08:52:28 +00:00
"github.com/chromedp/chromedp"
"github.com/ericpauley/go-quantize/quantize"
2019-05-29 08:29:01 +00:00
)
var (
2021-03-08 12:26:09 +00:00
version = "4.5.2"
srv http.Server
ctx context.Context
cancel context.CancelFunc
img = make(map[string]bytes.Buffer)
ismap = make(map[string]wrpReq)
2020-10-31 15:51:20 +00:00
noDel bool
defType string
defGeom geom
htmlTmpl *template.Template
2019-05-29 08:29:01 +00:00
)
2022-03-17 02:27:34 +00:00
// go:embed *.html
var fs embed.FS
type geom struct {
w int64
h int64
c int64
}
// Data for html template
type uiData struct {
Version string
URL string
2020-10-31 15:51:20 +00:00
BgColor string
NColors int64
Width int64
Height int64
2021-03-08 13:02:22 +00:00
Zoom float64
ImgType string
2020-10-31 15:51:20 +00:00
ImgURL string
ImgSize string
ImgWidth int
ImgHeight int
2020-10-31 15:51:20 +00:00
MapURL string
PageHeight string
}
// Parameters for HTML print function
type printParams struct {
2020-10-31 15:51:20 +00:00
bgColor string
pageHeight string
imgSize string
imgURL string
mapURL string
imgWidth int
imgHeight int
}
2020-04-24 10:06:21 +00:00
// WRP Request
2019-07-11 06:58:40 +00:00
type wrpReq struct {
url string // url
width int64 // width
height int64 // height
2021-03-08 13:02:22 +00:00
zoom float64 // zoom/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.
func parseForm(w *wrpReq) {
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
}
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 {
2020-10-31 15:51:20 +00:00
w.width = defGeom.w
w.height = defGeom.h
2019-05-29 08:52:28 +00:00
}
2021-03-08 13:02:22 +00:00
w.zoom, _ = strconv.ParseFloat(w.req.FormValue("z"), 64)
if w.zoom < 0.1 {
w.zoom = 1.0
2019-05-31 01:08:48 +00:00
}
w.colors, _ = strconv.ParseInt(w.req.FormValue("c"), 10, 64)
if w.colors < 2 || w.colors > 256 {
2020-10-31 15:51:20 +00:00
w.colors = defGeom.c
2019-06-03 00:06:41 +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" {
2020-10-31 15:51:20 +00:00
w.imgType = defType
2019-11-04 00:58:38 +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
func printHTML(w wrpReq, p printParams) {
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")
data := uiData{
Version: version,
URL: w.url,
2020-10-31 15:51:20 +00:00
BgColor: p.bgColor,
Width: w.width,
Height: w.height,
NColors: w.colors,
2021-03-08 13:02:22 +00:00
Zoom: w.zoom,
ImgType: w.imgType,
2020-10-31 15:51:20 +00:00
ImgSize: p.imgSize,
ImgWidth: p.imgWidth,
ImgHeight: p.imgHeight,
ImgURL: p.imgURL,
MapURL: p.mapURL,
PageHeight: p.pageHeight,
2019-11-04 01:53:20 +00:00
}
err := htmlTmpl.Execute(w.out, data)
if err != nil {
log.Fatal(err)
2019-11-04 00:58:38 +00:00
}
2019-06-26 08:07:13 +00:00
}
2020-10-26 08:21:42 +00:00
// Determine what action to take
func action(w wrpReq) chromedp.Action {
2020-04-24 10:09:20 +00:00
// Mouse Click
if w.mouseX > 0 && w.mouseY > 0 {
log.Printf("%s Mouse Click %d,%d\n", w.req.RemoteAddr, w.mouseX, w.mouseY)
2021-03-08 13:02:22 +00:00
return chromedp.MouseClickXY(float64(w.mouseX)/float64(w.zoom), float64(w.mouseY)/float64(w.zoom))
2020-10-26 08:21:42 +00:00
}
// Buttons
if len(w.buttons) > 0 {
log.Printf("%s Button %v\n", w.req.RemoteAddr, w.buttons)
switch w.buttons {
case "Bk":
2020-10-26 08:21:42 +00:00
return chromedp.NavigateBack()
case "St":
return chromedp.Stop()
case "Re":
return chromedp.Reload()
case "Bs":
2020-10-26 08:21:42 +00:00
return chromedp.KeyEvent("\b")
case "Rt":
2020-10-26 08:21:42 +00:00
return chromedp.KeyEvent("\r")
case "<":
2020-10-26 08:21:42 +00:00
return chromedp.KeyEvent("\u0302")
case "^":
2020-10-26 08:21:42 +00:00
return chromedp.KeyEvent("\u0304")
case "v":
2020-10-26 08:21:42 +00:00
return chromedp.KeyEvent("\u0301")
case ">":
2020-10-26 08:21:42 +00:00
return chromedp.KeyEvent("\u0303")
}
2020-10-26 08:21:42 +00:00
}
// Keys
if len(w.keys) > 0 {
log.Printf("%s Sending Keys: %#v\n", w.req.RemoteAddr, w.keys)
2020-10-26 08:21:42 +00:00
return chromedp.KeyEvent(w.keys)
2019-07-10 08:01:40 +00:00
}
2020-10-26 08:21:42 +00:00
// Navigate to URL
log.Printf("%s Processing Capture Request for %s\n", w.req.RemoteAddr, w.url)
return chromedp.Navigate(w.url)
}
// Process Keyboard and Mouse events or Navigate to the desired URL.
func navigate(w wrpReq) {
err := chromedp.Run(ctx, action(w))
2019-05-31 07:41:46 +00:00
if err != nil {
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())
2020-10-26 08:21:42 +00:00
return
}
2020-10-26 08:21:42 +00:00
log.Printf("%s %s", w.req.RemoteAddr, err)
fmt.Fprintf(w.out, "<BR>%s<BR>", err)
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
func capture(w wrpReq) {
2020-04-24 09:45:34 +00:00
var err error
2020-04-23 10:25:39 +00:00
var styles []*css.ComputedStyleProperty
var r, g, b int
var h int64
var pngcap []byte
2019-08-13 06:35:29 +00:00
chromedp.Run(ctx,
2021-03-08 13:02:22 +00:00
emulation.SetDeviceMetricsOverride(int64(float64(w.width)/w.zoom), 10, w.zoom, false),
chromedp.Location(&w.url),
chromedp.ComputedStyle("body", &styles, chromedp.ByQuery),
chromedp.ActionFunc(func(ctx context.Context) error {
2022-11-06 07:51:44 +00:00
_, _, _, _, _, s, err := page.GetLayoutMetrics().Do(ctx)
if err == nil {
h = int64(math.Ceil(s.Height))
}
return nil
}),
)
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)
}
}
log.Printf("%s Landed on: %s, Height: %v\n", w.req.RemoteAddr, w.url, h)
2021-03-08 13:02:22 +00:00
height := int64(float64(w.height) / w.zoom)
if w.height == 0 && h > 0 {
2020-10-26 08:49:03 +00:00
height = h + 30
}
2021-03-08 13:02:22 +00:00
chromedp.Run(ctx, emulation.SetDeviceMetricsOverride(int64(float64(w.width)/w.zoom), height, w.zoom, false))
// Capture screenshot...
err = chromedp.Run(ctx,
2020-10-27 11:39:14 +00:00
chromedp.Sleep(time.Second*2),
chromedp.CaptureScreenshot(&pngcap),
)
2019-06-24 07:40:34 +00:00
if err != nil {
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())
2020-10-26 08:21:42 +00:00
return
}
2020-10-26 08:21:42 +00:00
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)
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
var ssize string
var iw, ih int
2020-10-26 08:32:09 +00:00
switch w.imgType {
case "gif":
i, err := png.Decode(bytes.NewReader(pngcap))
if err != nil {
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)
return
}
2020-09-28 19:19:27 +00:00
if w.colors == 2 {
gray := halfgone.ImageToGray(i)
i = halfgone.FloydSteinbergDitherer{}.Apply(gray)
}
var gifbuf bytes.Buffer
2022-11-06 07:27:10 +00:00
st := time.Now()
err = gif.Encode(&gifbuf, i, &gif.Options{NumColors: int(w.colors), Quantizer: quantize.MedianCutQuantizer{}})
if err != nil {
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)
return
}
img[imgpath] = gifbuf
2020-04-26 08:25:28 +00:00
ssize = fmt.Sprintf("%.0f KB", float32(len(gifbuf.Bytes()))/1024.0)
iw = i.Bounds().Max.X
ih = i.Bounds().Max.Y
2022-11-06 07:27:10 +00:00
log.Printf("%s Encoded GIF image: %s, Size: %s, Colors: %d, %dx%d, Time: %vms\n", w.req.RemoteAddr, imgpath, ssize, w.colors, iw, ih, time.Since(st).Milliseconds())
2020-10-26 08:32:09 +00:00
case "png":
pngbuf := bytes.NewBuffer(pngcap)
img[imgpath] = *pngbuf
cfg, _, _ := image.DecodeConfig(pngbuf)
2020-04-26 08:25:28 +00:00
ssize = fmt.Sprintf("%.0f KB", float32(len(pngbuf.Bytes()))/1024.0)
iw = cfg.Width
ih = cfg.Height
log.Printf("%s Got PNG image: %s, Size: %s, %dx%d\n", w.req.RemoteAddr, imgpath, ssize, iw, ih)
}
printHTML(w, printParams{
2020-10-31 15:51:20 +00:00
bgColor: fmt.Sprintf("#%02X%02X%02X", r, g, b),
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", w.req.RemoteAddr, w.url)
2019-05-29 08:29:01 +00:00
}
// Process HTTP requests to WRP '/' url
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.req = req
w.out = out
parseForm(&w)
if len(w.url) < 4 {
printHTML(w, printParams{bgColor: "#FFFFFF"})
return
}
navigate(w)
capture(w)
}
// Process HTTP requests to ISMAP '/map/' url
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]
w.req = req
w.out = out
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
}
if !noDel {
defer delete(ismap, req.URL.Path)
}
n, err := fmt.Sscanf(req.URL.RawQuery, "%d,%d", &w.mouseX, &w.mouseY)
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.url) < 4 {
printHTML(w, printParams{bgColor: "#FFFFFF"})
return
}
navigate(w)
capture(w)
}
// Process HTTP requests for images '/img/' url
func imgServer(out http.ResponseWriter, req *http.Request) {
log.Printf("%s IMG Request for %s\n", req.RemoteAddr, req.URL.Path)
imgbuf, ok := img[req.URL.Path]
if !ok || imgbuf.Bytes() == nil {
fmt.Fprintf(out, "Unable to find image %s\n", req.URL.Path)
log.Printf("%s Unable to find image %s\n", req.RemoteAddr, req.URL.Path)
return
}
if !noDel {
defer delete(img, req.URL.Path)
}
switch {
case strings.HasPrefix(req.URL.Path, ".gif"):
out.Header().Set("Content-Type", "image/gif")
case strings.HasPrefix(req.URL.Path, ".png"):
out.Header().Set("Content-Type", "image/png")
}
out.Header().Set("Content-Length", strconv.Itoa(len(imgbuf.Bytes())))
out.Header().Set("Cache-Control", "max-age=0")
out.Header().Set("Expires", "-1")
out.Header().Set("Pragma", "no-cache")
out.Write(imgbuf.Bytes())
out.(http.Flusher).Flush()
}
// 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)
}
// returns html template, either from html file or built-in
2020-10-29 14:16:14 +00:00
func tmpl(t string) string {
var tmpl []byte
fh, err := os.Open(t)
if err != nil {
goto statik
}
tmpl, err = ioutil.ReadAll(fh)
if err != nil {
goto statik
}
log.Printf("Got UI template from %v file\n", t)
return string(tmpl)
statik:
2022-03-17 02:27:34 +00:00
fhs, err := fs.Open("/wrp.html")
2020-10-29 14:16:14 +00:00
if err != nil {
log.Fatal(err)
}
tmpl, err = ioutil.ReadAll(fhs)
if err != nil {
log.Fatal(err)
}
log.Printf("Got UI template from built-in\n")
return string(tmpl)
}
2020-04-24 10:06:21 +00:00
// Main...
2019-05-29 08:29:01 +00:00
func main() {
2021-03-08 12:26:09 +00:00
var addr, fgeom, tHTML string
var headless bool
var debug bool
var err error
2019-05-29 08:52:28 +00:00
flag.StringVar(&addr, "l", ":8080", "Listen address:port, default :8080")
flag.BoolVar(&headless, "h", true, "Headless mode - hide browser window")
flag.BoolVar(&debug, "d", false, "Debug ChromeDP")
2020-10-31 15:51:20 +00:00
flag.BoolVar(&noDel, "n", false, "Do not free maps and images after use")
flag.StringVar(&defType, "t", "gif", "Image type: gif|png")
flag.StringVar(&fgeom, "g", "1152x600x256", "Geometry: width x height x colors, height can be 0 for unlimited")
2021-03-08 12:26:09 +00:00
flag.StringVar(&tHTML, "ui", "wrp.html", "HTML template file for the UI")
2019-05-29 08:52:28 +00:00
flag.Parse()
if len(os.Getenv("PORT")) > 0 {
addr = ":" + os.Getenv(("PORT"))
}
2020-10-31 15:51:20 +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)
}
opts := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.Flag("headless", headless),
2019-07-11 06:58:40 +00:00
chromedp.Flag("hide-scrollbars", false),
)
2019-07-17 05:29:35 +00:00
actx, acancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer acancel()
if debug {
2019-06-04 08:23:46 +00:00
ctx, cancel = chromedp.NewContext(actx, chromedp.WithDebugf(log.Printf))
} else {
2019-06-04 08:23:46 +00:00
ctx, cancel = chromedp.NewContext(actx)
}
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)
2020-04-27 19:29:18 +00:00
log.Printf("Args: %q", os.Args)
2020-10-31 15:51:20 +00:00
log.Printf("Default Img Type: %v, Geometry: %+v", defType, defGeom)
2021-03-08 12:26:09 +00:00
htmlTmpl, err = template.New("wrp.html").Parse(tmpl(tHTML))
2020-10-29 14:16:14 +00:00
if err != nil {
log.Fatal(err)
}
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
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
}