Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use ip2location for hosting detection, allow manual overrides (#434) #450

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/lib/geoip/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { fastlyLookup } from './providers/fastly.js';
import { maxmindLookup } from './providers/maxmind.js';
import { ipmapLookup } from './providers/ipmap.js';
import { type Ip2LocationBundledResponse, ip2LocationLookup } from './providers/ip2location.js';
import { isHostingOverrides } from './overrides.js';

export type LocationInfo = Omit<ProbeLocation, 'region'>;
type Provider = 'ipmap' | 'ip2location' | 'ipinfo' | 'maxmind' | 'fastly';
Expand Down Expand Up @@ -42,7 +43,7 @@ export default class GeoipClient {
this.lookupWithCache<LocationInfo>(`geoip:fastly:${addr}`, async () => fastlyLookup(addr)),
])
.then(([ ipinfo, ip2location, maxmind, ipmap, fastly ]) => {
isHosting = ipinfo.status === 'fulfilled' ? ipinfo.value.isHosting : undefined;
isHosting = ip2location.status === 'fulfilled' ? ip2location.value.isHosting : undefined;
const fulfilled: (LocationInfoWithProvider | null)[] = [];

// Providers here are pushed in a desc prioritized order
Expand Down Expand Up @@ -74,6 +75,13 @@ export default class GeoipClient {
throw new ProbeError(`unresolvable geoip: ${addr}`);
}

for (const override of isHostingOverrides) {
if (override.normalizedNetwork.test(networkMatch.normalizedNetwork)) {
isHosting = override.isHosting;
break;
}
}

const region = getRegionByCountry(match.country);

return {
Expand Down
12 changes: 12 additions & 0 deletions src/lib/geoip/overrides.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const isHostingOverrides = [
// https://baxetgroup.com/
{
normalizedNetwork: /baxet group/,
isHosting: true,
},
// https://www.psychz.net/
{
normalizedNetwork: /psychz networks/,
isHosting: true,
},
];
6 changes: 6 additions & 0 deletions src/lib/geoip/providers/ip2location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ type Ip2LocationResponse = {
longitude?: number;
as?: string;
is_proxy?: boolean;
usage_type?: string;
};

export type Ip2LocationBundledResponse = {
location: LocationInfo,
isHosting: boolean | undefined,
isProxy: boolean,
};

// https://blog.ip2location.com/knowledge-base/what-is-usage-type/
const HOSTING_USAGE_TYPES = [ 'CDN', 'DCH' ];

export const ip2LocationLookup = async (addr: string): Promise<Ip2LocationBundledResponse> => {
const result = await got(`https://api.ip2location.io`, {
searchParams: {
Expand All @@ -58,6 +63,7 @@ export const ip2LocationLookup = async (addr: string): Promise<Ip2LocationBundle

return {
location,
isHosting: result.usage_type ? HOSTING_USAGE_TYPES.includes(result.usage_type) : undefined,
isProxy: result.is_proxy ?? false,
};
};
15 changes: 10 additions & 5 deletions test/mocks/nock-geoip.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"longitude": -96.8067,
"asn": "20001",
"as": "The Constant Company LLC",
"is_proxy": false
"is_proxy": false,
"usage_type": "DCH"
},
"argentina": {
"country_code": "AR",
Expand All @@ -28,7 +29,8 @@
"longitude": -74.0060,
"asn": "80001",
"as": "The Constant Company LLC",
"is_proxy": false
"is_proxy": false,
"usage_type": "CDN"
},
"washington": {
"country_code": "US",
Expand All @@ -37,7 +39,8 @@
"latitude": 38.89539,
"longitude": -77.039476,
"asn": "40676",
"as": "Psychz Networks"
"as": "Psychz Networks",
"usage_type": "ISP"
},
"falkenstein": {
"country_code": "DE",
Expand Down Expand Up @@ -102,7 +105,8 @@
"longitude": -96.8067,
"asn": "20001",
"as": "The Constant Company LLC",
"is_proxy": true
"is_proxy": true,
"usage_type": "DCH"
},
"noVpn": {
"country_code": "US",
Expand All @@ -111,7 +115,8 @@
"latitude": 32.7831,
"longitude": -96.8067,
"asn": "20001",
"as": "The Constant Company LLC"
"as": "The Constant Company LLC",
"usage_type": "DCH"
}
},
"ipmap": {
Expand Down
6 changes: 3 additions & 3 deletions test/tests/unit/geoip/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ describe('geoip service', () => {
longitude: -58.3772,
network: 'InterBS S.R.L. (BAEHOST)',
normalizedNetwork: 'interbs s.r.l. (baehost)',
isHosting: undefined,
isHosting: true,
});
});

Expand Down Expand Up @@ -427,7 +427,7 @@ describe('geoip service', () => {
longitude: -77.039476,
network: 'Psychz Networks',
normalizedNetwork: 'psychz networks',
isHosting: undefined,
isHosting: true,
});

nockGeoIpProviders({ ip2location: 'empty', ipmap: 'empty', maxmind: 'empty', ipinfo: 'washington', fastly: 'empty' });
Expand All @@ -446,7 +446,7 @@ describe('geoip service', () => {
longitude: -77.039476,
network: 'Verizon Business',
normalizedNetwork: 'verizon business',
isHosting: false,
isHosting: undefined,
});
});

Expand Down