Skip to content

Commit

Permalink
feat: add automatic header order collector / updater (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
barjin authored Apr 5, 2023
1 parent 8f81cc3 commit 7b0e171
Show file tree
Hide file tree
Showing 10 changed files with 1,394 additions and 7,843 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/model-updater.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ jobs:
- name: Install Dependencies
run: npm ci

- name: Install Playwright browsers and collect HTTP header orders
run: |
npx playwright install
RUN npx playwright install chrome
RUN npx playwright install msedge
node ./packages/header-order-collector/collector.js > ./packages/header-generator/src/data_files/headers_order.json
- name: Generate model and update versions
run: |
npm version patch --git-tag-version=false
Expand Down
9,013 changes: 1,171 additions & 7,842 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"test": "jest --silent",
"lint": "eslint packages/*/src test",
"lint:fix": "eslint packages/*/src test --fix",
"buildNetwork": "turbo run build && ./scripts/netgen.sh",
"buildNetwork": "turbo run build && ./scripts/netgen.sh && turbo run build",
"benchmark": "ts-node ./test/antibot-services/live-testing/cloudflare.ts"
},
"devDependencies": {
Expand Down
17 changes: 17 additions & 0 deletions packages/header-order-collector/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM mcr.microsoft.com/playwright:v1.30.0-focal

# Install browsers
RUN npx playwright install chrome
RUN npx playwright install msedge

# Install Node.js dependencies
COPY package.json package-lock.json ./
RUN npm install


# Copy the rest of the files
COPY ./collector.js ./
COPY ./server.js ./

# Run the server
CMD ["node", "collector.js"]
22 changes: 22 additions & 0 deletions packages/header-order-collector/cert/localhost-cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-----BEGIN CERTIFICATE-----
MIIDtTCCAp2gAwIBAgIUETM6CjfHl22Z8zGPhixP1J+JBLAwDQYJKoZIhvcNAQEL
BQAwajELMAkGA1UEBhMC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9FJb3e0XB
y06CNnF3MPB9wA/zHYrPPCyo+e5A+ArKsBDH6PWP0hX+MIvxPe64rjdf1vpraYV4
oRFhzOZ7HihCOjfwStwa76J9KvPTch5kugyx/FvLcCzonwctQ7AnNFY=
-----END CERTIFICATE-----
28 changes: 28 additions & 0 deletions packages/header-order-collector/cert/localhost-privkey.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC9hCsFSK2ByMv1
3fwpuY13xbXYUCcpLgJ8cIPTsMPzjtVmHUmBBv1rwsGUY+Hv2o+Ccx8qfm8HpjQn
x6IKtj1u+wJw1OuNClxAKUHdgDx1FddsyiAnW7ELhurGXWoULpa1Km+6U9bb9/cW
HR3MyCwyL9FznzstiTfpWYaFP6HQbDcgSfaW42HRnXeCbq/DVp92xVQ3XyOXwYNm
W8qfdfnx4uAZs/ni2m5EpnRqrVw1L5XLH6AmmQ/TGF992jSOfO74hYi49ZTCIFch
kySj1QHoH8a+BIYLxGcE27qOmkF+BHjna+4EAwEcVRhqwCYJYz7pofdy2FVNMq3o
9+SD9zbdAgMBAAECggEACn9s+6KyO79x7tlgS7Zjy/fncgdEmA8co6R+5OXEgPQP
RZQHO4610HeODgh3Xjgq3WS/m/S7dT7DYlW4V+QO5JwiXzooHxytDf/LDng5QRoX
4AaVP+DgHn2XeSv5AYuwHPJwFG/q9a3XYcH0oeCPtCr6bO9r0YKplnM3VJ+4+KjB
feBjxdppTVa4iY5FpEASsKnvXt9efqAnwR8lgKlWAD37OPMgrZldJlSL98IEp2bj
vcwabuWiFGAQeMeG5wzBVpsjad6h4eE9XVZi3Gwzup0Rz8OHqf4SsHen//ECT7j2
8la6RpwNlZjEcJtReaI9yu7USor3SugI2zJxY7X1CQKBgQDVaiQibd/4jS+vXsiC
yh6A/YAHGQlyvvLL4yzQ80XQvJYT4x9VDK7v8Se1RsTYs3LgDxGZ0EvECiorG3rI
i0du9UTip2n3OGKgZh2Memfe6j/lkNkvlGBQ3RdRflKUS7SsAWZY1PkCysJZ98N6
1a9zoIwiSONrKYQSRBVbODYdFQKBgQDjVTp2aAI4FKIdhImViIh2JZdz89kYCJdD
MK8LvSjDQCA3ADQqXXlzqgZwR27XE7CbGcRsIXGP1a8neQbkAxeSo68UUuCpHq9v
JoYrfRMS5CZDLHmr3N46wGpgzlgyAH0PtgoNdvHcxskX8jOvYjGsxh+r/XV/yeyT
RXPDX+70qQKBgQCx4Xtm2xAgmoWRdb5JDXDpcOIiiGLQqomzGZJEQ1EAz/+Daf62
pBC329iOBwOR39izGrw/6VcNeq91KL1ppHWQ7YxSItp2/h9utE6SxYAIVFWX4ei0
+MZjADUsbs8j3gokiDInfrRi+F+QH6xmmO9ZwAqtlPVBX+Q+WVM3qh2olQKBgEMJ
mQndtfnPq9bEhMYB0yvz/euFlxlNPz3jF1uA1optWJKfjsKHeg8vrmJhzj/CYKv8
Nz4jxpdBzjr3p56JguQNPM3jerikgdtnIt2kLoG9ocLhVKfno+jzYOtXLg7gsgB2
EBTUZJJUVGypUbCAZmGqsyBGx+V9KDRtoKuWmKKxAoGAGQjx7fKum+68X+cbCDjd
mbvWEPnbdPQIzcfuzb4HCNvoNb8yxkNEvfxbNZgXf+xU4YufPYqNxyvHPdKsBzEw
vN9+zAtyF7NodU3qyKyLVXO9Q26HXBMOijNkbSpTFYRan8mlWVUMdKzsjt8bcNrZ
fjT5CpfugV4xmmDhKzITvwE=
-----END PRIVATE KEY-----
55 changes: 55 additions & 0 deletions packages/header-order-collector/collector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const playwright = require('playwright');
const { runServer: v1 } = require('./server');
const { runServer: v2 } = require('./serverv2');

const HTTP1port = 3001;
const HTTP2port = 3002;

async function getHeadersFor(launcher, httpVersion) {
const browser = await launcher({
headless: false,
});

const context = await browser.newContext({
ignoreHTTPSErrors: true,
});
const page = await context.newPage();

if ( httpVersion === 1 ) {
await page.goto(`http://localhost:${HTTP1port}/`);
} else {
await page.goto(`https://localhost:${HTTP2port}/`);
}
await page.click('a');

const headerNames = await page.evaluate(() => {
return JSON.parse(document.body.innerText);
});
await browser.close();
return headerNames;
}

(async () => {
v1(HTTP1port);
v2(HTTP2port);

const browserTypes = {
// safari: () => playwright.webkit.launch(),
chrome: (p) => playwright.chromium.launch(p),
firefox: (p) => playwright.firefox.launch(p),
// edge: () => playwright.chromium.launch({ channel: 'msedge' }),
};

const x = await Promise.all(
Object.entries(browserTypes)
.map(async ([name, launcher]) => {
return [name, [...await getHeadersFor(launcher, 1), ...await getHeadersFor(launcher, 2)]];
})
);

console.log(JSON.stringify(Object.fromEntries(x), null, 4));

process.exit(0);
}
)();

17 changes: 17 additions & 0 deletions packages/header-order-collector/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "header-order-collector",
"version": "1.0.0",
"description": "",
"main": "p.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2",
"playwright": "^1.31.2"
}
}
38 changes: 38 additions & 0 deletions packages/header-order-collector/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const express = require('express');
const app = express();
const port = 3001;

function extractHeaderNames(rawHeaders) {
return rawHeaders.filter((_, i) => i % 2 === 0);
}

app.get('/', (req, res) => {
res.header('set-cookie', 'A=1; Path=/headers; HttpOnly');
res.send(`
<a href="/headers">Click here</a> to see the headers.
`)
})

app.get('/headers', (req, res) => {
const rawHeaders = req.rawHeaders;
const headerNames = extractHeaderNames(rawHeaders);
res.json(headerNames);
});


function runServer(p) {
return new Promise(r => {
const inst = app.listen(p ?? port, (err) => {
if (err) {
throw new Error(err);
}

r(inst);
});
});
}

module.exports = {
runServer,
app,
};
38 changes: 38 additions & 0 deletions packages/header-order-collector/serverv2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const http2 = require('node:http2');
const fs = require('fs');

const port = 3002;

const server = http2.createSecureServer({
key: fs.readFileSync(`${__dirname}/cert/localhost-privkey.pem`),
cert: fs.readFileSync(`${__dirname}/cert/localhost-cert.pem`),
});
server.on('error', (err) => console.error(err));

server.on('stream', (stream, headers) => {
if(headers[':path'] === '/headers'){
stream.respond({
'content-type': 'application/json; charset=utf-8',
':status': 200,
});
stream.end(JSON.stringify(Object.keys(headers)));
return;
};

stream.respond({
'content-type': 'text/html; charset=utf-8',
'set-cookie': 'A=1; Path=/headers; Secure; HttpOnly',
':status': 200,
});
stream.end(`
<a href="/headers">Click here</a> to see the headers.
`);
});

module.exports = {
runServer: (p) => server.listen(p ?? port),
}

if (require.main === module) {
server.listen(port);
}

0 comments on commit 7b0e171

Please sign in to comment.