Skip to content

Commit

Permalink
Elapsed time in JSON output (#61)
Browse files Browse the repository at this point in the history
* Add ElapsedTime to JSON output

Add the ElapsedTime to the JSON output and expose the tcp:// method in the documentation

* Run go fmt

* Address comment from ameshkov

Instead of manipulating JSON strings, load the data into a variable with an extended type and then marshal the variable

* Fix lint error

un-export JSONMsg type
  • Loading branch information
mohshami authored Jan 24, 2024
1 parent 0acf48e commit bbd6081
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# dnslookup

Simple command line utility to make DNS lookups. Supports all known DNS
protocols: plain DNS, DoH, DoT, DoQ, DNSCrypt.
protocols: plain DNS, plain DNS-over-TCP, DoH, DoT, DoQ, DNSCrypt.

### How to install

Expand Down Expand Up @@ -35,6 +35,12 @@ Plain DNS:
dnslookup example.org 94.140.14.14
```

Plain DNS-over-TCP:

```shell
dnslookup example.org tcp://94.140.14.14
```

DNS-over-TLS:

```shell
Expand Down
16 changes: 14 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import (
"github.com/miekg/dns"
)

type jsonMsg struct {
dns.Msg
Elapsed time.Duration `json:"elapsed"`
}

// VersionString -- see the makefile
var VersionString = "master"

Expand Down Expand Up @@ -171,8 +176,15 @@ func main() {
os.Stdout.WriteString(msg)
os.Stdout.WriteString(reply.String() + "\n")
} else {
// Prevent JSON parsing from skewing results
endTime := time.Now()

var JSONreply jsonMsg
JSONreply.Msg = *reply
JSONreply.Elapsed = endTime.Sub(startTime)

var b []byte
b, err = json.MarshalIndent(reply, "", " ")
b, err = json.MarshalIndent(JSONreply, "", " ")
if err != nil {
log.Fatalf("Cannot marshal json: %s", err)
}
Expand Down Expand Up @@ -285,7 +297,7 @@ func getRRType() (rrType uint16) {
func usage() {
os.Stdout.WriteString("Usage: dnslookup <domain> <server> [<providerName> <serverPk>]\n")
os.Stdout.WriteString("<domain>: mandatory, domain name to lookup\n")
os.Stdout.WriteString("<server>: mandatory, server address. Supported: plain, tls:// (DOT), https:// (DOH), sdns:// (DNSCrypt), quic:// (DOQ)\n")
os.Stdout.WriteString("<server>: mandatory, server address. Supported: plain, tcp:// (TCP), tls:// (DOT), https:// (DOH), sdns:// (DNSCrypt), quic:// (DOQ)\n")
os.Stdout.WriteString("<providerName>: optional, DNSCrypt provider name\n")
os.Stdout.WriteString("<serverPk>: optional, DNSCrypt server public key\n")
}
Expand Down

0 comments on commit bbd6081

Please sign in to comment.