forked from clowwindy/shadowsocks-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
domainRegex.rb
49 lines (41 loc) · 1008 Bytes
/
domainRegex.rb
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
#!/usr/bin/ruby
$TopDomain = ['com','co','edu','gov','net','org','mil','info','name','xxx','mobi','tel','post','biz','pro']
$domainRegex = nil
def getDomainRegex()
if $domainRegex != nil then return $domainRegex end
countryDomain = []
cfg_file = File.open('country_domain.txt')
cfg_file.each do |line|
countryDomain.push(line.strip.downcase)
end
cfg_file.close
domainAll = $TopDomain+countryDomain
domainString = "("
i = 0
domainAll.each { |x|
if i>0 then domainString += "|" end
domainString += "#{x}"
i += 1
}
domainString += ")"
$domainRegex = /([\w-]+)\.#{domainString}\.?#{domainString}?$/
end
def getIP(host)
num = /\d|[01]?\d\d|2[0-4]\d|25[0-5]/
ip_regex = /^(#{num}\.){3}#{num}$/
ret = ip_regex.match(host)
end
def getHostBase(host)
ret = getIP(host)
if ret != nil
return ret[0]
end
host_regex = getDomainRegex()
ret = host_regex.match(host)
if ret != nil
host_base = ret[0]
else
host_base = nil
end
host_base
end