Skip to content

Commit

Permalink
update SPKI fingerprint (#38)
Browse files Browse the repository at this point in the history
- Fix SKPI fingerprint
- Relaunch Jambox server if there is a version mismatch
- Update mockttp library
  • Loading branch information
ballercat authored Oct 24, 2023
1 parent 90ebb53 commit 29a633b
Show file tree
Hide file tree
Showing 32 changed files with 235 additions and 276 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.0.29 - Fix SKPI Fingerprint, minor improvements

- Fix SKPI fingerprint
- Relaunch Jambox server if there is a version mismatch
- Update mockttp library

## 0.0.28 - Fix cert expiration

- fix: Updated the local cert to now expire in Dec 26, 2031
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jambox",
"version": "0.0.28",
"version": "0.0.29",
"description": "Tool for recording and playing back HTTP requests.",
"bin": {
"jam": "./jam.mjs",
Expand Down Expand Up @@ -39,7 +39,7 @@
"global-agent": "^3.0.0",
"http-encoding": "^1.5.1",
"minimatch": "^5.1.0",
"mockttp": "^3.4.0",
"mockttp": "^3.9.4",
"nock": "^13.2.9",
"node-fetch": "2.6.7",
"osenv": "^0.1.5",
Expand Down
202 changes: 0 additions & 202 deletions src/__tests__/__snapshots__/cache.test.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,205 +60,3 @@ Generated by [AVA](https://avajs.dev).
type: 'cache.revert',
},
]

## reading cache

> Snapshot 1
{
request: {
body: {
buffer: Buffer @Uint8Array [
7b227175 65727922 3a227175 65727920 506f6b65 6d6f6e28 246e616d 653a2053
7472696e 6729207b 5c6e2070 6f6b656d 6f6e286e 616d653a 20246e61 6d652920
7b5c6e20 20206e61 6d655c6e 20202069 6d616765 5c6e207d 5c6e7d22 2c227661
72696162 6c657322 3a7b226e 616d6522 3a22536c 6f77706f 6b65227d 7d
],
getDecodedBuffer: Function getDecodedBuffer {},
getJson: AsyncFunction [],
getText: AsyncFunction [],
},
headers: {
accept: 'application/json',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9',
connection: 'keep-alive',
'content-length': '125',
'content-type': 'application/json',
host: 'graphql-pokemon2.vercel.app',
origin: 'https://jambox-demo-graphql.vercel.app',
referer: 'https://jambox-demo-graphql.vercel.app/',
'sec-ch-ua': '"Not?A_Brand";v="8", "Chromium";v="108"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Linux"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'cross-site',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
},
httpVersion: '1.1',
id: 'ebf337e5-ccdf-46be-b629-4651fefa8ad7',
matchedRuleId: '2963a70a-837b-4563-bc96-e9049673f097',
method: 'POST',
path: '/',
protocol: 'https',
rawHeaders: [
[
'Host',
'graphql-pokemon2.vercel.app',
],
[
'Connection',
'keep-alive',
],
[
'Content-Length',
'125',
],
[
'sec-ch-ua',
'"Not?A_Brand";v="8", "Chromium";v="108"',
],
[
'Accept',
'application/json',
],
[
'Content-Type',
'application/json',
],
[
'sec-ch-ua-mobile',
'?0',
],
[
'User-Agent',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
],
[
'sec-ch-ua-platform',
'"Linux"',
],
[
'Origin',
'https://jambox-demo-graphql.vercel.app',
],
[
'Sec-Fetch-Site',
'cross-site',
],
[
'Sec-Fetch-Mode',
'cors',
],
[
'Sec-Fetch-Dest',
'empty',
],
[
'Referer',
'https://jambox-demo-graphql.vercel.app/',
],
[
'Accept-Encoding',
'gzip, deflate, br',
],
[
'Accept-Language',
'en-US,en;q=0.9',
],
],
remoteIpAddress: '::1',
remotePort: 50448,
tags: [],
timingEvents: {
bodyReceivedTimestamp: 2040118.9439219981,
startTime: 1673212233255,
startTimestamp: 2040117.9996999986,
},
url: 'https://graphql-pokemon2.vercel.app/',
},
response: {
body: {
buffer: Buffer @Uint8Array [
7b226461 7461223a 7b22706f 6b656d6f 6e223a7b 226e616d 65223a22 536c6f77
706f6b65 222c2269 6d616765 223a2268 74747073 3a2f2f69 6d672e70 6f6b656d
6f6e6462 2e6e6574 2f617274 776f726b 2f736c6f 77706f6b 652e6a70 67227d7d
7d
],
getDecodedBuffer: Function getDecodedBuffer {},
getJson: AsyncFunction [],
getText: AsyncFunction [],
},
headers: {
'access-control-allow-methods': 'GET,HEAD,PUT,POST,DELETE',
'access-control-allow-origin': 'https://jambox-demo-graphql.vercel.app',
'cache-control': 'public, max-age=0, must-revalidate',
connection: 'keep-alive',
'content-length': '97',
'content-type': 'application/json; charset=utf-8',
date: 'Sun, 08 Jan 2023 21:10:33 GMT',
server: 'Vercel',
'strict-transport-security': 'max-age=63072000; includeSubDomains; preload',
'x-vercel-cache': 'MISS',
'x-vercel-id': 'iad1::sfo1::qnkr7-1673212233783-e24fced79c98',
},
id: 'ebf337e5-ccdf-46be-b629-4651fefa8ad7',
rawHeaders: [
[
'Access-Control-Allow-Methods',
'GET,HEAD,PUT,POST,DELETE',
],
[
'Access-Control-Allow-Origin',
'https://jambox-demo-graphql.vercel.app',
],
[
'Cache-Control',
'public, max-age=0, must-revalidate',
],
[
'Connection',
'keep-alive',
],
[
'Content-Length',
'97',
],
[
'Content-Type',
'application/json; charset=utf-8',
],
[
'Date',
'Sun, 08 Jan 2023 21:10:33 GMT',
],
[
'Server',
'Vercel',
],
[
'Strict-Transport-Security',
'max-age=63072000; includeSubDomains; preload',
],
[
'X-Vercel-Cache',
'MISS',
],
[
'X-Vercel-Id',
'iad1::sfo1::qnkr7-1673212233783-e24fced79c98',
],
],
statusCode: 200,
statusMessage: 'OK',
tags: [],
timingEvents: {
bodyReceivedTimestamp: 2040118.9439219981,
headersSentTimestamp: 2040368.6721789986,
responseSentTimestamp: 2040372.3621459976,
startTime: 1673212233255,
startTimestamp: 2040117.9996999986,
},
},
}
Binary file modified src/__tests__/__snapshots__/cache.test.mjs.snap
Binary file not shown.
2 changes: 1 addition & 1 deletion src/browser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { EXTENSION_PATH } from './constants.mjs';
import launcher from '@httptoolkit/browser-launcher';

// that's right
const SPKI_FINGERPRINT = 'eqkW7XlzpdM4UC0xkQzKX6t415TM398j5H16Dwjr/Jk=';
const SPKI_FINGERPRINT = 'ImPkfKy0ZYTFQr8oFYoEGm6FJOgHyRUkeMBTfHujwSQ=';

const detect = () => {
return new Promise((resolve) => {
Expand Down
18 changes: 18 additions & 0 deletions src/constants.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import * as url from 'url';
import path from 'path';
import fs from 'fs';

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

export const PROJECT_ROOT = path.join(__dirname, '..');
export const SCRIPT_HELPER = path.join(PROJECT_ROOT, 'src', 'script-helper.js');
export const EXTENSION_PATH = path.join(PROJECT_ROOT, 'build');
export const PACKAGE_JSON_PATH = path.join(PROJECT_ROOT, 'package.json');
export const CACHE_DIR_NAME = '.jambox';
export const CONFIG_FILE_NAME = 'jambox.config.js';
export const DEFAULT_TAPE_NAME = 'default.tape.zip';

let version = null;
export const getVersion = () => {
if (version) {
return version;
}

try {
const content = fs.readFileSync(PACKAGE_JSON_PATH, 'utf-8');
version = JSON.parse(content).version;
return version;
} catch (e) {
console.log('Failed while loading jambox pacakge.json', e);
return null;
}
};
43 changes: 32 additions & 11 deletions src/server-launcher.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import _debug from 'debug';
import waitOn from 'wait-on';
import path from 'path';
import fs from 'fs';
import fetch from 'node-fetch';
import { spawn } from 'child_process';
import { PROJECT_ROOT } from './constants.mjs';
import { PROJECT_ROOT, getVersion } from './constants.mjs';

const debug = _debug('jambox');

Expand All @@ -16,16 +17,7 @@ const ping = async (port) => {
}
};

// Returns when a server instance is available
const launcher = async ({ log, port, constants, config }) => {
debug('Checking if a server instance is running.');
const isServerAvailable = await ping(port);
if (isServerAvailable) {
return;
}

log('Jambox server not currently running. Launching an instance');

const spawnServerProcess = async ({ port, config, constants }) => {
const out = fs.openSync(config.logLocation, 'a');
const child = spawn(
'node',
Expand All @@ -52,4 +44,33 @@ const launcher = async ({ log, port, constants, config }) => {
});
};

// Returns when a server instance is available
const launcher = async ({ log, port, constants, config }) => {
debug('Checking if a server instance is running.');
const isServerAvailable = await ping(port);
if (isServerAvailable) {
// Would be slightly nicer if waitOn could return the values from the pinged endpoints...
const apiVersion = await fetch(`http://localhost:${port}/`).then((res) =>
res.text()
);
const currentVersion = getVersion();
if (currentVersion !== null && apiVersion !== currentVersion) {
log(
`Installed version (${getVersion()}) is different from running version (${
apiVersion || '??'
}).`
);
log(`Killing old version.`);
await fetch(`http://localhost:${port}/shutdown`);
log(`Starting a new Jambox process.`);
await spawnServerProcess({ port, config, constants });
}
return;
}

log('Jambox server not currently running. Launching an instance');

await spawnServerProcess({ port, config, constants });
};

export default launcher;
4 changes: 2 additions & 2 deletions src/server/__tests__/handlers.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test('localhost: basic forwarding', async (t) => {

t.deepEqual(t.context.explainRules(), [
'GlobMatcher {"target":"http://google.com/","paths":["**"]}',
`ProxyHandler {"ignoreHostHttpsErrors":["localhost:3000"],"forwarding":{"targetHost":"http://localhost:3000","updateHostHeader":"google.com"}}`,
`ProxyHandler {"ignoreHostHttpsErrors":true,"forwarding":{"targetHost":"http://localhost:3000","updateHostHeader":"google.com"}}`,
]);
});

Expand All @@ -61,7 +61,7 @@ test('localhost: glob matching', async (t) => {

t.deepEqual(t.context.explainRules(), [
'GlobMatcher {"target":"http://google.com/","paths":["/**/*","!/**/graphql"]}',
'ProxyHandler {"ignoreHostHttpsErrors":["localhost:3000"],"forwarding":{"targetHost":"http://localhost:3000","updateHostHeader":"google.com"}}',
'ProxyHandler {"ignoreHostHttpsErrors":true,"forwarding":{"targetHost":"http://localhost:3000","updateHostHeader":"google.com"}}',
'GlobMatcher {"target":"http://google.com/","paths":["/**/*","!/**/graphql"]}',
'forward the websocket to ws://localhost:3000',
]);
Expand Down
4 changes: 3 additions & 1 deletion src/server/api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import getConfig from '../config.mjs';
import setupHandlers from './handlers.mjs';
import debounce from '../utils/debounce.mjs';
import { serializeRequest, serializeResponse } from '../cache.mjs';
import { getVersion } from '../constants.mjs';

const debug = _debug('jambox.backend');

const getInfo = (svc, config) => {
Expand Down Expand Up @@ -40,7 +42,7 @@ const backend = async (svc, config) => {
});
};

svc.app.get('/', async (_, res) => res.send('OK'));
svc.app.get('/', async (_, res) => res.send(getVersion()));
svc.app.get('/api/config', async (_, res) => res.send(getInfo(svc, config)));
// Bandaid solution (mostly) for testing purposes (does not persist to disk)
svc.app.post('/api/config', async (req, res) => {
Expand Down
4 changes: 1 addition & 3 deletions src/server/handlers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ export const forward = (svc, config) => {
const changeHosts = originalURL.host !== targetURL.host;

const httpOptions = {
ignoreHostHttpsErrors: [
changeHosts ? targetURL.host : originalURL.host,
],
ignoreHostHttpsErrors: true,
forwarding: {
targetHost: `http${useSSL ? 's' : ''}://${targetURL.host}`,
updateHostHeader: changeHosts ? originalURL.host : false,
Expand Down
Loading

0 comments on commit 29a633b

Please sign in to comment.