php操作脚本

pull/6/head
gently 7 years ago
parent 00107d7a77
commit cb1200341d

@ -0,0 +1,36 @@
<?php
//黑名单域名,即直接封杀主域名,效果就是只要是使用该域名及其下级所有域名的请求全部被阻挡,慎重使用
return array( 'cnzz.com' => array('.cnzz.com'),
'mediav.com' => array('.mediav.com'),
'msn.com' => array('.msn.com'),
'baihe.com' => array('.baihe.com'),
'jiayuan.com' => array('.jiayuan.com'),//世纪佳缘,嗯!没这需求
'qbao.com' => array('.qbao.com'), //钱宝网
'dftoutiao.com' => array('.dftoutiao.com'),
'miaozhen.com' => array('miaozhen.com', '.miaozhen.com'),
'rubiconproject.com' => array('.rubiconproject.com'),
'adsame.com' => array('.adsame.com', 'adsame.com'),
'hexun.com' => array('.hexun.com'),
'2345.com' => array('.2345.com'),
'51.la' => array('.51.la'),
'778669.com' => array('.778669.com', '778669.com'), //恶意网站
'ddns.name' => array('.ddns.name'),
'7clink.com' => array('.7clink.com'),
'88shu.cn' => array('.88shu.cn'),
'51yes.com' => array('51yes.com', '.51yes.com'),
'3393.com' => array('3393.com', '.3393.com'),
'zedo.com' => array('zedo.com', '.zedo.com'),
'admaster.com.cn' => array('admaster.com.cn', '.admaster.com.cn'),
'adpush.cn' => array('adpush.cn', '.adpush.cn'),
'adsage.com' => array('adsage.com', '.adsage.com'),
'allyes.cn' => array('allyes.cn', '.allyes.cn'),
'allyes.com' => array('allyes.com', '.allyes.com'),
'baifendian.com' => array('.baifendian.com'),
'banmamedia.com' => array('.banmamedia.com'),
'behe.com' => array('.behe.com'),
'dnset.com' => array('.dnset.com'),
//'kankan.com' => array('.cpm.cm.kankan.com', '.float.kankan.com', '.stat.kankan.com'),
);

@ -0,0 +1,176 @@
<?php
//在命令行下运行直接生成dnsmasq的去广告用途的配置文件
//2017年12月31日
set_time_limit(60);
if(PHP_SAPI != 'cli'){
die('nothing.');
}
$arr_blacklist = require('./black_domain_list.php');
$arr_result = array();
echo '开始下载host1....',"\n";
$host1 = makeAddr::http_get('https://raw.githubusercontent.com/vokins/yhosts/master/dnsmasq/union.conf');
$arr_result = makeAddr::get_domain_list($host1);
echo '开始下载host2....',"\n";
$host2 = makeAddr::http_get('https://raw.githubusercontent.com/vokins/yhosts/master/hosts.txt');
$arr_result = array_merge_recursive($arr_result, makeAddr::get_domain_list($host2));
echo '开始下载host3....',"\n";
$host3 = makeAddr::http_get('http://www.malwaredomainlist.com/hostslist/hosts.txt');
$arr_result = array_merge_recursive($arr_result, makeAddr::get_domain_list($host3));
echo '写入文件大小:';
var_dump(makeAddr::write_to_conf($arr_result, './adblock-for-dnsmasq.conf'));
class makeAddr{
public static function http_get($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'T_T angent 2.0.5/' . phpversion());
$result = curl_exec($ch);
$errno = curl_errno($ch);
curl_close($ch);
return $result;
}
public static function extract_main_domain($str_domain){
if(empty($str_domain)){
return "";
}
$str_reg = '/^[a-z0-9\-\.]*?([a-z0-9\-]+(\.com|\.cn|\.net|\.org|\.cn|\.me|\.co|\.info|\.cc|\.tv';
$str_reg .= '|\.pw|\.biz|\.top|\.win|\.bid|\.cf|\.club|\.ne|\.de|\.la|\.us|\.mobi|\.hn|\.asia';
$str_reg .= '|\.jp|\.tw|\.am|\.hk|\.site|\.live|\.xyz|\.space|\.fr|\.es|\.nl|\.au|\.in|\.ru';
$str_reg .= '|\.su|\.world|\.io|\.trade|\.bet|\.im|\.fm|\.today|\.wang|\.rocks|\.vip|\.eu|\.run';
$str_reg .= '|\.online|\.website|\.cricket|\.date|\.men|\.ca|\.xxx|\.name|\.pl|\.be|\.il|\.gov|\.it';
$str_reg .= '|\.cl|\.tk|\.cz|\.hu|\.ro|\.vg|\.ws|\.nu|\.vn|\.lt|\.edu|\.lv|\.mx|\.by|\.gr|\.br|\.fi';
$str_reg .= '|\.pt|\.dk|\.se|\.at|\.id|\.ve|\.ir|\.ma|\.ch|\.nf|\.bg|\.ua|\.is|\.hr)';
$str_reg .= '(\.cn|\.tw|\.uk|\.jp|\.kr|\.th|\.au|\.ua|\.so|\.br|\.sg|\.pt|\.ec|\.ar|\.my|\.tr)?)$/';
preg_match($str_reg, $str_domain,$matchs);
return strval($matchs[1]);
}
public static function get_domain_list($str_hosts){
$strlen = strlen($str_hosts);
if($strlen < 10){
return array();
}
$str_hosts = $str_hosts . "\n"; //防止最后一行没有换行符
$i=0;
$arr_domains = array();
while($i < $strlen){
$end_pos = strpos($str_hosts, "\n", $i);
$line = trim(substr($str_hosts, $i, $end_pos - $i));
$i = $end_pos+1;
if(empty($line) || ($line{0} == '#')){//注释行忽略
continue;
}
$line = strtolower(preg_replace('/[\s\t]+/', "/", $line));
if((strpos($line, '127.0.0.1') === false) && (strpos($line, '0.0.0.0') === false)){
continue;
}
$row = explode('/', $line);
if(strpos($row[1], '.') === false){
continue;
}
$arr_domains[self::extract_main_domain($row[1])][] = $row[1];
}
return $arr_domains;
}
public static function write_to_conf($arr_result, $str_file){
$fp = fopen($str_file, 'w');
$write_len = 0;
foreach($arr_result as $rk => $rv){
if(array_key_exists($rk, $GLOBALS['arr_blacklist'])){//黑名单操作
foreach($GLOBALS['arr_blacklist'][$rk] as $bv){
$write_len += fwrite($fp, 'address=/' . $bv . '/127.0.0.1' . "\n");
}
continue;
}
if(empty($rk)){//遗漏的域名,不会写入到最终的配置里
// print_r($rv);
continue;
}
if(!is_array($rv)){
$write_len += fwrite($fp, 'address=/' . $rv . '/127.0.0.1' . "\n");
continue;
}
array_unique($rv);
$rk_found = false;
if(in_array('.' . $rk, $rv)){
$write_len += fwrite($fp, 'address=/.' . $rk . '/127.0.0.1' . "\n");
$rk_found = true;
}
foreach($rv as $rvv){
if(!$rk_found || (strpos($rvv, '.' . $rk) < 0)){
$write_len += fwrite($fp, 'address=/' . $rvv . '/127.0.0.1' . "\n");
}
}
}
fclose($fp);
return $write_len;
}
}

@ -0,0 +1,6 @@
<?php
$a = array('uu'=>array('xx' => 1, 'yy' => 2),'pp' => 123, 'zz' => 0);
$b = array('cc' => array('xx' => 78989, 'yy' => 99), 'uu' => array('zz' => 989), 'pp' => 65);
var_dump(array_merge_recursive($a, $b));
Loading…
Cancel
Save