-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 274b20e
Showing
16 changed files
with
791 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/ldddns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.