File tree Expand file tree Collapse file tree 3 files changed +14
-15
lines changed
Expand file tree Collapse file tree 3 files changed +14
-15
lines changed Original file line number Diff line number Diff line change 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 ,
Original file line number Diff line number Diff line change 11type 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
47function 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}
2125function 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+
114115function cidrRange ( cidr46 : string ) : IPRange {
115116 const v6 = isV6 ( cidr46 ) ;
116117
Original file line number Diff line number Diff line change 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+
26test ( 'ipv6 to 32 character string' , convertAddr6 ( 'abcd::efff' ) ) . equal (
37 'abcd000000000000000000000000efff'
48) ;
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
2220test ( 'compare' , convertAddr6 ( 'abcd:1234:1::2:127.0.0.1' ) ) . bigger (
2321 convertAddr6 ( 'abcd:1234:1::' )
You can’t perform that action at this time.
0 commit comments