add log() and go-arg

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-02-20 22:22:58 -06:00
parent d53d4f1647
commit 146249a13d
5 changed files with 212 additions and 37 deletions

View File

@ -1,3 +1,13 @@
# Intro
Attempt at some code to validate via DNS / DNSSEC
the hostname is valid on a simple socket listen
server
This recursively runs from the root TLD's and purposesly bypasses
any DNS cache so that the DNS <-> IP address mappings can be verified
## References
* https://github.com/mactsouk/opensource.com.git
* https://coderwall.com/p/wohavg/creating-a-simple-tcp-server-in-go

45
args.go Normal file
View File

@ -0,0 +1,45 @@
package dnssecsocket
//
// By using the package "github.com/alexflint/go-arg",
// these can be configured from the command line
//
import (
// arg "github.com/alexflint/go-arg"
// "log"
// "os"
)
type Args struct {
VerboseDnssec bool `arg:"--verbose-dnssec" help:"debug dnssec lookups"`
Foo string `arg:"env:USER"`
}
var args struct {
Args
Verbose bool
}
func Parse (b bool) {
args.Verbose = b
args.VerboseDnssec = b
}
// I attempted to pass the *arg.Parser down
// to see if I could find the value somewhere but I couldn't find it
/*
var conf arg.Config
func Parse (p *arg.Parser) {
// conf.Program = "control-panel-dns"
// conf.IgnoreEnv = false
// arg.NewParser(conf, &args)
log.Println("fuckit", p, args.VerboseDnssec)
for i, v := range p.SubcommandNames() {
log.Println("dnssec.Parse", i, v)
}
p.Jcarr()
exit()
}
*/

View File

@ -11,7 +11,7 @@ import "math/rand"
import "net"
import "strconv"
import "strings"
import log "github.com/sirupsen/logrus"
// import log "github.com/sirupsen/logrus"
// import "github.com/wercker/journalhook"
import "git.wit.org/wit/shell"
@ -49,41 +49,41 @@ func HandleConnection(conn *net.TCPConn) {
// spew.Dump(conn)
// ipv6client := GetRemoteAddr(c)
ipv6client := conn.RemoteAddr()
log.Println("Serving to %s as the IPv6 client", ipv6client)
log(args.VerboseDnssec, "Serving to %s as the IPv6 client", ipv6client)
// setup this TCP socket as the "standard input"
// newStdin, _ := bufio.NewReader(conn.File())
newStdin, _ := conn.File()
newreader := bufio.NewReader(newStdin)
log.Println("Waiting for the client to tell me its name")
log(args.VerboseDnssec, "Waiting for the client to tell me its name")
netData, err := newreader.ReadString('\n')
if err != nil {
log.Println(err)
log(args.VerboseDnssec, err)
return
}
clientHostname := strings.TrimSpace(netData)
log.Println("Recieved client hostname as:", clientHostname)
log(args.VerboseDnssec, "Recieved client hostname as:", clientHostname)
dnsRR := Dnstrace(clientHostname, "AAAA")
if (dnsRR == nil) {
log.Println("dnsRR IS NIL")
log.Println("dnsRR IS NIL")
log.Println("dnsRR IS NIL")
log(args.VerboseDnssec, "dnsRR IS NIL")
log(args.VerboseDnssec, "dnsRR IS NIL")
log(args.VerboseDnssec, "dnsRR IS NIL")
conn.Close()
return
}
ipaddr := dns.Field(dnsRR[1], 1)
log.Println("Client claims to be: ", ipaddr)
log.Println("Serving to IPv6 client:", ipv6client)
log(args.VerboseDnssec, "Client claims to be: ", ipaddr)
log(args.VerboseDnssec, "Serving to IPv6 client:", ipv6client)
/* TODO: figure out how to fix this check
if (ipaddr != ipv6client) {
log.Println()
log.Println("DNSSEC ERROR: client IPv6 does not work")
log.Println("DNSSEC ERROR: client IPv6 does not work")
log.Println("DNSSEC ERROR: client IPv6 does not work")
log.Println()
log(args.VerboseDnssec)
log(args.VerboseDnssec, "DNSSEC ERROR: client IPv6 does not work")
log(args.VerboseDnssec, "DNSSEC ERROR: client IPv6 does not work")
log(args.VerboseDnssec, "DNSSEC ERROR: client IPv6 does not work")
log(args.VerboseDnssec)
conn.Close()
return
}
@ -101,7 +101,7 @@ func HandleConnection(conn *net.TCPConn) {
defer conn.Close()
netData, err := newreader.ReadString('\n')
if err != nil {
log.Println(err)
log(args.VerboseDnssec, err)
return
}
@ -109,17 +109,17 @@ func HandleConnection(conn *net.TCPConn) {
if temp == "STOP" {
break
}
log.Println("Recieved: ", temp)
log(args.VerboseDnssec, "Recieved: ", temp)
if (temp == "list") {
log.Println("Should run list here")
log(args.VerboseDnssec, "Should run list here")
shell.SetStdout(f)
shell.Run("/root/bin/list.testing.com")
shell.SetStdout(os.Stdout)
}
if (temp == "cpuinfo") {
log.Println("Should cat /proc/cpuinfo")
log(args.VerboseDnssec, "Should cat /proc/cpuinfo")
shell.SetStdout(f)
shell.Run("cat /proc/cpuinfo")
shell.SetStdout(os.Stdout)

View File

@ -4,14 +4,14 @@ package dnssecsocket
import "fmt"
import "net"
import "os"
// import "os"
import "strings"
import "time"
import "github.com/miekg/dns"
import "github.com/rs/dnstrace/client"
import log "github.com/sirupsen/logrus"
// import log "github.com/sirupsen/logrus"
// this is cool, but breaks the Windows build
// import "github.com/wercker/journalhook"
@ -75,13 +75,13 @@ func Dnstrace(hostname string, qtypestr string) []dns.RR {
qname := m.Question[0].Name
qtype := dns.TypeToString[m.Question[0].Qtype]
if i > 1 {
log.Println()
log(args.VerboseDnssec)
}
log.Printf("%d - query %s %s", i, qtype, qname)
log(args.VerboseDnssec, "%d - query %s %s", i, qtype, qname)
if r != nil {
log.Printf(": %s", strings.Replace(strings.Replace(r.MsgHdr.String(), ";; ", "", -1), "\n", ", ", -1))
log(args.VerboseDnssec, ": %s", strings.Replace(strings.Replace(r.MsgHdr.String(), ";; ", "", -1), "\n", ", ", -1))
}
log.Println()
log(args.VerboseDnssec)
for _, pr := range rs {
ln := 0
if pr.Msg != nil {
@ -94,15 +94,15 @@ func Dnstrace(hostname string, qtypestr string) []dns.RR {
} else if pr.Server.LookupRTT > 0 {
lrtt = fmt.Sprintf("%.2fms", float64(pr.Server.LookupRTT)/float64(time.Millisecond))
}
log.Printf(col(" - %d bytes in %.2fms + %s lookup on %s(%s)", cDarkGray), ln, rtt, lrtt, pr.Server.Name, pr.Addr)
log(args.VerboseDnssec, col(" - %d bytes in %.2fms + %s lookup on %s(%s)", cDarkGray), ln, rtt, lrtt, pr.Server.Name, pr.Addr)
if pr.Err != nil {
err := pr.Err
if oerr, ok := err.(*net.OpError); ok {
err = oerr.Err
}
log.Printf(": %v", col(err, cRed))
log(args.VerboseDnssec, ": %v", col(err, cRed))
}
log.Print("\n")
log(args.VerboseDnssec, "\n")
}
switch rtype {
@ -122,28 +122,29 @@ func Dnstrace(hostname string, qtypestr string) []dns.RR {
} else {
glue = col("no glue", cYellow)
}
log.Printf("%s %d NS %s (%s)\n", label, s.TTL, s.Name, glue)
log(args.VerboseDnssec, "%s %d NS %s (%s)\n", label, s.TTL, s.Name, glue)
}
case client.ResponseTypeCNAME:
for _, rr := range r.Answer {
log.Println(rr)
log(args.VerboseDnssec, rr)
}
}
},
FollowingCNAME: func(domain, target string) {
log.Printf(col("\n~ following CNAME %s -> %s\n", cBlue), domain, target)
log(args.VerboseDnssec, col("\n~ following CNAME %s -> %s\n", cBlue), domain, target)
},
}
r, rtt, err := c.RecursiveQuery(m, t)
if err != nil {
log.Printf(col("*** error: %v\n", cRed), err)
os.Exit(1)
log(args.VerboseDnssec, col("*** error: %v\n", cRed), err)
exit(1)
}
log.Println()
log.Printf(col(";; Cold best path time: %s\n\n", cGray), rtt)
log(args.VerboseDnssec)
log(args.VerboseDnssec, col(";; Cold best path time: %s\n\n", cGray), rtt)
for i, rr := range r.Answer {
log.Println("r.Answer =", i, rr)
log(args.VerboseDnssec, "r.Answer =", i, rr, args.VerboseDnssec)
// exit(0)
}
return r.Answer
// for _, rr := range r.Answer {
@ -163,6 +164,6 @@ func ResolveIPv6hostname(hostname string) *net.TCPAddr {
}
func UseJournalctl() {
log.Println("journalhook is disabled because it breaks the Windows build right now")
log(args.VerboseDnssec, "journalhook is disabled because it breaks the Windows build right now")
// journalhook.Enable()
}

119
log.go Normal file
View File

@ -0,0 +1,119 @@
//
// version v1.1
//
// I like things to be easy.
//
// this means all the log settings are in one place. it should allow
// things to be over-ridden externally to the library
// but still allow command line --args to pass debugging settings
//
// I also have a generic sleep() and exit() in here because it's simple
//
// Usage:
//
// log("something", foo, bar)
// var DEBUG bool = true
// log(DEBUG, "something else", someOtherVariable) # if DEBUG == false, return doing nothing
// log(SPEW, "something else", someOtherVariable) # this get's sent to spew.Dump(). Very useful for debugging!
//
package dnssecsocket
import (
"os"
golog "log"
"time"
"reflect"
"github.com/davecgh/go-spew/spew"
// "net"
)
var LOGOFF bool = false // turn this off, all logging stops
var WARN bool
var INFO bool
type spewt struct {
a bool
}
var SPEW spewt
/*
sleep() # you know what this does? sleeps for 1 second. yep. dump. easy.
sleep(.1) # you know what this does? yes, it sleeps for 1/10th of a second
*/
func sleep(a ...any) {
if (a == nil) {
time.Sleep(time.Second)
return
}
log(args.Verbose, "sleep", a[0])
switch a[0].(type) {
case int:
time.Sleep(time.Duration(a[0].(int)) * time.Second)
case float64:
time.Sleep(time.Duration(a[0].(float64) * 1000) * time.Millisecond)
default:
log("sleep a[0], type = ", a[0], reflect.TypeOf(a[0]))
}
}
/*
exit() # yep. exits. I guess everything must be fine
exit(3) # I guess 3 it is then
exit("dont like apples") # ok. I'll make a note of that
*/
func exit(a ...any) {
log("exit", a)
//if (a) {
// os.Exit(a)
//}
os.Exit(0)
}
/*
I've spent, am spending, too much time thinking about 'logging'. 'log', 'logrus', 'zap', whatever.
I'm not twitter. i don't give a fuck about how many nanoseconds it takes to log. Anyway, this
implementation is probably faster than all of those because you just set one bool to FALSE
and it all stops.
Sometimes I need to capture to stdout, sometimes stdout can't
work because it doesn't exist for the user. This whole thing is a PITA. Then it's spread
over 8 million references in every .go file. I'm tapping out and putting
it in one place. here it is. Also, this makes having debug levels really fucking easy.
You can define whatever level of logging you want from anywhere (command line) etc.
log() # doesn't do anything
log(stuff) # sends it to whatever log you define in a single place. here is the place
*/
func log(a ...any) {
if (LOGOFF) {
return
}
if (a == nil) {
return
}
var blah bool
if (reflect.TypeOf(a[0]) == reflect.TypeOf(blah)) {
// golog.Println("\t a[0] = bool")
if (a[0] == false) {
return
}
a = a[1:]
}
if (reflect.TypeOf(a[0]) == reflect.TypeOf(SPEW)) {
a = a[1:]
spew.Dump(a)
return
}
golog.Println(a...)
// golog.Println("\t a[0] =", a[0])
// for argNum, arg := range a {
// golog.Println("\t", argNum, arg)
// }
}