update deps; switch to goutils syslog.

This commit is contained in:
Kyle Isom 2023-05-05 07:13:46 +00:00
parent 7b7f1ea8a9
commit c475287fb1
17 changed files with 139 additions and 234 deletions

View File

@ -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",
],
)

View File

@ -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 {

View File

@ -10,7 +10,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//iptools",
"//log",
"@dev_wntrmute_git_kyle_goutils//syslog",
"@in_gopkg_yaml_v2//:yaml_v2",
],
)

View File

@ -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"
)

View File

@ -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 {

View File

@ -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",
)

View File

@ -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"],
)

View File

@ -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
}

2
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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"],
)

View File

@ -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

View File

@ -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"],
)

View File

@ -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)
}

View File

@ -12,6 +12,6 @@ go_library(
deps = [
"//config",
"//dhcp",
"//log",
"@dev_wntrmute_git_kyle_goutils//syslog",
],
)

View File

@ -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 (

50
setup_deveth Executable file
View File

@ -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