Skip to content

Commit

Permalink
Merge pull request #3 from grishy/docs-and-refactor
Browse files Browse the repository at this point in the history
Docs and clean up
  • Loading branch information
grishy authored Jul 27, 2023
2 parents 103c76a + 406bcce commit f849624
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 27 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: commit
on:
push:
branches:
- main
pull_request:

permissions:
contents: read

jobs:
golangci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: "1.20"
cache: false

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.53
build_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: "1.20"
cache: false

- name: Verify dependencies
run: go mod verify

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: goreleaser
name: release

on:
push:
Expand All @@ -10,7 +10,7 @@ permissions:
contents: write

jobs:
goreleaser:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
9 changes: 5 additions & 4 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ builds:
- linux
- darwin
goarch:
- amd64
- arm
- arm64
- "386"
- "amd64"
- "arm"
- "arm64"
goarm:
- "6"
- "7"
Expand All @@ -34,7 +35,7 @@ release:
target_commitish: "{{ .Commit }}"
mode: replace
header: |
## ⚙️ The `{{ .Tag }}` release
## ℹ️ For information on how to install and use please see `README.md`
footer: |
Your PRs & issues are welcome! Thanks 😉
disable: false
Expand Down
88 changes: 83 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,88 @@
# go-avahi-cname
<img src="./docs/logo_x3.png" width="350">

![GitHub tag (with filter)](https://img.shields.io/github/v/tag/grishy/go-avahi-cname)
[![Go Report Card](https://goreportcard.com/badge/github.com/grishy/go-avahi-cname)](https://goreportcard.com/report/github.com/grishy/go-avahi-cname)
![Build Status](https://github.com/grishy/go-avahi-cname/actions/workflows/release.yml/badge.svg)

## About go-avahi-cname

This project publishe CNAME records pointing to the local host over multicast DNS using the **Avahi** daemon found in all major Linux distributions.
Since Avahi is compatible with Apple's Bonjour, these names are usable from MacOS X and Windows too.

### Goals

- ✅ No dependencies
- ✅ Small footprint, less than 3MB binary
- ✅ Support x86_64 and ARM
- ✅ Release binaries and containers

### Architecture

![Architecture](./docs/arch.excalidraw.svg)

## Usege and installation

All CNAMEs are specified as program arguments, no length limit.
You can use either just the name (`name1`), which will create a record as a subdomain for the current machine, or you can write the full FQDN (`name1.hostname.local.` domain with a dot on the end) format.

Example output on machine with hostname `lab`:

```plain
> ./go-avahi-cname git photo.local. example.lab.local.
2023/07/27 08:37:14 Creating publisher
2023/07/27 08:37:14 Formating CNAMEs:
2023/07/27 08:37:14 > 'git.lab.local.' (added current FQDN)
2023/07/27 08:37:14 > 'photo.local.'
2023/07/27 08:37:14 > 'example.lab.local.'
2023/07/27 08:37:14 Publishing every 5m0s and CNAME TTL=600s.
^C
2023/07/27 08:37:16 Closing publisher...
```

### Installation

#### Binary

Binary files can be taken as artifacts for [the Release](https://github.com/grishy/go-avahi-cname/releases). In this case, it would be better to create a systemd service.

#### Container

The images for each version are in [the Packages section](https://github.com/grishy/go-avahi-cname/pkgs/container/go-avahi-cname).
You need to provide the `/var/run/dbus/system_bus_socket` file to the container to be able to communicate with the host's Avahi daemon.

One-liner to run the container `v0.3.1`:

```bash
> docker run --restart=unless-stopped -d -v /var/run/dbus/system_bus_socket:/var/run/dbus/system_bus_socket ghcr.io/grishy/go-avahi-cname:v0.3.1 name1 name2.lab.local.
5a19790e06cca93016af6651d7af4046c24095a6909ace2fe26c3451fb98ceee

> docker logs 5a19790e06cca93016af6651d7af4046c24095a6909ace2fe26c3451fb98ceee
2023/07/27 08:49:02 Creating publisher
2023/07/27 08:49:02 Formating CNAMEs:
2023/07/27 08:49:02 > 'name1.lab.local.' (added current FQDN)
2023/07/27 08:49:02 > 'name2.lab.local.'
2023/07/27 08:49:02 Publishing every 5m0s ans CNAME TTL=600s.
```

Ansible task to run the container:

```yaml
- name: go-avahi-cname | Start container
community.docker.docker_container:
name: "go-avahi-cname"
image: "ghcr.io/grishy/go-avahi-cname:v0.3.1"
restart_policy: unless-stopped
volumes:
- "/var/run/dbus/system_bus_socket:/var/run/dbus/system_bus_socket" # access to avahi-daemon
command: "name1 name2 git"
```
## Source of inspiration
- https://gist.github.com/gdamjan/3168336
- https://github.com/tomoasleep/k8s-avahi
- https://web.archive.org/web/20151016190620/http://www.avahi.org/wiki/Examples/PythonPublishAlias
- https://pypi.org/project/mdns-publisher/
## TODO
## License
- [ ] Add FreeBSD support (https://github.com/godbus/dbus/issues/315)
Copyright © 2022 [Sergei G.](https://github.com/grishy)
This project is [MIT](./LICENSE) licensed.
17 changes: 17 additions & 0 deletions docs/arch.excalidraw.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/logo_x3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 31 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"fmt"
"log"
"os"
"os/signal"
Expand All @@ -11,23 +12,14 @@ import (
"github.com/miekg/dns"
)

const TTL = uint32(60 * 10) // seconds

func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

log.Println("Creating publisher")
publisher, err := publisher.NewPublisher()
if err != nil {
log.Fatalf("Can't create publisher: %v", err)
}
const TTL = uint32(10 * 60) // in seconds

func formatCname(hostnameFqdn string, cnames []string) []string {
log.Println("Formating CNAMEs:")
cnames := os.Args[1:]

for i, cname := range cnames {
if !dns.IsFqdn(cname) {
cnames[i] = dns.Fqdn(cname + "." + publisher.Fqdn())
cnames[i] = dns.Fqdn(cname + "." + hostnameFqdn)

log.Printf(" > '%s' (added current FQDN)", cnames[i])
continue
Expand All @@ -36,18 +28,27 @@ func main() {
log.Printf(" > '%s'", cname)
}

return cnames
}

func publishing(ctx context.Context, publisher *publisher.Publisher, cnames []string) {
resendDuration := time.Duration(TTL/2) * time.Second
log.Printf("Publishing every %v ans CNAME TTL=%ds.", resendDuration, TTL)
log.Printf("Publishing every %v and CNAME TTL=%ds.", resendDuration, TTL)

// To start publishing immediately
// https://github.com/golang/go/issues/17601
publisher.PublishCNAMES(os.Args[1:], TTL)
if err := publisher.PublishCNAMES(cnames, TTL); err != nil {
log.Fatalf("can't publish CNAMEs: %v", err)
}

for {
select {
case <-time.Tick(resendDuration):
publisher.PublishCNAMES(os.Args[1:], TTL)
if err := publisher.PublishCNAMES(cnames, TTL); err != nil {
log.Fatalf("can't publish CNAMEs: %v", err)
}
case <-ctx.Done():
fmt.Println()
log.Println("Closing publisher...")
if err := publisher.Close(); err != nil {
log.Fatalf("Can't close publisher: %v", err)
Expand All @@ -56,3 +57,17 @@ func main() {
}
}
}

func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

log.Println("Creating publisher")
publisher, err := publisher.NewPublisher()
if err != nil {
log.Fatalf("Can't create publisher: %v", err)
}

cnames := formatCname(publisher.Fqdn(), os.Args[1:])
publishing(ctx, publisher, cnames)
}

0 comments on commit f849624

Please sign in to comment.