forked from qwj418/Flora_Pac
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhash_ip.coffee
134 lines (106 loc) · 2.94 KB
/
hash_ip.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env coffee
# Faked test stubs to simulate PAC running env
isInNet = (ipaddr, pattern, maskstr) ->
host = convert_addr(ipaddr)
pat = convert_addr(pattern)
mask = convert_addr(maskstr)
(host & mask) is (pat & mask)
myIpAddress = ->
"192.168.0.2"
HASH_BASE = 3
MASK_STEP = 2
min_prefixlen = 10
max_prefixlen = 24
### LOCAL_IP_BALANCE_BEGIN ###
local_ip_balance = (proxies) ->
myseg = parseInt(myIpAddress().split(".")[3])
l = proxies.length
k = myseg % l
s = ''
s += proxies[(k+i) % l] for i in [0...l]
s
### LOCAL_IP_BALANCE_END ###
### TARGET_HOST_BALANCE_BEGIN ###
target_host_balance = (proxies, host) ->
hash_string = (s) ->
hash = 0
for c in s
hash = (hash << 5) - hash + c.charCodeAt(0)
hash = hash & hash & 0xFFFF
hash &= 0xFFFF
hash
l = proxies.length
console.log hash_string(host)
k = hash_string(host) % l
s = ''
s += proxies[(k+i) % l] for i in [0...l]
s
### TARGET_HOST_BALANCE_END ###
### FLORA_BEGIN ###
convert_addr = (ipchars) ->
bytes = ipchars.split(".")
result = ((bytes[0] & 0xff) << 24) | ((bytes[1] & 0xff) << 16) \
| ((bytes[2] & 0xff) << 8) | (bytes[3] & 0xff)
result
dot2num = (dot) ->
d = dot.split(".")
# ((((((+d[0]) << 8) | (+d[1])) << 8) | (+d[2])) << 8) | (+d[3])
((((((+d[0])*256)+(+d[1]))*256)+(+d[2]))*256)+(+d[3])
num2dot = (ip) ->
[ip >>> 24, ip >>> 16 & 0xFF, ip >>> 8 & 0xFF, ip & 0xFF].join "."
hash_masked_ip = (ip, mask_len, mod_base) ->
offset = 32 - mask_len
net = ip >>> offset
net *= 2 for i in [0...offset]
# console.log net
# console.log mod_base
return net % mod_base
prefixlen2mask = (prefixlen) ->
imask = 0xFFFFFFFF << (32 - prefixlen)
(imask >> 24 & 0xFF) + '.' + (imask >> 16 & 0xFF) + '.' + \
(imask >> 8 & 0xFF) + '.' + (imask & 0xFF)
rebuild_net = (pair) ->
result = ['', '']
result[0] = num2dot pair[0] << (32 - pair[1])
result[1] = prefixlen2mask(pair[1])
return result
lookup_ip = (ip) ->
len = min_prefixlen
n_ip = dot2num(ip)
# console.log n_ip
while len <= max_prefixlen
# console.log len
k = hash_masked_ip(n_ip, len, HASH_BASE)
# console.log k
for i in hashed_nets[k]
n = rebuild_net i
return true if isInNet ip, n[0], n[1]
# console.log n
len += MASK_STEP
false
### FLORA_END ###
hashed_nets = [
[
[13248647, 24],
[828605, 20],
[13305856, 24],
],
[
[3313729, 22],
],
[
[173367, 20],
],
]
console.log dot2num("192.168.1.23")
console.log hash_masked_ip(dot2num("192.168.1.23"), 24, 1009)
console.log hash_masked_ip(dot2num("192.168.1.3"), 24, 1009)
for a in hashed_nets
console.log a
for i in a
console.log i
console.log rebuild_net(i)
console.log lookup_ip "202.65.4.0"
console.log lookup_ip "203.208.37.20"
console.log local_ip_balance ['S0', 'S1', 'S2']
console.log target_host_balance ['S0', 'S1', 'S2'], "google.com"