@mytmpvpn/mytmpvpn-common 2.0.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -31,4 +31,5 @@ interface Vpn {
31
31
  declare function getVpnConfigTypes(): VpnType[];
32
32
  declare function newVpn(region: string, config: VpnConfig, state: VpnState): Vpn;
33
33
  declare function getVpnFrom(vpnId: string, state: VpnState, config: VpnConfig): Vpn;
34
- export { VpnState, type VpnMetrics, VpnType, type VpnConfig, type Vpn, rank, newVpn, getVpnFrom, getVpnConfigTypes };
34
+ declare function vpnIdToWgFileName(vpnId: string): string;
35
+ export { VpnState, type VpnMetrics, VpnType, type VpnConfig, type Vpn, rank, newVpn, getVpnFrom, getVpnConfigTypes, vpnIdToWgFileName };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getVpnConfigTypes = exports.getVpnFrom = exports.newVpn = exports.rank = exports.VpnType = exports.VpnState = void 0;
3
+ exports.vpnIdToWgFileName = exports.getVpnConfigTypes = exports.getVpnFrom = exports.newVpn = exports.rank = exports.VpnType = exports.VpnState = void 0;
4
4
  var VpnState;
5
5
  (function (VpnState) {
6
6
  VpnState["Failed"] = "Failed";
@@ -29,10 +29,21 @@ function getVpnConfigTypes() {
29
29
  });
30
30
  }
31
31
  exports.getVpnConfigTypes = getVpnConfigTypes;
32
+ function throwIfNotValidVpnId(vpnId) {
33
+ // e.g: YYYYMMDDHHmmssmss@az-test-7
34
+ if (!/\d{4}[01]\d[0-3]\d[0-2]\d[0-5]\d[0-5]\d\d{3}@[a-z]{2}-[a-z]+-\d/.test(vpnId)) {
35
+ throw new Error(`Incorrect vpnId: \'${vpnId}\'`);
36
+ }
37
+ }
32
38
  function newVpn(region, config, state) {
39
+ // e.g: ap-northeast-3
40
+ if (!/[a-z]{2}-[a-z]+-[1-9]+/.test(region)) {
41
+ throw new Error(`Incorrect region: \'${region}\'`);
42
+ }
33
43
  const createdAt = new Date();
34
- // Replace ':' in the id as it's forbiden in DDB
35
44
  const vpnId = `${dateToId(createdAt)}@${region}`;
45
+ // Assert we will be able to deserialize later on...
46
+ throwIfNotValidVpnId(vpnId);
36
47
  return {
37
48
  vpnId,
38
49
  createdAt,
@@ -43,9 +54,10 @@ function newVpn(region, config, state) {
43
54
  }
44
55
  exports.newVpn = newVpn;
45
56
  function getVpnFrom(vpnId, state, config) {
57
+ throwIfNotValidVpnId(vpnId);
46
58
  const tokens = vpnId.split('@');
47
59
  if (tokens.length !== 2) {
48
- throw new Error(`Bad vpnId format: ${vpnId}`);
60
+ throw new Error(`Incorrect vpnId: ${vpnId}`);
49
61
  }
50
62
  /* eslint-disable @typescript-eslint/no-non-null-assertion */
51
63
  const createdAt = idToDate(tokens[0]);
@@ -79,3 +91,60 @@ function idToDate(id) {
79
91
  const millis = id.substring(14);
80
92
  return new Date(`${year}-${month}-${day}T${hour}:${minute}:${second}.${millis}Z`);
81
93
  }
94
+ // The WireGuard Mobile Application has requirements for the wireguard configuration file:
95
+ // 1. must have extension '.conf'
96
+ // 2. file name must match the following regexp: "[a-zA-Z0-9_=+.-]{1,15}")
97
+ // https://git.zx2c4.com/wireguard-android/tree/tunnel/src/main/java/com/wireguard/android/backend/Tunnel.java#n19
98
+ // This function is to transform from vpnIn to Wireguard mobile application requirements
99
+ function vpnIdToWgFileName(vpnId) {
100
+ throwIfNotValidVpnId(vpnId);
101
+ // The vpnId is made of YYYYMMDDHHmmssmss@region
102
+ // Let's keep it simple: vpn is mostly temporary, so keeping
103
+ // the year is useless.
104
+ // Also the milliseconds are useless.
105
+ // Finally, the region itself is of the following format: ap-northeast-3
106
+ // We can compressing with something like:
107
+ // MMDDHHmmssapne3.conf
108
+ const ddhhmmss = vpnId.substring(4, 14);
109
+ const seps = vpnId.substring(18).split(/-/);
110
+ const ccld = seps[0];
111
+ let location;
112
+ switch (seps[1]) {
113
+ case 'north':
114
+ location = 'n';
115
+ break;
116
+ case 'northeast':
117
+ location = 'ne';
118
+ break;
119
+ case 'east':
120
+ location = 'e';
121
+ break;
122
+ case 'southeast':
123
+ location = 'se';
124
+ break;
125
+ case 'south':
126
+ location = 's';
127
+ break;
128
+ case 'southwest':
129
+ location = 'sw';
130
+ break;
131
+ case 'west':
132
+ location = 'w';
133
+ break;
134
+ case 'northwest':
135
+ location = 'nw';
136
+ break;
137
+ case 'central':
138
+ location = 'c';
139
+ break;
140
+ default:
141
+ '';
142
+ break;
143
+ }
144
+ const number = seps[2];
145
+ // Let's make sure the end file is of 15 char max
146
+ const result = `${ddhhmmss}${ccld}${location}${number}`.substring(0, 15);
147
+ console.log(`Transforming ${vpnId} to ${result}`);
148
+ return result;
149
+ }
150
+ exports.vpnIdToWgFileName = vpnIdToWgFileName;
@@ -11,3 +11,55 @@ describe('Testing VpnState', () => {
11
11
  expect((0, vpn_1.rank)(vpn_1.VpnState.Deprovisioning) <= (0, vpn_1.rank)(vpn_1.VpnState.Deleted)).toBe(true);
12
12
  });
13
13
  });
14
+ describe('Testing Vpn constructors', () => {
15
+ it('Should shout when regions is empty', async () => {
16
+ expect(() => (0, vpn_1.newVpn)('', { maxPeanuts: -1, type: vpn_1.VpnType.WireGuard }, vpn_1.VpnState.Created))
17
+ .toThrow(new Error("Incorrect region: \'\'"));
18
+ });
19
+ it('Should shout when regions is bad format', async () => {
20
+ expect(() => (0, vpn_1.newVpn)('foo', { maxPeanuts: -1, type: vpn_1.VpnType.WireGuard }, vpn_1.VpnState.Created))
21
+ .toThrow(new Error("Incorrect region: \'foo\'"));
22
+ });
23
+ it('Should create vpn with all fields set', async () => {
24
+ const vpn = (0, vpn_1.newVpn)('az-test-7', { maxPeanuts: -1, type: vpn_1.VpnType.WireGuard }, vpn_1.VpnState.Created);
25
+ expect(vpn.createdAt).toBeDefined();
26
+ expect(vpn.region).toEqual('az-test-7');
27
+ expect(vpn.state).toEqual(vpn_1.VpnState.Created);
28
+ expect(vpn.config).toEqual({ maxPeanuts: -1, type: vpn_1.VpnType.WireGuard });
29
+ expect(vpn.vpnId).toBeDefined();
30
+ });
31
+ it('Should deserialize a vpn with all fields set', async () => {
32
+ const vpn = (0, vpn_1.getVpnFrom)('20030902012345678@az-test-7', vpn_1.VpnState.Created, { maxPeanuts: -1, type: vpn_1.VpnType.WireGuard });
33
+ expect(vpn.createdAt).toEqual(new Date('2003-09-02T01:23:45.678Z'));
34
+ expect(vpn.region).toEqual('az-test-7');
35
+ expect(vpn.state).toEqual(vpn_1.VpnState.Created);
36
+ expect(vpn.config).toEqual({ maxPeanuts: -1, type: vpn_1.VpnType.WireGuard });
37
+ expect(vpn.vpnId).toBeDefined();
38
+ });
39
+ it('Should shout when vpnId is empty', async () => {
40
+ expect(() => (0, vpn_1.getVpnFrom)('', vpn_1.VpnState.Created, { maxPeanuts: -1, type: vpn_1.VpnType.WireGuard }))
41
+ .toThrow(new Error("Incorrect vpnId: \'\'"));
42
+ });
43
+ it('Should shout when vpnId has bad format', async () => {
44
+ expect(() => (0, vpn_1.getVpnFrom)('foo', vpn_1.VpnState.Created, { maxPeanuts: -1, type: vpn_1.VpnType.WireGuard }))
45
+ .toThrow(new Error("Incorrect vpnId: \'foo\'"));
46
+ });
47
+ it('Should shout when vpnId does not hold a region in good format', async () => {
48
+ expect(() => (0, vpn_1.getVpnFrom)('20030902012345678@test', vpn_1.VpnState.Created, { maxPeanuts: -1, type: vpn_1.VpnType.WireGuard }))
49
+ .toThrow(new Error("Incorrect vpnId: \'20030902012345678@test\'"));
50
+ });
51
+ });
52
+ describe('Testing vpnId to Wireguard file name functions ', () => {
53
+ it('Should shout when vpnId is empty', async () => {
54
+ expect(() => (0, vpn_1.vpnIdToWgFileName)(''))
55
+ .toThrow(new Error("Incorrect vpnId: \'\'"));
56
+ });
57
+ it('Should shout when vpnId is bad format', async () => {
58
+ expect(() => (0, vpn_1.vpnIdToWgFileName)('foo'))
59
+ .toThrow(new Error("Incorrect vpnId: \'foo\'"));
60
+ });
61
+ it('Should shout when region has bad format', async () => {
62
+ expect(() => (0, vpn_1.vpnIdToWgFileName)('20030902012345678@test'))
63
+ .toThrow(new Error("Incorrect vpnId: \'20030902012345678@test\'"));
64
+ });
65
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mytmpvpn/mytmpvpn-common",
3
- "version": "2.0.0",
3
+ "version": "2.1.1",
4
4
  "description": "Common library for all MyTmpVpn related projects",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [