-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathtls.js
More file actions
65 lines (50 loc) · 1.68 KB
/
tls.js
File metadata and controls
65 lines (50 loc) · 1.68 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
import axios from 'axios';
import middleware from './_common/middleware.js';
const MOZILLA_TLS_OBSERVATORY_API =
process.env. TLS_OBSERVATORY_API ||
'https://tls-observatory.services. mozilla.com/api/v1';
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const tlsHandler = async (url) => {
try {
const domain = new URL(url).hostname;
const scanResponse = await axios.post(
`${MOZILLA_TLS_OBSERVATORY_API}/scan? target=${domain}&rescan=true`
);
const scanId = scanResponse.data.scan_id;
if (scanId === undefined || scanId === null) {
return { statusCode: 500, body: { error: 'Failed to get scan_id' } };
}
const MAX_RETRIES = 20;
const POLLING_INTERVAL = 2000;
let attempts = 0;
let resultResponse;
while (attempts < MAX_RETRIES) {
attempts++;
resultResponse = await axios. get(
`${MOZILLA_TLS_OBSERVATORY_API}/results?id=${scanId}`
);
const data = resultResponse. data;
const completed =
data.completion_perc === 100 ||
data.state === 'FINISHED' ||
data.state === 'READY' ||
data.analysis?. state === 'COMPLETED';
if (completed) {
return { statusCode: 200, body: data };
}
await sleep(POLLING_INTERVAL);
}
return {
statusCode: 408,
body: {
error: 'TLS scan timed out awaiting results',
partial_data: resultResponse?.data,
},
};
} catch (error) {
console.error('TLS Observatory Error:', error.response?.data || error.message);
return { statusCode: 500, body: { error: error.message } };
}
};
export const handler = middleware(tlsHandler);
export default handler;