-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconvert.php
70 lines (57 loc) · 1.26 KB
/
convert.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
#!/usr/bin/env php
<?php
if (!is_cli())
{
exit('Please run script in CLI mode.');
}
function is_cli()
{
return (PHP_SAPI === 'cli');
}
function println($str)
{
echo $str . PHP_EOL;
exit;
}
if (!isset($argv[1]))
{
println('Please type hosts filename.');
}
$filename = $argv[1];
if (!is_file($filename))
{
println($filename . ' not found.');
}
$datas = urlencode(file_get_contents($filename));
$datas = str_replace(array('%0A', '%09', '+'), array("\n", ' ', ' '), $datas);
$datas = preg_replace('/[ ]{1,}/', ' ', $datas);
$origin = explode("\n", $datas);
foreach ($origin as $key => $value)
{
if ((strpos($value, '%23') !== false) || (trim($value) == ''))
{
unset($origin[$key]);
}
}
foreach ($origin as $key => $value)
{
$result[] = explode(' ', $value);
}
$export = '[General]
skip-proxy = 192.168.0.0/16, 10.0.0.0/8, 172.16.0.0/12, localhost, *.local
bypass-tun = 192.168.0.0/16, 10.0.0.0/8, 172.16.0.0/12
loglevel = notify
[Rule]
FINAL,DIRECT
[Host]
';
foreach ($result as $key => $value)
{
$export .= $value[1] . ' = ' . $value[0] . "\r\n";
}
$export = urldecode($export);
if (file_put_contents(dirname(__FILE__) . '/hosts.conf', $export))
{
println('Export to hosts.conf successfully.');
}
println('Error occured.');