From 9887fbf3a9f404250cf6da1a27b070402cace226 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Fri, 7 Jan 2022 21:49:22 +0100 Subject: [PATCH 01/15] added redis config --- src/config/config.go | 36 +++++++++++ src/go.mod | 13 ++++ src/go.sum | 141 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+) create mode 100644 src/config/config.go create mode 100644 src/go.mod create mode 100644 src/go.sum diff --git a/src/config/config.go b/src/config/config.go new file mode 100644 index 0000000..f9b9736 --- /dev/null +++ b/src/config/config.go @@ -0,0 +1,36 @@ +package config + +import ( + "time" + + . "github.com/kwitsch/go-dockerutils/config" +) + +type Config struct { + Redis RedisConfig `koanf:"redis"` + 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"` + Verbose bool +} + +const prefix = "TMD_" + +func Get() (*Config, error) { + var res Config + err := Load(prefix, &res) + if err == nil { + + res.Redis.Verbose = res.Verbose + + return &res, nil + } + return nil, err +} diff --git a/src/go.mod b/src/go.mod new file mode 100644 index 0000000..6bc402b --- /dev/null +++ b/src/go.mod @@ -0,0 +1,13 @@ +module github.com/kwitsch/TinyMacDns + +go 1.17 + +require ( + github.com/creasty/defaults v1.5.2 // indirect + github.com/knadh/koanf v1.3.3 // indirect + github.com/kwitsch/go-dockerutils v0.0.9 // indirect + github.com/mitchellh/copystructure v1.2.0 // indirect + github.com/mitchellh/mapstructure v1.4.1 // indirect + github.com/mitchellh/reflectwalk v1.0.2 // indirect + github.com/ramr/go-reaper v0.2.1 // indirect +) diff --git a/src/go.sum b/src/go.sum new file mode 100644 index 0000000..fe2d1ee --- /dev/null +++ b/src/go.sum @@ -0,0 +1,141 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= +github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= +github.com/aws/aws-sdk-go-v2 v1.9.2/go.mod h1:cK/D0BBs0b/oWPIcX/Z/obahJK1TT7IPVjy53i/mX/4= +github.com/aws/aws-sdk-go-v2/config v1.8.3/go.mod h1:4AEiLtAb8kLs7vgw2ZV3p2VZ1+hBavOc84hqxVNpCyw= +github.com/aws/aws-sdk-go-v2/credentials v1.4.3/go.mod h1:FNNC6nQZQUuyhq5aE5c7ata8o9e4ECGmS4lAXC7o1mQ= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.6.0/go.mod h1:gqlclDEZp4aqJOancXK6TN24aKhT0W0Ae9MHk3wzTMM= +github.com/aws/aws-sdk-go-v2/internal/ini v1.2.4/go.mod h1:ZcBrrI3zBKlhGFNYWvju0I3TR93I7YIgAfy82Fh4lcQ= +github.com/aws/aws-sdk-go-v2/service/appconfig v1.4.2/go.mod h1:FZ3HkCe+b10uFZZkFdvf98LHW21k49W8o8J366lqVKY= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.3.2/go.mod h1:72HRZDLMtmVQiLG2tLfQcaWLCssELvGl+Zf2WVxMmR8= +github.com/aws/aws-sdk-go-v2/service/sso v1.4.2/go.mod h1:NBvT9R1MEF+Ud6ApJKM0G+IkPchKS7p7c2YPKwHmBOk= +github.com/aws/aws-sdk-go-v2/service/sts v1.7.2/go.mod h1:8EzeIqfWt2wWT4rJVu3f21TfrhJ8AEMzVybRNSb/b4g= +github.com/aws/smithy-go v1.8.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E= +github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/creasty/defaults v1.5.2 h1:/VfB6uxpyp6h0fr7SPp7n8WJBoV8jfxQXPCnkVSjyls= +github.com/creasty/defaults v1.5.2/go.mod h1:FPZ+Y0WNrbqOVw+c6av63eyHUAl6pMHZwqLPvXUZGfY= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= +github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/go-ldap/ldap v3.0.2+incompatible/go.mod h1:qfd9rJvER9Q0/D/Sqn1DfHRoBp40uXYvFoEVrNEPqRc= +github.com/go-test/deep v1.0.2-0.20181118220953-042da051cf31/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= +github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= +github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd/go.mod h1:9bjs9uLqI8l75knNv3lV1kA55veR+WUPSiKIWcQHudI= +github.com/hashicorp/go-hclog v0.8.0/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ= +github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= +github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= +github.com/hashicorp/go-plugin v1.0.1/go.mod h1:++UyYGoz3o5w9ZzAdZxtQKrWWP+iqPBn3cQptSMzBuY= +github.com/hashicorp/go-retryablehttp v0.5.4/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs= +github.com/hashicorp/go-rootcerts v1.0.1/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8= +github.com/hashicorp/go-sockaddr v1.0.2/go.mod h1:rB4wwRAUzs07qva3c5SdrY/NEtAUjGlgmH/UkBUC97A= +github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/hashicorp/vault/api v1.0.4/go.mod h1:gDcqh3WGcR1cpF5AJz/B1UFheUEneMoIospckxBxk6Q= +github.com/hashicorp/vault/sdk v0.1.13/go.mod h1:B+hVj7TpuQY1Y/GPbCpffmgd+tSEwvhkWnjtSYCaS2M= +github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= +github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= +github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= +github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= +github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= +github.com/knadh/koanf v1.3.3 h1:eNtBOzQDzkzIIPRCJCx/Ha3DeD/ZFwCAp8JxyqoVAls= +github.com/knadh/koanf v1.3.3/go.mod h1:1cfH5223ZeZUOs8FU2UdTmaNfHpqgtjV0+NHjRO43gs= +github.com/kwitsch/go-dockerutils v0.0.9 h1:Nred9wGYPvBT0/x0NDpDgvnu2H9Nhie9yaXD2knwKG0= +github.com/kwitsch/go-dockerutils v0.0.9/go.mod h1:yYzMHpMV2F2GC5x7A7NnEdl6wk6cex5QN3VrNnWYBXA= +github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= +github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= +github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= +github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= +github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI= +github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI= +github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= +github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag= +github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= +github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ= +github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= +github.com/npillmayer/nestext v0.1.3/go.mod h1:h2lrijH8jpicr25dFY+oAJLyzlya6jhnuG+zWp9L0Uk= +github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= +github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= +github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE= +github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= +github.com/ramr/go-reaper v0.2.1 h1:zww+wlQOvTjBZuk1920R/e0GFEb6O7+B0WQLV6dM924= +github.com/ramr/go-reaper v0.2.1/go.mod h1:AVypdzrcCXjSc/JYnlXl8TsB+z84WyFzxWE8Jh0MOJc= +github.com/rhnvrm/simples3 v0.6.1/go.mod h1:Y+3vYm2V7Y4VijFoJHHTrja6OgPrJ2cBti8dPGkC3sA= +github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= +github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190129075346-302c3dd5f1cc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200331124033-c3d80250170d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190404172233-64821d5d2107/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/grpc v1.14.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.22.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d/go.mod h1:cuepJuh7vyXfUyUwEgHQXw849cJrilpS5NeIjOWESAw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/square/go-jose.v2 v2.3.1/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= From 52500b5051dc0edefc5f07c4ef5fc2679906280b Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Fri, 7 Jan 2022 22:23:45 +0100 Subject: [PATCH 02/15] more config values --- src/config/config.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/config/config.go b/src/config/config.go index f9b9736..7554fdd 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -7,18 +7,24 @@ import ( ) type Config struct { - Redis RedisConfig `koanf:"redis"` - Verbose bool `koanf:"verbose" default:"false"` + 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"` - Verbose bool + 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_" From 175173fec18d84f6636da3f325e42995f85c16a1 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Fri, 7 Jan 2022 23:17:46 +0100 Subject: [PATCH 03/15] added cache package --- src/cache/cache.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/cache/cache.go diff --git a/src/cache/cache.go b/src/cache/cache.go new file mode 100644 index 0000000..e9a39c8 --- /dev/null +++ b/src/cache/cache.go @@ -0,0 +1,54 @@ +package cache + +import ( + "fmt" + "strings" +) + +type Cache struct { + dns map[string]string + rdns map[string]string +} + +func New() *Cache { + return &Cache{ + dns: make(map[string]string), + rdns: make(map[string]string), + } +} + +func (c *Cache) Update(hostname, ip string) { + c.Delete(hostname) + if revIp, revErr := reverseIP(ip); revErr == nil { + c.dns[hostname] = ip + c.rdns[revIp] = hostname + } +} + +func (c *Cache) Delete(hostname string) { + if oip, dok := c.dns[hostname]; dok { + delete(c.dns, hostname) + revIp, _ := reverseIP(oip) + if _, rok := c.rdns[revIp]; rok { + delete(c.rdns, revIp) + } + } +} + +func (c *Cache) GetIp(hostname string) (string, bool) { + ip, ok := c.dns[hostname] + return ip, ok +} + +func (c *Cache) GetHostname(reverseIP string) (string, bool) { + hostname, ok := c.rdns[reverseIP] + return hostname, ok +} + +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") +} From f9b8d46e600e7a97a6b45e829019de237056fff3 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Fri, 7 Jan 2022 23:30:20 +0100 Subject: [PATCH 04/15] added GetMac and basic redis client --- src/config/config.go | 9 +++++ src/go.mod | 9 ++++- src/go.sum | 88 +++++++++++++++++++++++++++++++++++++++++++- src/redis/redis.go | 47 +++++++++++++++++++++++ 4 files changed, 150 insertions(+), 3 deletions(-) create mode 100644 src/redis/redis.go diff --git a/src/config/config.go b/src/config/config.go index 7554fdd..b9b4b03 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -40,3 +40,12 @@ func Get() (*Config, error) { } return nil, err } + +func (hc HostConfig) GetMac() string { + res := "" + for _, k := range hc.Mac { + res = k + break + } + return res +} diff --git a/src/go.mod b/src/go.mod index 6bc402b..9702d7f 100644 --- a/src/go.mod +++ b/src/go.mod @@ -3,11 +3,16 @@ 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 ( + 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/kwitsch/go-dockerutils v0.0.9 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/mapstructure v1.4.1 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect - github.com/ramr/go-reaper v0.2.1 // indirect ) diff --git a/src/go.sum b/src/go.sum index fe2d1ee..21b8703 100644 --- a/src/go.sum +++ b/src/go.sum @@ -13,23 +13,45 @@ github.com/aws/aws-sdk-go-v2/service/sso v1.4.2/go.mod h1:NBvT9R1MEF+Ud6ApJKM0G+ github.com/aws/aws-sdk-go-v2/service/sts v1.7.2/go.mod h1:8EzeIqfWt2wWT4rJVu3f21TfrhJ8AEMzVybRNSb/b4g= github.com/aws/smithy-go v1.8.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E= github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= +github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= +github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/creasty/defaults v1.5.2 h1:/VfB6uxpyp6h0fr7SPp7n8WJBoV8jfxQXPCnkVSjyls= github.com/creasty/defaults v1.5.2/go.mod h1:FPZ+Y0WNrbqOVw+c6av63eyHUAl6pMHZwqLPvXUZGfY= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/go-ldap/ldap v3.0.2+incompatible/go.mod h1:qfd9rJvER9Q0/D/Sqn1DfHRoBp40uXYvFoEVrNEPqRc= +github.com/go-redis/redis/v8 v8.11.4 h1:kHoYkfZP6+pe04aFTnhDH6GDROa5yJdHJVNxV3F46Tg= +github.com/go-redis/redis/v8 v8.11.4/go.mod h1:2Z2wHZXdQpCDXEGzqMockDpNyYvi2l4Pxt6RJr792+w= +github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/go-test/deep v1.0.2-0.20181118220953-042da051cf31/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= @@ -47,13 +69,16 @@ github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/vault/api v1.0.4/go.mod h1:gDcqh3WGcR1cpF5AJz/B1UFheUEneMoIospckxBxk6Q= github.com/hashicorp/vault/sdk v0.1.13/go.mod h1:B+hVj7TpuQY1Y/GPbCpffmgd+tSEwvhkWnjtSYCaS2M= github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= +github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/knadh/koanf v1.3.3 h1:eNtBOzQDzkzIIPRCJCx/Ha3DeD/ZFwCAp8JxyqoVAls= github.com/knadh/koanf v1.3.3/go.mod h1:1cfH5223ZeZUOs8FU2UdTmaNfHpqgtjV0+NHjRO43gs= @@ -76,55 +101,99 @@ github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ= github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/npillmayer/nestext v0.1.3/go.mod h1:h2lrijH8jpicr25dFY+oAJLyzlya6jhnuG+zWp9L0Uk= +github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= +github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= +github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= +github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc= +github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0= +github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= +github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/onsi/gomega v1.16.0 h1:6gjqkI8iiRHMvdccRJM8rVKjCWk6ZIm6FTm3ddIe4/c= +github.com/onsi/gomega v1.16.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY= github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= +github.com/pelletier/go-toml v1.7.0 h1:7utD74fnzVc/cpcyy8sjrlFr5vYpypUixARcHIMIGuI= github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE= github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= -github.com/ramr/go-reaper v0.2.1 h1:zww+wlQOvTjBZuk1920R/e0GFEb6O7+B0WQLV6dM924= github.com/ramr/go-reaper v0.2.1/go.mod h1:AVypdzrcCXjSc/JYnlXl8TsB+z84WyFzxWE8Jh0MOJc= github.com/rhnvrm/simples3 v0.6.1/go.mod h1:Y+3vYm2V7Y4VijFoJHHTrja6OgPrJ2cBti8dPGkC3sA= github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 h1:DzZ89McO9/gWPsQXS/FVKAlG02ZjaQ6AlZRBimEYOd0= +golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190129075346-302c3dd5f1cc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200331124033-c3d80250170d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210112080510-489259a85091/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.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +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/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= @@ -132,10 +201,27 @@ google.golang.org/genproto v0.0.0-20190404172233-64821d5d2107/go.mod h1:VzzqZJRn google.golang.org/grpc v1.14.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.22.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d/go.mod h1:cuepJuh7vyXfUyUwEgHQXw849cJrilpS5NeIjOWESAw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/square/go-jose.v2 v2.3.1/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/src/redis/redis.go b/src/redis/redis.go new file mode 100644 index 0000000..ec07d71 --- /dev/null +++ b/src/redis/redis.go @@ -0,0 +1,47 @@ +package redis + +import ( + "context" + "time" + + "github.com/go-redis/redis/v8" + "github.com/kwitsch/TinyMacDns/config" +) + +type Client struct { + cfg *config.RedisConfig + client *redis.Client + ctx context.Context + cancel context.CancelFunc +} + +// New creates a new redis client +func New(cfg *config.RedisConfig) (*Client, error) { + rdb := redis.NewClient(&redis.Options{ + Addr: cfg.Address, + Username: cfg.Username, + Password: cfg.Password, + DB: cfg.Database, + MaxRetries: cfg.Attempts, + MaxRetryBackoff: time.Duration(cfg.Cooldown), + }) + ctx, cancel := context.WithCancel(context.Background()) + + _, err := rdb.Ping(ctx).Result() + if err == nil { + res := &Client{ + cfg: cfg, + client: rdb, + ctx: ctx, + cancel: cancel, + } + return res, nil + } + cancel() + return nil, err +} + +// Close discards the redis client +func (c *Client) Close() { + c.cancel() +} From 41d77e68f70bf6a120326fdb5742e56792caa608 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Sat, 8 Jan 2022 00:24:30 +0100 Subject: [PATCH 05/15] added locks --- src/cache/cache.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/cache/cache.go b/src/cache/cache.go index e9a39c8..b527b9e 100644 --- a/src/cache/cache.go +++ b/src/cache/cache.go @@ -3,11 +3,13 @@ package cache import ( "fmt" "strings" + "sync" ) type Cache struct { dns map[string]string rdns map[string]string + lock sync.RWMutex } func New() *Cache { @@ -19,28 +21,37 @@ func New() *Cache { func (c *Cache) Update(hostname, ip string) { c.Delete(hostname) + if revIp, revErr := reverseIP(ip); revErr == nil { + c.lock.Lock() c.dns[hostname] = ip c.rdns[revIp] = hostname + c.lock.Unlock() } } func (c *Cache) Delete(hostname string) { if oip, dok := c.dns[hostname]; dok { + c.lock.Lock() delete(c.dns, hostname) 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 } From 73665870af8c0c1ad28dd7281c9119cdaa47031e Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Sat, 8 Jan 2022 00:25:15 +0100 Subject: [PATCH 06/15] added start & poll --- src/redis/redis.go | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/redis/redis.go b/src/redis/redis.go index ec07d71..6161bee 100644 --- a/src/redis/redis.go +++ b/src/redis/redis.go @@ -2,9 +2,11 @@ package redis import ( "context" + "fmt" "time" "github.com/go-redis/redis/v8" + "github.com/kwitsch/TinyMacDns/cache" "github.com/kwitsch/TinyMacDns/config" ) @@ -13,10 +15,12 @@ type Client struct { client *redis.Client ctx context.Context cancel context.CancelFunc + hosts *map[string]config.HostConfig + cache *cache.Cache } // New creates a new redis client -func New(cfg *config.RedisConfig) (*Client, error) { +func New(cfg *config.RedisConfig, hosts *map[string]config.HostConfig, cache *cache.Cache) (*Client, error) { rdb := redis.NewClient(&redis.Options{ Addr: cfg.Address, Username: cfg.Username, @@ -34,6 +38,8 @@ func New(cfg *config.RedisConfig) (*Client, error) { client: rdb, ctx: ctx, cancel: cancel, + hosts: hosts, + cache: cache, } return res, nil } @@ -45,3 +51,40 @@ func New(cfg *config.RedisConfig) (*Client, error) { func (c *Client) Close() { c.cancel() } + +func (c *Client) Start() { + go func() { + ticker := time.NewTicker(c.cfg.Intervall) + defer ticker.Stop() + for { + select { + case <-ticker.C: + c.poll() + case <-c.ctx.Done(): + fmt.Println("Redis Client stop") + return + } + } + }() +} + +func (c *Client) poll() { + for hostname, host := range *c.hosts { + c.pollHost(hostname, host) + } +} + +func (c *Client) pollHost(hostname string, host config.HostConfig) { + found := false + for _, mac := range host.Mac { + ip, err := c.client.Get(c.ctx, mac).Result() + if err == nil { + c.cache.Update(hostname, ip) + found = true + break + } + } + if !found { + c.cache.Delete(hostname) + } +} From 321fec36127813ef40126c71747a27cba8b07052 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Sat, 8 Jan 2022 02:23:31 +0100 Subject: [PATCH 07/15] redis.Nil change --- src/redis/redis.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/redis/redis.go b/src/redis/redis.go index 6161bee..f7d27ae 100644 --- a/src/redis/redis.go +++ b/src/redis/redis.go @@ -78,7 +78,7 @@ func (c *Client) pollHost(hostname string, host config.HostConfig) { found := false for _, mac := range host.Mac { ip, err := c.client.Get(c.ctx, mac).Result() - if err == nil { + if err == redis.Nil { c.cache.Update(hostname, ip) found = true break From 01d8464ab7d92d41906831df9b4c842ccffd28b1 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Sat, 8 Jan 2022 03:01:25 +0100 Subject: [PATCH 08/15] added ToLower --- src/cache/cache.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/cache/cache.go b/src/cache/cache.go index b527b9e..cb6a06c 100644 --- a/src/cache/cache.go +++ b/src/cache/cache.go @@ -20,20 +20,22 @@ func New() *Cache { } func (c *Cache) Update(hostname, ip string) { - c.Delete(hostname) + ihn := strings.ToLower(hostname) + c.Delete(ihn) if revIp, revErr := reverseIP(ip); revErr == nil { c.lock.Lock() - c.dns[hostname] = ip - c.rdns[revIp] = hostname + c.dns[ihn] = ip + c.rdns[revIp] = ihn c.lock.Unlock() } } func (c *Cache) Delete(hostname string) { - if oip, dok := c.dns[hostname]; dok { + ihn := strings.ToLower(hostname) + if oip, dok := c.dns[ihn]; dok { c.lock.Lock() - delete(c.dns, hostname) + delete(c.dns, ihn) revIp, _ := reverseIP(oip) if _, rok := c.rdns[revIp]; rok { delete(c.rdns, revIp) From e807b962835e287396b1e13a17433115a84ced5e Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Sat, 8 Jan 2022 04:14:28 +0100 Subject: [PATCH 09/15] added dns server --- src/go.mod | 9 ++++ src/go.sum | 16 +++++++ src/server/server.go | 99 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 src/server/server.go diff --git a/src/go.mod b/src/go.mod index 9702d7f..2cbbb85 100644 --- a/src/go.mod +++ b/src/go.mod @@ -7,11 +7,20 @@ require ( 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 diff --git a/src/go.sum b/src/go.sum index 21b8703..3c7e800 100644 --- a/src/go.sum +++ b/src/go.sum @@ -86,6 +86,8 @@ github.com/kwitsch/go-dockerutils v0.0.9 h1:Nred9wGYPvBT0/x0NDpDgvnu2H9Nhie9yaXD github.com/kwitsch/go-dockerutils v0.0.9/go.mod h1:yYzMHpMV2F2GC5x7A7NnEdl6wk6cex5QN3VrNnWYBXA= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/miekg/dns v1.1.45 h1:g5fRIhm9nx7g8osrAvgb16QJfmyMsyOCb+J7LSv+Qzk= +github.com/miekg/dns v1.1.45/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME= github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= @@ -133,6 +135,7 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5 github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= @@ -141,6 +144,8 @@ golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTk golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -150,8 +155,11 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 h1:DzZ89McO9/gWPsQXS/FVKAlG02ZjaQ6AlZRBimEYOd0= golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= +golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985 h1:4CSI6oo7cOjJKajidEljs9h+uP0rRZBPPPhcCbj5mw8= +golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -159,6 +167,7 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -174,8 +183,12 @@ golang.org/x/sys v0.0.0-20200331124033-c3d80250170d/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/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/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= @@ -190,9 +203,12 @@ golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3 golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2 h1:BonxutuHCTL0rBDnZlKjpGIQFTjyUVTexFOdWkB6Fg0= +golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= diff --git a/src/server/server.go b/src/server/server.go new file mode 100644 index 0000000..1d8e626 --- /dev/null +++ b/src/server/server.go @@ -0,0 +1,99 @@ +package server + +import ( + "fmt" + "net" + "strings" + + "github.com/kwitsch/TinyMacDns/cache" + "github.com/miekg/dns" +) + +type Server struct { + udp *dns.Server + cache *cache.Cache + ttl uint32 +} + +func New(cache *cache.Cache, ttl int) *Server { + res := &Server{ + udp: createUDPServer(), + cache: cache, + ttl: uint32(ttl), + } + + handler := res.udp.Handler.(*dns.ServeMux) + handler.HandleFunc(".", res.OnRequest) + + return res +} + +func (s *Server) Start() error { + return s.udp.ListenAndServe() +} + +func createUDPServer() *dns.Server { + return &dns.Server{ + Addr: "53", + Net: "udp", + Handler: dns.NewServeMux(), + NotifyStartedFunc: func() { + fmt.Println("UDP server is up and running") + }, + UDPSize: 65535, + } +} + +const rdnsSuf string = ".in-addr.arpa" + +func (s *Server) OnRequest(w dns.ResponseWriter, request *dns.Msg) { + q := request.Question[0] + m := new(dns.Msg) + m.SetReply(request) + if q.Qtype == dns.TypePTR || q.Qtype == dns.TypeA { + cname := strings.TrimSuffix(strings.ToLower(q.Name), ".") + exists := false + val := "" + + if q.Qtype == dns.TypePTR { + crname := strings.TrimSuffix(cname, rdnsSuf) + + val, exists = s.cache.GetHostname(crname) + if exists { + rr := new(dns.PTR) + rr.Hdr = dns.RR_Header{ + Name: q.Name, + Rrtype: dns.TypePTR, + Class: dns.ClassINET, + Ttl: s.ttl, + } + + rr.Ptr = val + + m.Answer = []dns.RR{rr} + } + } else if q.Qtype == dns.TypeA { + val, exists = s.cache.GetIp(cname) + if exists { + rr := new(dns.A) + rr.Hdr = dns.RR_Header{ + Name: q.Name, + Rrtype: dns.TypeA, + Class: dns.ClassINET, + Ttl: s.ttl, + } + + rr.A = net.ParseIP(val) + + m.Answer = []dns.RR{rr} + } + } + + if !exists { + m.SetRcode(request, dns.RcodeNameError) + } + } else { + m.SetRcode(request, dns.RcodeRefused) + } + w.WriteMsg(m) +} From 89789c71aa7eb0fd9f47e99d05851c91d09b0328 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Sat, 8 Jan 2022 17:47:38 +0100 Subject: [PATCH 10/15] added docker build --- .github/workflows/autobuild.yml | 59 +++++++++++++++++++++++++++++++++ Dockerfile | 9 +++++ 2 files changed, 68 insertions(+) create mode 100644 .github/workflows/autobuild.yml create mode 100644 Dockerfile diff --git a/.github/workflows/autobuild.yml b/.github/workflows/autobuild.yml new file mode 100644 index 0000000..c514df4 --- /dev/null +++ b/.github/workflows/autobuild.yml @@ -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'] }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1cea2dc --- /dev/null +++ b/Dockerfile @@ -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"] \ No newline at end of file From f878b67b3bcd141e7a48b0564fb2703b4a4cb56c Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Sat, 8 Jan 2022 18:54:09 +0100 Subject: [PATCH 11/15] build main loop --- src/main.go | 53 ++++++++++++++++++++++++++++++++++++++++++++ src/redis/redis.go | 25 +++------------------ src/server/server.go | 15 +++++++++++-- 3 files changed, 69 insertions(+), 24 deletions(-) create mode 100644 src/main.go diff --git a/src/main.go b/src/main.go new file mode 100644 index 0000000..5b14a3b --- /dev/null +++ b/src/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "fmt" + "os" + "os/signal" + "time" + + "github.com/kwitsch/TinyMacDns/cache" + "github.com/kwitsch/TinyMacDns/config" + "github.com/kwitsch/TinyMacDns/redis" + "github.com/kwitsch/TinyMacDns/server" + + _ "github.com/kwitsch/go-dockerutils" +) + +func main() { + cfg, cErr := config.Get() + if cErr == nil { + cache := cache.New() + + redis, rErr := redis.New(&cfg.Redis, cache) + if rErr == nil { + server := server.New(cache, int(cfg.Redis.Intervall.Seconds())) + server.Start() + + ticker := time.NewTicker(cfg.Redis.Intervall) + + intChan := make(chan os.Signal, 1) + signal.Notify(intChan, os.Interrupt) + + for { + select { + case <-ticker.C: + redis.Poll(&cfg.Hosts) + case <-intChan: + fmt.Println("Collector stopping") + server.Stop() + ticker.Stop() + redis.Close() + close(intChan) + os.Exit(0) + } + } + } else { + fmt.Println(rErr) + os.Exit(2) + } + } else { + fmt.Println(cErr) + os.Exit(1) + } +} diff --git a/src/redis/redis.go b/src/redis/redis.go index f7d27ae..69a481c 100644 --- a/src/redis/redis.go +++ b/src/redis/redis.go @@ -2,7 +2,6 @@ package redis import ( "context" - "fmt" "time" "github.com/go-redis/redis/v8" @@ -15,12 +14,11 @@ type Client struct { client *redis.Client ctx context.Context cancel context.CancelFunc - hosts *map[string]config.HostConfig cache *cache.Cache } // New creates a new redis client -func New(cfg *config.RedisConfig, hosts *map[string]config.HostConfig, cache *cache.Cache) (*Client, error) { +func New(cfg *config.RedisConfig, cache *cache.Cache) (*Client, error) { rdb := redis.NewClient(&redis.Options{ Addr: cfg.Address, Username: cfg.Username, @@ -38,7 +36,6 @@ func New(cfg *config.RedisConfig, hosts *map[string]config.HostConfig, cache *ca client: rdb, ctx: ctx, cancel: cancel, - hosts: hosts, cache: cache, } return res, nil @@ -52,24 +49,8 @@ func (c *Client) Close() { c.cancel() } -func (c *Client) Start() { - go func() { - ticker := time.NewTicker(c.cfg.Intervall) - defer ticker.Stop() - for { - select { - case <-ticker.C: - c.poll() - case <-c.ctx.Done(): - fmt.Println("Redis Client stop") - return - } - } - }() -} - -func (c *Client) poll() { - for hostname, host := range *c.hosts { +func (c *Client) Poll(hosts *map[string]config.HostConfig) { + for hostname, host := range *hosts { c.pollHost(hostname, host) } } diff --git a/src/server/server.go b/src/server/server.go index 1d8e626..e059593 100644 --- a/src/server/server.go +++ b/src/server/server.go @@ -28,8 +28,17 @@ func New(cache *cache.Cache, ttl int) *Server { return res } -func (s *Server) Start() error { - return s.udp.ListenAndServe() +func (s *Server) Start() { + go func() { + err := s.udp.ListenAndServe() + if err != nil { + fmt.Println("Server.Start error", err) + } + }() +} + +func (s *Server) Stop() error { + return s.udp.Shutdown() } func createUDPServer() *dns.Server { @@ -48,8 +57,10 @@ const rdnsSuf string = ".in-addr.arpa" func (s *Server) OnRequest(w dns.ResponseWriter, request *dns.Msg) { q := request.Question[0] + m := new(dns.Msg) m.SetReply(request) + if q.Qtype == dns.TypePTR || q.Qtype == dns.TypeA { cname := strings.TrimSuffix(strings.ToLower(q.Name), ".") exists := false From 9093b956aec1df7af9dc75ca87c0211725975a69 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Sat, 8 Jan 2022 19:04:11 +0100 Subject: [PATCH 12/15] ignore test folder --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 66fd13c..d06defc 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ # Dependency directories (remove the comment below to include it) # vendor/ + +test/ \ No newline at end of file From 110a1fdbd4e29157200246fd9a3366b04ec7e6e7 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Sat, 8 Jan 2022 21:29:00 +0100 Subject: [PATCH 13/15] small fixes and debug outputs --- src/config/config.go | 23 +++++++++++++++++------ src/main.go | 8 ++++++-- src/redis/redis.go | 15 ++++++++++++++- src/server/server.go | 23 ++++++++++++++--------- 4 files changed, 51 insertions(+), 18 deletions(-) diff --git a/src/config/config.go b/src/config/config.go index b9b4b03..03c83c7 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -1,6 +1,7 @@ package config import ( + "fmt" "time" . "github.com/kwitsch/go-dockerutils/config" @@ -36,16 +37,26 @@ func Get() (*Config, error) { res.Redis.Verbose = res.Verbose + if res.Verbose { + logHosts(res.Hosts) + } + return &res, nil } + return nil, err } -func (hc HostConfig) GetMac() string { - res := "" - for _, k := range hc.Mac { - res = k - break +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) } - return res } diff --git a/src/main.go b/src/main.go index 5b14a3b..9bbde38 100644 --- a/src/main.go +++ b/src/main.go @@ -21,7 +21,9 @@ func main() { redis, rErr := redis.New(&cfg.Redis, cache) if rErr == nil { - server := server.New(cache, int(cfg.Redis.Intervall.Seconds())) + fmt.Println("Server starting") + + server := server.New(cache, int(cfg.Redis.Intervall.Seconds()), cfg.Verbose) server.Start() ticker := time.NewTicker(cfg.Redis.Intervall) @@ -29,12 +31,14 @@ func main() { intChan := make(chan os.Signal, 1) signal.Notify(intChan, os.Interrupt) + redis.Poll(&cfg.Hosts) + for { select { case <-ticker.C: redis.Poll(&cfg.Hosts) case <-intChan: - fmt.Println("Collector stopping") + fmt.Println("Server stopping") server.Stop() ticker.Stop() redis.Close() diff --git a/src/redis/redis.go b/src/redis/redis.go index 69a481c..ff7417f 100644 --- a/src/redis/redis.go +++ b/src/redis/redis.go @@ -2,6 +2,7 @@ package redis import ( "context" + "fmt" "time" "github.com/go-redis/redis/v8" @@ -50,6 +51,10 @@ func (c *Client) Close() { } func (c *Client) Poll(hosts *map[string]config.HostConfig) { + if c.cfg.Verbose { + fmt.Println("redis.Client.Poll") + } + for hostname, host := range *hosts { c.pollHost(hostname, host) } @@ -59,13 +64,21 @@ func (c *Client) pollHost(hostname string, host config.HostConfig) { found := false for _, mac := range host.Mac { ip, err := c.client.Get(c.ctx, mac).Result() - if err == redis.Nil { + if err == nil { + if c.cfg.Verbose { + fmt.Println("redis.Client.pollhost", hostname, "=", ip) + } c.cache.Update(hostname, ip) found = true break + } else { + fmt.Println(err) } } if !found { + if c.cfg.Verbose { + fmt.Println("redis.Client.pollhost", hostname, "not found") + } c.cache.Delete(hostname) } } diff --git a/src/server/server.go b/src/server/server.go index e059593..8c1e27c 100644 --- a/src/server/server.go +++ b/src/server/server.go @@ -10,16 +10,18 @@ import ( ) type Server struct { - udp *dns.Server - cache *cache.Cache - ttl uint32 + udp *dns.Server + cache *cache.Cache + ttl uint32 + verbose bool } -func New(cache *cache.Cache, ttl int) *Server { +func New(cache *cache.Cache, ttl int, verbose bool) *Server { res := &Server{ - udp: createUDPServer(), - cache: cache, - ttl: uint32(ttl), + udp: createUDPServer(), + cache: cache, + ttl: uint32(ttl), + verbose: verbose, } handler := res.udp.Handler.(*dns.ServeMux) @@ -33,6 +35,7 @@ func (s *Server) Start() { err := s.udp.ListenAndServe() if err != nil { fmt.Println("Server.Start error", err) + panic(err) } }() } @@ -43,7 +46,7 @@ func (s *Server) Stop() error { func createUDPServer() *dns.Server { return &dns.Server{ - Addr: "53", + Addr: ":53", Net: "udp", Handler: dns.NewServeMux(), NotifyStartedFunc: func() { @@ -57,7 +60,9 @@ const rdnsSuf string = ".in-addr.arpa" func (s *Server) OnRequest(w dns.ResponseWriter, request *dns.Msg) { q := request.Question[0] - + if s.verbose { + fmt.Println("Requst:", q.Name, "Type:", q.Qtype) + } m := new(dns.Msg) m.SetReply(request) From d945f798305d658aa03dc093fc1297f07ba256b5 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Sun, 9 Jan 2022 00:40:29 +0100 Subject: [PATCH 14/15] bugfixes & logging --- src/cache/cache.go | 11 +++++++++++ src/main.go | 11 +++++++---- src/redis/redis.go | 3 +++ src/server/server.go | 16 ++++++---------- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/cache/cache.go b/src/cache/cache.go index cb6a06c..9a7cf57 100644 --- a/src/cache/cache.go +++ b/src/cache/cache.go @@ -58,6 +58,17 @@ func (c *Cache) GetHostname(reverseIP string) (string, bool) { 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 { diff --git a/src/main.go b/src/main.go index 9bbde38..faf0554 100644 --- a/src/main.go +++ b/src/main.go @@ -21,28 +21,31 @@ func main() { redis, rErr := redis.New(&cfg.Redis, cache) if rErr == nil { + defer redis.Close() fmt.Println("Server starting") server := server.New(cache, int(cfg.Redis.Intervall.Seconds()), cfg.Verbose) + defer server.Stop() server.Start() ticker := time.NewTicker(cfg.Redis.Intervall) + defer ticker.Stop() intChan := make(chan os.Signal, 1) signal.Notify(intChan, os.Interrupt) + defer close(intChan) redis.Poll(&cfg.Hosts) for { select { + case sErr := <-server.Error: + fmt.Println(sErr) + os.Exit(3) case <-ticker.C: redis.Poll(&cfg.Hosts) case <-intChan: fmt.Println("Server stopping") - server.Stop() - ticker.Stop() - redis.Close() - close(intChan) os.Exit(0) } } diff --git a/src/redis/redis.go b/src/redis/redis.go index ff7417f..76961b8 100644 --- a/src/redis/redis.go +++ b/src/redis/redis.go @@ -58,6 +58,9 @@ func (c *Client) Poll(hosts *map[string]config.HostConfig) { for hostname, host := range *hosts { c.pollHost(hostname, host) } + if c.cfg.Verbose { + c.cache.Print() + } } func (c *Client) pollHost(hostname string, host config.HostConfig) { diff --git a/src/server/server.go b/src/server/server.go index 8c1e27c..8d20df8 100644 --- a/src/server/server.go +++ b/src/server/server.go @@ -14,6 +14,7 @@ type Server struct { cache *cache.Cache ttl uint32 verbose bool + Error chan (error) } func New(cache *cache.Cache, ttl int, verbose bool) *Server { @@ -22,6 +23,7 @@ func New(cache *cache.Cache, ttl int, verbose bool) *Server { cache: cache, ttl: uint32(ttl), verbose: verbose, + Error: make(chan error, 1), } handler := res.udp.Handler.(*dns.ServeMux) @@ -32,16 +34,12 @@ func New(cache *cache.Cache, ttl int, verbose bool) *Server { func (s *Server) Start() { go func() { - err := s.udp.ListenAndServe() - if err != nil { - fmt.Println("Server.Start error", err) - panic(err) - } + s.Error <- s.udp.ListenAndServe() }() } -func (s *Server) Stop() error { - return s.udp.Shutdown() +func (s *Server) Stop() { + s.udp.Shutdown() } func createUDPServer() *dns.Server { @@ -84,7 +82,7 @@ func (s *Server) OnRequest(w dns.ResponseWriter, request *dns.Msg) { Ttl: s.ttl, } - rr.Ptr = val + rr.Ptr = fmt.Sprintf("%s.", val) m.Answer = []dns.RR{rr} } @@ -108,8 +106,6 @@ func (s *Server) OnRequest(w dns.ResponseWriter, request *dns.Msg) { if !exists { m.SetRcode(request, dns.RcodeNameError) } - } else { - m.SetRcode(request, dns.RcodeRefused) } w.WriteMsg(m) } From 6e4a28ff4fd2f5bd4093982885150da126defaef Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Sun, 9 Jan 2022 01:01:42 +0100 Subject: [PATCH 15/15] added tcp server --- src/server/server.go | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/server/server.go b/src/server/server.go index 8d20df8..60fa9db 100644 --- a/src/server/server.go +++ b/src/server/server.go @@ -11,6 +11,7 @@ import ( type Server struct { udp *dns.Server + tcp *dns.Server cache *cache.Cache ttl uint32 verbose bool @@ -20,14 +21,14 @@ type Server struct { func New(cache *cache.Cache, ttl int, verbose bool) *Server { res := &Server{ udp: createUDPServer(), + tcp: createTCPServer(), cache: cache, ttl: uint32(ttl), verbose: verbose, - Error: make(chan error, 1), + Error: make(chan error, 2), } - handler := res.udp.Handler.(*dns.ServeMux) - handler.HandleFunc(".", res.OnRequest) + res.setupHandlers() return res } @@ -36,12 +37,23 @@ func (s *Server) Start() { go func() { s.Error <- s.udp.ListenAndServe() }() + go func() { + s.Error <- s.tcp.ListenAndServe() + }() } func (s *Server) Stop() { s.udp.Shutdown() } +func (s *Server) setupHandlers() { + uh := s.udp.Handler.(*dns.ServeMux) + uh.HandleFunc(".", s.OnRequest) + + th := s.tcp.Handler.(*dns.ServeMux) + th.HandleFunc(".", s.OnRequest) +} + func createUDPServer() *dns.Server { return &dns.Server{ Addr: ":53", @@ -54,6 +66,17 @@ func createUDPServer() *dns.Server { } } +func createTCPServer() *dns.Server { + return &dns.Server{ + Addr: ":53", + Net: "tcp", + Handler: dns.NewServeMux(), + NotifyStartedFunc: func() { + fmt.Println("TCP server is up and running") + }, + } +} + const rdnsSuf string = ".in-addr.arpa" func (s *Server) OnRequest(w dns.ResponseWriter, request *dns.Msg) {