From c475287fb16f57ebf7e875f283e464757e95d878 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Fri, 5 May 2023 07:13:46 +0000 Subject: [PATCH] update deps; switch to goutils syslog. --- cmd/kdhcpd/BUILD.bazel | 2 +- cmd/kdhcpd/main.go | 4 +- config/BUILD.bazel | 2 +- config/config.go | 2 +- config/path.go | 2 +- deps.bzl | 51 ++++++++++ dhcp/BUILD.bazel | 5 +- dhcp/packet.go | 19 +++- go.mod | 2 + go.sum | 2 + iptools/BUILD.bazel | 9 +- kdhcpd.yaml | 2 +- log/BUILD.bazel | 9 -- log/logger.go | 208 ----------------------------------------- server/BUILD.bazel | 2 +- server/server.go | 2 +- setup_deveth | 50 ++++++++++ 17 files changed, 139 insertions(+), 234 deletions(-) delete mode 100644 log/BUILD.bazel delete mode 100644 log/logger.go create mode 100755 setup_deveth diff --git a/cmd/kdhcpd/BUILD.bazel b/cmd/kdhcpd/BUILD.bazel index 09dbf74..73957f8 100644 --- a/cmd/kdhcpd/BUILD.bazel +++ b/cmd/kdhcpd/BUILD.bazel @@ -7,9 +7,9 @@ go_library( visibility = ["//visibility:private"], deps = [ "//config", - "//log", "//server", "@com_github_peterbourgon_ff_v3//ffcli", + "@dev_wntrmute_git_kyle_goutils//syslog", ], ) diff --git a/cmd/kdhcpd/main.go b/cmd/kdhcpd/main.go index c51c940..91014e4 100644 --- a/cmd/kdhcpd/main.go +++ b/cmd/kdhcpd/main.go @@ -6,8 +6,8 @@ import ( "os" "strings" + log "git.wntrmute.dev/kyle/goutils/syslog" "git.wntrmute.dev/kyle/kdhcp/config" - "git.wntrmute.dev/kyle/kdhcp/log" "git.wntrmute.dev/kyle/kdhcp/server" "github.com/peterbourgon/ff/v3/ffcli" ) @@ -21,7 +21,7 @@ func main() { flag.Parse() logLevel = strings.ToUpper(logLevel) - log.Setup(logLevel, "kdhcpd") + log.Setup(log.DefaultDebugOptions("kdhcpd", false)) root := &ffcli.Command{ Exec: func(ctx context.Context, args []string) error { diff --git a/config/BUILD.bazel b/config/BUILD.bazel index 3b6981b..32df335 100644 --- a/config/BUILD.bazel +++ b/config/BUILD.bazel @@ -10,7 +10,7 @@ go_library( visibility = ["//visibility:public"], deps = [ "//iptools", - "//log", + "@dev_wntrmute_git_kyle_goutils//syslog", "@in_gopkg_yaml_v2//:yaml_v2", ], ) diff --git a/config/config.go b/config/config.go index c7f9b42..2a3cb1b 100644 --- a/config/config.go +++ b/config/config.go @@ -6,8 +6,8 @@ import ( "io/ioutil" "net" + log "git.wntrmute.dev/kyle/goutils/syslog" "git.wntrmute.dev/kyle/kdhcp/iptools" - "git.wntrmute.dev/kyle/kdhcp/log" "gopkg.in/yaml.v2" ) diff --git a/config/path.go b/config/path.go index 3ed5bae..de75701 100644 --- a/config/path.go +++ b/config/path.go @@ -5,7 +5,7 @@ import ( "os/user" "path/filepath" - "git.wntrmute.dev/kyle/kdhcp/log" + log "git.wntrmute.dev/kyle/goutils/syslog" ) func FindConfigPath() string { diff --git a/deps.bzl b/deps.bzl index 888261c..01652ac 100644 --- a/deps.bzl +++ b/deps.bzl @@ -1,6 +1,13 @@ load("@bazel_gazelle//:deps.bzl", "go_repository") def go_dependencies(): + go_repository( + name = "com_github_cloudflare_cfssl", + importpath = "github.com/cloudflare/cfssl", + sum = "h1:vFJDAvQgFSRbCn9zg8KpSrrEZrBAQ4KO5oNK7SXEyb0=", + version = "v1.5.0", + ) + go_repository( name = "com_github_davecgh_go_spew", importpath = "github.com/davecgh/go-spew", @@ -13,6 +20,13 @@ def go_dependencies(): sum = "h1:KaodqZuhUoZereWVIYmpUgZysurB1kBLX2j0MwMrUAE=", version = "v1.0.0", ) + go_repository( + name = "com_github_kr_text", + importpath = "github.com/kr/text", + sum = "h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=", + version = "v0.2.0", + ) + go_repository( name = "com_github_pelletier_go_toml", importpath = "github.com/pelletier/go-toml", @@ -25,6 +39,31 @@ def go_dependencies(): sum = "h1:PaKe7GW8orVFh8Unb5jNHS+JZBwWUMa2se0HM6/BI24=", version = "v3.3.0", ) + go_repository( + name = "com_github_pkg_errors", + importpath = "github.com/pkg/errors", + sum = "h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=", + version = "v0.9.1", + ) + go_repository( + name = "com_github_pkg_sftp", + importpath = "github.com/pkg/sftp", + sum = "h1:/f3b24xrDhkhddlaobPe2JgBqfdt+gC/NYl0QY9IOuI=", + version = "v1.12.0", + ) + go_repository( + name = "com_github_stretchr_testify", + importpath = "github.com/stretchr/testify", + sum = "h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=", + version = "v1.6.1", + ) + go_repository( + name = "dev_wntrmute_git_kyle_goutils", + importpath = "git.wntrmute.dev/kyle/goutils", + sum = "h1:CRCBlmSXOTkShbqC6j9lgxh4lb+khzc2zpIJYGQJtnc=", + version = "v1.6.6", + ) + go_repository( name = "in_gopkg_check_v1", importpath = "gopkg.in/check.v1", @@ -37,3 +76,15 @@ def go_dependencies(): sum = "h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=", version = "v2.4.0", ) + go_repository( + name = "org_golang_x_crypto", + importpath = "golang.org/x/crypto", + sum = "h1:Qwe1rC8PSniVfAFPFJeyUkB+zcysC3RgJBAGk7eqBEU=", + version = "v0.0.0-20220314234659-1baeb1ce4c0b", + ) + go_repository( + name = "org_golang_x_sys", + importpath = "golang.org/x/sys", + sum = "h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0=", + version = "v0.0.0-20220412211240-33da011f77ad", + ) diff --git a/dhcp/BUILD.bazel b/dhcp/BUILD.bazel index 105bf56..f608dc6 100644 --- a/dhcp/BUILD.bazel +++ b/dhcp/BUILD.bazel @@ -8,8 +8,5 @@ go_library( ], importpath = "git.wntrmute.dev/kyle/kdhcp/dhcp", visibility = ["//visibility:public"], - deps = [ - "//log", - "@com_github_davecgh_go_spew//spew", - ], + deps = ["@dev_wntrmute_git_kyle_goutils//syslog"], ) diff --git a/dhcp/packet.go b/dhcp/packet.go index 49d9ba5..ecae000 100644 --- a/dhcp/packet.go +++ b/dhcp/packet.go @@ -4,11 +4,11 @@ import ( "bytes" "encoding/binary" "fmt" + "io" "net" "strings" - "git.wntrmute.dev/kyle/kdhcp/log" - "github.com/davecgh/go-spew/spew" + log "git.wntrmute.dev/kyle/goutils/syslog" ) const ( @@ -127,7 +127,20 @@ func (req *BootRequest) Read(packet []byte) error { } req.FileName = string(bytes.Trim(tempBuf, "\x00")) - spew.Dump(*req) + for { + tag, err := buf.ReadByte() + if err != nil { + if err == io.EOF { + break + } + return err + } + + err = ReadOption(req, tag, buf) + if err != nil { + return err + } + } return nil } diff --git a/go.mod b/go.mod index 4450778..079ef78 100644 --- a/go.mod +++ b/go.mod @@ -9,3 +9,5 @@ require ( github.com/peterbourgon/ff/v3 v3.3.0 gopkg.in/yaml.v2 v2.4.0 ) + +require git.wntrmute.dev/kyle/goutils v1.6.6 // indirect diff --git a/go.sum b/go.sum index a74ed97..178e58e 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +git.wntrmute.dev/kyle/goutils v1.6.6 h1:CRCBlmSXOTkShbqC6j9lgxh4lb+khzc2zpIJYGQJtnc= +git.wntrmute.dev/kyle/goutils v1.6.6/go.mod h1:p0m2YprqMXkqtxTPKCiRcmgYo/D/9DtAIRfNVFE3JBg= 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/hashicorp/go-syslog v1.0.0 h1:KaodqZuhUoZereWVIYmpUgZysurB1kBLX2j0MwMrUAE= diff --git a/iptools/BUILD.bazel b/iptools/BUILD.bazel index 4257ee2..eac7b9c 100644 --- a/iptools/BUILD.bazel +++ b/iptools/BUILD.bazel @@ -3,8 +3,11 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "iptools", srcs = [ + "hardware_address.go", + "lease_info.go", "pool.go", "range.go", + "tools.go", ], importpath = "git.wntrmute.dev/kyle/kdhcp/iptools", visibility = ["//visibility:public"], @@ -12,6 +15,10 @@ go_library( go_test( name = "iptools_test", - srcs = ["range_test.go"], + srcs = [ + "hardware_address_test.go", + "pool_test.go", + "range_test.go", + ], embed = [":iptools"], ) diff --git a/kdhcpd.yaml b/kdhcpd.yaml index 8a6c671..adf1313 100644 --- a/kdhcpd.yaml +++ b/kdhcpd.yaml @@ -1,7 +1,7 @@ kdhcp: version: 1 lease_file: /tmp/kdhcp_lease.yaml - interface: enp89s0 + interface: deveth0 port: 67 network: address: 192.168.4.250 diff --git a/log/BUILD.bazel b/log/BUILD.bazel deleted file mode 100644 index a73a852..0000000 --- a/log/BUILD.bazel +++ /dev/null @@ -1,9 +0,0 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library") - -go_library( - name = "log", - srcs = ["logger.go"], - importpath = "git.wntrmute.dev/kyle/kdhcp/log", - visibility = ["//visibility:public"], - deps = ["@com_github_hashicorp_go_syslog//:go-syslog"], -) diff --git a/log/logger.go b/log/logger.go deleted file mode 100644 index 25a600b..0000000 --- a/log/logger.go +++ /dev/null @@ -1,208 +0,0 @@ -package log - -import ( - "fmt" - "os" - "strings" - "time" - - gsyslog "github.com/hashicorp/go-syslog" -) - -type logger struct { - l gsyslog.Syslogger - p gsyslog.Priority -} - -func (log *logger) printf(p gsyslog.Priority, format string, args ...interface{}) { - if !strings.HasSuffix(format, "\n") { - format += "\n" - } - - if p <= log.p { - fmt.Printf("%s [%s] ", prioritiev[p], timestamp()) - fmt.Printf(format, args...) - } - - if log.l != nil { - log.l.WriteLevel(p, []byte(fmt.Sprintf(format, args...))) - } -} - -func (log *logger) print(p gsyslog.Priority, args ...interface{}) { - if p <= log.p { - fmt.Printf("%s [%s] ", prioritiev[p], timestamp()) - fmt.Print(args...) - } - - if log.l != nil { - log.l.WriteLevel(p, []byte(fmt.Sprint(args...))) - } -} - -func (log *logger) println(p gsyslog.Priority, args ...interface{}) { - if p <= log.p { - fmt.Printf("%s [%s] ", prioritiev[p], timestamp()) - fmt.Println(args...) - } - - if log.l != nil { - log.l.WriteLevel(p, []byte(fmt.Sprintln(args...))) - } -} - -var log = &logger{p: gsyslog.LOG_WARNING} - -var priorities = map[string]gsyslog.Priority{ - "EMERG": gsyslog.LOG_EMERG, - "ALERT": gsyslog.LOG_ALERT, - "CRIT": gsyslog.LOG_CRIT, - "ERR": gsyslog.LOG_ERR, - "WARNING": gsyslog.LOG_WARNING, - "NOTICE": gsyslog.LOG_NOTICE, - "INFO": gsyslog.LOG_INFO, - "DEBUG": gsyslog.LOG_DEBUG, -} - -var prioritiev = map[gsyslog.Priority]string{ - gsyslog.LOG_EMERG: "EMERG", - gsyslog.LOG_ALERT: "ALERT", - gsyslog.LOG_CRIT: "CRIT", - gsyslog.LOG_ERR: "ERR", - gsyslog.LOG_WARNING: "WARNING", - gsyslog.LOG_NOTICE: "NOTICE", - gsyslog.LOG_INFO: "INFO", - gsyslog.LOG_DEBUG: "DEBUG", -} - -func timestamp() string { - return time.Now().Format("2006-01-02 15:04:05 MST") -} - -func Setup(level, tag string) error { - if tag == "" { - tag = "kdhcp" - } - - priority, ok := priorities[level] - if !ok { - return fmt.Errorf("log: unknown priority %s", level) - } - - log.p = priority - - var err error - log.l, err = gsyslog.NewLogger(priority, "daemon", tag) - if err != nil { - return err - } - - return nil -} - -func Debug(args ...interface{}) { - log.print(gsyslog.LOG_DEBUG, args...) -} - -func Info(args ...interface{}) { - log.print(gsyslog.LOG_INFO, args...) -} - -func Notice(args ...interface{}) { - log.print(gsyslog.LOG_NOTICE, args...) -} - -func Warning(args ...interface{}) { - log.print(gsyslog.LOG_WARNING, args...) -} - -func Err(args ...interface{}) { - log.print(gsyslog.LOG_ERR, args...) -} - -func Crit(args ...interface{}) { - log.print(gsyslog.LOG_CRIT, args...) -} - -func Alert(args ...interface{}) { - log.print(gsyslog.LOG_ALERT, args...) -} - -func Emerg(args ...interface{}) { - log.print(gsyslog.LOG_EMERG, args...) -} - -func Debugln(args ...interface{}) { - log.println(gsyslog.LOG_DEBUG, args...) -} - -func Infoln(args ...interface{}) { - log.println(gsyslog.LOG_INFO, args...) -} - -func Noticeln(args ...interface{}) { - log.println(gsyslog.LOG_NOTICE, args...) -} - -func Warningln(args ...interface{}) { - log.print(gsyslog.LOG_WARNING, args...) -} - -func Errln(args ...interface{}) { - log.println(gsyslog.LOG_ERR, args...) -} - -func Critln(args ...interface{}) { - log.println(gsyslog.LOG_CRIT, args...) -} - -func Alertln(args ...interface{}) { - log.println(gsyslog.LOG_ALERT, args...) -} - -func Emergln(args ...interface{}) { - log.println(gsyslog.LOG_EMERG, args...) -} - -func Debugf(format string, args ...interface{}) { - log.printf(gsyslog.LOG_DEBUG, format, args...) -} - -func Infof(format string, args ...interface{}) { - log.printf(gsyslog.LOG_INFO, format, args...) -} - -func Noticef(format string, args ...interface{}) { - log.printf(gsyslog.LOG_NOTICE, format, args...) -} - -func Warningf(format string, args ...interface{}) { - log.printf(gsyslog.LOG_WARNING, format, args...) -} - -func Errf(format string, args ...interface{}) { - log.printf(gsyslog.LOG_ERR, format, args...) -} - -func Critf(format string, args ...interface{}) { - log.printf(gsyslog.LOG_CRIT, format, args...) -} - -func Alertf(format string, args ...interface{}) { - log.printf(gsyslog.LOG_ALERT, format, args...) -} - -func Emergf(format string, args ...interface{}) { - log.printf(gsyslog.LOG_EMERG, format, args...) - os.Exit(1) -} - -func Fatal(args ...interface{}) { - log.println(gsyslog.LOG_ERR, args...) - os.Exit(1) -} - -func Fatalf(format string, args ...interface{}) { - log.printf(gsyslog.LOG_ERR, format, args...) - os.Exit(1) -} diff --git a/server/BUILD.bazel b/server/BUILD.bazel index 89ebadc..21fa194 100644 --- a/server/BUILD.bazel +++ b/server/BUILD.bazel @@ -12,6 +12,6 @@ go_library( deps = [ "//config", "//dhcp", - "//log", + "@dev_wntrmute_git_kyle_goutils//syslog", ], ) diff --git a/server/server.go b/server/server.go index 784f296..1440e8f 100644 --- a/server/server.go +++ b/server/server.go @@ -4,9 +4,9 @@ import ( "errors" "net" + log "git.wntrmute.dev/kyle/goutils/syslog" "git.wntrmute.dev/kyle/kdhcp/config" "git.wntrmute.dev/kyle/kdhcp/dhcp" - "git.wntrmute.dev/kyle/kdhcp/log" ) const ( diff --git a/setup_deveth b/setup_deveth new file mode 100755 index 0000000..1712f55 --- /dev/null +++ b/setup_deveth @@ -0,0 +1,50 @@ +#!/usr/bin/env bash + +set -euxo pipefail + +ensure_dummy_loaded () { + if [ -z "$(lsmod | grep dummy)"] + then + sudo modprobe dummy + fi +} + +maybe_add_device () { + DEVICE="${1}" + if [ -z "$(ip --brief link | grep ${DEVICE})"] + then + sudo ip link add ${DEVICE} type dummy + fi +} + +add_address () { + DEVICE="${1}" + ADDR="${2}" + + EXISTING="$(ip --brief addr show "${DEVICE}" | awk '{print $3}')" + if [ ! -z "${EXISTING}" ] + then + if [ "${EXISTING}" == "${ADDR}" ] + then + return + fi + + sudo ip addr flush dev "${DEVICE}" + fi + + sudo ip link set dev "${DEVICE}" up + sudo ip addr add "${ADDR}" dev "${DEVICE}" +} + +flush_device () { + DEVICE="${1}" + + sudo ip addr flush dev "${DEVICE}" + sudo ip link set dev "${DEVICE}" down +} + +ensure_dummy_loaded +maybe_add_device deveth0 +maybe_add_device deveth1 +add_address deveth0 192.168.4.250/24 +flush_device deveth1