@ucd-lib/theme-elements 0.0.15 → 0.0.16

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.
Files changed (2) hide show
  1. package/package.json +2 -1
  2. package/utils/ip-check.js +133 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ucd-lib/theme-elements",
3
- "version": "0.0.15",
3
+ "version": "0.0.16",
4
4
  "description": "Custom elements for the UCD brand theme",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -10,6 +10,7 @@
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "@ucd-lib/theme-sass": "^5.0.15",
13
+ "ip-cidr": "^3.0.4",
13
14
  "lit": "^2.0.2",
14
15
  "photoswipe": "^4.1.3",
15
16
  "slim-select": "^1.26.2"
@@ -0,0 +1,133 @@
1
+ const IPCIDR = require("ip-cidr");
2
+
3
+
4
+ /**
5
+ * @class IpDetect
6
+ * @classdesc Utility for detecting type of IP and if it is in campus ip list and vpn ips
7
+ */
8
+ export class IpDetect {
9
+ constructor(){
10
+ this.ip = '';
11
+ this.hasIP6 = false;
12
+ this.ip6 = '';
13
+ this.ipVPNRange = [
14
+ // VPN
15
+ {"setIP":"128.120.234.0", "mask":"24"},
16
+ {"setIP":"128.120.235.0", "mask":"24"},
17
+ {"setIP":"128.120.236.0", "mask":"24"},
18
+ {"setIP":"128.120.237.0", "mask":"24"},
19
+ {"setIP":"128.120.238.0", "mask":"24"},
20
+ {"setIP":"128.120.239.0", "mask":"24"},
21
+ {"setIP":"128.120.251.0", "mask":"24"},
22
+ {"setIP":"169.237.45.0", "mask":"24"},
23
+ // Staff VPN
24
+ {"setIP":"169.237.240.2", "mask":"32"},
25
+ {"setIP":"172.19.19.0", "mask":"24"},
26
+ {"setIP":"172.19.19.64", "mask":"26"}
27
+ ];
28
+
29
+ this.campusRange = [
30
+ {"setIP":"128.120.0.0", "mask":"16"},
31
+ {"setIP":"152.79.0.0", "mask":"16"},
32
+ {"setIP":"169.237.0.0", "mask":"16"},
33
+ {"setIP":"162.251.203.0", "mask":"27"},
34
+ {"setIP":"12.235.42.0", "mask":"25"},
35
+ {"setIP":"168.150.0.0", "mask":"17"},
36
+
37
+ ];
38
+
39
+ this.isVPN = false;
40
+ }
41
+ /**
42
+ * @method runIP
43
+ * @description This runs the ip check services
44
+ *
45
+ */
46
+ async runIP(){
47
+ this.isOnCampus = await this.verifyCidr(this.campusRange, this.ipVPNRange);
48
+ }
49
+
50
+ /**
51
+ * @method checkIp
52
+ * @description This retrieves the host's ip address
53
+ *
54
+ */
55
+ async checkIp(){
56
+ let ips;
57
+ await fetch('https://api64.ipify.org?format=json', )
58
+ .then(response => response.json())
59
+ .then(data => {ips = data; });
60
+ this.ip = ips["ip"];
61
+
62
+ if (this.ip.includes(":")){
63
+ this.hasIP6 = true;
64
+ this.ip6 = this.ip;
65
+ await fetch('https://api.ipify.org?format=json', )
66
+ .then(response => response.json())
67
+ .then(data => {ips = data; });
68
+ this.ip = ips["ip"];
69
+ }
70
+
71
+ }
72
+
73
+ /**
74
+ * @method isInRange
75
+ * @description verify if the ip is in the range given
76
+ *
77
+ * @param {Array} range
78
+ *
79
+ * @returns {Boolean}
80
+ */
81
+ checkRange(range){
82
+ if (range.includes(this.ip))
83
+ return true;
84
+ return false;
85
+ }
86
+
87
+ /**
88
+ * @method cidrCheck
89
+ * @description cidr check that takes a range
90
+ *
91
+ * @param {Object} identifier
92
+ *
93
+ * @returns {Array}
94
+ */
95
+ cidrCheck(identifier){
96
+ let cidr;
97
+
98
+ let currentIP = identifier["setIP"].concat('/', identifier["mask"] );
99
+ cidr = new IPCIDR(currentIP);
100
+ let ipRange = cidr.toArray();
101
+
102
+ return ipRange;
103
+
104
+ }
105
+
106
+ /**
107
+ * @method verifyCidr
108
+ * @description verify req ip addressess in cidr array
109
+ *
110
+ * @param {Array} campusAddress
111
+ * @param {Array} vpnaddress
112
+ *
113
+ * @returns {Boolean}
114
+ */
115
+ async verifyCidr(campusAddress, vpnaddress) {
116
+ await this.checkIp();
117
+ for (let i of vpnaddress){
118
+ let vpn_range = this.cidrCheck(i);
119
+ if(this.checkRange(vpn_range))
120
+ this.isVPN = true;
121
+ }
122
+
123
+ for (let i of campusAddress){
124
+ let campus_range = this.cidrCheck(i);
125
+ if(this.checkRange(campus_range))
126
+ return this.ip;
127
+ }
128
+ return null;
129
+ }
130
+
131
+
132
+ }
133
+