First working webservice

This commit is contained in:
Kuba Orlik 2022-04-24 17:24:27 +02:00
parent 813f9bad03
commit 78db6b79cf
7 changed files with 1408 additions and 4 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/node_modules/
/static/

View File

@ -40,5 +40,7 @@ RUN rm -rf /tmp/* /var/cache/apk/*
RUN apk add jq RUN apk add jq
RUN apk add sed RUN apk add sed
RUN apk add nodejs RUN apk add nodejs
COPY ./mozilla /root/.mozilla
COPY . /opt
WORKDIR /opt WORKDIR /opt

View File

@ -15,5 +15,5 @@ docker image build -t headless-fox .
## Running ## Running
``` ```
docker run -ti -v $PWD:/opt -v $PWD/mozilla:/root/.mozilla/ headless-fox ./script3.sh docker run -i -v $PWD:/opt headless-fox ./script3.sh '{"url": "pearson.pl", "third_party_domains": ["hotjar.com", "cookielaw.org"]}' 123
``` ```

View File

@ -46,8 +46,6 @@ annotate_header(){
continue continue
fi; fi;
coords=$(echo $matching_line | awk '{print $1 " " $2 " " $3 " " $4}') coords=$(echo $matching_line | awk '{print $1 " " $2 " " $3 " " $4}')
echo "Matched coords for" $header ", " $coords
echo "^ matching line: " $matching_line
highlight_margin=5 highlight_margin=5
rect_x1=$(($(echo "$coords" | awk '{print $1}') - highlight_margin + left)) rect_x1=$(($(echo "$coords" | awk '{print $1}') - highlight_margin + left))
rect_y1=$(($(echo "$coords" | awk '{print $2}') - highlight_margin + top)) rect_y1=$(($(echo "$coords" | awk '{print $2}') - highlight_margin + top))
@ -83,7 +81,7 @@ annotate_header(){
convert "$filename" "$overlay_filename" -compose Darken -composite "$annotated_filename.step.png" convert "$filename" "$overlay_filename" -compose Darken -composite "$annotated_filename.step.png"
convert "$annotated_filename.step.png" "$hardoverlay_filename" -compose src-over -composite "$annotated_filename" convert "$annotated_filename.step.png" "$hardoverlay_filename" -compose src-over -composite "$annotated_filename"
rm "$overlay_filename" "$annotated_filename.step.png" "$hardoverlay_filename" "$cropped_filename" "$filename" rm "$overlay_filename" "$annotated_filename.step.png" "$hardoverlay_filename" "$cropped_filename" "$filename"
echo "done annotate $filename"; echo "SCREENSHOT: <img width=\"720\" height=\"405\" src=\"http://localhost:3000/$(echo "$annotated_filename" | sed 's|/opt/||')\"/>"
} }
#annotate_header "set-cookie" "identyfikator internetowy z cookie" "Cookie" "identyfikator internetowy z cookie" #annotate_header "set-cookie" "identyfikator internetowy z cookie" "Cookie" "identyfikator internetowy z cookie"

81
index.js Normal file
View File

@ -0,0 +1,81 @@
const { v4: uuid } = require("uuid");
var serve = require("koa-static");
const Koa = require("koa");
const Router = require("@koa/router");
const mount = require("koa-mount");
const qs = require("qs");
const { Readable } = require("stream");
const { spawn } = require("child_process");
const router = new Router();
// response
const app = new Koa();
const static = new Koa();
static.use(serve("./static"));
app.use(mount("/static", static));
function attach(docker_id, output_stream) {
// to prevent browser timeout
const interval = setInterval(() => output_stream.push("<span></span>"), 500);
const task = spawn("docker", ["logs", "-f", docker_id]);
task.stdout.on("data", (d) => output_stream.push(d));
task.stdout.on("error", (d) => output_stream.push(d));
task.on("close", () => {
const check = spawn("docker", ["ps", "--filter", `id=${docker_id}`]);
let output = "";
check.on("data", (data) => (output += data));
check.on("close", () => {
console.log("CHECK", output);
clearInterval(interval);
});
check.on("error", (error) => {
console.error("error", error);
clearInterval(interval);
});
check.on("exit", (error) => {
console.error("exit", error);
clearInterval(interval);
});
});
}
router.get("/", async (ctx) => {
const s = new Readable({ read() {} });
// stream data
ctx.response.set("content-type", "txt/html");
ctx.type = "html"; // <-- THIS is the important step!
ctx.body = s;
ctx.body.push("<!doctype html>");
const params = qs.parse(ctx.querystring);
s.push(`Got request to screenshot ${params.url}<pre>`);
const id = uuid();
let docker_id = "";
const starter = spawn(
"docker",
[
"run",
"-i",
"-d",
"-v",
`${process.cwd()}/static:/opt/static`,
"headless-fox",
"./script3.sh",
'{"url": "pearson.pl", "third_party_domains": ["hotjar.com", "cookielaw.org"]}',
id,
],
{ cwd: process.cwd() }
);
starter.stdout.on("data", (data) => {
docker_id += data.toString().replace(/\n/g, "");
});
starter.on("close", () => {
ctx.body.push("spawned " + docker_id);
attach(docker_id, ctx.body);
});
});
app.use(router.routes()).use(router.allowedMethods());
app.listen(3000);

1297
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

24
package.json Normal file
View File

@ -0,0 +1,24 @@
{
"name": "screenshot-service",
"version": "1.0.0",
"description": "## Dependencies",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "gitea@git.internet-czas-dzialac.pl:icd/screenshot-service.git"
},
"author": "",
"license": "ISC",
"dependencies": {
"@koa/router": "^10.1.1",
"amqplib": "^0.8.0",
"koa": "^2.13.4",
"koa-mount": "^4.0.0",
"koa-static": "^5.0.0",
"qs": "^6.10.3",
"uuid": "^8.3.2"
}
}