randomized image url

This commit is contained in:
Antoni Sawicki 2019-05-30 02:03:17 -07:00
parent a897f76e20
commit 9ad651c72c
1 changed files with 23 additions and 11 deletions

34
wrp.go
View File

@ -16,6 +16,7 @@ import (
"image/gif"
"image/png"
"log"
"math/rand"
"net/http"
"net/url"
"os"
@ -32,7 +33,7 @@ import (
var (
ctx context.Context
cancel context.CancelFunc
gifbuf bytes.Buffer
gifmap = make(map[string]bytes.Buffer)
)
func pageServer(out http.ResponseWriter, req *http.Request) {
@ -42,9 +43,9 @@ func pageServer(out http.ResponseWriter, req *http.Request) {
if len(furl) >= 1 && len(furl[0]) > 4 {
gourl = furl[0]
} else {
gourl = "https://en.wikipedia.org/wiki/"
gourl = "https://www.bbc.com/news"
}
log.Printf("%s Page Reqest for %s URL=%s\n", req.RemoteAddr, req.URL.Path, gourl)
log.Printf("%s Page Reqest for %s [%s]\n", req.RemoteAddr, gourl, req.URL.Path)
out.Header().Set("Content-Type", "text/html")
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)
@ -56,10 +57,13 @@ func pageServer(out http.ResponseWriter, req *http.Request) {
}
func imgServer(out http.ResponseWriter, req *http.Request) {
log.Printf("%s Img Reqest for %s\n", req.RemoteAddr, req.URL.Path)
log.Printf("%s Img Request for %s\n", req.RemoteAddr, req.URL.Path)
gifbuf := gifmap[req.URL.Path]
defer delete(gifmap, req.URL.Path)
out.Header().Set("Content-Type", "image/gif")
out.Header().Set("Content-Length", strconv.Itoa(len(gifbuf.Bytes())))
out.Write(gifbuf.Bytes())
out.(http.Flusher).Flush()
}
func haltServer(out http.ResponseWriter, req *http.Request) {
@ -74,22 +78,25 @@ func haltServer(out http.ResponseWriter, req *http.Request) {
func capture(gourl string, out http.ResponseWriter) {
var nodes []*cdp.Node
ctxx := chromedp.FromContext(ctx)
var scrcap []byte
var pngbuf []byte
var gifbuf bytes.Buffer
var loc string
log.Printf("Caputure Request for %s\n", gourl)
log.Printf("Processing Caputure Request for %s\n", gourl)
// Run ChromeDP Magic
chromedp.Run(ctx,
emulation.SetDeviceMetricsOverride(1024, 768, 1.0, false),
chromedp.Navigate(gourl),
chromedp.Sleep(time.Second*2),
chromedp.CaptureScreenshot(&scrcap),
chromedp.CaptureScreenshot(&pngbuf),
chromedp.Location(&loc),
chromedp.Nodes("a", &nodes, chromedp.ByQueryAll))
log.Printf("Landed on: %s, Got %d nodes\n", loc, len(nodes))
img, err := png.Decode(bytes.NewReader(scrcap))
// Process Screenshot Image
img, err := png.Decode(bytes.NewReader(pngbuf))
if err != nil {
log.Printf("Failed to decode screenshot: %s\n", err)
fmt.Fprintf(out, "<BR>Unable to decode page screenshot:<BR>%s<BR>\n", err)
@ -102,9 +109,13 @@ func capture(gourl string, out http.ResponseWriter) {
fmt.Fprintf(out, "<BR>Unable to encode GIF:<BR>%s<BR>\n", err)
return
}
imgpath := fmt.Sprintf("/img/%04d.gif", rand.Intn(9999))
gifmap[imgpath] = gifbuf
// Process Nodes
base, _ := url.Parse(loc)
fmt.Fprintf(out, "<IMG SRC=\"/wrp.gif\" ALT=\"wrp\" USEMAP=\"#map\">\n<MAP NAME=\"map\">\n")
fmt.Fprintf(out, "<IMG SRC=\"%s\" ALT=\"wrp\" USEMAP=\"#map\">\n<MAP NAME=\"map\">\n", imgpath)
log.Printf("Image path will be: %s", imgpath)
for _, n := range nodes {
b, err := dom.GetBoxModel().WithNodeID(n.NodeID).Do(cdp.WithExecutor(ctx, ctxx.Target))
@ -124,6 +135,7 @@ func capture(gourl string, out http.ResponseWriter) {
}
fmt.Fprintf(out, "</MAP>\n")
out.(http.Flusher).Flush()
log.Printf("Done with caputure for %s\n", gourl)
}
@ -133,9 +145,9 @@ func main() {
var addr string
flag.StringVar(&addr, "l", ":8080", "Listen address:port, default :8080")
flag.Parse()
rand.Seed(time.Now().UnixNano())
http.HandleFunc("/", pageServer)
http.HandleFunc("/wrp.gif", imgServer)
http.HandleFunc("/img/", imgServer)
http.HandleFunc("/favicon.ico", http.NotFound)
http.HandleFunc("/halt", haltServer)
log.Printf("Starting http server on %s\n", addr)