Skip to content

Commit

Permalink
Merge pull request #1 from kwitsch/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
kwitsch authored Jan 9, 2022
2 parents 0ce36c3 + 6e4a28f commit 244f4c1
Show file tree
Hide file tree
Showing 10 changed files with 761 additions and 0 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/autobuild.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: autobuild

on:
push:
branches:
- development
tags:
- v*


env:
IMAGE: tinymacdns

jobs:
docker:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: Docker meta
id: meta
uses: docker/metadata-action@v3
with:
images: ghcr.io/kwitsch/${{ env.IMAGE }}
tags: |
type=edge,branch=development
type=ref,event=tag
-
name: Set up QEMU
uses: docker/setup-qemu-action@v1
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
-
name: Login to Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
-
name: Build and push
uses: docker/build-push-action@v2
with:
context: .
platforms: linux/amd64,linux/arm64,linux/arm/v6,linux/arm/v7
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
BUILDTIME=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}
VERSION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}
REVISION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

# Dependency directories (remove the comment below to include it)
# vendor/

test/
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM ghcr.io/kwitsch/docker-buildimage:main AS build-env

ADD src .
RUN gobuild.sh -o tinymacdns

FROM scratch
COPY --from=build-env /builddir/tinymacdns /tinymacdns

ENTRYPOINT ["/tinymacdns"]
78 changes: 78 additions & 0 deletions src/cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package cache

import (
"fmt"
"strings"
"sync"
)

type Cache struct {
dns map[string]string
rdns map[string]string
lock sync.RWMutex
}

func New() *Cache {
return &Cache{
dns: make(map[string]string),
rdns: make(map[string]string),
}
}

func (c *Cache) Update(hostname, ip string) {
ihn := strings.ToLower(hostname)
c.Delete(ihn)

if revIp, revErr := reverseIP(ip); revErr == nil {
c.lock.Lock()
c.dns[ihn] = ip
c.rdns[revIp] = ihn
c.lock.Unlock()
}
}

func (c *Cache) Delete(hostname string) {
ihn := strings.ToLower(hostname)
if oip, dok := c.dns[ihn]; dok {
c.lock.Lock()
delete(c.dns, ihn)
revIp, _ := reverseIP(oip)
if _, rok := c.rdns[revIp]; rok {
delete(c.rdns, revIp)
}
c.lock.Unlock()
}
}

func (c *Cache) GetIp(hostname string) (string, bool) {
c.lock.RLock()
defer c.lock.RUnlock()
ip, ok := c.dns[hostname]
return ip, ok
}

func (c *Cache) GetHostname(reverseIP string) (string, bool) {
c.lock.RLock()
defer c.lock.RUnlock()
hostname, ok := c.rdns[reverseIP]
return hostname, ok
}

func (c *Cache) Print() {
fmt.Println("dns cache:")
for n, v := range c.dns {
fmt.Println("-", n, "=", v)
}
fmt.Println("rdns cache:")
for n, v := range c.rdns {
fmt.Println("-", n, "=", v)
}
}

func reverseIP(ip string) (string, error) {
parts := strings.Split(ip, ".")
if len(parts) == 4 {
return fmt.Sprintf("%s.%s.%s.%s", parts[3], parts[2], parts[1], parts[0]), nil
}
return "", fmt.Errorf("invalid ip")
}
62 changes: 62 additions & 0 deletions src/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package config

import (
"fmt"
"time"

. "github.com/kwitsch/go-dockerutils/config"
)

type Config struct {
Redis RedisConfig `koanf:"redis"`
Hosts map[string]HostConfig `koanf:"hosts"`
Verbose bool `koanf:"verbose" default:"false"`
}

type RedisConfig struct {
Address string `koanf:"address"`
Username string `koanf:"username"`
Password string `koanf:"password"`
Database int `koanf:"database" default:"0"`
Attempts int `koanf:"attempts" default:"3"`
Cooldown time.Duration `koanf:"cooldown" default:"1s"`
Intervall time.Duration `koan:"intervall" default:"5m"`
Verbose bool
}

type HostConfig struct {
Mac map[int]string `koanf:"mac"`
}

const prefix = "TMD_"

func Get() (*Config, error) {
var res Config
err := Load(prefix, &res)
if err == nil {

res.Redis.Verbose = res.Verbose

if res.Verbose {
logHosts(res.Hosts)
}

return &res, nil
}

return nil, err
}

func logHosts(hosts map[string]HostConfig) {
fmt.Println("Configured hosts:")
for k, v := range hosts {
macs := ""
for _, m := range v.Mac {
if len(macs) > 0 {
macs += ", "
}
macs += m
}
fmt.Println("-", k, ":", macs)
}
}
27 changes: 27 additions & 0 deletions src/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module github.com/kwitsch/TinyMacDns

go 1.17

require (
github.com/go-redis/redis/v8 v8.11.4
github.com/kwitsch/go-dockerutils v0.0.9
)

require (
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
)

require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/creasty/defaults v1.5.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/knadh/koanf v1.3.3 // indirect
github.com/miekg/dns v1.1.45
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
)
Loading

0 comments on commit 244f4c1

Please sign in to comment.