Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
arnested committed Dec 27, 2020
0 parents commit 274b20e
Show file tree
Hide file tree
Showing 16 changed files with 791 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: "/"
schedule:
interval: daily
timezone: Europe/Copenhagen
assignees:
- arnested
- package-ecosystem: gomod
directory: "/"
schedule:
interval: daily
timezone: Europe/Copenhagen
assignees:
- arnested
29 changes: 29 additions & 0 deletions .github/workflows/bump-version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Bump version
on:
push:
branches:
- main
jobs:
bump-version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Bump version and push tag
uses: anothrNick/github-tag-action@1.33.0
id: version
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
WITH_V: true
DEFAULT_BUMP: patch
- name: Create release
if: ${{ steps.version.outputs.new_tag }} != ""
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.version.outputs.new_tag }}
release_name: Release ${{ steps.version.outputs.new_tag }}
draft: false
prerelease: false
53 changes: 53 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: "CodeQL"

on:
push:
branches: [main]
pull_request:
# The branches below must be a subset of the branches above
branches: [main]
schedule:
- cron: '0 2 * * 5'

jobs:
analyse:
name: Analyse
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
# We must fetch at least the immediate parents so that if this is
# a pull request then we can checkout the head.
fetch-depth: 2

# If this run was triggered by a pull request event, then checkout
# the head of the pull request instead of the merge commit.
- run: git checkout HEAD^2
if: ${{ github.event_name == 'pull_request' }}

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: go

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
14 changes: 14 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: golangci-lint
on:
pull_request:
jobs:
golangci:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: latest
only-new-issues: true
68 changes: 68 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
on: push
name: Build and test
jobs:
go_generate:
name: Check generated code is up to date
if: '!github.event.deleted'
runs-on: ubuntu-latest
env:
workdir: go/src/arnested.dk/go/ldddns
steps:
- uses: actions/checkout@v2.3.1
with:
path: ${{env.workdir}}
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: '1.15'
- name: go generate
env:
GO111MODULE: 'on'
GOPATH: ${{ github.workspace }}/go
working-directory: ${{env.workdir}}
run: go generate -x
- name: Diff after go generate
working-directory: ${{env.workdir}}
run: git diff --exit-code
build_and_test:
name: Build and test
if: '!github.event.deleted'
runs-on: ubuntu-latest
strategy:
matrix:
go-version: ['1.15']
steps:
- uses: actions/checkout@v2.3.1
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: go test
env:
GO111MODULE: 'on'
run: go test -v -race -cover -covermode=atomic -coverprofile=coverage.txt ./...
- name: Upload coverage report to Codecov
uses: codecov/codecov-action@v1.1.0
with:
flags: go${{ matrix.go-version }}
token: ${{secrets.CODECOV_TOKEN}}
license_check:
name: License check
if: '!github.event.deleted'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2.3.1
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: '1.15'
- name: Install wwhrd
env:
GO111MODULE: 'off'
run: go get -u github.com/frapposelli/wwhrd
- name: go mod vendor
env:
GO111MODULE: 'on'
run: go mod vendor
- name: wwhrd check
run: wwhrd check
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/ldddns
10 changes: 10 additions & 0 deletions .wwhrd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
whitelist:
- MIT
- Apache-2.0
- BSD-2-Clause
- BSD-3-Clause
- CC-BY-SA-4.0

exceptions:
- github.com/gogo/protobuf/proto
105 changes: 105 additions & 0 deletions container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package main

import (
"context"
"fmt"
"log"
"strconv"
"strings"

"github.com/docker/docker/api/types"
"honnef.co/go/netdb"
)

func handleContainer(
ctx context.Context,
container types.ContainerJSON,
egs *EntryGroups,
status string,
) {
eg, commit, err := egs.Get(container.ID)
defer commit()

if err != nil {
panic(fmt.Errorf("cannot get entry group for container %q: %w", container.ID, err))
}

empty, _ := eg.IsEmpty()
if !empty {
_ = eg.Reset()
}

if status == "die" || status == "kill" || status == "pause" {
return
}

ips := extractIPNumbers(ctx, container)
if len(ips) == 0 {
return
}

hostname := extractHostname(ctx, container)
if hostname == "" {
return
}

services := extractServices(ctx, container)

hostname = rewriteHostname(ctx, hostname)

addToDNS(eg, hostname, ips, services)
}

// extractIPnumbers from a container.
func extractIPNumbers(_ context.Context, container types.ContainerJSON) []string {
ips := []string{}

if container.NetworkSettings.IPAddress != "" {
ips = append(ips, container.NetworkSettings.IPAddress)
}

for _, v := range container.NetworkSettings.Networks {
ips = append(ips, v.IPAddress)
}

return ips
}

// extractServices from a container.
func extractServices(_ context.Context, container types.ContainerJSON) map[string]uint16 {
services := map[string]uint16{}

for k := range container.NetworkSettings.Ports {
port := strings.SplitN(string(k), "/", 2)

proto := netdb.GetProtoByName(port[1])

portNumber, err := strconv.Atoi(port[0])
if err != nil {
log.Printf("Could not get port number from %q", k)

continue
}

service := netdb.GetServByPort(portNumber, proto)

if service == nil || proto == nil {
continue
}

services[fmt.Sprintf("_%s._%s", service.Name, proto.Name)] = uint16(portNumber)
}

return services
}

// extractHostname from a container.
func extractHostname(_ context.Context, container types.ContainerJSON) string {
for _, v := range container.Config.Env {
if strings.HasPrefix(v, "VIRTUAL_HOST=") {
return v[13:]
}
}

return ""
}
43 changes: 43 additions & 0 deletions dns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"fmt"

"github.com/holoplot/go-avahi"
)

func addToDNS(eg *avahi.EntryGroup, hostname string, ips []string, services map[string]uint16) {
if hostname == "" {
return
}

for _, ip := range ips {
if ip == "" {
continue
}

err := eg.AddAddress(avahi.InterfaceUnspec, avahi.ProtoUnspec, 0, hostname, ip)
if err != nil {
panic(fmt.Errorf("AddAddess() failed: %w", err))
}

logf(PriDebug, "added address for %q pointing to %q", hostname, ip)

for service, portNumber := range services {
err = eg.AddService(
avahi.InterfaceUnspec,
avahi.ProtoUnspec,
0,
hostname,
service,
"local",
hostname,
portNumber,
nil,
)
if err != nil {
panic(fmt.Errorf("AddService() failed: %w", err))
}
}
}
}
47 changes: 47 additions & 0 deletions entry_groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"fmt"
"sync"

"github.com/holoplot/go-avahi"
)

type EntryGroups struct {
avahiServer *avahi.Server
groups map[string]*avahi.EntryGroup
mutex sync.Mutex
}

func NewEntryGroups(avahiServer *avahi.Server) *EntryGroups {
return &EntryGroups{
avahiServer: avahiServer,
groups: make(map[string]*avahi.EntryGroup),
}
}

func (e *EntryGroups) Get(containerID string) (*avahi.EntryGroup, func(), error) {
commit := func() {
empty, _ := e.groups[containerID].IsEmpty()
if !empty {
err := e.groups[containerID].Commit()
if err != nil {
logf(PriErr, "error committing: %v\n", err)
}
}

e.mutex.Unlock()
}

e.mutex.Lock()
if _, ok := e.groups[containerID]; !ok {
eg, err := e.avahiServer.EntryGroupNew()
if err != nil {
return nil, commit, fmt.Errorf("error creating new entry group: %w", err)
}

e.groups[containerID] = eg
}

return e.groups[containerID], commit, nil
}
Loading

0 comments on commit 274b20e

Please sign in to comment.