-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathget_dns.php
executable file
·125 lines (94 loc) · 3.84 KB
/
get_dns.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
<?php
$base_dir = dirname(__FILE__);
# Load main config file.
require_once $base_dir . "/conf_default.php";
require_once $base_dir . "/tools.php";
# Include user-defined overrides if they exist.
if( file_exists( $base_dir . "/conf.php" ) ) {
include_once $base_dir . "/conf.php";
}
$site_id = is_numeric($_REQUEST['site_id']) ? $_REQUEST['site_id'] : -1;
if ( !isset($_REQUEST['hostname'])) {
die("Need to supply hostname");
}
# What's the name of the remote script to execute if we need to invoke a remote
$conf['remote_exe'] = basename ( __FILE__ );
if ( isset($_REQUEST['query_type']) and in_array($_REQUEST['query_type'], $conf['allowed_dns_query_types']) ) {
$query_type = $_REQUEST['query_type'];
} else {
$query_type = "A";
}
###############################################################################
# Test needs to be executed locally
if ( !isset($_REQUEST['site_id']) || $_REQUEST['site_id'] == -1 ) {
$results = get_dns_record_with_timing(trim($_REQUEST['hostname']), $query_type);
# Return JSON response
if ( isset($_REQUEST['json']) && $_REQUEST['json'] == 1 ) {
header('Content-type: application/json');
print json_encode($results);
exit(1);
} else {
$myresults["-1"] = $results;
print_dns_results($myresults);
}
} else if ( $site_id == -100 ) {
$mh = curl_multi_init();
// Get results from all remotes
foreach ( $conf['remotes'] as $id => $remote ) {
$url = $remote['base_url'] . $conf['remote_exe'] . "?json=1&site_id=-1&hostname="
. htmlentities($_REQUEST['hostname']) . "&query_type=" . $query_type;
$url_parts = parse_url($url);
$curly[$id] = curl_init();
curl_setopt($curly[$id], CURLOPT_HEADER, 1);
# How long to wait for CURL response. DNS responses should be quick so 4 seconds should be plenty
curl_setopt($curly[$id], CURLOPT_TIMEOUT, 4);
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
switch ( $url_parts['scheme'] ) {
case "http":
curl_setopt($curly[$id], CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
curl_setopt($curly[$id], CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP);
break;
case "https":
curl_setopt($curly[$id], CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
curl_setopt($curly[$id], CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS);
break;
default:
die("<h3>Invalid protocol supplied. You need either http:// or https://</h3>");
}
curl_setopt($curly[$id], CURLOPT_ENCODING , "gzip");
curl_setopt($curly[$id], CURLOPT_URL, $url);
# Disable SSL peer verify ie. don't check remote side SSL certificates
if ( ! $conf['ssl_peer_verify'] ) {
curl_setopt($curly[$id], CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curly[$id], CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curly[$id], CURLOPT_VERBOSE , TRUE);
}
curl_multi_add_handle($mh, $curly[$id]);
}
// execute the handles
$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
$results = array();
foreach($curly as $id => $c) {
if(curl_errno($c)) {
print "<h3>" . curl_error($c) . "</h3>";
}
$response = curl_multi_getcontent($c);
if ( $response != "" ) {
list($header, $content) = explode("\r\n\r\n", $response);
$results[$id] = json_decode($content, TRUE);
}
}
#print "<PRE>"; print_r($results);
print_dns_results($results);
} else if ( isset($conf['remotes'][$site_id]['name'] ) ) {
$sslOptions=array("ssl"=>array("verify_peer"=>false,"verify_peer_name"=>false));
$content = file_get_contents($conf['remotes'][$site_id]['base_url'] . $conf['remote_exe'] . "?json=1&site_id=-1" .
"&hostname=" . htmlentities($_REQUEST['hostname'] ) . "&query_type=" . $query_type, FALSE, stream_context_create($sslOptions)) ;
$results[$site_id] = json_decode($content, TRUE);
print_dns_results($results);
} else {
die("No valid site_id supplied");
}