Skip to content

Commit 96f98e0

Browse files
author
Student Main
committed
v0.1.1
1 parent adae5f0 commit 96f98e0

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pacfw",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "A simple PAC",
55
"main": "dist/index.js",
66
"private": true,

src/ip.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
type IPRange = [string, string];
2-
2+
const zero32 = '00000000000000000000000000000000';
3+
const ffff32 = 'ffffffffffffffffffffffffffffffff';
4+
const zero8 = '00000000';
5+
const ffff8 = 'ffffffff';
36
// check a ip string or unified address is ipv6
47
function isV6(s: string): boolean {
58
// 255.255.255.255/32 , 18
@@ -16,7 +19,8 @@ function convertAddr4(ip4: string): string {
1619
(parseInt(m[1], 10) << 16) +
1720
(parseInt(m[2], 10) << 8) +
1821
parseInt(m[3], 10);
19-
return ipnum.toString(16);
22+
const ret = ipnum.toString(16);
23+
return zero8.substr(ret.length) + ret;
2024
}
2125
function convertAddr6(ip6: string): string {
2226
const ipstr = ip6.split(':');
@@ -107,10 +111,7 @@ const hmask = [
107111
'33337777bbbbffff',
108112
'1133557799bbddff'
109113
];
110-
const zero32 = '00000000000000000000000000000000';
111-
const ffff32 = 'ffffffffffffffffffffffffffffffff';
112-
const zero8 = '00000000';
113-
const ffff8 = 'ffffffff';
114+
114115
function cidrRange(cidr46: string): IPRange {
115116
const v6 = isV6(cidr46);
116117

test/ip.test.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
///../src/ip.ts
2+
test('ipv4', convertAddr('127.0.0.1')).equal('7f000001');
3+
test('ipv4', convertAddr('10.11.12.13')).equal('0a0b0c0d');
4+
test('ipv4', convertAddr('255.1.3.99')).equal('ff010363');
5+
26
test('ipv6 to 32 character string', convertAddr6('abcd::efff')).equal(
37
'abcd000000000000000000000000efff'
48
);
@@ -10,14 +14,8 @@ test(
1014
convertAddr6('abcd:1234:1::2:127.0.0.1')
1115
).equal('abcd123400010000000000027f000001');
1216

13-
test('cidr6', cidr6Range('abcd:aaaa::1234:5678/24')).equal([
14-
'abcdaa00000000000000000000000000',
15-
'abcdaaffffffffffffffffffffffffff'
16-
]);
17-
test('cidr6', cidr6Range('abcd:aaaa::1234:5678/25')).equal([
18-
'abcdaa80000000000000000000000000',
19-
'abcdaaffffffffffffffffffffffffff'
20-
]);
17+
test('cidr4', cidrRange('10.0.0.1/8')).equal(['0a000000', '0affffff']);
18+
test('cidr4', cidrRange('172.16.100.64/22')).equal(['ac106400', 'ac1067ff']);
2119

2220
test('compare', convertAddr6('abcd:1234:1::2:127.0.0.1')).bigger(
2321
convertAddr6('abcd:1234:1::')

0 commit comments

Comments
 (0)