2019-05-30 01:53:05 +00:00
|
|
|
//
|
|
|
|
// WRP - Web Rendering Proxy
|
|
|
|
//
|
|
|
|
// Copyright (c) 2013-2018 Antoni Sawicki
|
|
|
|
// Copyright (c) 2019 Google LLC
|
|
|
|
//
|
|
|
|
|
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-05-30 06:49:39 +00:00
|
|
|
"image/gif"
|
|
|
|
"image/png"
|
2019-05-29 08:52:28 +00:00
|
|
|
"log"
|
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-05-29 08:52:28 +00:00
|
|
|
"strconv"
|
2019-05-31 07:36:53 +00:00
|
|
|
"strings"
|
2019-05-29 08:52:28 +00:00
|
|
|
"time"
|
2019-05-29 08:29:01 +00:00
|
|
|
|
2019-05-29 09:39:06 +00:00
|
|
|
"github.com/chromedp/cdproto/emulation"
|
2019-05-31 07:19:10 +00:00
|
|
|
"github.com/chromedp/cdproto/runtime"
|
2019-05-29 09:39:06 +00:00
|
|
|
|
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-07-11 01:09:20 +00:00
|
|
|
version = "4.0"
|
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-06-02 23:19:08 +00:00
|
|
|
gifmap = make(map[string]bytes.Buffer)
|
2019-07-10 08:01:40 +00:00
|
|
|
ismap = make(map[string]WrpReq)
|
2019-05-29 08:29:01 +00:00
|
|
|
)
|
|
|
|
|
2019-07-10 08:01:40 +00:00
|
|
|
// WrpReq - WRP Page Request
|
|
|
|
type WrpReq struct {
|
2019-06-26 00:07:43 +00:00
|
|
|
U string // url
|
|
|
|
P int64 // page
|
|
|
|
W int64 // width
|
|
|
|
H int64 // height
|
|
|
|
S float64 // scale
|
|
|
|
C int64 // #colors
|
2019-07-10 08:01:40 +00:00
|
|
|
X int64 // mouseX
|
|
|
|
Y int64 // mouseY
|
2019-06-26 00:07:43 +00:00
|
|
|
}
|
2019-06-24 07:40:34 +00:00
|
|
|
|
2019-07-10 08:01:40 +00:00
|
|
|
func (p *WrpReq) parseForm(req *http.Request) {
|
2019-06-26 00:07:43 +00:00
|
|
|
req.ParseForm()
|
|
|
|
p.U = req.FormValue("url")
|
|
|
|
if len(p.U) > 1 && !strings.HasPrefix(p.U, "http") {
|
|
|
|
p.U = fmt.Sprintf("http://www.google.com/search?q=%s", url.QueryEscape(p.U))
|
|
|
|
}
|
|
|
|
p.P, _ = strconv.ParseInt(req.FormValue("p"), 10, 64)
|
2019-06-04 08:20:41 +00:00
|
|
|
if req.FormValue("pg") == "Dn" {
|
2019-06-26 00:07:43 +00:00
|
|
|
p.P++
|
2019-06-04 08:20:41 +00:00
|
|
|
} else if req.FormValue("pg") == "Up" {
|
2019-06-26 00:07:43 +00:00
|
|
|
p.P--
|
2019-06-01 00:49:14 +00:00
|
|
|
} else {
|
2019-06-26 00:07:43 +00:00
|
|
|
p.P = 0
|
2019-05-31 23:41:25 +00:00
|
|
|
}
|
2019-06-26 00:07:43 +00:00
|
|
|
p.W, _ = strconv.ParseInt(req.FormValue("w"), 10, 64)
|
2019-06-26 08:07:13 +00:00
|
|
|
if p.W < 10 {
|
|
|
|
p.W = 1024
|
2019-05-31 01:08:48 +00:00
|
|
|
}
|
2019-06-26 00:07:43 +00:00
|
|
|
p.H, _ = strconv.ParseInt(req.FormValue("h"), 10, 64)
|
|
|
|
if p.H < 10 {
|
2019-07-11 01:09:20 +00:00
|
|
|
p.H = 600
|
2019-05-29 08:52:28 +00:00
|
|
|
}
|
2019-06-26 00:07:43 +00:00
|
|
|
p.S, _ = strconv.ParseFloat(req.FormValue("s"), 64)
|
|
|
|
if p.S < 0.1 {
|
|
|
|
p.S = 1.0
|
2019-05-31 01:08:48 +00:00
|
|
|
}
|
2019-06-26 00:07:43 +00:00
|
|
|
p.C, _ = strconv.ParseInt(req.FormValue("c"), 10, 64)
|
|
|
|
if p.C < 2 || p.C > 256 {
|
|
|
|
p.C = 256
|
2019-06-03 00:06:41 +00:00
|
|
|
}
|
2019-07-10 08:01:40 +00:00
|
|
|
log.Printf("WrpReq from Form: %+v\n", p)
|
2019-06-26 00:07:43 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 08:01:40 +00:00
|
|
|
func (p WrpReq) printPage(out http.ResponseWriter) {
|
2019-05-29 08:52:28 +00:00
|
|
|
out.Header().Set("Content-Type", "text/html")
|
2019-06-02 23:24:46 +00:00
|
|
|
fmt.Fprintf(out, "<!-- Web Rendering Proxy Version %s -->\n", version)
|
2019-06-26 00:07:43 +00:00
|
|
|
fmt.Fprintf(out, "<HTML>\n<HEAD><TITLE>WRP %s</TITLE></HEAD>\n<BODY BGCOLOR=\"#F0F0F0\">\n", p.U)
|
|
|
|
fmt.Fprintf(out, "<FORM ACTION=\"/\"><INPUT TYPE=\"TEXT\" NAME=\"url\" VALUE=\"%s\" SIZE=\"20\">", p.U)
|
2019-06-03 00:06:41 +00:00
|
|
|
fmt.Fprintf(out, "<INPUT TYPE=\"SUBMIT\" VALUE=\"Go\"> \n")
|
2019-06-04 08:20:41 +00:00
|
|
|
fmt.Fprintf(out, "<INPUT TYPE=\"SUBMIT\" NAME=\"pg\" VALUE=\"Up\"> \n")
|
2019-06-26 00:07:43 +00:00
|
|
|
fmt.Fprintf(out, "<INPUT TYPE=\"TEXT\" NAME=\"p\" VALUE=\"%d\" SIZE=\"2\"> \n", p.P)
|
2019-06-04 08:20:41 +00:00
|
|
|
fmt.Fprintf(out, "<INPUT TYPE=\"SUBMIT\" NAME=\"pg\" VALUE=\"Dn\"> \n")
|
2019-06-26 00:07:43 +00:00
|
|
|
fmt.Fprintf(out, "W <INPUT TYPE=\"TEXT\" NAME=\"w\" VALUE=\"%d\" SIZE=\"4\"> \n", p.W)
|
|
|
|
fmt.Fprintf(out, "H <INPUT TYPE=\"TEXT\" NAME=\"h\" VALUE=\"%d\" SIZE=\"4\"> \n", p.H)
|
|
|
|
fmt.Fprintf(out, "S <INPUT TYPE=\"TEXT\" NAME=\"s\" VALUE=\"%1.2f\" SIZE=\"3\"> \n", p.S)
|
|
|
|
fmt.Fprintf(out, "C <INPUT TYPE=\"TEXT\" NAME=\"c\" VALUE=\"%d\" SIZE=\"3\"> \n", p.C)
|
2019-06-04 08:20:41 +00:00
|
|
|
fmt.Fprintf(out, "</FORM><BR>\n")
|
2019-06-26 08:07:13 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 08:01:40 +00:00
|
|
|
func (p WrpReq) printFooter(out http.ResponseWriter) {
|
2019-06-26 08:07:13 +00:00
|
|
|
fmt.Fprintf(out, "\n<P><A HREF=\"/?url=https://github.com/tenox7/wrp/&w=%d&h=%d&s=%1.2f&c=%d\">"+
|
|
|
|
"Web Rendering Proxy Version %s</A> | <A HREF=\"/shutdown/\">Shutdown WRP</A></BODY>\n</HTML>\n", p.W, p.H, p.S, p.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)
|
2019-07-10 08:01:40 +00:00
|
|
|
var p WrpReq
|
2019-06-26 08:07:13 +00:00
|
|
|
p.parseForm(req)
|
2019-07-11 01:09:20 +00:00
|
|
|
|
2019-06-26 08:07:13 +00:00
|
|
|
if len(p.U) > 4 {
|
|
|
|
p.capture(req.RemoteAddr, out)
|
2019-07-11 01:09:20 +00:00
|
|
|
} else {
|
|
|
|
p.printPage(out)
|
|
|
|
p.printFooter(out)
|
2019-06-26 08:07:13 +00:00
|
|
|
}
|
2019-07-11 01:09:20 +00:00
|
|
|
|
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)
|
|
|
|
p, 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)
|
2019-07-10 08:01:40 +00:00
|
|
|
n, err := fmt.Sscanf(req.URL.RawQuery, "%d,%d", &p.X, &p.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, p)
|
2019-06-26 08:07:13 +00:00
|
|
|
if len(p.U) > 4 {
|
|
|
|
p.capture(req.RemoteAddr, out)
|
2019-07-11 01:09:20 +00:00
|
|
|
} else {
|
|
|
|
p.printPage(out)
|
|
|
|
p.printFooter(out)
|
2019-06-26 08:07:13 +00:00
|
|
|
}
|
2019-07-11 01:09:20 +00:00
|
|
|
|
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-06-03 05:23:41 +00:00
|
|
|
gifbuf, ok := gifmap[req.URL.Path]
|
|
|
|
if !ok || gifbuf.Bytes() == nil {
|
|
|
|
fmt.Fprintf(out, "Unable to find image %s\n", req.URL.Path)
|
|
|
|
log.Printf("Unable to find image %s\n", req.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
2019-05-30 09:03:17 +00:00
|
|
|
defer delete(gifmap, req.URL.Path)
|
2019-05-30 01:02:29 +00:00
|
|
|
out.Header().Set("Content-Type", "image/gif")
|
|
|
|
out.Header().Set("Content-Length", strconv.Itoa(len(gifbuf.Bytes())))
|
|
|
|
out.Write(gifbuf.Bytes())
|
2019-05-30 09:03:17 +00:00
|
|
|
out.(http.Flusher).Flush()
|
2019-05-29 08:29:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 08:01:40 +00:00
|
|
|
func (p WrpReq) capture(c string, out http.ResponseWriter) {
|
2019-05-30 09:03:17 +00:00
|
|
|
var pngbuf []byte
|
|
|
|
var gifbuf bytes.Buffer
|
2019-05-30 01:48:07 +00:00
|
|
|
var loc string
|
2019-05-31 07:19:10 +00:00
|
|
|
var res *runtime.RemoteObject
|
2019-07-10 08:01:40 +00:00
|
|
|
var err error
|
|
|
|
|
2019-07-10 08:20:18 +00:00
|
|
|
// Navigate to page or process click request
|
2019-07-10 08:01:40 +00:00
|
|
|
if p.X > 0 && p.Y > 0 {
|
|
|
|
log.Printf("%s Mouse Click %d,%d\n", c, p.X, p.Y)
|
|
|
|
chromedp.Run(ctx,
|
2019-07-11 01:09:20 +00:00
|
|
|
chromedp.MouseClickXY(int64(float64(p.X)/p.S), int64(float64(p.Y)/p.S)),
|
2019-07-10 08:01:40 +00:00
|
|
|
chromedp.Sleep(time.Second*3),
|
|
|
|
chromedp.Location(&loc))
|
|
|
|
} else {
|
|
|
|
log.Printf("%s Processing Capture Request for %s\n", c, p.U)
|
|
|
|
err = chromedp.Run(ctx,
|
|
|
|
emulation.SetDeviceMetricsOverride(int64(float64(p.W)/p.S), int64(float64(p.H)/p.S), p.S, false),
|
|
|
|
chromedp.Navigate(p.U),
|
2019-07-11 01:09:20 +00:00
|
|
|
chromedp.Evaluate(fmt.Sprintf("window.scrollTo(0, %d);", p.P*int64(float64(p.H)*p.S*float64(0.9))), &res),
|
2019-07-10 08:01:40 +00:00
|
|
|
chromedp.Sleep(time.Second*1),
|
|
|
|
chromedp.Location(&loc))
|
|
|
|
}
|
2019-05-29 08:29:01 +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" {
|
|
|
|
log.Printf("%s Contex cancelled, try again", c)
|
|
|
|
fmt.Fprintf(out, "<BR>%s<BR> -- restarting, try again", err)
|
|
|
|
ctx, cancel = chromedp.NewContext(context.Background())
|
|
|
|
} else {
|
|
|
|
log.Printf("%s %s", c, err)
|
|
|
|
fmt.Fprintf(out, "<BR>%s<BR>", err)
|
|
|
|
}
|
2019-06-17 07:21:32 +00:00
|
|
|
return
|
2019-05-31 07:41:46 +00:00
|
|
|
}
|
2019-07-10 08:01:40 +00:00
|
|
|
|
2019-06-26 08:07:13 +00:00
|
|
|
log.Printf("%s Landed on: %s\n", c, loc)
|
2019-07-11 01:09:20 +00:00
|
|
|
if p.U != loc {
|
|
|
|
p.U = loc
|
|
|
|
p.P = 0
|
|
|
|
}
|
|
|
|
p.printPage(out)
|
2019-05-30 01:48:07 +00:00
|
|
|
|
2019-05-30 09:03:17 +00:00
|
|
|
// Process Screenshot Image
|
2019-06-24 07:40:34 +00:00
|
|
|
err = chromedp.Run(ctx, chromedp.CaptureScreenshot(&pngbuf))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("%s Failed to capture screenshot: %s\n", c, err)
|
|
|
|
fmt.Fprintf(out, "<BR>Unable to capture screenshot:<BR>%s<BR>\n", err)
|
|
|
|
return
|
|
|
|
}
|
2019-05-30 09:15:52 +00:00
|
|
|
bytes.NewReader(pngbuf).Seek(0, 0)
|
2019-05-30 09:03:17 +00:00
|
|
|
img, err := png.Decode(bytes.NewReader(pngbuf))
|
2019-05-30 01:48:07 +00:00
|
|
|
if err != nil {
|
2019-06-02 23:05:36 +00:00
|
|
|
log.Printf("%s Failed to decode screenshot: %s\n", c, err)
|
2019-05-30 06:49:39 +00:00
|
|
|
fmt.Fprintf(out, "<BR>Unable to decode page screenshot:<BR>%s<BR>\n", err)
|
|
|
|
return
|
2019-05-30 01:48:07 +00:00
|
|
|
}
|
|
|
|
gifbuf.Reset()
|
2019-06-26 00:07:43 +00:00
|
|
|
err = gif.Encode(&gifbuf, img, &gif.Options{NumColors: int(p.C), Quantizer: quantize.MedianCutQuantizer{}})
|
2019-05-30 07:53:59 +00:00
|
|
|
if err != nil {
|
2019-06-02 23:05:36 +00:00
|
|
|
log.Printf("%s Failed to encode GIF: %s\n", c, err)
|
2019-05-30 07:53:59 +00:00
|
|
|
fmt.Fprintf(out, "<BR>Unable to encode GIF:<BR>%s<BR>\n", err)
|
|
|
|
return
|
|
|
|
}
|
2019-05-29 08:29:01 +00:00
|
|
|
|
2019-06-26 08:07:13 +00:00
|
|
|
// Compose map and gif
|
|
|
|
seq := rand.Intn(9999)
|
|
|
|
imgpath := fmt.Sprintf("/img/%04d.gif", seq)
|
|
|
|
mappath := fmt.Sprintf("/map/%04d.map", seq)
|
|
|
|
gifmap[imgpath] = gifbuf
|
|
|
|
ismap[mappath] = p
|
|
|
|
log.Printf("%s Encoded GIF image: %s, Size: %dKB, Colors: %d\n", c, imgpath, len(gifbuf.Bytes())/1024, p.C)
|
|
|
|
fmt.Fprintf(out, "<A HREF=\"%s\"><IMG SRC=\"%s\" ALT=\"wrp\" BORDER=\"0\" ISMAP></A>", mappath, imgpath)
|
2019-07-11 01:09:20 +00:00
|
|
|
p.printFooter(out)
|
2019-06-26 00:07:43 +00:00
|
|
|
log.Printf("%s Done with caputure for %s\n", c, p.U)
|
2019-05-29 08:29:01 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 06:53:22 +00:00
|
|
|
func haltServer(out http.ResponseWriter, req *http.Request) {
|
|
|
|
log.Printf("%s Shutdown Request for %s\n", req.RemoteAddr, req.URL.Path)
|
|
|
|
defer cancel()
|
|
|
|
srv.Shutdown(context.Background())
|
|
|
|
}
|
|
|
|
|
2019-05-29 08:29:01 +00:00
|
|
|
func main() {
|
2019-05-29 08:52:28 +00:00
|
|
|
var addr 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-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-05-29 08:52:28 +00:00
|
|
|
flag.Parse()
|
2019-06-04 00:50:16 +00:00
|
|
|
if head {
|
|
|
|
headless = false
|
|
|
|
} else {
|
|
|
|
headless = true
|
|
|
|
}
|
|
|
|
opts := append(chromedp.DefaultExecAllocatorOptions[:],
|
2019-06-04 07:58:45 +00:00
|
|
|
chromedp.Flag("headless", headless),
|
2019-06-04 00:50:16 +00:00
|
|
|
)
|
|
|
|
actx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
|
|
|
|
defer cancel()
|
|
|
|
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-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
|
|
|
|
err := srv.ListenAndServe()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-05-29 08:29:01 +00:00
|
|
|
}
|