mirror of
https://github.com/tenox7/wrp.git
synced 2026-02-10 21:32:04 +00:00
Compare commits
55 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7baaa0bd6e | ||
|
|
e5e5e321e8 | ||
|
|
650ac026c3 | ||
|
|
579d67f7fb | ||
|
|
eb1476e579 | ||
|
|
f599a51c8d | ||
|
|
dedf7479b8 | ||
|
|
1f5592cbde | ||
|
|
404af50aa1 | ||
|
|
bb59229438 | ||
|
|
faa0818f18 | ||
|
|
26f999f262 | ||
|
|
4d02165619 | ||
|
|
1578b14fcd | ||
|
|
ceb6a67ff3 | ||
|
|
3ee146dee7 | ||
|
|
1e58c94263 | ||
|
|
ce51eb6226 | ||
|
|
1b68593fd2 | ||
|
|
8f16abacde | ||
|
|
b1e0b417c3 | ||
|
|
fd6f8592ef | ||
|
|
210a12fe3d | ||
|
|
92f3cb7aee | ||
|
|
d9381ef71a | ||
|
|
64f86b4fd9 | ||
|
|
2d41aa1044 | ||
|
|
6e43026100 | ||
|
|
3285a60c69 | ||
|
|
c873e53df0 | ||
|
|
dc6c8eca52 | ||
|
|
849239fc8e | ||
|
|
d71a48b746 | ||
|
|
2671fc236c | ||
|
|
15b227ccf1 | ||
|
|
89f5f556f9 | ||
|
|
af3aef5c39 | ||
|
|
26ad732d99 | ||
|
|
033f2f3578 | ||
|
|
277d70f4c3 | ||
|
|
ab4122a9ba | ||
|
|
9bd1359a4d | ||
|
|
adff09c6b9 | ||
|
|
93c84fdfca | ||
|
|
de780b353d | ||
|
|
d64ae7e5d0 | ||
|
|
6f702d74e5 | ||
|
|
6a8f655953 | ||
|
|
1b8d3544ed | ||
|
|
b5f5d6c576 | ||
|
|
99f4c8cac3 | ||
|
|
6e75da10f3 | ||
|
|
dd1031a35b | ||
|
|
fec97243ba | ||
|
|
aabc8cf021 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +0,0 @@
|
||||
.vscode
|
||||
9
Makefile
9
Makefile
@@ -3,6 +3,12 @@ all: linux freebsd openbsd macos windows rpi
|
||||
linux:
|
||||
GOOS=linux GOARCH=amd64 go build -a -o wrp-linux wrp.go
|
||||
|
||||
linux-ppc64le:
|
||||
GOOS=linux GOARCH=ppc64le go build -a -o wrp-linux-ppc64le wrp.go
|
||||
|
||||
rpi:
|
||||
GOOS=linux GOARCH=arm go build -a -o wrp-linux-rpi wrp.go
|
||||
|
||||
freebsd:
|
||||
GOOS=freebsd GOARCH=amd64 go build -a -o wrp-freebsd wrp.go
|
||||
|
||||
@@ -15,8 +21,5 @@ macos:
|
||||
windows:
|
||||
GOOS=windows GOARCH=amd64 go build -a -o wrp-windows.exe wrp.go
|
||||
|
||||
rpi:
|
||||
GOOS=linux GOARCH=arm go build -a -o wrp-linux-rpi wrp.go
|
||||
|
||||
clean:
|
||||
rm -rf wrp-linux wrp-freebsd wrp-openbsd wrp-macos wrp-windows.exe wrp-linux-rpi
|
||||
|
||||
51
README.md
51
README.md
@@ -2,30 +2,41 @@
|
||||
|
||||
A HTTP proxy server that allows to use historical and obsolete web browsers on the modern web. It works by rendering the web page in to a GIF image associated with clickable imagemap of original web links.
|
||||
|
||||
**You are looking at a GoLang / CDP branch of WRP.**
|
||||
## Current Status
|
||||
|
||||
**This code is under active development and not fully usable yet.**
|
||||
* This is the new GoLang/[ChdomeDP](https://github.com/chromedp/chromedp) version.
|
||||
* It's still lacking some features of the [older version](/old) but far surpasses it in terms of stability and usability.
|
||||
* It's beta quality but unlike the old version, it's now fully supported an maintained.
|
||||
* Currently works as browser-in-browser. A real http proxy mode is being investigated. Check [issue #35](https://github.com/tenox7/wrp/issues/35) for updates.
|
||||
|
||||
## Done so far
|
||||
## Usage
|
||||
|
||||
* basic browser-in-browser mode
|
||||
* screenshot and serve image+map via CDP
|
||||
* gif with Floyd–Steinberg dithering
|
||||
* multiple concurent client support
|
||||
* resolve relative links
|
||||
* paginated scrolling
|
||||
* google search on input not starting with ^http
|
||||
* ISMAP, although for a redirect to work `-i` flag must be specified
|
||||
otherwise http-equiv refresh will be used and/or link provided
|
||||
* headed mode and chromedp debug output
|
||||
1. [Download a WRP binary](https://github.com/tenox7/wrp/releases) and run it on a machine that will become your WRP server.
|
||||
2. Point your legacy browser to `http://address:port` of WRP server. Do not set or use it as a "Proxy Server" (yet).
|
||||
3. Type a search string or a http/https URL and click Go.
|
||||
4. Adjust your screen width/height/scale/#colors to fit in your old browser.
|
||||
5. For very very very old browsers such as Mosaic 2.x and IBM WebExplorer 1.x tick the I checkbox to enable ISMAP mode. However this normally should not be needed.
|
||||
6. Scroll web page by clicking Up/Down. To go to top enter 0 and click Go.
|
||||
|
||||
## Todo
|
||||

|
||||
|
||||
* configurable color palete and quantization
|
||||
* real http proxy support
|
||||
* padded box model coordinates
|
||||
* better http server shutdown
|
||||
## Flags
|
||||
```
|
||||
-l listen address:port, default :8080
|
||||
-h headed mode, display browser window
|
||||
-d chromedp debug logging
|
||||
```
|
||||
|
||||
## Old Python version
|
||||
## More info and screenshots
|
||||
* http://virtuallyfun.superglobalmegacorp.com/2014/03/11/web-rendering-proxy-update/
|
||||
* http://virtuallyfun.superglobalmegacorp.com/2014/03/03/surfing-modern-web-with-ancient-browsers/
|
||||
|
||||
Check [pywebkit/](/pywebkit) folder for the old Python-Webkit version.
|
||||
## Credits
|
||||
* Uses [chromedp](https://github.com/chromedp), thanks to [mvdan](https://github.com/mvdan) for dealing with my issues
|
||||
* Uses [go-quantize](https://github.com/ericpauley/go-quantize), thanks to [ericpauley](https://github.com/ericpauley) for developing the missing go quantizer
|
||||
* Thanks to Jason Stevens of [Fun With Virtualization](https://virtuallyfun.com/) for graciously hosting my rumblings
|
||||
|
||||
## Legal Stuff
|
||||
License: Apache 2.0
|
||||
Copyright (c) 2013-2018 Antoni Sawicki
|
||||
Copyright (c) 2019 Google LLC
|
||||
|
||||
@@ -284,10 +284,10 @@ if sys.platform.startswith('linux') or sys.platform.startswith('freebsd'):
|
||||
if ISMAP == True:
|
||||
mapfile.write("rect %s %i,%i %i,%i\n".decode('utf-8', errors='ignore') % (turl, xmin, ymin, xmax, ymax))
|
||||
else:
|
||||
httpout.write("<AREA SHAPE=\"RECT\""
|
||||
httpout.write(("<AREA SHAPE=\"RECT\""
|
||||
" COORDS=\"%i,%i,%i,%i\""
|
||||
" ALT=\"%s\" HREF=\"%s\">\n".decode('utf-8', errors='ignore')
|
||||
% (xmin, ymin, xmax, ymax, turl, turl))
|
||||
% (xmin, ymin, xmax, ymax, turl, turl)).encode("utf-8"))
|
||||
|
||||
if ISMAP != True:
|
||||
httpout.write("</MAP>\n")
|
||||
@@ -1,36 +0,0 @@
|
||||
## [2.0] - 2017-05-10
|
||||
### Added
|
||||
- Support PyQt5 if available.
|
||||
- Sets title from original one.
|
||||
- Returns server errors as is.
|
||||
- Download non-HTML files as is.
|
||||
- For JavaScript capable browsers detect and automatically set view width.
|
||||
- Add support for configuring which image format to use.
|
||||
- Added support for PythonMagick. If found, allows to dither, color-reduce, or convert to grayscale or monochrome.
|
||||
- If PythonMagick is found, render as PNG and convert to user-requested format using it, for better quality.
|
||||
|
||||
### Changed
|
||||
- Support www prepented to http://wrp.stop command.
|
||||
|
||||
### Fixed
|
||||
- Prevent python crashes with non-ASCII character in URLs.
|
||||
|
||||
## [1.4] - 2017-01-22
|
||||
### Added
|
||||
- Suport for ISMAP on Linux.
|
||||
- Use queues instead of globals in Linux.
|
||||
|
||||
## [1.3] - 2017-01-21
|
||||
### Changed
|
||||
- Merged mac OS and Linux in a single executable.
|
||||
- Use queues instead of globals in Linux.
|
||||
|
||||
### Fixed
|
||||
- Call PyQt to close application on http://wrp.stop
|
||||
|
||||
## [1.2] - 2016-12-27
|
||||
### Added
|
||||
- Support for IMAP on mac OS.
|
||||
|
||||
### Changed
|
||||
- Use queues instead of globals in mac OS.
|
||||
@@ -1,29 +0,0 @@
|
||||
# WRP - Web Rendering Proxy
|
||||
A HTTP proxy server that allows to use historical and obsolete web browsers on the modern web. It works by rendering the web page in to a GIF/PNG/JPEG image associated with clickable imagemap of original web links.
|
||||
|
||||
|
||||
# Current Status
|
||||
* This is a WebKit / Python version of WRP.
|
||||
* No longer maintained / supported.
|
||||
* You should be using GoLang/CDP version instead.
|
||||
* It mostly works for casual browsing but it's not very stable.
|
||||
* Secure aka https/SSL/TLS websites might work with use of [sslstrip](https://moxie.org/software/sslstrip/) cheat (enabled by default).
|
||||
|
||||
|
||||
## OS Support
|
||||
WRP works on macOS (Mac OS X), Linux and FreeBSD. On macOS it uses Cocoa Webkit, on Linux/FreeBSD QT Webkit, for which needs PyQT4 or PyQT5. It does not work on Windows. Use Go/CDP version for that.
|
||||
|
||||
## Installation
|
||||
* macOS - should just work
|
||||
* Linux/FreeBSD install `python-pyqt5.qtwebkit` and `sslstrip`
|
||||
* For PythonMagick (Imagemagick library) install `python-pythonmagick`
|
||||
|
||||
## Configuration
|
||||
Edit wrp.py, scroll past Copyright section to find config parameters
|
||||
|
||||
## Usage
|
||||
Configure your web browser to use HTTP proxy at IP address and port where WRP is running. If using browsers prior to HTML 3.2, ISMAP option may need to be enabled. Check configuration.
|
||||
|
||||
## More info and screenshots
|
||||
* http://virtuallyfun.superglobalmegacorp.com/2014/03/11/web-rendering-proxy-update/
|
||||
* http://virtuallyfun.superglobalmegacorp.com/2014/03/03/surfing-modern-web-with-ancient-browsers/
|
||||
297
wrp.go
297
wrp.go
@@ -12,7 +12,6 @@ import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
_ "image"
|
||||
"image/gif"
|
||||
"image/png"
|
||||
"log"
|
||||
@@ -24,93 +23,109 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/chromedp/cdproto/emulation"
|
||||
"github.com/chromedp/cdproto/runtime"
|
||||
|
||||
"github.com/chromedp/cdproto/cdp"
|
||||
"github.com/chromedp/cdproto/dom"
|
||||
"github.com/chromedp/chromedp"
|
||||
|
||||
"github.com/ericpauley/go-quantize/quantize"
|
||||
)
|
||||
|
||||
// Ismap for server side processing
|
||||
type Ismap struct {
|
||||
xmin int64
|
||||
ymin int64
|
||||
xmax int64
|
||||
ymax int64
|
||||
url string
|
||||
}
|
||||
|
||||
var (
|
||||
version = "3.0"
|
||||
version = "4.0"
|
||||
srv http.Server
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
gifmap = make(map[string]bytes.Buffer)
|
||||
ismap = make(map[string][]Ismap)
|
||||
ismap = make(map[string]wrpReq)
|
||||
)
|
||||
|
||||
func pageServer(out http.ResponseWriter, req *http.Request) {
|
||||
type wrpReq struct {
|
||||
U string // url
|
||||
W int64 // width
|
||||
H int64 // height
|
||||
S float64 // scale
|
||||
C int64 // #colors
|
||||
X int64 // mouseX
|
||||
Y int64 // mouseY
|
||||
}
|
||||
|
||||
func (w *wrpReq) parseForm(req *http.Request) {
|
||||
req.ParseForm()
|
||||
u := req.FormValue("url")
|
||||
var istr string
|
||||
var ion string
|
||||
var i bool
|
||||
if req.FormValue("i") == "on" {
|
||||
istr = "CHECKED"
|
||||
i = true
|
||||
ion = "&i=on"
|
||||
} else {
|
||||
istr = ""
|
||||
i = false
|
||||
w.U = req.FormValue("url")
|
||||
if len(w.U) > 1 && !strings.HasPrefix(w.U, "http") {
|
||||
w.U = fmt.Sprintf("http://www.google.com/search?q=%s", url.QueryEscape(w.U))
|
||||
}
|
||||
p, _ := strconv.ParseInt(req.FormValue("p"), 10, 64)
|
||||
if req.FormValue("pg") == "Dn" {
|
||||
p++
|
||||
} else if req.FormValue("pg") == "Up" {
|
||||
p--
|
||||
} else {
|
||||
p = 0
|
||||
w.W, _ = strconv.ParseInt(req.FormValue("w"), 10, 64)
|
||||
if w.W < 10 {
|
||||
w.W = 1152
|
||||
}
|
||||
w, _ := strconv.ParseInt(req.FormValue("w"), 10, 64)
|
||||
if w < 10 {
|
||||
w = 1024
|
||||
w.H, _ = strconv.ParseInt(req.FormValue("h"), 10, 64)
|
||||
if w.H < 10 {
|
||||
w.H = 600
|
||||
}
|
||||
h, _ := strconv.ParseInt(req.FormValue("h"), 10, 64)
|
||||
if h < 10 {
|
||||
h = 768
|
||||
w.S, _ = strconv.ParseFloat(req.FormValue("s"), 64)
|
||||
if w.S < 0.1 {
|
||||
w.S = 1.0
|
||||
}
|
||||
s, _ := strconv.ParseFloat(req.FormValue("s"), 64)
|
||||
if s < 0.1 {
|
||||
s = 1.0
|
||||
w.C, _ = strconv.ParseInt(req.FormValue("c"), 10, 64)
|
||||
if w.C < 2 || w.C > 256 {
|
||||
w.C = 256
|
||||
}
|
||||
c, _ := strconv.ParseInt(req.FormValue("c"), 10, 64)
|
||||
if c < 2 || c > 256 {
|
||||
c = 256
|
||||
}
|
||||
log.Printf("%s Page Reqest for url=\"%s\" [%s]\n", req.RemoteAddr, u, req.URL.Path)
|
||||
log.Printf("WrpReq from Form: %+v\n", w)
|
||||
}
|
||||
|
||||
func (w wrpReq) printPage(out http.ResponseWriter) {
|
||||
out.Header().Set("Content-Type", "text/html")
|
||||
fmt.Fprintf(out, "<!-- Web Rendering Proxy Version %s -->\n", version)
|
||||
fmt.Fprintf(out, "<HTML>\n<HEAD><TITLE>WRP %s</TITLE></HEAD>\n<BODY BGCOLOR=\"#F0F0F0\">\n", u)
|
||||
fmt.Fprintf(out, "<FORM ACTION=\"/\"><INPUT TYPE=\"TEXT\" NAME=\"url\" VALUE=\"%s\" SIZE=\"20\">", u)
|
||||
fmt.Fprintf(out, "<HTML>\n<HEAD><TITLE>WRP %s</TITLE></HEAD>\n<BODY BGCOLOR=\"#F0F0F0\">\n", w.U)
|
||||
fmt.Fprintf(out, "<FORM ACTION=\"/\"><INPUT TYPE=\"TEXT\" NAME=\"url\" VALUE=\"%s\" SIZE=\"40\">", w.U)
|
||||
fmt.Fprintf(out, "<INPUT TYPE=\"SUBMIT\" VALUE=\"Go\"> \n")
|
||||
fmt.Fprintf(out, "<INPUT TYPE=\"SUBMIT\" NAME=\"pg\" VALUE=\"Up\"> \n")
|
||||
fmt.Fprintf(out, "<INPUT TYPE=\"TEXT\" NAME=\"p\" VALUE=\"%d\" SIZE=\"2\"> \n", p)
|
||||
fmt.Fprintf(out, "<INPUT TYPE=\"SUBMIT\" NAME=\"pg\" VALUE=\"Dn\"> \n")
|
||||
fmt.Fprintf(out, "I <INPUT TYPE=\"CHECKBOX\" NAME=\"i\" %s> \n", istr)
|
||||
fmt.Fprintf(out, "W <INPUT TYPE=\"TEXT\" NAME=\"w\" VALUE=\"%d\" SIZE=\"4\"> \n", w)
|
||||
fmt.Fprintf(out, "H <INPUT TYPE=\"TEXT\" NAME=\"h\" VALUE=\"%d\" SIZE=\"4\"> \n", h)
|
||||
fmt.Fprintf(out, "S <INPUT TYPE=\"TEXT\" NAME=\"s\" VALUE=\"%1.2f\" SIZE=\"3\"> \n", s)
|
||||
fmt.Fprintf(out, "C <INPUT TYPE=\"TEXT\" NAME=\"c\" VALUE=\"%d\" SIZE=\"3\"> \n", c)
|
||||
fmt.Fprintf(out, "W <INPUT TYPE=\"TEXT\" NAME=\"w\" VALUE=\"%d\" SIZE=\"4\"> \n", w.W)
|
||||
fmt.Fprintf(out, "H <INPUT TYPE=\"TEXT\" NAME=\"h\" VALUE=\"%d\" SIZE=\"4\"> \n", w.H)
|
||||
fmt.Fprintf(out, "S <INPUT TYPE=\"TEXT\" NAME=\"s\" VALUE=\"%1.2f\" SIZE=\"3\"> \n", w.S)
|
||||
fmt.Fprintf(out, "C <INPUT TYPE=\"TEXT\" NAME=\"c\" VALUE=\"%d\" SIZE=\"3\"> \n", w.C)
|
||||
fmt.Fprintf(out, "</FORM><BR>\n")
|
||||
if len(u) > 1 {
|
||||
if strings.HasPrefix(u, "http") {
|
||||
capture(u, w, h, s, int(c), p, i, req.RemoteAddr, out)
|
||||
} else {
|
||||
capture(fmt.Sprintf("http://www.google.com/search?q=%s", url.QueryEscape(u)), w, h, s, int(c), p, i, req.RemoteAddr, out)
|
||||
}
|
||||
}
|
||||
|
||||
func (w wrpReq) printFooter(out http.ResponseWriter) {
|
||||
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", w.W, w.H, w.S, w.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)
|
||||
var w wrpReq
|
||||
w.parseForm(req)
|
||||
|
||||
if len(w.U) > 4 {
|
||||
w.capture(req.RemoteAddr, out)
|
||||
} else {
|
||||
fmt.Fprintf(out, "No URL or search query specified")
|
||||
w.printPage(out)
|
||||
w.printFooter(out)
|
||||
}
|
||||
}
|
||||
|
||||
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]
|
||||
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)
|
||||
n, err := fmt.Sscanf(req.URL.RawQuery, "%d,%d", &w.X, &w.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, w)
|
||||
if len(w.U) > 4 {
|
||||
w.capture(req.RemoteAddr, out)
|
||||
} else {
|
||||
w.printPage(out)
|
||||
w.printFooter(out)
|
||||
}
|
||||
fmt.Fprintf(out, "\n<P><A HREF=\"/?url=https://github.com/tenox7/wrp/&w=%d&h=%d&s=%1.2f&c=%d%s\">Web Rendering Proxy Version %s</A></BODY>\n</HTML>\n", w, h, s, c, ion, version)
|
||||
}
|
||||
|
||||
func imgServer(out http.ResponseWriter, req *http.Request) {
|
||||
@@ -128,66 +143,51 @@ func imgServer(out http.ResponseWriter, req *http.Request) {
|
||||
out.(http.Flusher).Flush()
|
||||
}
|
||||
|
||||
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)
|
||||
var loc string
|
||||
var x, y int64
|
||||
n, err := fmt.Sscanf(req.URL.RawQuery, "%d,%d", &x, &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
|
||||
}
|
||||
is, ok := ismap[req.URL.Path]
|
||||
if !ok || is == nil {
|
||||
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)
|
||||
for _, i := range is {
|
||||
if x >= i.xmin && x <= i.xmax && y >= i.ymin && y <= i.ymax {
|
||||
loc = i.url
|
||||
}
|
||||
}
|
||||
if len(loc) < 1 {
|
||||
loc = is[0].url
|
||||
}
|
||||
log.Printf("%s ISMAP Redirect to: http://%s%s\n", req.RemoteAddr, req.Context().Value(http.LocalAddrContextKey), loc)
|
||||
http.Redirect(out, req, fmt.Sprintf("http://%s%s", req.Context().Value(http.LocalAddrContextKey), loc), 301)
|
||||
}
|
||||
|
||||
func capture(gourl string, w int64, h int64, s float64, co int, p int64, i bool, c string, out http.ResponseWriter) {
|
||||
var nodes []*cdp.Node
|
||||
ctxx := chromedp.FromContext(ctx)
|
||||
func (w wrpReq) capture(c string, out http.ResponseWriter) {
|
||||
var pngbuf []byte
|
||||
var gifbuf bytes.Buffer
|
||||
var loc string
|
||||
var res *runtime.RemoteObject
|
||||
is := make([]Ismap, 0)
|
||||
var ion string
|
||||
var err error
|
||||
|
||||
log.Printf("%s Processing Caputure Request for %s\n", c, gourl)
|
||||
|
||||
// Run ChromeDP Magic
|
||||
err := chromedp.Run(ctx,
|
||||
emulation.SetDeviceMetricsOverride(int64(float64(w)/s), int64(float64(h)/s), s, false),
|
||||
chromedp.Navigate(gourl),
|
||||
chromedp.Evaluate(fmt.Sprintf("window.scrollTo(0, %d);", p*int64(float64(h)*float64(0.9))), &res),
|
||||
chromedp.Sleep(time.Second*1),
|
||||
chromedp.CaptureScreenshot(&pngbuf),
|
||||
chromedp.Location(&loc),
|
||||
chromedp.Nodes("a", &nodes, chromedp.ByQueryAll))
|
||||
// Navigate to page or process click request
|
||||
if w.X > 0 && w.Y > 0 {
|
||||
log.Printf("%s Mouse Click %d,%d\n", c, w.X, w.Y)
|
||||
chromedp.Run(ctx,
|
||||
chromedp.MouseClickXY(int64(float64(w.X)/w.S), int64(float64(w.Y)/w.S)),
|
||||
chromedp.Sleep(time.Second*3),
|
||||
chromedp.Location(&loc))
|
||||
} else {
|
||||
log.Printf("%s Processing Capture Request for %s\n", c, w.U)
|
||||
err = chromedp.Run(ctx,
|
||||
emulation.SetDeviceMetricsOverride(int64(float64(w.W)/w.S), int64(float64(w.H)/w.S), w.S, false),
|
||||
chromedp.Navigate(w.U),
|
||||
chromedp.Sleep(time.Second*3),
|
||||
chromedp.Location(&loc))
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Printf("%s %s", c, err)
|
||||
fmt.Fprintf(out, "<BR>%s<BR>", err)
|
||||
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)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("%s Landed on: %s, Nodes: %d\n", c, loc, len(nodes))
|
||||
log.Printf("%s Landed on: %s\n", c, loc)
|
||||
w.U = loc
|
||||
w.printPage(out)
|
||||
|
||||
// Process Screenshot Image
|
||||
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
|
||||
}
|
||||
bytes.NewReader(pngbuf).Seek(0, 0)
|
||||
img, err := png.Decode(bytes.NewReader(pngbuf))
|
||||
if err != nil {
|
||||
@@ -196,60 +196,29 @@ func capture(gourl string, w int64, h int64, s float64, co int, p int64, i bool,
|
||||
return
|
||||
}
|
||||
gifbuf.Reset()
|
||||
err = gif.Encode(&gifbuf, img, &gif.Options{NumColors: co})
|
||||
err = gif.Encode(&gifbuf, img, &gif.Options{NumColors: int(w.C), Quantizer: quantize.MedianCutQuantizer{}})
|
||||
if err != nil {
|
||||
log.Printf("%s Failed to encode GIF: %s\n", c, err)
|
||||
fmt.Fprintf(out, "<BR>Unable to encode GIF:<BR>%s<BR>\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Compose map and gif
|
||||
seq := rand.Intn(9999)
|
||||
imgpath := fmt.Sprintf("/img/%04d.gif", seq)
|
||||
mappath := fmt.Sprintf("/map/%04d.map", seq)
|
||||
log.Printf("%s Encoded GIF image: %s, Size: %dKB\n", c, imgpath, len(gifbuf.Bytes())/1024)
|
||||
gifmap[imgpath] = gifbuf
|
||||
ismap[mappath] = w
|
||||
log.Printf("%s Encoded GIF image: %s, Size: %dKB, Colors: %d\n", c, imgpath, len(gifbuf.Bytes())/1024, w.C)
|
||||
fmt.Fprintf(out, "<A HREF=\"%s\"><IMG SRC=\"%s\" ALT=\"wrp\" BORDER=\"0\" ISMAP></A>", mappath, imgpath)
|
||||
w.printFooter(out)
|
||||
log.Printf("%s Done with caputure for %s\n", c, w.U)
|
||||
}
|
||||
|
||||
// Process Nodes
|
||||
base, _ := url.Parse(loc)
|
||||
if i {
|
||||
fmt.Fprintf(out, "<A HREF=\"%s\"><IMG SRC=\"%s\" ALT=\"wrp\" BORDER=\"0\" ISMAP></A>", mappath, imgpath)
|
||||
is = append(is, Ismap{xmin: -1, xmax: -1, ymin: -1, ymax: -1, url: fmt.Sprintf("/?url=%s&w=%d&h=%d&s=%1.2f&c=%d&i=on", loc, w, h, s, co)})
|
||||
ion = "&i=on"
|
||||
} else {
|
||||
fmt.Fprintf(out, "<IMG SRC=\"%s\" ALT=\"wrp\" BORDER=\"0\" USEMAP=\"#map\">\n<MAP NAME=\"map\">\n", imgpath)
|
||||
}
|
||||
|
||||
for _, n := range nodes {
|
||||
b, err := dom.GetBoxModel().WithNodeID(n.NodeID).Do(cdp.WithExecutor(ctx, ctxx.Target))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
tgt, err := base.Parse(n.AttributeValue("href"))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
target := fmt.Sprintf("/?url=%s&w=%d&h=%d&s=%1.2f&c=%d%s", tgt, w, h, s, co, ion) // no page# here
|
||||
|
||||
if len(b.Content) > 6 && len(target) > 7 {
|
||||
if i {
|
||||
is = append(is, Ismap{
|
||||
xmin: int64(b.Content[0] * s), ymin: int64(b.Content[1] * s),
|
||||
xmax: int64(b.Content[4] * s), ymax: int64(b.Content[5] * s),
|
||||
url: target})
|
||||
} else {
|
||||
fmt.Fprintf(out, "<AREA SHAPE=\"RECT\" COORDS=\"%.f,%.f,%.f,%.f\" ALT=\"%s\" TITLE=\"%s\" HREF=\"%s\">\n",
|
||||
b.Content[0]*s, b.Content[1]*s, b.Content[4]*s, b.Content[5]*s, n.AttributeValue("href"), n.AttributeValue("href"), target)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if i {
|
||||
log.Printf("%s Encoded ISMAP %s\n", c, mappath)
|
||||
} else {
|
||||
fmt.Fprintf(out, "</MAP>\n")
|
||||
}
|
||||
out.(http.Flusher).Flush()
|
||||
log.Printf("%s Done with caputure for %s\n", c, gourl)
|
||||
ismap[mappath] = is
|
||||
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())
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -267,6 +236,7 @@ func main() {
|
||||
}
|
||||
opts := append(chromedp.DefaultExecAllocatorOptions[:],
|
||||
chromedp.Flag("headless", headless),
|
||||
chromedp.Flag("hide-scrollbars", false),
|
||||
)
|
||||
actx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
|
||||
defer cancel()
|
||||
@@ -278,10 +248,15 @@ func main() {
|
||||
defer cancel()
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
http.HandleFunc("/", pageServer)
|
||||
http.HandleFunc("/img/", imgServer)
|
||||
http.HandleFunc("/map/", mapServer)
|
||||
http.HandleFunc("/img/", imgServer)
|
||||
http.HandleFunc("/shutdown/", haltServer)
|
||||
http.HandleFunc("/favicon.ico", http.NotFound)
|
||||
log.Printf("Web Rendering Proxy Version %s\n", version)
|
||||
log.Printf("Starting WRP http server on %s\n", addr)
|
||||
http.ListenAndServe(addr, nil)
|
||||
srv.Addr = addr
|
||||
err := srv.ListenAndServe()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user