Skip to content

Commit

Permalink
Add some godoc goodness
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-campbell committed Jul 16, 2018
1 parent b25b2eb commit 5b3061c
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,28 @@ import (
)

var (
inConfig = flag.String("configfile", "crapdns.conf", "Use configuration file ( default: crapdns.conf)")
inDomains = flag.String("domains", "", "A comma-separated list of domains to answer for. (Disables config file).")
// inConfig is a flag to pass a configuration file with a list of domains.
inConfig = flag.String("configfile", "crapdns.conf", "Use configuration file ( default: crapdns.conf)")

// inDomains is a flag for passing domains on the commanf-line
inDomains = flag.String("domains", "", "A comma-separated list of domains to answer for. (Disables config file).")

// parsedDomains stores an array of domain strings.
parsedDomains []string
)

const (
// targetDir is the path to the MacOS resolver directory.
targetDir = "/etc/resolver/"
fileSig = "###CRAPDNS###"

// fileSig is added toe resolver files we generate
// so that we only delete our own files.
fileSig = "###CRAPDNS###"
)

// panicExit is for passing an exit code up through panic()
// instead of calling os.Exit() directly. This means we can
// use a deferred fumction to cleanup everything.
type panicExit struct{ Code int }

func main() {
Expand All @@ -45,8 +57,10 @@ func main() {

parsedDomains = setupDomains()

// server listens only on loopback port 53 UDP
server := &dns.Server{Addr: "127.0.0.1:53", Net: "udp"}

// Run our server in a goroutine.
go func() {

if err := server.ListenAndServe(); err != nil {
Expand Down Expand Up @@ -76,6 +90,9 @@ func handleExit() {
}
}

// handleRequest(w dns.ResponseWriter, r *dns.Msg) handles incoming DNS
// queries and returns an "A" record pointing to the loopback address.
// If the domain is not listed in the configuration, it returns NXDOMAIN.
func handleRequest(w dns.ResponseWriter, r *dns.Msg) {

var found = false
Expand Down Expand Up @@ -106,6 +123,8 @@ func handleRequest(w dns.ResponseWriter, r *dns.Msg) {
w.WriteMsg(m)
}

// setupDomains sets up the resolvers for each domain
// passed either on the command-line or from a config file.
func setupDomains() []string {

var myDomains []string
Expand Down Expand Up @@ -140,6 +159,8 @@ func setupDomains() []string {
return myDomains
}

// cleanupDomains iterates through the resover directry, looking for files
// with our signature (fileSig) and removing them.
func cleanupDomains() {
// Look for our files in the resolver directory
fmt.Println("Cleaning up")
Expand Down

0 comments on commit 5b3061c

Please sign in to comment.