-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathbuild.php
134 lines (117 loc) · 3.25 KB
/
build.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
<?php
/**
* Author: rehiy <https://www.rehiy.com>
*/
chdir(__DIR__);
date_default_timezone_set('Asia/Shanghai');
is_dir('./data') || mkdir('./data');
////////////////////////////////////////////////////
//开始时间
$time1 = time();
echo "获取APNIC数据\n";
$apnic = get_apnic_data();
echo "获取中国IPv4地址\n";
get_country_ipv4('china', 'cn', $apnic);
echo "获取海外IPv4地址\n";
get_country_ipv4('oversea', '(?!cn)\w{2}', $apnic);
//计算耗时
$time2 = time() - $time1;
echo "\n\n[+] 转换过程消耗时长约为{$time2}秒.";
////////////////////////////////////////////////////
/**
* 询问操作
*/
function confirm($text)
{
echo "{$text}[yes/no]: ";
$stat = trim(fgets(STDIN));
return $stat == 'y' || $stat == 'yes';
}
/**
* 获取APNIC数据
*/
function get_apnic_data()
{
$file = './data/apnic.txt';
if (is_file($file) && filectime($file) > strtotime('-1 day')) {
return file_get_contents($file);
}
$site = 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest';
file_put_contents($file, $data = file_get_contents($site));
return $data;
}
/**
* 获取指定国家IPv4地址
*/
function get_country_ipv4($name, $expr, &$data)
{
$expr = "/apnic\|{$expr}\|ipv4\|([0-9\.]+\|[0-9]+)\|[0-9]+\|a.*/i";
preg_match_all($expr, $data, $match);
$rest = array(array(), array());
foreach ($match[1] as $val) {
list($net, $ips) = explode('|', $val);
$rest[0][] = $net . '/' . (32 - log($ips, 2));
$rest[1][] = $net . '/' . (long2ip(ip2long('255.255.255.255') << log($ips, 2)));
}
file_put_contents("./data/apnic_{$name}_0_v4.txt", implode("\n", $rest[0]));
file_put_contents("./data/apnic_{$name}_1_v4.txt", implode("\n", $rest[1]));
//生成Linux路由表
$route = 'route add -net $1 netmask $2 gw ${gwip}';
$route = preg_replace('@([^/]+)/([^/]+)@', $route, $rest[1]);
file_put_contents("./data/apnic_{$name}_v4_linux_route.add", implode("\n", $route));
}
///////////////////////////////////////////////////////////////////////////////////////////////
/**
* IPv4地址转换类
* $ip = new ipv4('192.168.2.1', 24);
*/
class ipv4
{
//变量表
private $address;
private $netbits;
//构造函数
public function __construct($address, $netbits, $type = '')
{
$this->address = $address;
$this->netbits = $netbits;
if ($type == 'netips') {
$this->set_netbits_by_netips();
}
}
//获取IP地址
public function address()
{
return ($this->address);
}
//获取掩码位数
public function netbits()
{
return ($this->netbits);
}
//获取网络掩码
public function netmask()
{
return (long2ip(ip2long('255.255.255.255') << (32 - $this->netbits)));
}
//获取反掩码
public function inverse()
{
return (long2ip(~(ip2long('255.255.255.255') << (32 - $this->netbits))));
}
//获取子网地址
public function network()
{
return (long2ip((ip2long($this->address)) & (ip2long($this->netmask()))));
}
//获取广播地址
public function broadcast()
{
return (long2ip(ip2long($this->network()) | (~(ip2long($this->netmask())))));
}
//根据可用IP获取掩码位数
private function set_netbits_by_netips()
{
$this->netbits = 32 - log($this->netbits, 2);
}
}