Skip to content

Commit

Permalink
feat: shorten adoption ipv6 (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh authored and MartinKolarik committed Aug 9, 2024
1 parent 31f8af3 commit 8997d5b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/extensions/endpoints/adoption-code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@directus/errors": "^0.0.2",
"@isaacs/ttlcache": "^1.4.1",
"axios": "^1.5.1",
"ipaddr.js": "^2.1.0",
"joi": "^17.10.2",
"rate-limiter-flexible": "^3.0.0"
}
Expand Down
9 changes: 8 additions & 1 deletion src/extensions/endpoints/adoption-code/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { RateLimiterMemory } from 'rate-limiter-flexible';
import { createError, isDirectusError } from '@directus/errors';
import { defineEndpoint } from '@directus/extensions-sdk';
import Joi from 'joi';
import ipaddr from 'ipaddr.js';
import { createAdoptedProbe, findAdoptedProbe } from './repositories/directus.js';
import type { EndpointExtensionContext } from '@directus/extensions';

Expand Down Expand Up @@ -84,7 +85,13 @@ export default defineEndpoint((router, context) => {
}

const userId = value.accountability.user;
const ip = value.body.ip as string;
let ip: string;

try {
ip = ipaddr.parse(value.body.ip).toString();
} catch (err) {
throw new (createError('INVALID_PAYLOAD_ERROR', 'Probe ip format is wrong', 400))();
}

await rateLimiter.consume(userId, 1).catch(() => { throw new TooManyRequestsError(); });

Expand Down
70 changes: 70 additions & 0 deletions src/extensions/endpoints/adoption-code/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,76 @@ describe('adoption code endpoints', () => {
expect(resSend.args[0]).to.deep.equal([ 'Code was sent to the probe.' ]);
});

it('should accept full IPv6 ip, generate code and send it to globalping api', async () => {
endpoint(router, endpointContext);
const req = {
accountability: {
user: 'f3115997-31d1-4cf5-8b41-0617a99c5706',
},
body: {
ip: '2a04:4e42:0200:0000:0000:0000:0000:0485',
},
};
nock('https://api.globalping.io').post('/v1/adoption-code?systemkey=system', (body) => {
expect(body.ip).to.equal('2a04:4e42:200::485');
expect(body.code.length).to.equal(6);
return true;
}).reply(200, {
uuid: '35cadbfd-2079-4b1f-a4e6-5d220035132a',
version: '0.26.0',
nodeVersion: '18.17.0',
hardwareDevice: null,
status: 'ready',
city: 'Paris',
country: 'FR',
latitude: 48.8534,
longitude: 2.3488,
asn: 12876,
network: 'SCALEWAY S.A.S.',
});

await request('/send-code', req, res);

expect(nock.isDone()).to.equal(true);
expect(resSend.callCount).to.equal(1);
expect(resSend.args[0]).to.deep.equal([ 'Code was sent to the probe.' ]);
});

it('should accept short IPv6 ip, generate code and send it to globalping api', async () => {
endpoint(router, endpointContext);
const req = {
accountability: {
user: 'f3115997-31d1-4cf5-8b41-0617a99c5706',
},
body: {
ip: '2a04:4e42:200::485',
},
};
nock('https://api.globalping.io').post('/v1/adoption-code?systemkey=system', (body) => {
expect(body.ip).to.equal('2a04:4e42:200::485');
expect(body.code.length).to.equal(6);
return true;
}).reply(200, {
uuid: '35cadbfd-2079-4b1f-a4e6-5d220035132a',
version: '0.26.0',
nodeVersion: '18.17.0',
hardwareDevice: null,
status: 'ready',
city: 'Paris',
country: 'FR',
latitude: 48.8534,
longitude: 2.3488,
asn: 12876,
network: 'SCALEWAY S.A.S.',
});

await request('/send-code', req, res);

expect(nock.isDone()).to.equal(true);
expect(resSend.callCount).to.equal(1);
expect(resSend.args[0]).to.deep.equal([ 'Code was sent to the probe.' ]);
});

it('should reject non authorized requests', async () => {
endpoint(router, endpointContext);
const req = {
Expand Down

0 comments on commit 8997d5b

Please sign in to comment.