2021-04-02 20:52:34 +02:00
* stop ( )
function dnswatch _search ( ) {
2021-04-03 14:12:19 +02:00
// get domain
var domain = document . getElementById ( 'dnswatch-search' ) . value . toLowerCase ( ) ;
// test if domain is invalid
if ( ! is _valid _domain ( domain ) ) {
document . getElementById ( 'results' ) . innerHTML = '<table></table>' ;
feather . replace ( ) ;
document . getElementById ( 'invaliddomain' ) . style . visibility = 'visible' ;
document . getElementById ( 'invaliddomain' ) . style . display = 'block' ;
return ;
}
document . getElementById ( 'invaliddomain' ) . style . visibility = 'hidden' ;
document . getElementById ( 'invaliddomain' ) . style . display = 'none' ;
2021-04-03 13:00:42 +02:00
// activate spinner
document . getElementById ( 'loadani' ) . style . visibility = 'visible' ;
document . getElementById ( 'loadani' ) . style . display = 'block' ;
2021-04-02 20:52:34 +02:00
// send api request
var request = new XMLHttpRequest ( ) ;
2021-05-08 18:10:55 +02:00
request . open ( 'POST' , 'https://dns-watch.org/lookup' , true ) ;
2021-04-02 20:52:34 +02:00
request . setRequestHeader ( 'Content-Type' , 'application/x-www-form-urlencoded' ) ;
request . onload = function ( ) {
if ( request . status >= 200 && request . status < 400 ) {
var response = JSON . parse ( this . response ) ;
if ( response [ 'type' ] == 'success' ) { // success
var table = '<table>\n' ;
2021-04-04 15:39:32 +02:00
2021-04-02 20:52:34 +02:00
//// reference dns
2021-05-07 15:05:08 +02:00
let first _ref = true ;
2021-04-04 15:39:32 +02:00
response [ 'data' ] [ 'reference' ] . forEach ( i => {
2021-05-07 15:05:08 +02:00
table += gen _tablerow ( {
status _color : 'ref' ,
status _icon : get _status _icon ( i [ 'status' ] ) ,
dns _icon : i [ 'icon' ] ,
dns _name : i [ 'name' ] ,
dns _address : i [ 'address' ] ,
desc _color : 'ref' ,
desc _text : 'reference' ,
2021-05-08 12:08:17 +02:00
show _help : first _ref ,
help _type : 'reference'
2021-05-07 15:05:08 +02:00
} )
first _ref = false ;
2021-04-04 15:39:32 +02:00
} ) ;
2021-04-02 20:52:34 +02:00
2021-04-04 16:20:28 +02:00
table += '</table>' ;
2021-05-07 15:05:08 +02:00
2021-04-04 16:20:28 +02:00
// randomized response message
if ( response [ 'data' ] [ 'randomized_response' ] ) {
2021-05-08 12:08:17 +02:00
table += '<small class="random">This domain uses randomized responses. <button onclick="show_modal(\'random\')">Learn more</button></small>\n' ;
2021-04-04 16:20:28 +02:00
}
2021-04-04 15:39:32 +02:00
if ( response [ 'data' ] [ 'found' ] ) { // only if reference confirms
2021-04-04 16:20:28 +02:00
table += '<table><tr></tr>\n' ; // spacer
2021-04-02 20:52:34 +02:00
//// search rows
2021-05-07 15:05:08 +02:00
let causes = [ ] ;
2021-04-02 20:52:34 +02:00
response [ 'data' ] [ 'search' ] . forEach ( i => {
2021-05-07 15:05:08 +02:00
table += gen _tablerow ( {
status _color : get _status _color ( i [ 'status' ] ) ,
status _icon : get _status _icon ( i [ 'status' ] ) ,
dns _icon : i [ 'icon' ] ,
dns _name : i [ 'name' ] ,
dns _address : i [ 'address' ] ,
desc _color : i [ 'cause' ] !== null ? 'blocked' : '' ,
desc _text : get _blocked _text ( i [ 'cause' ] ) ,
2021-05-08 12:08:17 +02:00
show _help : i [ 'cause' ] !== null && ! causes . includes ( i [ 'cause' ] ) ,
help _type : 'blocked-' + i [ 'cause' ]
2021-05-07 15:05:08 +02:00
} )
if ( i [ 'cause' ] !== null ) causes . push ( i [ 'cause' ] )
2021-04-02 20:52:34 +02:00
} ) ;
}
table += '</table>' ;
} else { // no success
2021-04-03 14:12:19 +02:00
if ( response [ 'error' ] == 'malformed_domain' ) { // invalid domain - should not happen, but just in case i guess
var table = '<table></table>' ;
document . getElementById ( 'results' ) . innerHTML = '<table></table>' ;
feather . replace ( ) ;
document . getElementById ( 'invaliddomain' ) . style . visibility = 'visible' ;
document . getElementById ( 'invaliddomain' ) . style . display = 'block' ;
} else {
var table = '<table>\n' ;
2021-05-08 12:08:17 +02:00
table += gen _tablerow ( {
status _color : 'cross' ,
status _icon : 'x' ,
dns _icon : 'x' ,
dns _name : ` ERROR: ${ response [ 'error' ] } ` ,
dns _address : '' ,
desc _color : 'ref' ,
desc _text : '' ,
show _help : false ,
help _type : ''
} )
2021-04-03 14:12:19 +02:00
table += '</table>' ;
}
2021-04-02 20:52:34 +02:00
}
document . getElementById ( 'results' ) . innerHTML = table ;
feather . replace ( ) ;
}
2021-04-03 13:00:42 +02:00
// deactivate spinner
document . getElementById ( 'loadani' ) . style . visibility = 'hidden' ;
document . getElementById ( 'loadani' ) . style . display = 'none' ;
2021-04-02 20:52:34 +02:00
}
request . send ( ` domain= ${ window . btoa ( domain ) } ` ) ;
}
2021-04-03 14:12:19 +02:00
function is _valid _domain ( str ) {
2021-04-03 16:00:32 +02:00
let regex = /^([a-z0-9\-]{1,64}\.){1,16}[a-z0-9]{2,}$/ ;
2021-04-03 14:12:19 +02:00
return regex . test ( str ) ;
}
2021-05-07 15:05:08 +02:00
function gen _tablerow ( options ) {
return `
< tr >
< td class = "status ${ options.status_color }" > < i data - feather = "${ options.status_icon }" > < / i > < / t d >
< td class = "icon" > < img class = "uncolored-svg" src = "${ options.dns_icon }" > < / t d >
< td class = "name" > $ { options . dns _name } < small > $ { options . dns _address } < / s m a l l > < / t d >
< td class = "desc ${ options.desc_color }" > $ { options . desc _text } < / t d >
2021-05-08 12:08:17 +02:00
$ { options . show _help ? '<td class="help"><button onclick="show_modal(\'' + options . help _type + '\')"><i data-feather="help-circle"></i></button></td>' : '' }
2021-05-07 15:05:08 +02:00
< / t r >
`
}
function get _status _color ( status ) {
if ( status === true ) return 'check'
if ( status === false ) return 'cross'
return 'offline'
}
function get _status _icon ( status ) {
if ( status === true ) return 'check'
if ( status === false ) return 'x'
return 'wifi-off'
}
function get _blocked _text ( cause ) {
if ( cause == 'cuii' ) return 'blocked by cuii'
if ( cause !== null ) return ` bocked: ${ cause } `
return ''
}
2021-05-08 12:08:17 +02:00
function show _modal ( type ) {
let msg ;
switch ( type ) {
case 'search' :
msg = '<b>dns-watch.org</b> is a service that allows you to see how (mainly German) Internet Service Providers (ISPs) deal with Internet censorship. <a href="/why">More Information</a><br>In order to test a website for availability, enter the domain into the search bar and press Enter.<br><span>Try out these examples if you are unsure: gnu.org, wikipedia.org, s.to</span>' ;
break ;
case 'reference' :
msg = 'In order to validate the DNS responses, we use <b>references</b>.<br>These references are from trustworthy DNS providers, such as Cloudflare or UncensoredDNS.<br>The responses of the DNS providers that should be tested (located in the bottom table) will be compared to the results from the references.'
break ;
case 'blocked-cuii' :
msg = 'This ISP (and all others with the same error message) blocked the request on behalf of <b>CUII</b>.<br>CUII is a German institution that censors parts of the Internet because of so-called copyright claims.<br>For more information about the CUII, read our <a href="/why">Why?</a> page.'
break ;
case 'random' :
2021-05-08 19:04:19 +02:00
msg = 'This domain uses <b>randomized responses</b>.<br>Some websites return different IP addresses for load balancing.<br>Therefore, dns-watch.org can\'t compare the results of these DNS responses and will only check for common blocking patterns.'
2021-05-08 12:08:17 +02:00
break ;
default :
msg = ` It seems like something happend that shouldn \' t have happened!<br><span>Dialog message type: ${ type } </span> `
break ;
}
document . getElementById ( 'modal-message' ) . innerHTML = msg
document . getElementById ( 'modal' ) . style . display = 'block'
}
function close _modal ( ) {
document . getElementById ( 'modal' ) . style . display = 'none'
}
2021-04-02 20:52:34 +02:00
// add event triggers
document . addEventListener ( 'DOMContentLoaded' , ( ) => {
document . getElementById ( 'dnswatch-search-submit' ) . addEventListener ( 'click' , ( event ) => {
dnswatch _search ( ) ;
} ) ;
document . getElementById ( 'dnswatch-search' ) . addEventListener ( 'keyup' , ( event ) => {
if ( event . keyCode === 13 ) {
event . preventDefault ( ) ;
dnswatch _search ( ) ;
}
} ) ;
2021-05-08 12:08:17 +02:00
window . onclick = function ( event ) {
if ( event . target == document . getElementById ( 'modal' ) )
close _modal ( )
}
2021-04-02 20:52:34 +02:00
} ) ;