Skip to content

Commit

Permalink
Merge pull request #2 from wojas/add-close
Browse files Browse the repository at this point in the history
Add Close() receiver and go.mod
  • Loading branch information
Habbie authored Jun 10, 2020
2 parents e557afd + 130196c commit 562aa6b
Show file tree
Hide file tree
Showing 7 changed files with 447 additions and 4 deletions.
12 changes: 12 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# See https://github.com/golangci/golangci-lint/blob/master/.golangci.yml for more examples

linters:
enabled:
# In addition to default standard linters (see: golangci-lint help linters)
# Try extra ones line this: golangci-lint run --disable-all --enable=lll
- goimports
# For future consideration (requires some extra work, but suggestions look useful)
# - gocritic
# - golint # can sometimes be a bit pedantic
# - lll

10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
sudo: false
language: go
before_script:
- go get github.com/golangci/golangci-lint/cmd/golangci-lint
- go install github.com/golangci/golangci-lint/cmd/golangci-lint
env:
- GO111MODULE=on
go:
- 1.x # latest version
- 1.12 # lowest supported version
script:
- golangci-lint run --enable-all # this is excessive but we get away with it for now
- ./test.sh
10 changes: 10 additions & 0 deletions dnsdist/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import (
"golang.org/x/crypto/nacl/secretbox"
)

// Conn is a connection to the dnsdist console
type Conn struct {
conn net.Conn
readingNonce [24]byte
writingNonce [24]byte
key [32]byte
}

// Dial connects to a dnsdist server. The target address is a host with port.
// The secret is a base64 encoded 32 byte key as returned by makeKey() in the console.
func Dial(target string, secret string) (*Conn, error) {
ourNonce := make([]byte, 24)
_, err := rand.Read(ourNonce)
Expand Down Expand Up @@ -82,6 +85,8 @@ func incrementNonce(nonce *[24]byte) {
binary.BigEndian.PutUint32(nonce[:4], value)
}

// Command sends one command to dnsdist. It returns an error if the call failed.
// Application level (dnsdist Lua) errors do not result in a Go error.
func (dc *Conn) Command(cmd string) (string, error) {
encodedcommand := secretbox.Seal(nil, []byte(cmd), &dc.writingNonce, &dc.key)
incrementNonce(&dc.writingNonce)
Expand Down Expand Up @@ -117,3 +122,8 @@ func (dc *Conn) Command(cmd string) (string, error) {
}
return string(decodedresponse), nil
}

// Close closes the connection
func (dc *Conn) Close() error {
return dc.conn.Close()
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/PowerDNS/go-dnsdist-client

go 1.14

require (
github.com/golangci/golangci-lint v1.27.0
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9
)
399 changes: 399 additions & 0 deletions go.sum

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

set -ex
go test ./...
go run github.com/golangci/golangci-lint/cmd/golangci-lint run
7 changes: 7 additions & 0 deletions tools/tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build tools

package tools

import (
_ "github.com/golangci/golangci-lint/cmd/golangci-lint"
)

0 comments on commit 562aa6b

Please sign in to comment.