From a0a28c2786fcf1c6ab2e66a605aa7b8ffaedf308 Mon Sep 17 00:00:00 2001 From: Cellularhacker Date: Sat, 27 Aug 2022 09:33:44 +0900 Subject: [PATCH 01/46] Refactoring starting with go 1.19 --- build.sh | 2 +- dnschecks/dnschecks.go | 6 +- dnsqueue/dnsqueue.go | 67 ++++++++++++------- go.mod | 25 +++++++ go.sum | 116 +++++++++++++++++++++++++++++++++ history/filter.go | 20 +++--- history/history.go | 100 ++++++++++++++++++---------- namebench.go => main.go | 23 ++++--- ui/nodejs-webkit/redirect.html | 5 +- ui/ui.go | 35 +++++----- util/logger/logger.go | 76 +++++++++++++++++++++ 11 files changed, 374 insertions(+), 101 deletions(-) create mode 100644 go.mod create mode 100644 go.sum rename namebench.go => main.go (60%) create mode 100644 util/logger/logger.go diff --git a/build.sh b/build.sh index 75e1c4f..c6f6b21 100755 --- a/build.sh +++ b/build.sh @@ -1,4 +1,4 @@ #!/bin/sh packages=$(grep -hr "^package" */*.go | sort -u | cut -d" " -f2 | sed s/"^"/"\.\.\."/ | xargs) echo "Rebuilding $packages" -go install $packages +go install "$packages" diff --git a/dnschecks/dnschecks.go b/dnschecks/dnschecks.go index e474ce8..2636b97 100644 --- a/dnschecks/dnschecks.go +++ b/dnschecks/dnschecks.go @@ -2,7 +2,7 @@ package dnschecks import ( "github.com/google/namebench/dnsqueue" - "log" + "namebench/util/logger" "strings" ) @@ -17,10 +17,10 @@ func DnsSec(ip string) (ok bool, err error) { for _, answer := range result.Answers { // TODO(tstromberg): Implement properly. if strings.Contains(answer.String, "RRSIG") { - log.Printf("DnsSec for %s: true", ip) + logger.L.Infof("DnsSec for %s: true", ip) return true, err } } - log.Printf("DnsSec for %s: false", ip) + logger.L.Infof("DnsSec for %s: false", ip) return false, err } diff --git a/dnsqueue/dnsqueue.go b/dnsqueue/dnsqueue.go index 9a10e22..2b146b7 100644 --- a/dnsqueue/dnsqueue.go +++ b/dnsqueue/dnsqueue.go @@ -1,37 +1,56 @@ -// dnsqueue is a library for queueing up a large number of DNS requests. +// Package dnsqueue is a library for queueing up a large number of DNS requests. package dnsqueue import ( "errors" "fmt" + json "github.com/json-iterator/go" "github.com/miekg/dns" - "log" + "namebench/util/logger" "time" ) // Request contains data for making a DNS request type Request struct { - Destination string - RecordType string - RecordName string - VerifySignature bool + Destination string `json:"destination"` + RecordType string `json:"record_type"` + RecordName string `json:"record_name"` + VerifySignature bool `json:"verify_signature"` exit bool } +func (r *Request) ToJSON() []byte { + bs, _ := json.Marshal(r) + return bs +} + +func (r *Request) String() string { + return string(r.ToJSON()) +} + // Answer contains a single answer returned by a DNS server. type Answer struct { - Ttl uint32 - Name string - String string + Ttl uint32 `json:"ttl"` + Name string `json:"name"` + String string `json:"string"` } // Result contains metadata relating to a set of DNS server results. type Result struct { - Request Request - Duration time.Duration - Answers []Answer - Error string + Request Request `json:"request"` + Duration time.Duration `json:"duration"` + Answers []Answer `json:"answers"` + Error string `json:"error,omitempty"` +} + +func (r *Result) ToJSON() []byte { + bs, _ := json.Marshal(r) + return bs +} + +func (r *Result) String() string { + return string(r.ToJSON()) } // Queue contains methods and state for setting up a request queue. @@ -55,7 +74,7 @@ func StartQueue(size, workers int) (q *Queue) { return } -// Queue.Add adds a request to the queue. Only blocks if queue is full. +// Add Queue.Add adds a request to the queue. Only blocks if queue is full. func (q *Queue) Add(dest, record_type, record_name string) { q.Requests <- &Request{ Destination: dest, @@ -64,9 +83,9 @@ func (q *Queue) Add(dest, record_type, record_name string) { } } -// Queue.SendDieSignal sends a signal to the workers that they can go home now. +// SendCompletionSignal Queue.SendDieSignal sends a signal to the workers that they can go home now. func (q *Queue) SendCompletionSignal() { - log.Printf("Sending completion signal...") + logger.L.Infof("Sending completion signal...") for i := 0; i < q.WorkerCount; i++ { q.Requests <- &Request{exit: true} } @@ -76,26 +95,26 @@ func (q *Queue) SendCompletionSignal() { func startWorker(queue <-chan *Request, results chan<- *Result) { for request := range queue { if request.exit { - log.Printf("Completion received, worker is done.") + logger.L.Infof("Completion received, worker is done.") return } result, err := SendQuery(request) if err != nil { - log.Printf("Error sending query: %s", err) + logger.L.Errorf("Error sending query: %s", err) } - log.Printf("Sending back result: %s", result) + logger.L.Infof("Sending back result: %s", result.String()) results <- &result } } -// Send a DNS query via UDP, configured by a Request object. If successful, +// SendQuery Send a DNS query via UDP, configured by a Request object. If successful, // stores response details in Result object, otherwise, returns Result object // with an error string. func SendQuery(request *Request) (result Result, err error) { - log.Printf("Sending query: %s", request) + logger.L.Infof("Sending query: %s", request) result.Request = *request - record_type, ok := dns.StringToType[request.RecordType] + recordType, ok := dns.StringToType[request.RecordType] if !ok { result.Error = fmt.Sprintf("Invalid type: %s", request.RecordType) return result, errors.New(result.Error) @@ -103,10 +122,10 @@ func SendQuery(request *Request) (result Result, err error) { m := new(dns.Msg) if request.VerifySignature == true { - log.Printf("SetEdns0 for %s", request.RecordName) + logger.L.Infof("SetEdns0 for %s", request.RecordName) m.SetEdns0(4096, true) } - m.SetQuestion(request.RecordName, record_type) + m.SetQuestion(request.RecordName, recordType) c := new(dns.Client) in, rtt, err := c.Exchange(m, request.Destination) // log.Printf("Answer: %s [%d] %s", in, rtt, err) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2fc6315 --- /dev/null +++ b/go.mod @@ -0,0 +1,25 @@ +module namebench + +go 1.19 + +require ( + github.com/google/namebench v0.0.0-20181017155511-573c0eccd53d + github.com/mattn/go-sqlite3 v1.14.15 + github.com/miekg/dns v1.1.50 + golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b +) + +require ( + github.com/json-iterator/go v1.1.12 // indirect + github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/onsi/ginkgo v1.16.5 // indirect + github.com/sirupsen/logrus v1.9.0 // indirect + go.uber.org/atomic v1.7.0 // indirect + go.uber.org/multierr v1.6.0 // indirect + go.uber.org/zap v1.23.0 // indirect + golang.org/x/mod v0.4.2 // indirect + golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect + golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2 // indirect + golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..de708d0 --- /dev/null +++ b/go.sum @@ -0,0 +1,116 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/namebench v0.0.0-20181017155511-573c0eccd53d h1:oPUzfPPBx2xHwzsbienxORzgkjj6h2rwbK9ZWTROz6A= +github.com/google/namebench v0.0.0-20181017155511-573c0eccd53d/go.mod h1:BJj1NvwTN7Cg/kfDlcQflLuqOGoORLd7AAPTN6cSQME= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI= +github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= +github.com/miekg/dns v1.1.50 h1:DQUfb9uc6smULcREF09Uc+/Gd46YWqJd5DbpPE9xkcA= +github.com/miekg/dns v1.1.50/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= +github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= +github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= +github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= +github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= +github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= +github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= +go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= +go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= +go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY= +go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b h1:ZmngSVLe/wycRns9MKikG9OWIEjGcGAkacif7oYQaUY= +golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 h1:WIoqL4EROvwiPdUtaip4VcDdpZ4kha7wBWZrbVKCIZg= +golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2 h1:BonxutuHCTL0rBDnZlKjpGIQFTjyUVTexFOdWkB6Fg0= +golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/history/filter.go b/history/filter.go index c6d6527..6eae814 100644 --- a/history/filter.go +++ b/history/filter.go @@ -1,40 +1,40 @@ -// part of the history package, provides filtering capabilities. +// Package history part of the history package, provides filtering capabilities. package history import ( "golang.org/x/net/publicsuffix" - "log" "math/rand" + "namebench/util/logger" "net/url" "regexp" ) var ( - internal_re = regexp.MustCompile(`\.corp|\.sandbox\.|\.borg\.|\.hot\.|internal|dmz|\._[ut][dc]p\.|intra|\.\w$|\.\w{5,}$`) + internalRe = regexp.MustCompile(`\.corp|\.sandbox\.|\.borg\.|\.hot\.|internal|dmz|\._[ut][dc]p\.|intra|\.\w$|\.\w{5,}$`) ) func isPossiblyInternal(addr string) bool { // note: this happens to reject IPs and anything with a port at the end. _, icann := publicsuffix.PublicSuffix(addr) if !icann { - log.Printf("%s does not have a public suffix", addr) + logger.L.Infof("%s does not have a public suffix", addr) return true } - if internal_re.MatchString(addr) { - log.Printf("%s may be internal.", addr) + if internalRe.MatchString(addr) { + logger.L.Infof("%s may be internal.", addr) return true } return false } -// Filter out external hostnames from history +// ExternalHostnames Filter out external hostnames from history func ExternalHostnames(entries []string) (hostnames []string) { counter := 0 for _, uString := range entries { u, err := url.ParseRequestURI(uString) if err != nil { - log.Printf("Error parsing %s: %s", uString, err) + logger.L.Errorf("Error parsing %s: %s", uString, err) continue } if !isPossiblyInternal(u.Host) { @@ -45,7 +45,7 @@ func ExternalHostnames(entries []string) (hostnames []string) { return } -// Filter input array for unique entries. +// Uniq Filter input array for unique entries. func Uniq(input []string) (output []string) { last := "" for _, i := range input { @@ -56,7 +56,7 @@ func Uniq(input []string) (output []string) { return } -// Randomly select X number of entries. +// Random Randomly select X number of entries. func Random(count int, input []string) (output []string) { selected := make(map[int]bool) diff --git a/history/history.go b/history/history.go index 37f8c3e..f883f48 100644 --- a/history/history.go +++ b/history/history.go @@ -1,4 +1,4 @@ -// the history package is a collection of functions for reading history files from browsers. +// Package history the history package is a collection of functions for reading history files from browsers. package history import ( @@ -6,20 +6,42 @@ import ( "fmt" _ "github.com/mattn/go-sqlite3" "io" - "io/ioutil" - "log" + "namebench/util/logger" "os" + "sync" ) +type chromePaths struct { + urls []string + m *sync.Mutex +} + +func (cps *chromePaths) Append(path string) { + cps.m.Lock() + cps.urls = append(cps.urls, path) + cps.m.Unlock() +} + +func (cps *chromePaths) Get() []string { + return cps.urls +} + +func NewChromePaths() *chromePaths { + return &chromePaths{ + urls: make([]string, 0), + m: &sync.Mutex{}, + } +} + // unlockDatabase is a bad hack for opening potentially locked SQLite databases. -func unlockDatabase(path string) (unlocked_path string, err error) { +func unlockDatabase(path string) (string, error) { f, err := os.Open(path) if err != nil { return "", err } defer f.Close() - t, err := ioutil.TempFile("", "") + t, err := os.CreateTemp("", "") if err != nil { return "", err } @@ -29,12 +51,12 @@ func unlockDatabase(path string) (unlocked_path string, err error) { if err != nil { return "", err } - log.Printf("%d bytes written to %s", written, t.Name()) + logger.L.Infof("%d bytes written to %s", written, t.Name()) return t.Name(), err } // Chrome returns an array of URLs found in Chrome's history within X days -func Chrome(days int) (urls []string, err error) { +func Chrome(days int) ([]string, error) { paths := []string{ "${HOME}/Library/Application Support/Google/Chrome/Default/History", "${HOME}/.config/google-chrome/Default/History", @@ -42,6 +64,7 @@ func Chrome(days int) (urls []string, err error) { "${USERPROFILE}/Local Settings/Application Data/Google/Chrome/User Data/Default/History", } + cps := NewChromePaths() query := fmt.Sprintf( `SELECT urls.url FROM visits LEFT JOIN urls ON visits.url = urls.id @@ -50,36 +73,45 @@ func Chrome(days int) (urls []string, err error) { ORDER BY visit_time DESC`, days) for _, p := range paths { - path := os.ExpandEnv(p) - log.Printf("Checking %s", path) - _, err := os.Stat(path) - if err != nil { - continue - } + findAndAppendPath(p, query, cps) + } + return cps.Get(), nil +} - unlocked_path, err := unlockDatabase(path) - if err != nil { - return nil, err - } - defer os.Remove(unlocked_path) +func findAndAppendPath(cPath string, query string, cps *chromePaths) { + p := os.ExpandEnv(cPath) + logger.L.Infof("Checking %s", cPath) + _, err := os.Stat(p) + if err != nil { + logger.L.Errorln("os.Stat(p)", err) + return + } - db, err := sql.Open("sqlite3", unlocked_path) - if err != nil { - return nil, err - } + unlockedPath, err := unlockDatabase(p) + if err != nil { + logger.L.Errorln("unlockDatabase(p)", err) + return + } + defer os.Remove(unlockedPath) - rows, err := db.Query(query) - if err != nil { - log.Printf("Query failed: %s", err) - return nil, err - } - var url string - for rows.Next() { - rows.Scan(&url) - urls = append(urls, url) + db, err := sql.Open("sqlite3", unlockedPath) + if err != nil { + logger.L.Errorln(`sql.Open("sqlite3", unlockedPath)`, err) + return + } + + rows, err := db.Query(query) + if err != nil { + logger.L.Errorf("Query failed: %s", err) + return + } + defer rows.Close() + + for rows.Next() { + url := "" + if err2 := rows.Scan(&url); err2 != nil { + continue } - rows.Close() - return urls, err + cps.Append(url) } - return } diff --git a/namebench.go b/main.go similarity index 60% rename from namebench.go rename to main.go index c1c4463..598de23 100644 --- a/namebench.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "log" + "namebench/util/logger" "net" "net/http" "os" @@ -12,18 +13,22 @@ import ( "github.com/google/namebench/ui" ) -var nw_path = flag.String("nw_path", "/Applications/node-webkit.app/Contents/MacOS/node-webkit", +var nwPath = flag.String("nw_path", "/Applications/node-webkit.app/Contents/MacOS/node-webkit", "Path to nodejs-webkit binary") -var nw_package = flag.String("nw_package", "./ui/app.nw", "Path to nodejs-webkit package") +var nwPackage = flag.String("nw_package", "./ui/app.nw", "Path to nodejs-webkit package") var port = flag.Int("port", 0, "Port to listen on") +func init() { + logger.Init() +} + // openWindow opens a nodejs-webkit window, and points it at the given URL. func openWindow(url string) (err error) { os.Setenv("APP_URL", url) - cmd := exec.Command(*nw_path, *nw_package) - if err := cmd.Run(); err != nil { - log.Printf("error running %s %s: %s", *nw_path, *nw_package, err) - return err + cmd := exec.Command(*nwPath, *nwPackage) + if err = cmd.Run(); err != nil { + logger.L.Errorf("error running %s %s: %s", *nwPath, *nwPackage, err) + return } return } @@ -33,7 +38,7 @@ func main() { ui.RegisterHandlers() if *port != 0 { - log.Printf("Listening at :%d", *port) + logger.L.Infof("Listening at :%d", *port) err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil) if err != nil { log.Fatalf("Failed to listen on %d: %s", *port, err) @@ -44,8 +49,8 @@ func main() { log.Fatalf("Failed to listen: %s", err) } url := fmt.Sprintf("http://%s/", listener.Addr().String()) - log.Printf("URL: %s", url) + logger.L.Infof("URL: %s", url) go openWindow(url) - panic(http.Serve(listener, nil)) + logger.L.Fatal(http.Serve(listener, nil)) } } diff --git a/ui/nodejs-webkit/redirect.html b/ui/nodejs-webkit/redirect.html index eb61ab7..4e8396b 100644 --- a/ui/nodejs-webkit/redirect.html +++ b/ui/nodejs-webkit/redirect.html @@ -1,5 +1,6 @@ - - + + + Waiting for page...