summaryrefslogtreecommitdiff
path: root/packages/integrations/cloudflare/src/utils/getCFObject.ts
blob: 7a4cd8a0cab7b440323cc896ebf1021253de3d2b (plain) (blame)
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
import type { IncomingRequestCfProperties } from '@cloudflare/workers-types/experimental';

export async function getCFObject(
	runtimeMode: string
): Promise<IncomingRequestCfProperties | void> {
	const CF_ENDPOINT = 'https://workers.cloudflare.com/cf.json';
	const CF_FALLBACK: IncomingRequestCfProperties = {
		asOrganization: '',
		asn: 395747,
		colo: 'DFW',
		city: 'Austin',
		region: 'Texas',
		regionCode: 'TX',
		metroCode: '635',
		postalCode: '78701',
		country: 'US',
		continent: 'NA',
		timezone: 'America/Chicago',
		latitude: '30.27130',
		longitude: '-97.74260',
		clientTcpRtt: 0,
		httpProtocol: 'HTTP/1.1',
		requestPriority: 'weight=192;exclusive=0',
		tlsCipher: 'AEAD-AES128-GCM-SHA256',
		tlsVersion: 'TLSv1.3',
		tlsClientAuth: {
			certPresented: '0',
			certVerified: 'NONE',
			certRevoked: '0',
			certIssuerDN: '',
			certSubjectDN: '',
			certIssuerDNRFC2253: '',
			certSubjectDNRFC2253: '',
			certIssuerDNLegacy: '',
			certSubjectDNLegacy: '',
			certSerial: '',
			certIssuerSerial: '',
			certSKI: '',
			certIssuerSKI: '',
			certFingerprintSHA1: '',
			certFingerprintSHA256: '',
			certNotBefore: '',
			certNotAfter: '',
		},
		edgeRequestKeepAliveStatus: 0,
		hostMetadata: undefined,
		clientTrustScore: 99,
		botManagement: {
			corporateProxy: false,
			verifiedBot: false,
			ja3Hash: '25b4882c2bcb50cd6b469ff28c596742',
			staticResource: false,
			detectionIds: [],
			score: 99,
		},
	};

	if (runtimeMode === 'local') {
		return CF_FALLBACK;
	} else if (runtimeMode === 'remote') {
		try {
			const res = await fetch(CF_ENDPOINT);
			const cfText = await res.text();
			const storedCf = JSON.parse(cfText);
			return storedCf;
		} catch (e: any) {
			return CF_FALLBACK;
		}
	}
}