wrp/wrp.go

127 lines
3.3 KiB
Go
Raw Normal View History

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-29 08:52:28 +00:00
"context"
"flag"
"fmt"
"log"
"net/http"
"strconv"
"time"
2019-05-30 01:02:29 +00:00
"bytes"
_ "image"
"image/png"
"image/gif"
2019-05-30 01:48:07 +00:00
"net/url"
2019-05-29 08:29:01 +00:00
2019-05-29 09:39:06 +00:00
"github.com/chromedp/cdproto/emulation"
2019-05-29 08:52:28 +00:00
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/dom"
"github.com/chromedp/chromedp"
2019-05-29 08:29:01 +00:00
)
var (
2019-05-29 08:52:28 +00:00
ctx context.Context
cancel context.CancelFunc
2019-05-30 01:02:29 +00:00
gifbuf bytes.Buffer
2019-05-29 08:29:01 +00:00
)
func pageServer(out http.ResponseWriter, req *http.Request) {
2019-05-29 08:52:28 +00:00
req.ParseForm()
furl := req.Form["url"]
2019-05-30 01:48:07 +00:00
var gourl string
2019-05-29 08:52:28 +00:00
if len(furl) >= 1 && len(furl[0]) > 4 {
2019-05-30 01:48:07 +00:00
gourl = furl[0]
2019-05-29 08:52:28 +00:00
} else {
2019-05-30 01:48:07 +00:00
gourl = "https://en.wikipedia.org/wiki/"
2019-05-29 08:52:28 +00:00
}
2019-05-30 01:48:07 +00:00
log.Printf("%s Page Reqest for %s URL=%s\n", req.RemoteAddr, req.URL.Path, gourl)
2019-05-29 08:52:28 +00:00
out.Header().Set("Content-Type", "text/html")
2019-05-30 01:48:07 +00:00
fmt.Fprintf(out, "<HTML>\n<HEAD><TITLE>WRP %s</TITLE>\n<BODY BGCOLOR=\"#F0F0F0\">", gourl)
fmt.Fprintf(out, "<FORM ACTION=\"/\">URL: <INPUT TYPE=\"TEXT\" NAME=\"url\" VALUE=\"%s\">", gourl)
2019-05-29 08:52:28 +00:00
fmt.Fprintf(out, "<INPUT TYPE=\"SUBMIT\" VALUE=\"Go\"></FORM><P>\n")
2019-05-30 01:48:07 +00:00
if len(gourl) > 4 {
capture(gourl, out)
2019-05-29 08:52:28 +00:00
}
fmt.Fprintf(out, "</BODY>\n</HTML>\n")
2019-05-29 08:29:01 +00:00
}
func imgServer(out http.ResponseWriter, req *http.Request) {
2019-05-29 08:52:28 +00:00
log.Printf("%s Img Reqest for %s\n", req.RemoteAddr, 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-29 08:29:01 +00:00
}
2019-05-30 01:48:07 +00:00
func capture(gourl string, out http.ResponseWriter) {
2019-05-29 08:52:28 +00:00
var nodes []*cdp.Node
ctxx := chromedp.FromContext(ctx)
2019-05-30 01:02:29 +00:00
var scrcap []byte
2019-05-30 01:48:07 +00:00
var loc string
2019-05-29 08:29:01 +00:00
2019-05-30 01:48:07 +00:00
log.Printf("Caputure Request for %s\n", gourl)
2019-05-29 09:39:06 +00:00
2019-05-29 08:52:28 +00:00
chromedp.Run(ctx,
2019-05-29 09:39:06 +00:00
emulation.SetDeviceMetricsOverride(1024, 768, 1.0, false),
2019-05-30 01:48:07 +00:00
chromedp.Navigate(gourl),
2019-05-29 08:52:28 +00:00
chromedp.Sleep(time.Second*2),
chromedp.CaptureScreenshot(&scrcap),
2019-05-30 01:48:07 +00:00
chromedp.Location(&loc),
2019-05-29 08:52:28 +00:00
chromedp.Nodes("a", &nodes, chromedp.ByQueryAll))
2019-05-29 08:29:01 +00:00
2019-05-30 01:48:07 +00:00
log.Printf("Landed on: %s, Got %d nodes\n", loc, len(nodes))
img, err:= png.Decode(bytes.NewReader(scrcap) )
if err != nil {
log.Fatal(err)
}
gifbuf.Reset()
gif.Encode(&gifbuf, img, nil)
2019-05-30 01:02:29 +00:00
2019-05-30 01:48:07 +00:00
base, _ := url.Parse(loc)
fmt.Fprintf(out, "<!-- Location: %s -->\n", base)
2019-05-30 01:02:29 +00:00
fmt.Fprintf(out, "<IMG SRC=\"/wrp.gif\" ALT=\"wrp\" USEMAP=\"#map\">\n<MAP NAME=\"map\">\n")
2019-05-29 08:29:01 +00:00
2019-05-29 08:52:28 +00:00
for _, n := range nodes {
b, err := dom.GetBoxModel().WithNodeID(n.NodeID).Do(cdp.WithExecutor(ctx, ctxx.Target))
2019-05-30 01:48:07 +00:00
if err != nil {
continue
}
tgt, err := base.Parse(n.AttributeValue("href"))
if err != nil {
continue
2019-05-29 08:52:28 +00:00
}
2019-05-30 01:48:07 +00:00
target := fmt.Sprintf("/?url=%s", tgt)
2019-05-29 08:29:01 +00:00
2019-05-30 01:48:07 +00:00
if len(b.Content) > 6 && len(target) > 7 {
2019-05-29 08:52:28 +00:00
fmt.Fprintf(out, "<AREA SHAPE=\"RECT\" COORDS=\"%.f,%.f,%.f,%.f\" ALT=\"%s\" TITLE=\"%s\" HREF=\"%s\">\n",
b.Content[0], b.Content[1], b.Content[4], b.Content[5], n.AttributeValue("href"), n.AttributeValue("href"), target)
}
}
2019-05-29 08:29:01 +00:00
2019-05-29 08:52:28 +00:00
fmt.Fprintf(out, "</MAP>\n")
2019-05-30 01:48:07 +00:00
log.Printf("Done with caputure for %s\n", gourl)
2019-05-29 08:29:01 +00:00
}
func main() {
2019-05-29 08:52:28 +00:00
ctx, cancel = chromedp.NewContext(context.Background())
defer cancel()
var addr string
flag.StringVar(&addr, "l", ":8080", "Listen address:port, default :8080")
flag.Parse()
2019-05-29 08:29:01 +00:00
2019-05-29 08:52:28 +00:00
http.HandleFunc("/", pageServer)
2019-05-30 01:02:29 +00:00
http.HandleFunc("/wrp.gif", imgServer)
2019-05-30 01:47:03 +00:00
http.HandleFunc("/favicon.ico", http.NotFound)
2019-05-29 08:52:28 +00:00
log.Printf("Starting http server on %s\n", addr)
http.ListenAndServe(addr, nil)
2019-05-29 08:29:01 +00:00
}