Skip to content

Commit

Permalink
systemd-resolved (#1)
Browse files Browse the repository at this point in the history
* system-resolved implementation
  • Loading branch information
aloababa committed May 25, 2021
1 parent 9a6c6e1 commit 263a199
Show file tree
Hide file tree
Showing 6 changed files with 1,592 additions and 0 deletions.
158 changes: 158 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,161 @@ if watchdog != nil {
}()
}
```

## Resolved

[![PkgGoDev](https://pkg.go.dev/badge/github.com/iguanesolutions/go-systemd/resolved/resolved)](https://pkg.go.dev/github.com/iguanesolutions/go-systemd/v5/resolved)

This package is still under development and very experimental, do not use it in production.
We started this package in order to go deep into the DNS world. So we are opened to any suggestions/contributions on this.
DNS is not trivial at all so there can be some stuff that are not rfc compliant (like sorting addresses etc...).

The resolved package features:
* Pure Go implementation of `org.freedesktop.resolve1` dbus interface
* Resolver type (which uses the underlying dbus interface) that tries to implement the same methods as `net.Resolver` from Go standard library
* Unit tests (make sure Go resolver and systemd-resolved query the same dns server)

### Dbus

The following example shows how to use the resolve1 dbus connection to resolve an host:

```go
package main

import (
"context"
"fmt"
"log"
"syscall"

"github.com/iguanesolutions/go-systemd/v5/resolved"
)

func main() {
c, err := resolved.NewConn()
if err != nil {
log.Fatal("ERROR: ", err)
}
ctx := context.Background()
addrs, canonical, flags, err := c.ResolveHostname(ctx, 0, "google.com", syscall.AF_UNSPEC, 0)
if err != nil {
log.Println("ERROR: ", err)
} else {
fmt.Println("Addresses: ", addrs)
fmt.Println("Canonical: ", canonical)
fmt.Println("OutputFlags: ", flags)
}
err = c.Close()
if err != nil {
log.Println("ERROR: ", err)
}
}
```

Output:

```output
Addresses: [{
IfIndex: 2,
Family: 2,
IP: 142.250.74.238,
} {
IfIndex: 2,
Family: 10,
IP: 2a00:1450:4007:80b::200e,
}]
Canonical: google.com
Flags: 1
```

### Resolver

The following example shows how to use the resolved Resolver to resolve an host:

```go
package main

import (
"context"
"fmt"
"log"

"github.com/iguanesolutions/go-systemd/v5/resolved"
)

func main() {
r, err := resolved.NewResolver()
if err != nil {
log.Fatal("ERROR: ", err)
}
ctx := context.Background()
addrs, err := r.LookupHost(ctx, "google.com")
if err != nil {
log.Println("ERROR: ", err)
} else {
fmt.Println("Addresses: ", addrs)
}
err = r.Close()
if err != nil {
log.Println("ERROR: ", err)
}
}
```

Output:

```output
Addresses: [2a00:1450:4007:80b::200e 142.250.74.238]
```

### HTTP Client

The following example shows how to use the systemd-resolved Resolver with the Go http client from the standard library:

```go
package main

import (
"fmt"
"log"
"net/http"

"github.com/iguanesolutions/go-systemd/v5/resolved"
)

func main() {
r, err := resolved.NewResolver()
if err != nil {
log.Fatal("ERROR: ", err)
}
// if you want to make a custom http client using systemd-resolved as resolver
httpCli := &http.Client{
Transport: &http.Transport{
DialContext: r.DialContext,
},
}
// or if you don't have an http client you can call HTTPClient method on resolver
// it comes with some nice default values.
httpCli = r.HTTPClient()
resp, err := httpCli.Get("https://google.com")
if err != nil {
log.Println("ERROR: ", err)
} else {
fmt.Println("Status: ", resp.Status)
err = resp.Body.Close()
if err != nil {
log.Println("ERROR: ", err)
}
}
err = r.Close()
if err != nil {
log.Println("ERROR: ", err)
}
}
```

Output:

```output
Status: 200 OK
```
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
module github.com/iguanesolutions/go-systemd/v5

go 1.13

require (
github.com/godbus/dbus/v5 v5.0.4
github.com/miekg/dns v1.1.42
golang.org/x/net v0.0.0-20210525063256-abc453219eb5
)
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/miekg/dns v1.1.42 h1:gWGe42RGaIqXQZ+r3WUGEKBEtvPHY2SXo4dqixDNxuY=
github.com/miekg/dns v1.1.42/go.mod h1:+evo5L0630/F6ca/Z9+GAqzhjGyn8/c+TBaOyfEl0V4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 h1:wjuX4b5yYQnEQHzd+CBcrcC6OVR2J1CN6mUy0oSxIPo=
golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
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-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da h1:b3NXsE2LusjYGGjL5bxEVZZORm/YEFFrWFjR8eFrw/c=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
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=

0 comments on commit 263a199

Please sign in to comment.