Skip to content

Instantly share code, notes, and snippets.

@simonkuang
Created March 3, 2020 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonkuang/ffa0a7236277492c92e3b4b3d5ff3c90 to your computer and use it in GitHub Desktop.
Save simonkuang/ffa0a7236277492c92e3b4b3d5ff3c90 to your computer and use it in GitHub Desktop.
根据 gfwlist 生成 dns 污染的域名列表。用于 coredns 配置
<?php
function parse($str) {
$s = ltrim($str, '|@.');
$host = null;
if (false !== strpos($s, '//')) {
$url = parse_url($s);
if (!empty($url['host'])) {
$host = $url['host'];
}
} elseif (false !== ($pos = strpos($s, '/'))) {
$host = substr($s, 0, $pos);
} else {
$host = $s;
}
if (preg_match('/^[\d\.]+$/', $host)) { // host is ip
return null;
}
$sections = explode('.', $host);
$len = count($sections);
$host = implode('.', [$sections[$len - 2], $sections[$len - 1]]);
return false === strpos($host, '*') ? $host : null;
}
//$content = base64_decode(file_get_contents('/data/gfwlist.txt'));
$content = base64_decode(file_get_contents('https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt'));
$pos = 0;
$result = [];
while (false != ($newpos = strpos($content, "\n", $pos + 1))) {
$line = trim(substr($content, $pos, $newpos - $pos));
$pos = $newpos;
if (empty($line) || ($line{0} === '!') || false === strpos($line, '.') || false !== strpos($line, ' ')) {
continue;
}
$domain = parse($line);
if (!isset($domain)) {
continue;
}
in_array($domain, $result) || ($result[] = $domain);
}
echo implode(' ', $result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment