Skip to content

Instantly share code, notes, and snippets.

@doismellburning
Created January 6, 2020 23:07
Show Gist options
  • Save doismellburning/dd86c3b0247ff774d5cfb9270ea590fe to your computer and use it in GitHub Desktop.
Save doismellburning/dd86c3b0247ff774d5cfb9270ea590fe to your computer and use it in GitHub Desktop.
// Package asyncdns is a terrible, inflexible, async reverse-DNS lookup tool
package asyncdns
import "fmt"
import "net"
type Wibble struct {
host string // IP address in theory
hostname string
err error
hostnameChan chan string
errChan chan error
}
func NewWibble(host string) *Wibble {
var wibble = new(Wibble)
wibble.host = host
wibble.hostnameChan = make(chan string)
wibble.errChan = make(chan error)
go wibble.lookup()
return wibble
}
func (wibble *Wibble) lookup() {
var names, err = net.LookupAddr(wibble.host)
if err != nil {
wibble.errChan <- err
} else if len(names) > 0 {
wibble.hostnameChan <- names[0]
}
}
func (wibble *Wibble) Host() string {
return wibble.host
}
func (wibble *Wibble) Describe() string {
if wibble.hostname != "" {
return wibble.hostname
}
if wibble.err != nil {
return fmt.Sprintf("%s (%s)", wibble.host, wibble.err)
}
select {
case hostname := <-wibble.hostnameChan:
wibble.hostname = hostname
return wibble.Describe()
case err := <-wibble.errChan:
wibble.err = err
return wibble.Describe()
default:
return fmt.Sprintf("%s (looking up)", wibble.host)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment