-
Notifications
You must be signed in to change notification settings - Fork 78
/
index.coffee
164 lines (130 loc) · 3.53 KB
/
index.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
_ = require 'underscore'
net = require 'net'
SocksClient = require('socks').SocksClient
url = require 'url'
punycode = require 'punycode/'
util = require 'util'
@SERVERS = require './servers.json'
cleanParsingErrors = (string) =>
return string.replace(/^[:\s]+/, '').replace(/^https?[:\/]+/, '') || string
@lookup = (addr, options, done) =>
if typeof done is 'undefined' and typeof options is 'function'
done = options
options = {}
if addr == '__proto__'
done new Error 'lookup: __proto__ is not allowed to lookup'
return
_.defaults options,
follow: 2
timeout: 60000 # 60 seconds in ms
done = _.once done
server = options.server
proxy = options.proxy
timeout = options.timeout
if not server
switch true
when _.contains addr, '@'
done new Error 'lookup: email addresses not supported'
return
when net.isIP(addr) isnt 0
server = @SERVERS['_']['ip']
else
tld = url.domainToASCII(addr)
while true
server = @SERVERS[tld]
if not tld or server
break
tld = tld.replace /^.+?(\.|$)/, ''
if not server
done new Error 'lookup: no whois server is known for this kind of object'
return
if typeof server is 'string'
parts = server.split ':'
server =
host: parts[0]
port: parts[1]
if typeof proxy is 'string'
parts = proxy.split ':'
proxy =
ipaddress: parts[0]
port: parseInt parts[1]
_.defaults server,
port: 43
query: "$addr\r\n"
if proxy
_.defaults proxy,
type: 5
_lookup = (socket, done) =>
idn = addr
if server.punycode isnt false and options.punycode isnt false
idn = url.domainToASCII(addr)
if options.encoding
socket.setEncoding options.encoding
socket.write server.query.replace '$addr', idn
data = ''
socket.on 'data', (chunk) =>
data += chunk
socket.on 'timeout', =>
socket.destroy()
done new Error 'lookup: timeout'
socket.on 'error', (err) =>
done err
socket.on 'close', (err) =>
if options.follow > 0
match = data.replace(/\r/gm, '').match /(ReferralServer|Registrar Whois|Whois Server|WHOIS Server|Registrar WHOIS Server|refer):[^\S\n]*((?:r?whois|https?):\/\/)?([0-9A-Za-z\.\-_]*)/
if match? and match[3] != server.host
options = _.extend {}, options,
follow: options.follow - 1
server: match[3].trim()
options.server = cleanParsingErrors(options.server)
@lookup addr, options, (err, parts) =>
if err?
if options.verbose
return done err, [
server: if ('object' == typeof server) then server.host.trim() else server.trim()
data: data
]
return done err
if options.verbose
done null, [
server: if ('object' == typeof server) then server.host.trim() else server.trim()
data: data
].concat(parts)
else
done null, parts
return
if options.verbose
done null, [
server: if ('object' == typeof server) then server.host.trim() else server.trim()
data: data
]
else
done null, data
if !Number.isInteger(server.port)
server.port = 43
if proxy
SocksClient.createConnection
proxy: proxy
destination:
host: server.host
port: server.port
command: 'connect'
timeout: timeout
, (err, info) =>
if err?
return done err
{socket} = info
if timeout
socket.setTimeout timeout
_lookup socket, done
socket.resume()
else
sockOpts =
host: server.host
port: server.port
if options.bind
sockOpts.localAddress = options.bind
socket = net.connect sockOpts
if timeout
socket.setTimeout timeout
_lookup socket, done