-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDomain2Ip.php
142 lines (113 loc) · 3.4 KB
/
Domain2Ip.php
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
<?php
require_once 'HostsFile.php';
class OPTION {
public static $DEBUG = false;
public static $HOSTS = 'C:\Windows\System32\drivers\etc\hosts';
}
class Domain2Ip {
private $driver;
private $hosts;
public function __construct($driver, $hosts) {
$this->driver = $driver;
$this->hosts = $hosts;
}
public function add_all_ips($start_domain, $pattern_add, $start_url = null) {
$this->add_ip($start_domain);
$this->hosts->save();
if (!$start_url) {
$start_url = 'http://'.$start_domain;
}
$raw = file_get_contents($start_url);
$pattern = '/https?:\/\/([\w\.]+)/';
if (preg_match_all($pattern, $raw, $m)) {
$addresses = array_unique($m[1]);
if (OPTION::$DEBUG) {
print_r($addresses);exit;//debug
}
foreach ($addresses as $domain) {
if (preg_match($pattern_add, $domain)) {
$this->add_ip($domain);
}
}
$this->hosts->save();
}
}
private function add_ip($domain) {
$ip = $this->driver->get_ip($domain);
$this->hosts->delete_domain($domain);
$this->hosts->add($ip, $domain);
echo "$ip $domain\n";
}
}
abstract class Ip_Driver_Abstract {
public function send_post($url, $data) {
if (strpos($url, 'http://') === false) {
$url = 'http://' . $url;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_TIMEOUT, 150);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$retry = 10;
$response = curl_exec($ch);
while (!$response && $retry--) {
$response = curl_exec($ch);
}
return $response;
}
public abstract function get_ip($domain);
}
class Ip_Driver_IpLookup extends Ip_Driver_Abstract {
public function get_ip($domain) {
$target = 'http://ip-lookup.net/domain.php';
$data = array('domain' => $domain);
$response = $this->send_post($target, $data);
$pattern = '/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})<.*title="Lookup.../';
if (preg_match($pattern, $response, $m)) {
return $m[1];
}
if (strpos($domain, 'www.') === 0) {
return $this->get_ip(substr($domain, 4));
}
throw new Exception('Cant find IP address:'.$domain);
}
}
function obtain_option($code, $default = '') {
global $argv;
for ($i=1, $n=count($argv); $i<$n; $i++) {
$part = "-{$code}=";
if (strpos($argv[$i], $part) === 0) {
return substr($argv[$i], strlen($part));
}
}
return $default;
}
if (count($argv) < 3) {
$help = <<<HELP
php Domain2Ip.php <DOMAIN> <PATTERN> [-s=<START_URL>] [-d=<0/1>] [-h=<HOST_FILE>]
Options:
<DOMAIN> : the first domain to be added to hosts file so we can
open <START_URL>
<PATTERN> : regex to match which domain to be added
-s=<START_URL>: by default <DOMAIN> will be used to grab the other domains
-d=<0/1> : if 1 then all domain candidates will be printed out but not
added
-h=<HOST_FILE>: where the hosts file is located
HELP;
echo $help;
} else {
$domain = $argv[1];
$pattern = $argv[2];
$start_url = obtain_option('s', null);
OPTION::$DEBUG = (bool)obtain_option('d', false);
OPTION::$HOSTS = obtain_option('h', OPTION::$HOSTS);
$driver = new Ip_Driver_IpLookup();
$hosts = new HostsFile(OPTION::$HOSTS);
$d2ip = new Domain2Ip($driver, $hosts);
$d2ip->add_all_ips($domain, $pattern, $start_url);
}