Skip to content

Commit

Permalink
feat: add network tags
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKolarik committed Oct 6, 2023
1 parent 03edd29 commit 0361f23
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 22 deletions.
23 changes: 17 additions & 6 deletions public/v1/components/examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ components:
"longitude": -77.4875
},
"tags": [
"aws-us-east-1"
"aws-us-east-1",
"datacenter-network"
],
"resolvers": [
"private"
Expand Down Expand Up @@ -106,7 +107,9 @@ components:
"longitude": 174.76667,
"latitude": -36.86667,
"network": "Zappie Host LLC",
"tags": [ ],
"tags": [
"datacenter-network"
],
"resolvers": [
"1.1.1.1",
"8.8.8.8"
Expand Down Expand Up @@ -146,7 +149,9 @@ components:
"longitude": 151.207052,
"latitude": -33.86778,
"network": "OVH SAS",
"tags": [ ],
"tags": [
"datacenter-network"
],
"resolvers": [
"213.186.33.99"
]
Expand Down Expand Up @@ -195,7 +200,9 @@ components:
"longitude": 151.207052,
"latitude": -33.86778,
"network": "G-Core Labs S.A.",
"tags": [ ],
"tags": [
"datacenter-network"
],
"resolvers": [
"8.8.8.8"
]
Expand Down Expand Up @@ -352,7 +359,9 @@ components:
"longitude": 28.04355,
"latitude": -26.20225,
"network": "G-Core Labs S.A.",
"tags": [ ],
"tags": [
"datacenter-network"
],
"resolvers": [
"private"
]
Expand Down Expand Up @@ -417,7 +426,9 @@ components:
"longitude": 126.977207,
"latitude": 37.566309,
"network": "Psychz Networks",
"tags": [ ],
"tags": [
"datacenter-network"
],
"resolvers": [
"8.8.8.8",
"8.8.4.4"
Expand Down
1 change: 1 addition & 0 deletions public/v1/components/schemas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ components:
An array of additional values that can be used to target the probe.
Probes hosted in [AWS](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html#concepts-available-regions)
and [Google Cloud](https://cloud.google.com/compute/docs/regions-zones#available) are automatically assigned the service region code.
Probes are also automatically assigned `datacenter-network` and `eyeball-network` tags to distinguish between datacenter and end-user locations.
items:
type: string
TestResult:
Expand Down
3 changes: 3 additions & 0 deletions src/lib/geoip/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default class GeoipClient {
constructor (private readonly cache: CacheInterface) {}

async lookup (addr: string): Promise<ProbeLocation> {
let isHosting = undefined;
const results = await Promise
.allSettled([
this.lookupWithCache<LocationInfo>(`geoip:ipinfo:${addr}`, async () => ipinfoLookup(addr)),
Expand All @@ -46,6 +47,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;
const fulfilled: (LocationInfoWithProvider | null)[] = [];

// Providers here are pushed in a desc prioritized order
Expand Down Expand Up @@ -92,6 +94,7 @@ export default class GeoipClient {
longitude: Number(match.longitude),
network: networkMatch.network,
normalizedNetwork: networkMatch.normalizedNetwork,
isHosting,
};
}

Expand Down
4 changes: 4 additions & 0 deletions src/lib/geoip/providers/ipinfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type IpinfoResponse = {
region: string | undefined;
org: string | undefined;
loc: string | undefined;
privacy?: {
hosting: boolean;
}
};

export const ipinfoLookup = async (addr: string): Promise<LocationInfo> => {
Expand All @@ -40,5 +43,6 @@ export const ipinfoLookup = async (addr: string): Promise<LocationInfo> => {
longitude: Number(lon),
network,
normalizedNetwork: normalizeNetworkName(network),
isHosting: result.privacy?.hosting,
};
};
16 changes: 14 additions & 2 deletions src/probe/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const buildProbe = async (socket: Socket): Promise<Probe> => {

const location = getLocation(ipInfo);

const tags = getTags(clientIp);
const tags = getTags(clientIp, ipInfo);

// Storing index as string[][] so every category will have it's exact position in the index array across all probes
const index = [
Expand Down Expand Up @@ -112,7 +112,7 @@ const getLocation = (ipInfo: ProbeLocation): ProbeLocation => ({
normalizedNetwork: ipInfo.normalizedNetwork,
});

const getTags = (clientIp: string) => {
const getTags = (clientIp: string, ipInfo: ProbeLocation) => {
const tags: Tag[] = [];
const cloudRegion = getRegion(clientIp);

Expand All @@ -123,5 +123,17 @@ const getTags = (clientIp: string) => {
});
}

if (ipInfo.isHosting === true) {
tags.push({
type: 'system',
value: 'datacenter-network',
});
} else if (ipInfo.isHosting === false) {
tags.push({
type: 'system',
value: 'eyeball-network',
});
}

return tags;
};
1 change: 1 addition & 0 deletions src/probe/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type ProbeLocation = {
state: string | undefined;
network: string;
normalizedNetwork: string;
isHosting?: boolean | undefined;
};

export type ProbeStats = {
Expand Down
15 changes: 12 additions & 3 deletions test/mocks/nock-geoip.json
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,10 @@
"region": "Texas",
"country": "US",
"loc": "32.7831,-96.8067",
"org": "AS20004 The Constant Company LLC"
"org": "AS20004 The Constant Company LLC",
"privacy": {
"hosting": true
}
},
"argentina": {
"city": "Buenos Aires",
Expand Down Expand Up @@ -363,14 +366,20 @@
"region": "New York",
"country": "US",
"loc": "40.7143,-74.0060",
"org": "AS80004 The Constant Company LLC"
"org": "AS80004 The Constant Company LLC",
"privacy": {
"hosting": true
}
},
"washington": {
"city": "Washington",
"region": "Washington, D.C.",
"country": "US",
"loc": "38.89539,-77.039476",
"org": "AS701 Verizon Business"
"org": "AS701 Verizon Business",
"privacy": {
"hosting": false
}
},
"emptyNetwork": {
"city": "Dallas",
Expand Down
10 changes: 5 additions & 5 deletions test/tests/integration/measurement/probe-communication.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe('Create measurement request', () => {
longitude: -96.8067,
latitude: 32.7831,
network: 'The Constant Company LLC',
tags: [ 'gcp-us-west4' ],
tags: [ 'gcp-us-west4', 'datacenter-network' ],
resolvers: [],
},
result: {
Expand Down Expand Up @@ -159,7 +159,7 @@ describe('Create measurement request', () => {
longitude: -96.8067,
latitude: 32.7831,
network: 'The Constant Company LLC',
tags: [ 'gcp-us-west4' ],
tags: [ 'gcp-us-west4', 'datacenter-network' ],
resolvers: [],
},
result: { status: 'in-progress', rawOutput: 'abc' },
Expand Down Expand Up @@ -200,7 +200,7 @@ describe('Create measurement request', () => {
longitude: -96.8067,
latitude: 32.7831,
network: 'The Constant Company LLC',
tags: [ 'gcp-us-west4' ],
tags: [ 'gcp-us-west4', 'datacenter-network' ],
resolvers: [],
},
result: { status: 'in-progress', rawOutput: 'abcdef' },
Expand Down Expand Up @@ -255,7 +255,7 @@ describe('Create measurement request', () => {
longitude: -96.8067,
latitude: 32.7831,
network: 'The Constant Company LLC',
tags: [ 'gcp-us-west4' ],
tags: [ 'gcp-us-west4', 'datacenter-network' ],
resolvers: [],
},
result: {
Expand Down Expand Up @@ -324,7 +324,7 @@ describe('Create measurement request', () => {
longitude: -96.8067,
network: 'The Constant Company LLC',
},
tags: [ 'gcp-us-west4' ],
tags: [ 'gcp-us-west4', 'datacenter-network' ],
resolvers: [],
host: '',
stats: {
Expand Down
4 changes: 2 additions & 2 deletions test/tests/integration/measurement/timeout-result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('Timeout results', () => {
longitude: -96.8067,
latitude: 32.7831,
network: 'The Constant Company LLC',
tags: [ 'gcp-us-west4' ],
tags: [ 'gcp-us-west4', 'datacenter-network' ],
resolvers: [],
},
result: { status: 'in-progress', rawOutput: '' },
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('Timeout results', () => {
longitude: -96.8067,
latitude: 32.7831,
network: 'The Constant Company LLC',
tags: [ 'gcp-us-west4' ],
tags: [ 'gcp-us-west4', 'datacenter-network' ],
resolvers: [],
},
result: { status: 'failed', rawOutput: '\n\nThe measurement timed out' },
Expand Down
27 changes: 23 additions & 4 deletions test/tests/integration/probes/get-probes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe('Get Probes', () => {
longitude: -96.8067,
network: 'The Constant Company LLC',
},
tags: [],
tags: [ 'datacenter-network' ],
resolvers: [],
},
]);
Expand All @@ -135,17 +135,20 @@ describe('Get Probes', () => {
});
});

it('should detect 3 probes in "ready: true" status', async () => {
it('should detect 4 probes in "ready: true" status', async () => {
nockGeoIpProviders({ ip2location: 'argentina', ipmap: 'argentina', maxmind: 'argentina', ipinfo: 'argentina', fastly: 'argentina' });
nockGeoIpProviders();
nockGeoIpProviders({ ip2location: 'newYork', ipmap: 'argentina', maxmind: 'newYork', ipinfo: 'newYork', fastly: 'newYork' });
nockGeoIpProviders({ ip2location: 'washington', ipmap: 'argentina', maxmind: 'default', ipinfo: 'washington', fastly: 'newYork' });

const probe1 = await addProbe();
const probe2 = await addProbe();
const probe3 = await addProbe();
const probe4 = await addProbe();
probe1.emit('probe:status:update', 'ready');
probe2.emit('probe:status:update', 'ready');
probe3.emit('probe:status:update', 'ready');
probe4.emit('probe:status:update', 'ready');

await requestAgent.get('/v1/probes')
.send()
Expand Down Expand Up @@ -180,7 +183,7 @@ describe('Get Probes', () => {
longitude: -96.8067,
network: 'The Constant Company LLC',
},
tags: [],
tags: [ 'datacenter-network' ],
resolvers: [],
},
{
Expand All @@ -196,7 +199,23 @@ describe('Get Probes', () => {
longitude: -74.0060,
network: 'The Constant Company LLC',
},
tags: [],
tags: [ 'datacenter-network' ],
resolvers: [],
},
{
version: '0.14.0',
location: {
asn: 701,
city: 'Washington',
continent: 'NA',
country: 'US',
latitude: 38.89539,
longitude: -77.039476,
network: 'Verizon Business',
region: 'Northern America',
state: 'DC',
},
tags: [ 'eyeball-network' ],
resolvers: [],
},
]);
Expand Down
Loading

0 comments on commit 0361f23

Please sign in to comment.