Skip to content

Commit a7a832e

Browse files
committed
Host internal invisible servers in the ephemeral port range
This should reduce the change of conflicts here.
1 parent a603a94 commit a7a832e

File tree

4 files changed

+35
-26
lines changed

4 files changed

+35
-26
lines changed

src/cert-check-server.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
import { getLocal, Mockttp } from 'mockttp';
22

33
import { HtkConfig } from './config';
4+
import { EPHEMERAL_PORT_RANGE } from './constants';
45
import { getDeferred } from './util/promise';
56

6-
const buildPage = (config: HtkConfig, style: string, script?: string, body?: string) =>
7-
`<html>
8-
<head>
9-
<title>HTTP Toolkit HTTPS Test</title>
10-
<meta charset="UTF-8" />
11-
<link href="http://fonts.googleapis.com/css?family=Lato" rel="stylesheet" />
12-
<style>
13-
body {
14-
margin: 0;
15-
padding: 20px;
16-
}
17-
18-
${style}
19-
</style>
20-
${script}
21-
</head>
22-
<body>
23-
${body}
24-
</body>
25-
</html>`;
7+
const buildPage = (style: string, script?: string, body?: string) =>
8+
`<html>
9+
<head>
10+
<title>HTTP Toolkit HTTPS Test</title>
11+
<meta charset="UTF-8" />
12+
<link href="http://fonts.googleapis.com/css?family=Lato" rel="stylesheet" />
13+
<style>
14+
body {
15+
margin: 0;
16+
padding: 20px;
17+
}
18+
19+
${style}
20+
</style>
21+
${script}
22+
</head>
23+
<body>
24+
${body}
25+
</body>
26+
</html>`;
2627

2728
export class CertCheckServer {
2829

@@ -34,7 +35,7 @@ export class CertCheckServer {
3435

3536
async start(targetUrl: string) {
3637
this.server = getLocal({ https: this.config.https, cors: true });
37-
await this.server.start();
38+
await this.server.start(EPHEMERAL_PORT_RANGE);
3839

3940
await Promise.all([
4041
this.server.get('/test-https').thenCallback(() => {
@@ -47,7 +48,7 @@ export class CertCheckServer {
4748

4849
return {
4950
statusCode: 200,
50-
body: buildPage(this.config, `
51+
body: buildPage(`
5152
body {
5253
background-color: #d8e2e6;
5354
font-family: Lato, Arial;
@@ -122,7 +123,7 @@ export class CertCheckServer {
122123

123124
return {
124125
statusCode: 200,
125-
body: buildPage(this.config, `
126+
body: buildPage(`
126127
body {
127128
background-color: #d8e2e6;
128129
font-family: Lato, Arial;

src/constants.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@ export const ALLOWED_ORIGINS = IS_PROD_BUILD
2222
/^https?:\/\/localhost(:\d+)?$/,
2323
/^http:\/\/local\.httptoolkit\.tech(:\d+)?$/,
2424
/^https:\/\/app\.httptoolkit\.tech$/
25-
]
25+
]
26+
27+
// The range of ports that should be used by invisible ephemeral services, such as Firefox's
28+
// certificate check server and Chrome's "hide warning" server. These ports are extra likely
29+
// not to conflict with normal user usage, and are specifically designated by the IANA for
30+
// use for dynamic ports.
31+
export const EPHEMERAL_PORT_RANGE = { startPort: 49152, endPort: 65535 } as const;

src/hide-warning-server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getLocal, Mockttp } from 'mockttp';
22
import { HtkConfig } from './config';
3+
import { EPHEMERAL_PORT_RANGE } from './constants';
34

45
// The first tab that opens in a new Chrome/Edge window warns about dangerous flags.
56
// Closing it and immediately opening a new one is a bit cheeky, but
@@ -24,7 +25,7 @@ export class HideWarningServer {
2425
})
2526

2627
async start(targetUrl: string) {
27-
await this.server.start();
28+
await this.server.start(EPHEMERAL_PORT_RANGE);
2829

2930
await this.server.get('/hide-warning').thenReply(200, `
3031
<html>

src/message-server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getLocal, Mockttp } from 'mockttp';
22

33
import { HtkConfig } from './config';
4+
import { EPHEMERAL_PORT_RANGE } from './constants';
45
import { getDeferred } from './util/promise';
56

67
export class MessageServer {
@@ -16,7 +17,7 @@ export class MessageServer {
1617

1718
async start() {
1819
this.server = getLocal({ https: this.config.https, cors: true });
19-
await this.server.start();
20+
await this.server.start(EPHEMERAL_PORT_RANGE);
2021

2122
await this.server.get('/')
2223
.thenCallback(() => {

0 commit comments

Comments
 (0)