Compare commits

...

62 Commits

Author SHA1 Message Date
Antoni Sawicki 75233ccb97
Merge pull request #113 from luRaichu/master
Add PgUp, PgDn, and Select All buttons
2024-05-24 01:19:23 -07:00
Antoni Sawicki 04954f8be6 latest 2024-05-23 13:25:59 -07:00
Antoni Sawicki 884374edcd remove gcr.io 2024-05-23 01:05:26 -07:00
Antoni Sawicki ba522e3a86 readme update 2024-05-23 00:55:41 -07:00
Antoni Sawicki e05135e433 fix makefile 2024-05-23 00:39:06 -07:00
Antoni Sawicki 67d550672b legit update 2024-05-23 00:36:58 -07:00
Antoni Sawicki 71889effe7 bump ver 2024-05-23 00:34:46 -07:00
Antoni Sawicki 6a7ffbb67b bump ver 2024-05-23 00:34:10 -07:00
Antoni Sawicki f93058fed3 print architecture 2024-05-23 00:34:00 -07:00
Antoni Sawicki 67ed26b8f4 add arm64 docker container 2024-05-23 00:33:40 -07:00
Antoni Sawicki 6aefcdacc5 add linux arm64 build target 2024-05-23 00:19:34 -07:00
Antoni Sawicki de315fb95f go mod update 2024-05-23 00:18:12 -07:00
Antoni Sawicki b098b3632a README update 2024-01-06 02:13:22 -08:00
Antoni Sawicki 5ec0266f75 update README 2024-01-06 02:10:01 -08:00
Antoni Sawicki 7d9cec6297 add some anti crawler detection 2024-01-02 21:55:39 -08:00
Antoni Sawicki 43df501d97 add useragent override flag 2024-01-02 21:54:59 -08:00
Antoni Sawicki 85d64a3577 copyright update 2024-01-02 02:09:38 -08:00
Antoni Sawicki 993e5723ed more mod updates 2024-01-02 02:08:14 -08:00
Antoni Sawicki 9056c798d6 go mod update 2024-01-02 02:00:30 -08:00
luRaichu b937bea370
Add PgUp, PgDn, and Select All buttons 2023-10-18 18:30:40 -04:00
Antoni Sawicki 8fd65b3e47 readme update 2022-12-10 03:59:30 -08:00
Antoni Sawicki 8716a983ce readme update 2022-12-10 03:42:13 -08:00
Antoni Sawicki 4757cfd32b readme update 2022-12-10 03:38:56 -08:00
Antoni Sawicki 79e6f3a17e readme update 2022-12-10 03:38:22 -08:00
Antoni Sawicki d8f5c6fb28 print my own IP addresses 2022-12-08 01:49:46 -08:00
Antoni Sawicki c816ef712a readme update 2022-12-08 01:03:04 -08:00
Antoni Sawicki d28924583c move png to first case 2022-12-08 00:56:24 -08:00
Antoni Sawicki 1c17b39ea5 add jpeg encoding 2022-12-08 00:55:56 -08:00
Antoni Sawicki ecb2cc0c06 add todos for error handling 2022-12-08 00:28:28 -08:00
Antoni Sawicki b571df7a37 single function to handle context errors 2022-12-08 00:18:13 -08:00
Antoni Sawicki 78c568ac09 remove debug flag, never used 2022-12-07 23:54:18 -08:00
Antoni Sawicki 04a755749e close template files 2022-12-07 23:50:58 -08:00
Antoni Sawicki e983f244f8 move flags top top var() 2022-12-07 23:44:09 -08:00
Antoni Sawicki 9b76c045d6 fix html template embed 2022-11-30 02:00:53 -08:00
Antoni Sawicki c8e391274a update dependencies 2022-11-30 00:02:59 -08:00
Antoni Sawicki 7b1274b9d4 ver bump 2022-11-30 00:01:01 -08:00
Antoni Sawicki 0128b3ff8e add delay/sleep flag 2022-11-29 23:59:43 -08:00
Antoni Sawicki 6de3fad580
Merge pull request #101 from tenox7/fastgif
Fastgif
2022-11-29 23:45:53 -08:00
Antoni Sawicki d7dcb58adc readme update 2022-11-29 23:45:01 -08:00
Antoni Sawicki e5f83225f7 update readme 2022-11-29 07:02:06 -08:00
Antoni Sawicki 36803c4312 update readme 2022-11-29 06:57:31 -08:00
Antoni Sawicki 40e081be77 refactor fastgif code for readability 2022-11-29 05:50:17 -08:00
Antoni Sawicki 1c18fb9b81
Merge pull request #99 from mahiuchun/master
Introduce "fastgif" image type
2022-11-29 04:58:01 -08:00
Antoni Sawicki 363fbcd225 remove 32bit windows build 2022-11-24 02:11:05 -08:00
Hill Ma bf7e7bfb2c improve fastgif 2022-11-10 20:39:41 -08:00
Hill Ma fec812bc32 Add "fastgif" image type that provides much faster (but lower quality) GIF quantization.
The default image type is changed to PNG as it is the most "trouble free" choice.
2022-11-09 21:32:46 -08:00
Hill Ma a238a0ea6f gif: faster median cut quantization 2022-11-09 20:48:18 -08:00
Antoni Sawicki 444b7b31d7 update copyrights 2022-11-06 01:41:40 -08:00
Antoni Sawicki 76e72d5368 bump version 2022-11-06 01:39:03 -08:00
Antoni Sawicki c4d9833707 add todo 2022-11-06 01:38:31 -08:00
Antoni Sawicki 9cd286add8 if/else to switch 2022-11-06 01:20:18 -08:00
Antoni Sawicki fdfbe80024 more fortunate name static->builtin 2022-11-06 01:12:26 -08:00
Antoni Sawicki d602124ed6 replace out/req with more common w/r 2022-11-06 01:11:49 -08:00
Antoni Sawicki 6e5e829b02 rename w to rq so that w can be used for http writer 2022-11-06 01:07:21 -08:00
Antoni Sawicki f978d91ba9 make wrp request have methods for readability 2022-11-06 01:04:00 -08:00
Antoni Sawicki 9b15feacb2 fix chromedp scrollbar capture 2022-11-06 01:47:59 -07:00
Antoni Sawicki 08a89c6097 update dependencies 2022-11-06 00:51:44 -07:00
Antoni Sawicki fa3bd3f8fb print time took to encode gif 2022-11-06 00:27:10 -07:00
Antoni Sawicki 55f4e45b4c reverse number of colors dropdown 2022-11-05 23:36:25 -07:00
Antoni Sawicki 9e77aa7261
Merge pull request #97 from DrJosh9000/master
Replace statik with embed
2022-03-16 20:52:28 -07:00
Josh Deprez 8a9870d8e2 Remove unneeded go:generate directive 2022-03-17 02:34:22 +00:00
Josh Deprez 7c33bc67dc Replace statik with embed 2022-03-17 02:27:34 +00:00
7 changed files with 496 additions and 343 deletions

View File

@ -1,5 +1,6 @@
FROM chromedp/headless-shell
ADD wrp /wrp
ARG TARGETARCH
ADD wrp-${TARGETARCH}-linux /wrp
ENTRYPOINT ["/wrp"]
ENV PATH="/headless-shell:${PATH}"
LABEL maintainer="as@tenoware.com"

22
Makefile Normal file → Executable file
View File

@ -1,30 +1,22 @@
all: wrp
wrp: wrp.go statik
wrp: wrp.go
go build wrp.go
cross: statik
cross:
GOOS=linux GOARCH=amd64 go build -a -o wrp-amd64-linux wrp.go
GOOS=freebsd GOARCH=amd64 go build -a -o wrp-amd64-freebsd wrp.go
GOOS=openbsd GOARCH=amd64 go build -a -o wrp-amd64-openbsd wrp.go
GOOS=darwin GOARCH=amd64 go build -a -o wrp-amd64-macos wrp.go
GOOS=darwin GOARCH=arm64 go build -a -o wrp-arm64-macos wrp.go
GOOS=windows GOARCH=amd64 go build -a -o wrp-amd64-windows.exe wrp.go
GOOS=windows GOARCH=386 go build -a -o wrp-386-windows.exe wrp.go
GOOS=linux GOARCH=arm go build -a -o wrp-arm-linux wrp.go
GOOS=linux GOARCH=arm64 go build -a -o wrp-arm64-linux wrp.go
statik: wrp.html
go generate
docker: wrp
docker build -t tenox7/wrp:latest .
dockerhub:
docker push tenox7/wrp:latest
gcrio:
docker tag tenox7/wrp:latest gcr.io/tenox7/wrp
docker push gcr.io/tenox7/wrp
GOOS=linux GOARCH=amd64 go build -a -o wrp-amd64-linux wrp.go
GOOS=linux GOARCH=arm64 go build -a -o wrp-arm64-linux wrp.go
docker buildx build --platform linux/amd64,linux/arm64 -t tenox7/wrp:latest --push .
clean:
rm -rf wrp-* wrp statik
rm -rf wrp-* wrp

138
README.md
View File

@ -4,62 +4,21 @@ A browser-in-browser "proxy" server that allows to use historical / vintage web
![Internet Explorer 1.5 doing Gmail](wrp.png)
## Usage
## Usage Instructions
* [Download a WRP binary](https://github.com/tenox7/wrp/releases/) and run it on a machine that will become your WRP gateway/server.
This machine should be pretty modern, high spec and Google Chrome / Chromium Browser is required to be preinstalled.
* Point your legacy browser to `http://address:port` of WRP server. Do not set or use it as a "proxy server".
* Type a search string or a http/https URL and click **Go**.
* [Download a WRP binary](https://github.com/tenox7/wrp/releases/) and run it on a machine that will become your WRP gateway/server. This should be modern hardware, OS and Google Chrome / Chromium Browser is required to be preinstalled. Do not try to run WRP on an old machine like Windows XP or 98.
* Make sure you have disabled firewall or open port WRP is listening on (by default 8080).
* Point your legacy browser to `http://address:port` of the WRP server. Do not set or use it as a "proxy server".
* Type a search string or a full http/https URL and click **Go**.
* Adjust your screen **W**idth/**H**eight/**S**cale/**C**olors to fit in your old browser.
* Scroll web page by clicking on the in-image scroll bar.
* WRP also allows **a single tall image without the vertical scrollbar** and use client scrolling. To enable this, simply height **H** to `0` . However this should not be used with old and low spec clients. Such tall images will be very large, take a lot of memory and long time to process, especially for GIFs.
* Do not use client browser history-back, instead use **Bk** button in the app.
* To send keystrokes, fill **K** input box and press **Go**. There also are buttons for backspace, enter and arrow keys.
* You can set height **H** to `0` to render pages in to **a single tall image without the vertical scrollbar** and use client scrolling. However this should not be used with old and low spec clients. Such tall images will be very large and take long time to process, especially for GIFs.
* Prefer PNG over GIF if your browser supports it. PNG is much faster, whereas GIF requires a lot of additional processing on both client and server.
* You can re-capture page screenshot without reloading by using **St** (Stop).
* You can re-capture page screenshot without reloading by using **St** (Stop). This is useful if page didn't render fully before screenshot is taken.
* You can also reload and re-capture current page with **Re** (Reload).
## Customization
Since 4.5.1 WRP supports customizing it's own UI using HTML Template file. Download [wrp.html](wrp.html) place in the same directory with wrp binary customize it to your liking.
## Docker
```shell
$ docker run -d -p 80:8080 tenox7/wrp
```
## Google Cloud Run
```shell
$ gcloud run deploy --platform managed --image=gcr.io/tenox7/wrp:latest --memory=2Gi --args='-t=png','-g=1280x0x256'
```
Or from [Gcloud Console](https://console.cloud.google.com/run). Use `gcr.io/tenox7/wrp:latest` as container image URL.
Note that unfortunately GCR forces https. Your browser support of encryption protocols and certification authorities will vary.
## Azure Container Instances
```shell
$ az container create --resource-group wrp --name wrp --image gcr.io/tenox7/wrp:latest --cpu 1 --memory 2 --ports 80 --protocol tcp --os-type Linux --ip-address Public --command-line '/wrp -l :80 -t png -g 1280x0x256'
```
Or from the [Azure Console](https://portal.azure.com/#create/Microsoft.ContainerInstances). Use `gcr.io/tenox7/wrp:latest` or `tenox7/wrp:latest` for image name.
Fortunately ACI allows port 80 without encryption.
## Flags
```
-l listen address:port (default :8080)
-t image type gif or png (default gif)
-g image geometry, WxHxC, height can be 0 for unlimited (default 1152x600x256)
-h headless mode, hide browser window on the server (default true)
-d chromedp debug logging (default false)
-n do not free maps and gif images after use (default false)
```
* To send keystrokes, fill **K** input box and press **Go**. There also are buttons for backspace, enter and arrow keys.
* Prefer PNG over GIF if your browser supports it. PNG is much faster, whereas GIF requires a lot of additional processing on both client and server to encode/decode. Jpeg encoding is also quite fast.
* GIF images are by default encoded with 216 colors, "web safe" palette. This uses an ultra fast but not very accurate color mapping algorithm. If you want better color representation switch to 256 color mode.
## UI explanation
@ -83,7 +42,7 @@ used with PNG and lots of memory on a client side.
**Z** is zoom or scale
**C** is colors, for GIF images only (unused in PNG)
**C** is colors, for GIF images only (unused in PNG, JPG)
**K** is keystroke input, you can type some letters in it and when you click Go it will be typed in the remote browser.
@ -93,6 +52,50 @@ used with PNG and lots of memory on a client side.
**< ^ v >** are arrow keys, typically for navigating a map, buggy.
### UI Customization
WRP supports customizing it's own UI using HTML Template file. Download [wrp.html](wrp.html) place in the same directory with wrp binary customize it to your liking.
## Docker
```shell
$ docker run -d -p 8080:8080 tenox7/wrp:latest
```
## Google Cloud Run
```shell
$ gcloud run deploy --platform managed --image=tenox7/wrp:latest --memory=2Gi --args='-t=png','-g=1280x0x256'
```
Note that unfortunately GCR forces https. Your browser support of encryption protocols and certification authorities will vary.
## Azure Container Instances
```shell
$ az container create --resource-group wrp --name wrp --image tenox7/wrp:latest --cpu 1 --memory 2 --ports 80 --protocol tcp --os-type Linux --ip-address Public --command-line '/wrp -l :80 -t png -g 1280x0x256'
```
Or from the [Azure Console](https://portal.azure.com/#create/Microsoft.ContainerInstances). Use `tenox7/wrp:latest` for image name.
Fortunately ACI allows port 80 without encryption.
## Flags
```text
-l listen address:port (default :8080)
-t image type gif, png or jpg (default gif)
-g image geometry, WxHxC, height can be 0 for unlimited (default 1152x600x216)
C (number of colors) is only used for GIF
-q Jpeg image quality, default 80%
-h headless mode, hide browser window on the server (default true)
-d chromedp debug logging (default false)
-n do not free maps and images after use (default false)
-ui html template file (default "wrp.html")
-ua user agent, override the default "headless" agent
-s delay/sleep after page is rendered before screenshot is taken (default 2s)
```
## Minimal Requirements
* Server/Gateway requires modern hardware and operating system that is supported by [Go language](https://github.com/golang/go/wiki/MinimumRequirements) and Chrome/Chromium Browser, which must be installed.
@ -102,12 +105,22 @@ used with PNG and lots of memory on a client side.
### I can't get it to run
This program does not have a GUI and is run from the command line. You may need to enable executable bit on Unix systems, for example:
This program does not have a GUI and is run from the command line. After downloading, you may need to enable executable bit on Unix systems, for example:
```bash
```shell
$ cd ~/Downloads
$ chmod +x wrp-amd64-macos
$ ./wrp-amd64-macos -t png
$ ./wrp-amd64-macos
```
### Websites are blocking headless browsers
This is a well known issue. WRP has some provisions to work around it, but it's a cat and mouse game. The first and
foremost recommendation is to change `User Agent`, so that it doesn't say "headless". Add `-ua="my agent"` to override the default one.
Obtain your regular desktop browser user agent and specify it as the flag. For example
```shell
$ wrp -ua="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
```
## History
@ -116,9 +129,11 @@ $ ./wrp-amd64-macos -t png
* Version 2.0 became a stand alone http-proxy server, supporting both Linux and MacOS, [another post](https://virtuallyfun.com/wordpress/2014/03/11/web-rendering-proxy-update//).
* In 2016 thanks to EFF/Certbot the whole internet migrated to HTTPS/SSL/TLS and WRP largely stopped working. Python code became unmaintainable and there was no easy way to make it work on Windows, even under WSL.
* Version 3.0 (2019) has been rewritten in [Go](https://golang.org/) using [Chromedp](https://github.com/chromedp) as browser-in-browser instead of http-proxy. The initial version was [less than 100 lines of code](https://gist.github.com/tenox7/b0f03c039b0a8b67f6c1bf47e2dd0df0).
* Version 4.0 has been completely refactored to use mouse clicks via imagemap instead parsing a href nodes.
* Version 4.1 added sending keystrokes in to input boxes. You can now login to Gmail. Also now runs as a Docker container and on Cloud Run/Azure Containers.
* Version 4.0 has been completely refactored to use mouse clicks via imagemap instead parsing a href nodes.
* Version 4.1 added sending keystrokes in to input boxes. You can now login to Gmail. Also now runs as a Docker container and on Cloud Run/Azure Containers.
* Version 4.5 introduces rendering whole pages in to a single tall image with client scrolling.
* Version 4.6 adds blazing fast gif encoding by [Hill Ma](https://github.com/mahiuchun).
* Version 4.6.3 adds arm64 / aarch64 Docker container support - you can run it on Raspberry PI!
## Credits
@ -126,10 +141,13 @@ $ ./wrp-amd64-macos -t png
* 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
* Thanks to [claunia](https://github.com/claunia/) for help with the Python/Webkit version in the past
* Thanks to [Hill Ma](https://github.com/mahiuchun) for ultra fast gif encoding algorithm
* Historical Python/Webkit versions and prior art can be seen in [wrp-old](https://github.com/tenox7/wrp-old) repo
## Legal Stuff
License: Apache 2.0
Copyright (c) 2013-2018 Antoni Sawicki
Copyright (c) 2019-2020 Google LLC
```text
License: Apache 2.0
Copyright (c) 2013-2024 Antoni Sawicki
Copyright (c) 2019-2024 Google LLC
```

20
go.mod
View File

@ -1,12 +1,20 @@
module github.com/tenox7/wrp
go 1.16
go 1.21.5
require (
github.com/MaxHalford/halfgone v0.0.0-20171017091812-482157b86ccb
github.com/chromedp/cdproto v0.0.0-20210305224431-50b9f457e822
github.com/chromedp/chromedp v0.6.8
github.com/ericpauley/go-quantize v0.0.0-20200331213906-ae555eb2afa4
github.com/rakyll/statik v0.1.7
golang.org/x/sys v0.0.0-20210305230114-8fe3ee5dd75b // indirect
github.com/chromedp/cdproto v0.0.0-20240519224452-66462be74baa
github.com/chromedp/chromedp v0.9.5
github.com/soniakeys/quant v1.0.0
)
require (
github.com/chromedp/sysutil v1.0.0 // indirect
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/gobwas/ws v1.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
golang.org/x/sys v0.20.0 // indirect
)

41
go.sum
View File

@ -1,25 +1,40 @@
github.com/MaxHalford/halfgone v0.0.0-20171017091812-482157b86ccb h1:YQ+d0g0P0F/06oDoeEgDHeZCIrnKgLxXcqYOpe8sTuU=
github.com/MaxHalford/halfgone v0.0.0-20171017091812-482157b86ccb/go.mod h1:J86XzS1wgzJPjpQmpriJ+SetP17JSQUd9l+HWQK86jA=
github.com/chromedp/cdproto v0.0.0-20210305224431-50b9f457e822 h1:iuUUtGk8vteAyrzYTdvn4gzVAw5Q7WEoDRinBQEiIe4=
github.com/chromedp/cdproto v0.0.0-20210305224431-50b9f457e822/go.mod h1:At5TxYYdxkbQL0TSefRjhLE3Q0lgvqKKMSFUglJ7i1U=
github.com/chromedp/chromedp v0.6.8 h1:hsMFAylK8PxqmpxzoxHkSJAV5qnixG0UJcNL70IRntg=
github.com/chromedp/chromedp v0.6.8/go.mod h1:4NiJ4rKpkhU1Eor5stea0NvibADXd8drgtzj6+3eXZ4=
github.com/chromedp/cdproto v0.0.0-20231011050154-1d073bb38998/go.mod h1:GKljq0VrfU4D5yc+2qA6OVr8pmO/MBbPEWqWQ/oqGEs=
github.com/chromedp/cdproto v0.0.0-20231205062650-00455a960d61 h1:XD280QPATe9jaz20dylKe3vBsNcH1w3mkssGY0lidn8=
github.com/chromedp/cdproto v0.0.0-20231205062650-00455a960d61/go.mod h1:GKljq0VrfU4D5yc+2qA6OVr8pmO/MBbPEWqWQ/oqGEs=
github.com/chromedp/cdproto v0.0.0-20240202021202-6d0b6a386732/go.mod h1:GKljq0VrfU4D5yc+2qA6OVr8pmO/MBbPEWqWQ/oqGEs=
github.com/chromedp/cdproto v0.0.0-20240519224452-66462be74baa h1:T3Ho4BWIkoEoMPCj90W2HIPF/k56qk4JWzTs6JUBxVw=
github.com/chromedp/cdproto v0.0.0-20240519224452-66462be74baa/go.mod h1:GKljq0VrfU4D5yc+2qA6OVr8pmO/MBbPEWqWQ/oqGEs=
github.com/chromedp/chromedp v0.9.3 h1:Wq58e0dZOdHsxaj9Owmfcf+ibtpYN1N0FWVbaxa/esg=
github.com/chromedp/chromedp v0.9.3/go.mod h1:NipeUkUcuzIdFbBP8eNNvl9upcceOfWzoJn6cRe4ksA=
github.com/chromedp/chromedp v0.9.5 h1:viASzruPJOiThk7c5bueOUY91jGLJVximoEMGoH93rg=
github.com/chromedp/chromedp v0.9.5/go.mod h1:D4I2qONslauw/C7INoCir1BJkSwBYMyZgx8X276z3+Y=
github.com/chromedp/sysutil v1.0.0 h1:+ZxhTpfpZlmchB58ih/LBHX52ky7w2VhQVKQMucy3Ic=
github.com/chromedp/sysutil v1.0.0/go.mod h1:kgWmDdq8fTzXYcKIBqIYvRRTnYb9aNS9moAV0xufSww=
github.com/ericpauley/go-quantize v0.0.0-20200331213906-ae555eb2afa4 h1:BBade+JlV/f7JstZ4pitd4tHhpN+w+6I+LyOS7B4fyU=
github.com/ericpauley/go-quantize v0.0.0-20200331213906-ae555eb2afa4/go.mod h1:H7chHJglrhPPzetLdzBleF8d22WYOv7UM/lEKYiwlKM=
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
github.com/gobwas/ws v1.0.4 h1:5eXU1CZhpQdq5kXbKb+sECH5Ia5KiO6CYzIzdlVx6Bs=
github.com/gobwas/ws v1.0.4/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
github.com/gobwas/ws v1.3.0/go.mod h1:hRKAFb8wOxFROYNsT1bqfWnhX+b5MFeJM9r2ZSwg/KY=
github.com/gobwas/ws v1.3.1 h1:Qi34dfLMWJbiKaNbDVzM9x27nZBjmkaW6i4+Ku+pGVU=
github.com/gobwas/ws v1.3.1/go.mod h1:hRKAFb8wOxFROYNsT1bqfWnhX+b5MFeJM9r2ZSwg/KY=
github.com/gobwas/ws v1.3.2/go.mod h1:hRKAFb8wOxFROYNsT1bqfWnhX+b5MFeJM9r2ZSwg/KY=
github.com/gobwas/ws v1.4.0 h1:CTaoG1tojrh4ucGPcoJFiAQUAsEWekEWvLy7GsVNqGs=
github.com/gobwas/ws v1.4.0/go.mod h1:G3gNqMNtPppf5XUz7O4shetPpcZ1VJ7zt18dlUeakrc=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80 h1:6Yzfa6GP0rIo/kULo2bwGEkFvCePZ3qHDDTC3/J9Swo=
github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/rakyll/statik v0.1.7 h1:OF3QCZUuyPxuGEP7B4ypUa7sB/iHtqOTDYZXGM8KOdQ=
github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc=
golang.org/x/sys v0.0.0-20210305215415-5cdee2b1b5a0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210305230114-8fe3ee5dd75b h1:ggRgirZABFolTmi3sn6Ivd9SipZwLedQ5wR0aAKnFxU=
golang.org/x/sys v0.0.0-20210305230114-8fe3ee5dd75b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde h1:x0TT0RDC7UhAVbbWWBzr41ElhJx5tXPWkIHA2HWPRuw=
github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde/go.mod h1:nZgzbfBr3hhjoZnS66nKrHmduYNpc34ny7RK4z5/HM0=
github.com/soniakeys/quant v1.0.0 h1:N1um9ktjbkZVcywBVAAYpZYSHxEfJGzshHCxx/DaI0Y=
github.com/soniakeys/quant v1.0.0/go.mod h1:HI1k023QuVbD4H8i9YdfZP2munIHU4QpjsImz6Y6zds=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

598
wrp.go
View File

@ -1,31 +1,35 @@
//
// WRP - Web Rendering Proxy
//
// Copyright (c) 2013-2018 Antoni Sawicki
// Copyright (c) 2019-2021 Google LLC
// Copyright (c) 2013-2024 Antoni Sawicki
// Copyright (c) 2019-2024 Google LLC
//
//go:generate statik -f -src=. -include=wrp.html
package main
import (
"bytes"
"context"
"embed"
"flag"
"fmt"
"html/template"
"image"
"image/color/palette"
"image/gif"
"image/jpeg"
"image/png"
"io"
"io/ioutil"
"log"
"math"
"math/rand"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"runtime"
"strconv"
"strings"
"syscall"
@ -35,25 +39,35 @@ import (
"github.com/chromedp/cdproto/css"
"github.com/chromedp/cdproto/emulation"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/cdproto/input"
"github.com/chromedp/chromedp"
"github.com/ericpauley/go-quantize/quantize"
"github.com/rakyll/statik/fs"
_ "github.com/tenox7/wrp/statik"
"github.com/soniakeys/quant/median"
)
const version = "4.6.3"
var (
version = "4.5.2"
srv http.Server
ctx context.Context
cancel context.CancelFunc
img = make(map[string]bytes.Buffer)
ismap = make(map[string]wrpReq)
noDel bool
defType string
defGeom geom
htmlTmpl *template.Template
addr = flag.String("l", ":8080", "Listen address:port, default :8080")
headless = flag.Bool("h", true, "Headless mode / hide browser window (default true)")
noDel = flag.Bool("n", false, "Do not free maps and images after use")
defType = flag.String("t", "gif", "Image type: png|gif|jpg")
jpgQual = flag.Int("q", 80, "Jpeg image quality, default 80%")
fgeom = flag.String("g", "1152x600x216", "Geometry: width x height x colors, height can be 0 for unlimited")
htmFnam = flag.String("ui", "wrp.html", "HTML template file for the UI")
delay = flag.Duration("s", 2*time.Second, "Delay/sleep after page is rendered and before screenshot is taken")
userAgent = flag.String("ua", "", "override chrome user agent")
srv http.Server
actx, ctx context.Context
acncl, cncl context.CancelFunc
img = make(map[string]bytes.Buffer)
ismap = make(map[string]wrpReq)
defGeom geom
htmlTmpl *template.Template
)
//go:embed *.html
var fs embed.FS
type geom struct {
w int64
h int64
@ -101,55 +115,59 @@ type wrpReq struct {
keys string // keys to send
buttons string // Fn buttons
imgType string // imgtype
out http.ResponseWriter
req *http.Request
w http.ResponseWriter
r *http.Request
}
// 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))
func (rq *wrpReq) parseForm() {
rq.r.ParseForm()
rq.url = rq.r.FormValue("url")
if len(rq.url) > 1 && !strings.HasPrefix(rq.url, "http") {
rq.url = fmt.Sprintf("http://www.google.com/search?q=%s", url.QueryEscape(rq.url))
}
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 {
w.width = defGeom.w
w.height = defGeom.h
rq.width, _ = strconv.ParseInt(rq.r.FormValue("w"), 10, 64)
rq.height, _ = strconv.ParseInt(rq.r.FormValue("h"), 10, 64)
if rq.width < 10 && rq.height < 10 {
rq.width = defGeom.w
rq.height = defGeom.h
}
w.zoom, _ = strconv.ParseFloat(w.req.FormValue("z"), 64)
if w.zoom < 0.1 {
w.zoom = 1.0
rq.zoom, _ = strconv.ParseFloat(rq.r.FormValue("z"), 64)
if rq.zoom < 0.1 {
rq.zoom = 1.0
}
w.colors, _ = strconv.ParseInt(w.req.FormValue("c"), 10, 64)
if w.colors < 2 || w.colors > 256 {
w.colors = defGeom.c
rq.colors, _ = strconv.ParseInt(rq.r.FormValue("c"), 10, 64)
if rq.colors < 2 || rq.colors > 256 {
rq.colors = defGeom.c
}
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" {
w.imgType = defType
rq.keys = rq.r.FormValue("k")
rq.buttons = rq.r.FormValue("Fn")
rq.imgType = rq.r.FormValue("t")
switch rq.imgType {
case "png":
case "gif":
case "jpg":
default:
rq.imgType = *defType
}
log.Printf("%s WrpReq from UI Form: %+v\n", w.req.RemoteAddr, w)
log.Printf("%s WrpReq from UI Form: %+v\n", rq.r.RemoteAddr, rq)
}
// 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")
func (rq *wrpReq) printHTML(p printParams) {
rq.w.Header().Set("Cache-Control", "max-age=0")
rq.w.Header().Set("Expires", "-1")
rq.w.Header().Set("Pragma", "no-cache")
rq.w.Header().Set("Content-Type", "text/html")
data := uiData{
Version: version,
URL: w.url,
URL: rq.url,
BgColor: p.bgColor,
Width: w.width,
Height: w.height,
NColors: w.colors,
Zoom: w.zoom,
ImgType: w.imgType,
Width: rq.width,
Height: rq.height,
NColors: rq.colors,
Zoom: rq.zoom,
ImgType: rq.imgType,
ImgSize: p.imgSize,
ImgWidth: p.imgWidth,
ImgHeight: p.imgHeight,
@ -157,23 +175,23 @@ func printHTML(w wrpReq, p printParams) {
MapURL: p.mapURL,
PageHeight: p.pageHeight,
}
err := htmlTmpl.Execute(w.out, data)
err := htmlTmpl.Execute(rq.w, data)
if err != nil {
log.Fatal(err)
}
}
// Determine what action to take
func action(w wrpReq) chromedp.Action {
func (rq *wrpReq) action() chromedp.Action {
// Mouse Click
if w.mouseX > 0 && w.mouseY > 0 {
log.Printf("%s Mouse Click %d,%d\n", w.req.RemoteAddr, w.mouseX, w.mouseY)
return chromedp.MouseClickXY(float64(w.mouseX)/float64(w.zoom), float64(w.mouseY)/float64(w.zoom))
if rq.mouseX > 0 && rq.mouseY > 0 {
log.Printf("%s Mouse Click %d,%d\n", rq.r.RemoteAddr, rq.mouseX, rq.mouseY)
return chromedp.MouseClickXY(float64(rq.mouseX)/float64(rq.zoom), float64(rq.mouseY)/float64(rq.zoom))
}
// Buttons
if len(w.buttons) > 0 {
log.Printf("%s Button %v\n", w.req.RemoteAddr, w.buttons)
switch w.buttons {
if len(rq.buttons) > 0 {
log.Printf("%s Button %v\n", rq.r.RemoteAddr, rq.buttons)
switch rq.buttons {
case "Bk":
return chromedp.NavigateBack()
case "St":
@ -192,46 +210,113 @@ func action(w wrpReq) chromedp.Action {
return chromedp.KeyEvent("\u0301")
case ">":
return chromedp.KeyEvent("\u0303")
}
case "Up":
return chromedp.KeyEvent("\u0308")
case "Dn":
return chromedp.KeyEvent("\u0307")
case "All": // Select all
return chromedp.KeyEvent("a", chromedp.KeyModifiers(input.ModifierCtrl))
}
}
// Keys
if len(w.keys) > 0 {
log.Printf("%s Sending Keys: %#v\n", w.req.RemoteAddr, w.keys)
return chromedp.KeyEvent(w.keys)
if len(rq.keys) > 0 {
log.Printf("%s Sending Keys: %#v\n", rq.r.RemoteAddr, rq.keys)
return chromedp.KeyEvent(rq.keys)
}
// Navigate to URL
log.Printf("%s Processing Capture Request for %s\n", w.req.RemoteAddr, w.url)
return chromedp.Navigate(w.url)
log.Printf("%s Processing Capture Request for %s\n", rq.r.RemoteAddr, rq.url)
return chromedp.Navigate(rq.url)
}
// Process Keyboard and Mouse events or Navigate to the desired URL.
func navigate(w wrpReq) {
err := chromedp.Run(ctx, action(w))
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())
return
}
log.Printf("%s %s", w.req.RemoteAddr, err)
fmt.Fprintf(w.out, "<BR>%s<BR>", err)
// Navigate to the desired URL.
func (rq *wrpReq) navigate() {
ctxErr(chromedp.Run(ctx, rq.action()), rq.w)
}
// Handle context errors
func ctxErr(err error, w io.Writer) {
// TODO: callers should have retry logic, perhaps create another function
// that takes ...chromedp.Action and retries with give up
if err == nil {
return
}
log.Printf("Context error: %s", err)
fmt.Fprintf(w, "Context error: %s<BR>\n", err)
if err.Error() != "context canceled" {
return
}
ctx, cncl = chromedp.NewContext(actx)
log.Printf("Created new context, try again")
fmt.Fprintln(w, "Created new context, try again")
}
// https://github.com/chromedp/chromedp/issues/979
func chromedpCaptureScreenshot(res *[]byte, h int64) chromedp.Action {
if res == nil {
panic("res cannot be nil")
}
if h == 0 {
return chromedp.CaptureScreenshot(res)
}
return chromedp.ActionFunc(func(ctx context.Context) error {
var err error
*res, err = page.CaptureScreenshot().Do(ctx)
return err
})
}
func gifPalette(i image.Image, n int64) image.Image {
switch n {
case 2:
i = halfgone.FloydSteinbergDitherer{}.Apply(halfgone.ImageToGray(i))
case 216:
var FastGifLut = [256]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}
r := i.Bounds()
// NOTE: the color index computation below works only for palette.WebSafe!
p := image.NewPaletted(r, palette.WebSafe)
if i64, ok := i.(image.RGBA64Image); ok {
for y := r.Min.Y; y < r.Max.Y; y++ {
for x := r.Min.X; x < r.Max.X; x++ {
c := i64.RGBA64At(x, y)
r6 := FastGifLut[c.R>>8]
g6 := FastGifLut[c.G>>8]
b6 := FastGifLut[c.B>>8]
p.SetColorIndex(x, y, uint8(36*r6+6*g6+b6))
}
}
} else {
for y := r.Min.Y; y < r.Max.Y; y++ {
for x := r.Min.X; x < r.Max.X; x++ {
c := i.At(x, y)
r, g, b, _ := c.RGBA()
r6 := FastGifLut[r&0xff]
g6 := FastGifLut[g&0xff]
b6 := FastGifLut[b&0xff]
p.SetColorIndex(x, y, uint8(36*r6+6*g6+b6))
}
}
}
i = p
default:
q := median.Quantizer(n)
i = q.Paletted(i)
}
return i
}
// Capture currently rendered web page to an image and fake ISMAP
func capture(w wrpReq) {
var err error
func (rq *wrpReq) capture() {
var styles []*css.ComputedStyleProperty
var r, g, b int
var h int64
var pngcap []byte
var pngCap []byte
chromedp.Run(ctx,
emulation.SetDeviceMetricsOverride(int64(float64(w.width)/w.zoom), 10, w.zoom, false),
chromedp.Location(&w.url),
emulation.SetDeviceMetricsOverride(int64(float64(rq.width)/rq.zoom), 10, rq.zoom, false),
chromedp.Location(&rq.url),
chromedp.ComputedStyle("body", &styles, chromedp.ByQuery),
chromedp.ActionFunc(func(ctx context.Context) error {
_, _, s, err := page.GetLayoutMetrics().Do(ctx)
_, _, _, _, _, s, err := page.GetLayoutMetrics().Do(ctx)
if err == nil {
h = int64(math.Ceil(s.Height))
}
@ -243,160 +328,170 @@ func capture(w wrpReq) {
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)
height := int64(float64(w.height) / w.zoom)
if w.height == 0 && h > 0 {
log.Printf("%s Landed on: %s, Height: %v\n", rq.r.RemoteAddr, rq.url, h)
height := int64(float64(rq.height) / rq.zoom)
if rq.height == 0 && h > 0 {
height = h + 30
}
chromedp.Run(ctx, emulation.SetDeviceMetricsOverride(int64(float64(w.width)/w.zoom), height, w.zoom, false))
// Capture screenshot...
err = chromedp.Run(ctx,
chromedp.Sleep(time.Second*2),
chromedp.CaptureScreenshot(&pngcap),
chromedp.Run(
ctx, emulation.SetDeviceMetricsOverride(int64(float64(rq.width)/rq.zoom), height, rq.zoom, false),
chromedp.Sleep(*delay), // TODO(tenox): find a better way to determine if page is rendered
)
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())
return
}
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)
return
}
// Capture screenshot...
ctxErr(chromedp.Run(ctx, chromedpCaptureScreenshot(&pngCap, rq.height)), rq.w)
seq := rand.Intn(9999)
imgpath := fmt.Sprintf("/img/%04d.%s", seq, w.imgType)
mappath := fmt.Sprintf("/map/%04d.map", seq)
ismap[mappath] = w
var ssize string
var iw, ih int
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
}
if w.colors == 2 {
gray := halfgone.ImageToGray(i)
i = halfgone.FloydSteinbergDitherer{}.Apply(gray)
}
var gifbuf bytes.Buffer
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
ssize = fmt.Sprintf("%.0f KB", float32(len(gifbuf.Bytes()))/1024.0)
iw = i.Bounds().Max.X
ih = i.Bounds().Max.Y
log.Printf("%s Encoded GIF image: %s, Size: %s, Colors: %d, %dx%d\n", w.req.RemoteAddr, imgpath, ssize, w.colors, iw, ih)
imgPath := fmt.Sprintf("/img/%04d.%s", seq, rq.imgType)
mapPath := fmt.Sprintf("/map/%04d.map", seq)
ismap[mapPath] = *rq
var sSize string
var iW, iH int
switch rq.imgType {
case "png":
pngbuf := bytes.NewBuffer(pngcap)
img[imgpath] = *pngbuf
cfg, _, _ := image.DecodeConfig(pngbuf)
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)
pngBuf := bytes.NewBuffer(pngCap)
img[imgPath] = *pngBuf
cfg, _, _ := image.DecodeConfig(pngBuf)
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, Res: %dx%d\n", rq.r.RemoteAddr, imgPath, sSize, iW, iH)
case "gif":
i, err := png.Decode(bytes.NewReader(pngCap))
if err != nil {
log.Printf("%s Failed to decode PNG screenshot: %s\n", rq.r.RemoteAddr, err)
fmt.Fprintf(rq.w, "<BR>Unable to decode page PNG screenshot:<BR>%s<BR>\n", err)
return
}
st := time.Now()
var gifBuf bytes.Buffer
err = gif.Encode(&gifBuf, gifPalette(i, rq.colors), &gif.Options{})
if err != nil {
log.Printf("%s Failed to encode GIF: %s\n", rq.r.RemoteAddr, err)
fmt.Fprintf(rq.w, "<BR>Unable to encode GIF:<BR>%s<BR>\n", err)
return
}
img[imgPath] = gifBuf
sSize = fmt.Sprintf("%.0f KB", float32(len(gifBuf.Bytes()))/1024.0)
iW = i.Bounds().Max.X
iH = i.Bounds().Max.Y
log.Printf("%s Encoded GIF image: %s, Size: %s, Colors: %d, Res: %dx%d, Time: %vms\n", rq.r.RemoteAddr, imgPath, sSize, rq.colors, iW, iH, time.Since(st).Milliseconds())
case "jpg":
i, err := png.Decode(bytes.NewReader(pngCap))
if err != nil {
log.Printf("%s Failed to decode PNG screenshot: %s\n", rq.r.RemoteAddr, err)
fmt.Fprintf(rq.w, "<BR>Unable to decode page PNG screenshot:<BR>%s<BR>\n", err)
return
}
st := time.Now()
var jpgBuf bytes.Buffer
err = jpeg.Encode(&jpgBuf, i, &jpeg.Options{Quality: *jpgQual})
if err != nil {
log.Printf("%s Failed to encode JPG: %s\n", rq.r.RemoteAddr, err)
fmt.Fprintf(rq.w, "<BR>Unable to encode JPG:<BR>%s<BR>\n", err)
return
}
img[imgPath] = jpgBuf
sSize = fmt.Sprintf("%.0f KB", float32(len(jpgBuf.Bytes()))/1024.0)
iW = i.Bounds().Max.X
iH = i.Bounds().Max.Y
log.Printf("%s Encoded JPG image: %s, Size: %s, Quality: %d, Res: %dx%d, Time: %vms\n", rq.r.RemoteAddr, imgPath, sSize, *jpgQual, iW, iH, time.Since(st).Milliseconds())
}
printHTML(w, printParams{
rq.printHTML(printParams{
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,
imgSize: sSize,
imgURL: imgPath,
mapURL: mapPath,
imgWidth: iW,
imgHeight: iH,
})
log.Printf("%s Done with capture for %s\n", w.req.RemoteAddr, w.url)
log.Printf("%s Done with capture for %s\n", rq.r.RemoteAddr, rq.url)
}
// 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"})
func pageServer(w http.ResponseWriter, r *http.Request) {
log.Printf("%s Page Request for %s [%+v]\n", r.RemoteAddr, r.URL.Path, r.URL.RawQuery)
rq := wrpReq{
r: r,
w: w,
}
rq.parseForm()
if len(rq.url) < 4 {
rq.printHTML(printParams{bgColor: "#FFFFFF"})
return
}
navigate(w)
capture(w)
rq.navigate() // TODO: if error from navigate do not capture
rq.capture()
}
// 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
func mapServer(w http.ResponseWriter, r *http.Request) {
log.Printf("%s ISMAP Request for %s [%+v]\n", r.RemoteAddr, r.URL.Path, r.URL.RawQuery)
rq, ok := ismap[r.URL.Path]
rq.r = r
rq.w = w
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)
fmt.Fprintf(w, "Unable to find map %s\n", r.URL.Path)
log.Printf("Unable to find map %s\n", r.URL.Path)
return
}
if !noDel {
defer delete(ismap, req.URL.Path)
if !*noDel {
defer delete(ismap, r.URL.Path)
}
n, err := fmt.Sscanf(req.URL.RawQuery, "%d,%d", &w.mouseX, &w.mouseY)
n, err := fmt.Sscanf(r.URL.RawQuery, "%d,%d", &rq.mouseX, &rq.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)
fmt.Fprintf(w, "n=%d, err=%s\n", n, err)
log.Printf("%s ISMAP n=%d, err=%s\n", r.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"})
log.Printf("%s WrpReq from ISMAP: %+v\n", r.RemoteAddr, rq)
if len(rq.url) < 4 {
rq.printHTML(printParams{bgColor: "#FFFFFF"})
return
}
navigate(w)
capture(w)
rq.navigate() // TODO: if error from navigate do not capture
rq.capture()
}
// 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)
func imgServer(w http.ResponseWriter, r *http.Request) {
log.Printf("%s IMG Request for %s\n", r.RemoteAddr, r.URL.Path)
imgBuf, ok := img[r.URL.Path]
if !ok || imgBuf.Bytes() == nil {
fmt.Fprintf(w, "Unable to find image %s\n", r.URL.Path)
log.Printf("%s Unable to find image %s\n", r.RemoteAddr, r.URL.Path)
return
}
if !noDel {
defer delete(img, req.URL.Path)
if !*noDel {
defer delete(img, r.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")
case strings.HasPrefix(r.URL.Path, ".gif"):
w.Header().Set("Content-Type", "image/gif")
case strings.HasPrefix(r.URL.Path, ".png"):
w.Header().Set("Content-Type", "image/png")
case strings.HasPrefix(r.URL.Path, ".jpg"):
w.Header().Set("Content-Type", "image/jpeg")
}
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()
w.Header().Set("Content-Length", strconv.Itoa(len(imgBuf.Bytes())))
w.Header().Set("Cache-Control", "max-age=0")
w.Header().Set("Expires", "-1")
w.Header().Set("Pragma", "no-cache")
w.Write(imgBuf.Bytes())
w.(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()
func haltServer(w http.ResponseWriter, r *http.Request) {
log.Printf("%s Shutdown Request for %s\n", r.RemoteAddr, r.URL.Path)
w.Header().Set("Cache-Control", "max-age=0")
w.Header().Set("Expires", "-1")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "Shutting down WRP...\n")
w.(http.Flusher).Flush()
time.Sleep(time.Second * 2)
cancel()
cncl()
acncl()
srv.Shutdown(context.Background())
os.Exit(1)
}
@ -406,67 +501,87 @@ func tmpl(t string) string {
var tmpl []byte
fh, err := os.Open(t)
if err != nil {
goto statik
goto builtin
}
defer fh.Close()
tmpl, err = ioutil.ReadAll(fh)
if err != nil {
goto statik
goto builtin
}
log.Printf("Got UI template from %v file\n", t)
log.Printf("Got HTML UI template from %v file, size %v \n", t, len(tmpl))
return string(tmpl)
statik:
sfs, err := fs.New()
if err != nil {
log.Fatal(err)
}
fhs, err := sfs.Open("/wrp.html")
builtin:
fhs, err := fs.Open("wrp.html")
if err != nil {
log.Fatal(err)
}
defer fhs.Close()
tmpl, err = ioutil.ReadAll(fhs)
if err != nil {
log.Fatal(err)
}
log.Printf("Got UI template from built-in\n")
log.Printf("Got HTML UI template from embed\n")
return string(tmpl)
}
// Main...
func main() {
var addr, fgeom, tHTML string
var headless bool
var debug bool
var err error
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")
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")
flag.StringVar(&tHTML, "ui", "wrp.html", "HTML template file for the UI")
flag.Parse()
if len(os.Getenv("PORT")) > 0 {
addr = ":" + os.Getenv(("PORT"))
// Print my own IP addresses
func printIPs(b string) {
ap := strings.Split(b, ":")
if len(ap) < 1 {
log.Fatal("Wrong format of ipaddress:port")
}
n, err := fmt.Sscanf(fgeom, "%dx%dx%d", &defGeom.w, &defGeom.h, &defGeom.c)
log.Printf("Listen address: %v", b)
if ap[0] != "" && ap[0] != "0.0.0.0" {
return
}
a, err := net.InterfaceAddrs()
if err != nil {
log.Print("Unable to get interfaces: ", err)
return
}
var m string
for _, i := range a {
n, ok := i.(*net.IPNet)
if !ok || n.IP.IsLoopback() || strings.Contains(n.IP.String(), ":") {
continue
}
m = m + n.IP.String() + " "
}
log.Print("My IP addresses: ", m)
}
// Main
func main() {
var err error
log.SetFlags(log.LstdFlags | log.Lshortfile)
flag.Parse()
log.Printf("Web Rendering Proxy Version %s (%v)\n", version, runtime.GOARCH)
log.Printf("Args: %q", os.Args)
if len(os.Getenv("PORT")) > 0 {
*addr = ":" + os.Getenv(("PORT"))
}
printIPs(*addr)
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),
chromedp.Flag("headless", *headless),
chromedp.Flag("hide-scrollbars", false),
chromedp.Flag("enable-automation", false),
chromedp.Flag("disable-blink-features", "AutomationControlled"),
)
actx, acancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer acancel()
if debug {
ctx, cancel = chromedp.NewContext(actx, chromedp.WithDebugf(log.Printf))
} else {
ctx, cancel = chromedp.NewContext(actx)
if *userAgent != "" {
opts = append(opts, chromedp.UserAgent(*userAgent))
}
defer cancel()
actx, acncl = chromedp.NewExecAllocator(context.Background(), opts...)
defer acncl()
ctx, cncl = chromedp.NewContext(actx)
defer cncl()
rand.Seed(time.Now().UnixNano())
@ -475,7 +590,8 @@ func main() {
go func() {
<-c
log.Printf("Interrupt - shutting down.")
cancel()
cncl()
acncl()
srv.Shutdown(context.Background())
os.Exit(1)
}()
@ -486,17 +602,15 @@ func main() {
http.HandleFunc("/shutdown/", haltServer)
http.HandleFunc("/favicon.ico", http.NotFound)
log.Printf("Web Rendering Proxy Version %s\n", version)
log.Printf("Args: %q", os.Args)
log.Printf("Default Img Type: %v, Geometry: %+v", defType, defGeom)
log.Printf("Default Img Type: %v, Geometry: %+v", *defType, defGeom)
htmlTmpl, err = template.New("wrp.html").Parse(tmpl(tHTML))
htmlTmpl, err = template.New("wrp.html").Parse(tmpl(*htmFnam))
if err != nil {
log.Fatal(err)
}
log.Printf("Starting WRP http server on %s\n", addr)
srv.Addr = addr
log.Print("Starting WRP http server")
srv.Addr = *addr
err = srv.ListenAndServe()
if err != nil {
log.Fatal(err)

View File

@ -9,6 +9,8 @@
<INPUT TYPE="SUBMIT" NAME="Fn" VALUE="Bk">
<INPUT TYPE="SUBMIT" NAME="Fn" VALUE="St">
<INPUT TYPE="SUBMIT" NAME="Fn" VALUE="Re">
<INPUT TYPE="SUBMIT" NAME="Fn" VALUE="Up">
<INPUT TYPE="SUBMIT" NAME="Fn" VALUE="Dn">
W <INPUT TYPE="TEXT" NAME="w" VALUE="{{.Width}}" SIZE="4">
H <INPUT TYPE="TEXT" NAME="h" VALUE="{{.Height}}" SIZE="4">
Z <SELECT NAME="z">
@ -21,19 +23,22 @@
<OPTION VALUE="1.3" {{ if eq .Zoom 1.3}}SELECTED{{end}}>1.3 x</OPTION>
</SELECT>
T <SELECT NAME="t">
<OPTION VALUE="gif" {{ if eq .ImgType "gif"}}SELECTED{{end}}>GIF</OPTION>
<OPTION VALUE="png" {{ if eq .ImgType "png"}}SELECTED{{end}}>PNG</OPTION>
<OPTION VALUE="gif" {{ if eq .ImgType "gif"}}SELECTED{{end}}>GIF</OPTION>
<OPTION VALUE="jpg" {{ if eq .ImgType "jpg"}}SELECTED{{end}}>JPG</OPTION>
</SELECT>
C <SELECT NAME="c">
<OPTION VALUE="2" {{ if eq .NColors 2}}SELECTED{{end}}>2</OPTION>
<OPTION VALUE="16" {{ if eq .NColors 16}}SELECTED{{end}}>16</OPTION>
<OPTION VALUE="64" {{ if eq .NColors 64}}SELECTED{{end}}>64</OPTION>
<OPTION VALUE="128" {{ if eq .NColors 128}}SELECTED{{end}}>128</OPTION>
<OPTION VALUE="256" {{ if eq .NColors 256}}SELECTED{{end}}>256</OPTION>
<OPTION VALUE="216" {{ if eq .NColors 216}}SELECTED{{end}}>216</OPTION>
<OPTION VALUE="128" {{ if eq .NColors 128}}SELECTED{{end}}>128</OPTION>
<OPTION VALUE="64" {{ if eq .NColors 64}}SELECTED{{end}}>64</OPTION>
<OPTION VALUE="16" {{ if eq .NColors 16}}SELECTED{{end}}>16</OPTION>
<OPTION VALUE="2" {{ if eq .NColors 2}}SELECTED{{end}}>2</OPTION>
</SELECT>
K <INPUT TYPE="TEXT" NAME="k" VALUE="" SIZE="4">
<INPUT TYPE="SUBMIT" NAME="Fn" VALUE="Bs">
<INPUT TYPE="SUBMIT" NAME="Fn" VALUE="Rt"><!--
<INPUT TYPE="SUBMIT" NAME="Fn" VALUE="Rt">
<INPUT TYPE="SUBMIT" NAME="Fn" VALUE="All"><!--
<INPUT TYPE="SUBMIT" NAME="Fn" VALUE="&lt;">
<INPUT TYPE="SUBMIT" NAME="Fn" VALUE="^">
<INPUT TYPE="SUBMIT" NAME="Fn" VALUE="v">