This repository was archived by the owner on Jan 13, 2025. It is now read-only.
forked from httptoolkit/httptoolkit-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcert-check-server.ts
More file actions
190 lines (168 loc) · 6.8 KB
/
cert-check-server.ts
File metadata and controls
190 lines (168 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { getLocal, Mockttp } from "mockttp";
import { HtkConfig } from "./config";
import { EPHEMERAL_PORT_RANGE } from "./constants";
import { getDeferred } from "./util/promise";
const buildPage = (style: string, script?: string, body?: string) =>
`<html>
<head>
<title>HTTP Toolkit HTTPS Test</title>
<meta charset="UTF-8" />
<link href="http://fonts.googleapis.com/css?family=Lato" rel="stylesheet" />
<style>
body {
margin: 0;
padding: 20px;
}
${style}
</style>
${script}
</head>
<body>
${body}
</body>
</html>`;
export class CertCheckServer {
constructor(private config: HtkConfig) {}
private server: Mockttp | undefined;
private certCheckedSuccessfully = getDeferred<boolean>();
async start(targetUrl: string) {
this.server = getLocal({ https: this.config.https, cors: true });
await this.server.start(EPHEMERAL_PORT_RANGE);
await Promise.all([
this.server.forGet("/test-https").thenCallback(() => {
console.log("Request to /test-https successfully received");
this.certCheckedSuccessfully.resolve(true);
return { statusCode: 200 };
}),
this.server.forGet("/check-cert").thenCallback(() => {
console.log("Request to /check-cert received");
return {
statusCode: 200,
body: buildPage(
`
body {
background-color: #d8e2e6;
font-family: Lato, Arial;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
box-sizing: border-box;
text-align: center;
}
p {
font-size: 16pt;
}
iframe {
display: none;
}
`,
`
<script>
let installingCert = false;
const targetUrl = ${JSON.stringify(targetUrl)};
function ensureCertificateIsInstalled() {
const testUrl = window.location.href.replace('http://', 'https://').replace('check-cert', 'test-https');
const failedUrl = window.location.href.replace('check-cert', 'failed-test');
fetch(testUrl)
.then(() => true)
.catch(() => false)
.then((certificateIsTrusted) => {
if (certificateIsTrusted) {
window.location.replace(targetUrl);
} else {
window.location.replace(failedUrl);
}
});
}
ensureCertificateIsInstalled();
</script>
`,
`
<svg
version="1.1"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
x="0px"
y="0px"
width="400px"
height="400px"
viewBox="0 0 50 50"
style="enable-background:new 0 0 50 50;"
>
<path fill="#b6c2ca" d="M25.251,6.461c-10.318,0-18.683,8.365-18.683,18.683h4.068c0-8.071,6.543-14.615,14.615-14.615V6.461z">
<animateTransform
attributeType="xml"
attributeName="transform"
type="rotate"
from="0 25 25"
to="360 25 25"
dur="6s"
repeatCount="indefinite"
/>
</path>
</svg>
`
),
};
}),
this.server.forGet("/failed-test").thenCallback(() => {
console.log("Request to /failed-test received");
this.certCheckedSuccessfully.resolve(false);
return {
statusCode: 200,
body: buildPage(
`
body {
background-color: #d8e2e6;
font-family: Lato, Arial;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 60%;
box-sizing: border-box;
text-align: center;
}
p {
font-size: 16pt;
}
iframe {
display: none;
}
`,
``,
`
<p>
This browser does not trust thePipe certificate authority, so HTTPS traffic can't be intercepted.
</p><p>
Closing the browser and starting it again fromPipe will often resolve this. If not,
please file a bug at <strong>github.com/httptoolkit/httptoolkit</strong>.
</p>
`
),
};
}),
]);
}
get host(): string {
return this.server!.url.replace("https://", "");
}
get url(): string {
return this.server!.url.replace("https://", "http://").replace(
/\/?$/,
"/check-cert"
);
}
async waitForSuccess(): Promise<void> {
return this.certCheckedSuccessfully.promise.then((result) => {
if (result !== true) throw new Error("Certificate check failed");
});
}
async stop() {
if (this.server) {
await this.server.stop();
this.server = undefined;
}
}
}