@ -13,12 +13,13 @@ import (
@@ -13,12 +13,13 @@ import (
// to the dnsseeder
func startHTTP ( port string ) {
http . HandleFunc ( "/dns" , dnsHandler )
http . HandleFunc ( "/twistee" , twiste eHandler )
http . HandleFunc ( "/dns" , dnsWeb Handler )
http . HandleFunc ( "/node" , nod eHandler )
http . HandleFunc ( "/statusRG" , statusRGHandler )
http . HandleFunc ( "/statusCG" , statusCGHandler )
http . HandleFunc ( "/statusWG" , statusWGHandler )
http . HandleFunc ( "/statusNG" , statusNGHandler )
http . HandleFunc ( "/summary" , summaryHandler )
http . HandleFunc ( "/" , emptyHandler )
// listen only on localhost
err := http . ListenAndServe ( "127.0.0.1:" + port , nil )
@ -28,18 +29,30 @@ func startHTTP(port string) {
@@ -28,18 +29,30 @@ func startHTTP(port string) {
}
// reflect Handler processes all requests and returns output in the requested format
func dnsHandler ( w http . ResponseWriter , r * http . Request ) {
// dnsWeb Handler processes all requests and returns output in the requested format
func dnsWeb Handler ( w http . ResponseWriter , r * http . Request ) {
st := time . Now ( )
// skip the s= from the raw query
n := r . FormValue ( "s" )
s := getSeederByName ( n )
if s == nil {
writeHeader ( w , r )
fmt . Fprintf ( w , "No seeder found: %s" , html . EscapeString ( n ) )
writeFooter ( w , r , st )
return
}
// FIXME - This is ugly code and needs to be cleaned up a lot
// get v4 std addresses
v4std := getv4stdRR ( )
v4non := getv4nonRR ( )
v6std := getv6stdRR ( )
v6non := getv6nonRR ( )
config . dnsmtx . RLock ( )
// if the dns map does not have a key for the request it will return an empty slice
v4std := config . dns [ s . dnsHost + ".A" ]
v4non := config . dns [ "nonstd." + s . dnsHost + ".A" ]
v6std := config . dns [ s . dnsHost + ".AAAA" ]
v6non := config . dns [ "nonstd." + s . dnsHost + ".AAAA" ]
config . dnsmtx . RUnlock ( )
var v4stdstr , v4nonstr [ ] string
var v6stdstr , v6nonstr [ ] string
@ -172,28 +185,39 @@ func statusNGHandler(w http.ResponseWriter, r *http.Request) {
@@ -172,28 +185,39 @@ func statusNGHandler(w http.ResponseWriter, r *http.Request) {
}
type webstatus struct {
Key string
Value string
Key string
Value string
Seeder string
}
func statusHandler ( w http . ResponseWriter , r * http . Request , status uint32 ) {
startT := time . Now ( )
// read the seeder name
n := r . FormValue ( "s" )
s := getSeederByName ( n )
if s == nil {
writeHeader ( w , r )
fmt . Fprintf ( w , "No seeder found called %s" , html . EscapeString ( n ) )
writeFooter ( w , r , startT )
return
}
// gather all the info before writing anything to the remote browser
ws := generateWebStatus ( status )
ws := generateWebStatus ( s , s tatus)
st := `
< center >
< table border = 1 >
< tr >
< th > Twistee < / th >
< th > Nod e< / th >
< th > Summary < / th >
< / tr >
{ { range . } }
< tr >
< td >
< a href = "/twistee?tw ={{.Key}}" > { { . Key } } < / a >
< a href = "/node?s={{.Seeder}}&nd ={{.Key}}" > { { . Key } } < / a >
< / td >
< td >
{ { . Value } }
@ -207,18 +231,18 @@ func statusHandler(w http.ResponseWriter, r *http.Request, status uint32) {
@@ -207,18 +231,18 @@ func statusHandler(w http.ResponseWriter, r *http.Request, status uint32) {
writeHeader ( w , r )
if len ( ws ) == 0 {
fmt . Fprintf ( w , "No Twiste es found with this status" )
fmt . Fprintf ( w , "No Nod es found with this status" )
} else {
switch status {
case statusRG :
fmt . Fprintf ( w , "<center><b>Twiste e Status: statusRG - (Reported Good) Have not been able to get addresses yet</b></center>" )
fmt . Fprintf ( w , "<center><b>Nod e Status: statusRG - (Reported Good) Have not been able to get addresses yet</b></center>" )
case statusCG :
fmt . Fprintf ( w , "<center><b>Twiste e Status: statusCG - (Currently Good) Able to connect and get addresses</b></center>" )
fmt . Fprintf ( w , "<center><b>Nod e Status: statusCG - (Currently Good) Able to connect and get addresses</b></center>" )
case statusWG :
fmt . Fprintf ( w , "<center><b>Twiste e Status: statusWG - (Was Good) Was Ok but now can not get addresses</b></center>" )
fmt . Fprintf ( w , "<center><b>Nod e Status: statusWG - (Was Good) Was Ok but now can not get addresses</b></center>" )
case statusNG :
fmt . Fprintf ( w , "<center><b>Twiste e Status: statusNG - (No Good) Unable to get addresses</b></center>" )
fmt . Fprintf ( w , "<center><b>Nod e Status: statusNG - (No Good) Unable to get addresses</b></center>" )
}
t := template . New ( "Status template" )
t , err := t . Parse ( st )
@ -234,7 +258,59 @@ func statusHandler(w http.ResponseWriter, r *http.Request, status uint32) {
@@ -234,7 +258,59 @@ func statusHandler(w http.ResponseWriter, r *http.Request, status uint32) {
writeFooter ( w , r , startT )
}
// copy Twistee details into a template friendly struct
// generateWebStatus is given a node status and returns a slice of webstatus structures
// ready to be ranged over by an html/template
func generateWebStatus ( s * dnsseeder , status uint32 ) ( ws [ ] webstatus ) {
s . mtx . RLock ( )
defer s . mtx . RUnlock ( )
var valueStr string
for k , v := range s . theList {
if v . status != status {
continue
}
switch status {
case statusRG :
valueStr = fmt . Sprintf ( "<b>Fail Count:</b> %v <b>DNS Type:</b> %s" ,
v . connectFails ,
v . dns2str ( ) )
case statusCG :
valueStr = fmt . Sprintf ( "<b>Remote Version:</b> %v%s <b>Last Block:</b> %v <b>DNS Type:</b> %s" ,
v . version ,
v . strVersion ,
v . lastBlock ,
v . dns2str ( ) )
case statusWG :
valueStr = fmt . Sprintf ( "<b>Last Try:</b> %s ago <b>Last Status:</b> %s\n" ,
time . Since ( v . lastTry ) . String ( ) ,
v . statusStr )
case statusNG :
valueStr = fmt . Sprintf ( "<b>Fail Count:</b> %v <b>Last Try:</b> %s ago <b>Last Status:</b> %s\n" ,
v . connectFails ,
time . Since ( v . lastTry ) . String ( ) ,
v . statusStr )
default :
valueStr = ""
}
ows := webstatus {
Key : k ,
Value : valueStr ,
Seeder : s . name ,
}
ws = append ( ws , ows )
}
return ws
}
// copy Node details into a template friendly struct
type webtemplate struct {
Key string
IP string
@ -257,16 +333,16 @@ type webtemplate struct {
@@ -257,16 +333,16 @@ type webtemplate struct {
Nonstdip string
}
// reflectHandler processes all requests and returns output in the requested format
func twiste eHandler( w http . ResponseWriter , r * http . Request ) {
// nodeHandler displays details about one node
func nod eHandler( w http . ResponseWriter , r * http . Request ) {
st := time . Now ( )
tw t := `
nd t := `
< center >
< table border = 1 >
< tr >
< th > Twiste e { { . Key } } < / th > < th > Details < / th >
< th > Nod e { { . Key } } < / th > < th > Details < / th >
< / tr >
< tr > < td > IP Address < / td > < td > { { . IP } } < / td > < / tr >
< tr > < td > Port < / td > < td > { { . Port } } < / td > < / tr >
@ -285,111 +361,68 @@ func twisteeHandler(w http.ResponseWriter, r *http.Request) {
@@ -285,111 +361,68 @@ func twisteeHandler(w http.ResponseWriter, r *http.Request) {
< / table >
< / center >
`
s := config . seeder
// read the seeder name
n := r . FormValue ( "s" )
s := getSeederByName ( n )
if s == nil {
writeHeader ( w , r )
fmt . Fprintf ( w , "No seeder found called %s" , html . EscapeString ( n ) )
writeFooter ( w , r , st )
return
}
s . mtx . RLock ( )
defer s . mtx . RUnlock ( )
// skip the tw= from the raw query
k := html . UnescapeString ( r . URL . RawQuery [ 3 : ] )
k := r . FormValue ( "nd" )
writeHeader ( w , r )
if _ , ok := s . theList [ k ] ; ok == false {
fmt . Fprintf ( w , "Sorry there is no Twiste e with those details\n" )
fmt . Fprintf ( w , "Sorry there is no Nod e with those details\n" )
} else {
tw := s . theList [ k ]
nd := s . theList [ k ]
wt := webtemplate {
IP : tw . na . IP . String ( ) ,
Port : tw . na . Port ,
Dnstype : tw . dns2str ( ) ,
Nonstdip : tw . nonstdIP . String ( ) ,
Statusstr : tw . statusStr ,
Lastconnect : tw . lastConnect . String ( ) ,
Lastconnectago : time . Since ( tw . lastConnect ) . String ( ) ,
Lasttry : tw . lastTry . String ( ) ,
Lasttryago : time . Since ( tw . lastTry ) . String ( ) ,
Crawlstart : tw . crawlStart . String ( ) ,
Crawlstartago : time . Since ( tw . crawlStart ) . String ( ) ,
Connectfails : tw . connectFails ,
Crawlactive : tw . crawlActive ,
Version : tw . version ,
Strversion : tw . strVersion ,
Services : tw . services . String ( ) ,
Lastblock : tw . lastBlock ,
IP : nd . na . IP . String ( ) ,
Port : nd . na . Port ,
Dnstype : nd . dns2str ( ) ,
Nonstdip : nd . nonstdIP . String ( ) ,
Statusstr : nd . statusStr ,
Lastconnect : nd . lastConnect . String ( ) ,
Lastconnectago : time . Since ( nd . lastConnect ) . String ( ) ,
Lasttry : nd . lastTry . String ( ) ,
Lasttryago : time . Since ( nd . lastTry ) . String ( ) ,
Crawlstart : nd . crawlStart . String ( ) ,
Crawlstartago : time . Since ( nd . crawlStart ) . String ( ) ,
Connectfails : nd . connectFails ,
Crawlactive : nd . crawlActive ,
Version : nd . version ,
Strversion : nd . strVersion ,
Services : nd . services . String ( ) ,
Lastblock : nd . lastBlock ,
}
// display details for the Twiste e
t := template . New ( "Twiste e template" )
t , err := t . Parse ( tw t)
// display details for the Nod e
t := template . New ( "Nod e template" )
t , err := t . Parse ( nd t)
if err != nil {
log . Printf ( "error parsing Twiste e template %v\n" , err )
log . Printf ( "error parsing Nod e template %v\n" , err )
}
err = t . Execute ( w , wt )
if err != nil {
log . Printf ( "error executing Twiste e template %v\n" , err )
log . Printf ( "error executing Nod e template %v\n" , err )
}
}
writeFooter ( w , r , st )
}
// generateWebStatus is given a twistee status and returns a slice of webstatus structures
// ready to be ranged over by an html/template
func generateWebStatus ( status uint32 ) ( ws [ ] webstatus ) {
s := config . seeder
s . mtx . RLock ( )
defer s . mtx . RUnlock ( )
var valueStr string
for k , v := range s . theList {
if v . status != status {
continue
}
// summaryHandler displays details about one node
func summaryHandler ( w http . ResponseWriter , r * http . Request ) {
switch status {
case statusRG :
valueStr = fmt . Sprintf ( "<b>Fail Count:</b> %v <b>DNS Type:</b> %s" ,
v . connectFails ,
v . dns2str ( ) )
case statusCG :
valueStr = fmt . Sprintf ( "<b>Remote Version:</b> %v%s <b>Last Block:</b> %v <b>DNS Type:</b> %s" ,
v . version ,
v . strVersion ,
v . lastBlock ,
v . dns2str ( ) )
case statusWG :
valueStr = fmt . Sprintf ( "<b>Last Try:</b> %s ago <b>Last Status:</b> %s\n" ,
time . Since ( v . lastTry ) . String ( ) ,
v . statusStr )
case statusNG :
valueStr = fmt . Sprintf ( "<b>Fail Count:</b> %v <b>Last Try:</b> %s ago <b>Last Status:</b> %s\n" ,
v . connectFails ,
time . Since ( v . lastTry ) . String ( ) ,
v . statusStr )
default :
valueStr = ""
}
ows := webstatus {
Key : k ,
Value : valueStr ,
}
ws = append ( ws , ows )
}
return ws
}
// genHeader will output the standard header
func writeHeader ( w http . ResponseWriter , r * http . Request ) {
st := time . Now ( )
var hc struct {
Name string
RG uint32
RGS uint32
CG uint32
@ -406,65 +439,85 @@ func writeHeader(w http.ResponseWriter, r *http.Request) {
@@ -406,65 +439,85 @@ func writeHeader(w http.ResponseWriter, r *http.Request) {
DNSTotal uint32
}
// fill the structs so they can be displayed via the template
counts . mtx . RLock ( )
hc . RG = counts . TwStatus [ statusRG ]
hc . RGS = counts . TwStarts [ statusRG ]
hc . CG = counts . TwStatus [ statusCG ]
hc . CGS = counts . TwStarts [ statusCG ]
hc . WG = counts . TwStatus [ statusWG ]
hc . WGS = counts . TwStarts [ statusWG ]
hc . NG = counts . TwStatus [ statusNG ]
hc . NGS = counts . TwStarts [ statusNG ]
hc . Total = hc . RG + hc . CG + hc . WG + hc . NG
hc . V4Std = counts . DNSCounts [ dnsV4Std ]
hc . V4Non = counts . DNSCounts [ dnsV4Non ]
hc . V6Std = counts . DNSCounts [ dnsV6Std ]
hc . V6Non = counts . DNSCounts [ dnsV6Non ]
hc . DNSTotal = hc . V4Std + hc . V4Non + hc . V6Std + hc . V6Non
counts . mtx . RUnlock ( )
// we are using basic and simple html here. No fancy graphics or css
h := `
< ! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html > < head > < title > dnsseeder < / title > < / head > < body >
< center >
< a href = "/statusRG" > statusRG < / a >
< a href = "/statusCG" > statusCG < / a >
< a href = "/statusWG" > statusWG < / a >
< a href = "/statusNG" > statusNG < / a >
< a href = "/dns" > DNS < / a >
< br >
writeHeader ( w , r )
// loop through each of the seeders
for _ , s := range config . seeders {
hc . Name = s . name
// fill the structs so they can be displayed via the template
s . counts . mtx . RLock ( )
hc . RG = s . counts . NdStatus [ statusRG ]
hc . RGS = s . counts . NdStarts [ statusRG ]
hc . CG = s . counts . NdStatus [ statusCG ]
hc . CGS = s . counts . NdStarts [ statusCG ]
hc . WG = s . counts . NdStatus [ statusWG ]
hc . WGS = s . counts . NdStarts [ statusWG ]
hc . NG = s . counts . NdStatus [ statusNG ]
hc . NGS = s . counts . NdStarts [ statusNG ]
hc . Total = hc . RG + hc . CG + hc . WG + hc . NG
hc . V4Std = s . counts . DNSCounts [ dnsV4Std ]
hc . V4Non = s . counts . DNSCounts [ dnsV4Non ]
hc . V6Std = s . counts . DNSCounts [ dnsV6Std ]
hc . V6Non = s . counts . DNSCounts [ dnsV6Non ]
hc . DNSTotal = hc . V4Std + hc . V4Non + hc . V6Std + hc . V6Non
s . counts . mtx . RUnlock ( )
// we are using basic and simple html here. No fancy graphics or css
sp := `
< b > Stats for seeder : { { . Name } } < / b >
< center >
< table > < tr > < td >
Twiste e Stats ( count / started ) < br >
Node Stats ( count / started ) < br >
< table border = 1 > < tr >
< td > RG : { { . RG } } / { { . RGS } } < / td > < td > CG : { { . CG } } / { { . CGS } } < / td > < td > WG : { { . WG } } / { { . WGS } } < / td > < td > NG : { { . NG } } / { { . NGS } } < / td > < td > Total : { { . Total } } < / td >
< td > < a href = "/statusRG?s={{.Name}}" > RG : { { . RG } } / { { . RGS } } < / a > < / td >
< td > < a href = "/statusCG?s={{.Name}}" > CG : { { . CG } } / { { . CGS } } < / a > < / td >
< td > < a href = "/statusWG?s={{.Name}}" > WG : { { . WG } } / { { . WGS } } < / a > < / td >
< td > < a href = "/statusNG?s={{.Name}}" > NG : { { . NG } } / { { . NGS } } < / a > < / td >
< td > Total : { { . Total } } < / td >
< / tr > < / table >
< / td > < td >
DNS Requests < br >
< table border = 1 > < tr >
< td > V4 Std : { { . V4Std } } < / td > < td > V4 Non : { { . V4Non } } < / td > < td > V6 Std : { { . V6Std } } < / td > < td > V6 Non : { { . V6Non } } < / td > < td > Total : { { . DNSTotal } } < / td >
< td > V4 Std : { { . V4Std } } < / td >
< td > V4 Non : { { . V4Non } } < / td >
< td > V6 Std : { { . V6Std } } < / td >
< td > V6 Non : { { . V6Non } } < / td >
< td > < a href = "/dns?s={{.Name}}" > Total : { { . DNSTotal } } < / a > < / td >
< / tr > < / table >
< / td > < / tr > < / table >
< / center >
< hr >
`
t := template . New ( "Header template" )
t , err := t . Parse ( sp )
if err != nil {
log . Printf ( "error parsing summary template %v\n" , err )
}
t := template . New ( "Header template" )
t , err := t . Parse ( h )
if err != nil {
log . Printf ( "error parsing template %v\n" , err )
}
err = t . Execute ( w , hc )
if err != nil {
log . Printf ( "error executing template %v\n" , err )
err = t . Execute ( w , hc )
if err != nil {
log . Printf ( "error executing summary template %v\n" , err )
}
}
writeFooter ( w , r , st )
}
// writeHeader will output the standard header
func writeHeader ( w http . ResponseWriter , r * http . Request ) {
// we are using basic and simple html here. No fancy graphics or css
h := `
< ! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html > < head > < title > dnsseeder < / title > < / head > < body >
< center >
< a href = "/summary" > Summary < / a >
< / center >
< hr >
< br >
`
fmt . Fprintf ( w , h )
}
// genFooter will output the standard footer
// write Footer will output the standard footer
func writeFooter ( w http . ResponseWriter , r * http . Request , st time . Time ) {
// Footer needs to be exported for template processing to work
@ -483,7 +536,7 @@ func writeFooter(w http.ResponseWriter, r *http.Request, st time.Time) {
@@ -483,7 +536,7 @@ func writeFooter(w http.ResponseWriter, r *http.Request, st time.Time) {
< / center >
< / body > < / html >
`
Footer . Uptime = time . Since ( config . seeder . uptime ) . String ( )
Footer . Uptime = time . Since ( config . uptime ) . String ( )
Footer . Version = config . version
Footer . Rt = time . Since ( st ) . String ( )