From c454983409d2004456ad381cce98fb503ebe49bb Mon Sep 17 00:00:00 2001 From: Elvis de Freitas Souza Date: Thu, 31 May 2018 12:56:05 -0300 Subject: [PATCH] Fixing log level and wildcard features (#72) * Fixing log level that stopped of work after **2.5.0** * Fixing and increasing docs development instructions * Fixing wildcard resolution were not solving main domain to docker container, just the subdomains --- README.md | 18 ++-- RELEASE-NOTES.md | 5 ++ VERSION | 2 +- builder | 7 +- conf/conf.go | 25 ++++-- conf/conf_test.go | 7 +- dns.go | 7 +- docker-compose.yml | 3 +- docs/Compiling-the-project-from-source.md | 28 ------- docs/Features.md | 52 ------------ docs/README.md | 5 +- .../Compiling-the-project-from-source.md | 39 +++++++++ .../Developing-at-the-project.md | 3 +- docs/{ => developing}/Generate-version.md | 0 docs/features.md | 83 +++++++++++++++++++ events/docker/docker.go | 7 +- flags/flags.go | 2 +- log/log.go | 4 + proxy/docker.go | 32 +++++-- .../github.com/mageddo/go-logging/logging.go | 58 +++++++++++-- .../mageddo/go-logging/nativelogger.go | 13 ++- vendor/vendor.json | 6 +- 22 files changed, 269 insertions(+), 137 deletions(-) delete mode 100644 docs/Compiling-the-project-from-source.md delete mode 100644 docs/Features.md create mode 100644 docs/developing/Compiling-the-project-from-source.md rename docs/{ => developing}/Developing-at-the-project.md (91%) rename docs/{ => developing}/Generate-version.md (100%) create mode 100644 docs/features.md diff --git a/README.md b/README.md index 5bb104516..67788e255 100644 --- a/README.md +++ b/README.md @@ -2,15 +2,15 @@ Build Status

-### Features +### [Features](http://mageddo.github.io/dns-proxy-server/docs/features) +* [Take a look at features for more details](http://mageddo.github.io/dns-proxy-server/docs/features) + Dns-proxy-server is a end user(developers, Server Administrators) DNS server tool to develop systems with docker solving docker containers hostnames: * Solve hostnames from local configuration database * Solve hostnames from docker containers using docker **hostname** option or **HOSTNAMES** env * Solve hostnames from a list of configured DNS servers(as a proxy) if no answer of two above -* Solve hostnames using wildcards -> If you register a hostname with `.` at start, then all subdomains will solve to that container/local storage entry - +* [Solve hostnames using wildcards](http://mageddo.github.io/dns-proxy-server/docs/features#Solve-hostnames-using-wildcards) * [Graphic interface to manage it](http:/127.0.0.1:5380/static/) * List and edit DNS local entries @@ -128,14 +128,14 @@ Start the server at [custom port](#configure-your-dns) and solving from it -log-file string Log to file instead of console, (true=log to default log file, /tmp/log.log=log to custom log location) (default "console") -log-level string - Log Level CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG (default "DEBUG") + Log Level ERROR, WARNING, INFO, DEBUG (default "DEBUG") -server-port int The DNS server to start into (default 53) -service string Setup as service, starting with machine at boot docker = start as docker service, normal = start as normal service, - uninstall = uninstall the service from machine + uninstall = uninstall the service from machine -service-publish-web-port Publish web port when running as service in docker mode (default true) -tsig string @@ -186,8 +186,8 @@ if you don't want this service anymore ### Rest API - -* [Latest API documentation](https://github.com/mageddo/dns-proxy-server/tree/master/docs/api) of DNS proxy server APIs +* [Latest API features](http://mageddo.github.io/dns-proxy-server/docs/api/) of DNS proxy server APIs ### Developing -Take a look at the [wiki](docs) for more details of how develop at this project + +Take a look at the [wiki](docs/developing) for more details of how develop at this project diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 76d0f5aca..7069c11cf 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -1,3 +1,8 @@ +### 2.5.2 +* Fixing log level that stopped of work after **2.5.0** +* Fixing and increasing docs development instructions +* Fixing wildcard resolution were not solving main domain to docker container, just the subdomains + ### 2.5.1 * Fixing ping slowness, takes more than 10 seconds to respond diff --git a/VERSION b/VERSION index 73462a5a1..f225a78ad 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.5.1 +2.5.2 diff --git a/builder b/builder index 94ad49a9c..d1cdaf5ce 100755 --- a/builder +++ b/builder @@ -86,7 +86,12 @@ case $1 in rm -rf build/ mkdir -p build/ - go test -v -race -cover -ldflags "-X github.com/mageddo/dns-proxy-server/flags.version=test" ./.../ + + echo "> Testing ..." + go test -race -cover -ldflags "-X github.com/mageddo/dns-proxy-server/flags.version=test" ./.../ + echo "> Tests completed" + + echo "> Building..." go build -v -o build/dns-proxy-server -ldflags "-X github.com/mageddo/dns-proxy-server/flags.version=$APP_VERSION" cp -r static build/ cd build/ diff --git a/conf/conf.go b/conf/conf.go index 50690bcda..91f0dfd1a 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -6,6 +6,7 @@ import ( "os" "github.com/mageddo/dns-proxy-server/utils/env" "strings" + "github.com/mageddo/go-logging" ) func CpuProfile() string { @@ -49,17 +50,31 @@ func getConf() (*local.LocalConfiguration, error) { return local.LoadConfiguration() } -func LogLevel() string { +func LogLevel() int { if lvl := os.Getenv(env.MG_LOG_LEVEL); lvl != "" { - return lvl + return logKeyToSyslogCode(lvl) } - if conf, _ := getConf(); conf != nil && conf.LogLevel != "" { - return conf.LogLevel + return logKeyToSyslogCode(conf.LogLevel) } - return flags.LogLevel() + return logKeyToSyslogCode(flags.LogLevel()) } +func logKeyToSyslogCode(key string) int { + switch strings.ToUpper(key) { + case "DEBUG": + return logging.DEBUG + case "INFO": + return logging.INFO + case "WARNING": + return logging.WARNING + case "ERROR": + return logging.ERROR + } + panic("Unknow log level: " + key) +} + + func LogFile() string { f := os.Getenv(env.MG_LOG_FILE) if conf, _ := getConf(); f == "" && conf != nil && conf.LogFile != "" { diff --git a/conf/conf_test.go b/conf/conf_test.go index 1d83e8f28..fce49068a 100644 --- a/conf/conf_test.go +++ b/conf/conf_test.go @@ -9,6 +9,7 @@ import ( "github.com/mageddo/dns-proxy-server/utils" "flag" "github.com/mageddo/dns-proxy-server/utils/env" + "github.com/mageddo/go-logging" ) func TestDefaultFlagValues(t *testing.T) { @@ -40,7 +41,7 @@ func TestFlagValuesFromConf(t *testing.T) { func TestLogLevel_DefaultValue(t *testing.T) { - assert.Equal(t, "DEBUG", LogLevel()) + assert.Equal(t, logging.DEBUG, LogLevel()) } func TestLogLevel_ReadFromConfig(t *testing.T) { @@ -55,7 +56,7 @@ func TestLogLevel_ReadFromConfig(t *testing.T) { level := LogLevel() // assert - assert.Equal(t, "INFO", level) + assert.Equal(t, logging.INFO, level) os.Remove(local.GetConfPath()) } @@ -69,7 +70,7 @@ func TestLogLevel_ReadFromEnv(t *testing.T) { level := LogLevel() // assert - assert.Equal(t, "WARNING", level) + assert.Equal(t, logging.WARNING, level) } diff --git a/dns.go b/dns.go index 44e6cdb89..0d82cb1f9 100644 --- a/dns.go +++ b/dns.go @@ -1,8 +1,8 @@ package main import ( + _ "github.com/mageddo/dns-proxy-server/log" _ "github.com/mageddo/dns-proxy-server/controller" - "github.com/mageddo/dns-proxy-server/log" "fmt" "os" "runtime/pprof" @@ -24,11 +24,6 @@ import ( "context" ) -func init(){ - // TODO unavailable log.SetLevel(conf.LogLevel()) - log.SetOutput(conf.LogFile()) -} - func handleQuestion(respWriter dns.ResponseWriter, reqMsg *dns.Msg) { defer func() { diff --git a/docker-compose.yml b/docker-compose.yml index 3877ebe45..ef9af1c86 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,7 +4,7 @@ services: # Base Image to build project compiler-dps: image: golang:1.9 - container_name: docker-dns-gocompiler + container_name: gocompiler-dps working_dir: /app/src/github.com/mageddo/dns-proxy-server volumes: - $PWD:/app/src/github.com/mageddo/dns-proxy-server @@ -20,7 +20,6 @@ services: # Run from docker image prod-dps: - container_name: dns-proxy-server image: defreitas/dns-proxy-server:2.5.1 hostname: dns.mageddo volumes: diff --git a/docs/Compiling-the-project-from-source.md b/docs/Compiling-the-project-from-source.md deleted file mode 100644 index 00687b380..000000000 --- a/docs/Compiling-the-project-from-source.md +++ /dev/null @@ -1,28 +0,0 @@ -# Building from source with docker - -Docker-Dns-proxy uses docker to simplify the compile process - - -Generate the binaries - - $ docker-compose up prod-build-binary-dps - Starting docker-dns-server-compiler - Attaching to docker-dns-server-compiler - docker-dns-server-compiler | ok github.com/mageddo/dns-proxy-server/conf 0.008s - docker-dns-server-compiler | ? github.com/mageddo/dns-proxy-server/controller [no test files] - ... - docker-dns-server-compiler | github.com/mageddo/dns-proxy-server/flags - ... - docker-dns-server-compiler | _/app/src - docker-dns-server-compiler exited with code 0 - -Create the docker image - - $ docker-compose build prod-build-image-dps - -# Used tecnologies - -* Docker -* Docker Compose -* Git -* Golang 1.9 diff --git a/docs/Features.md b/docs/Features.md deleted file mode 100644 index 73e9a94d7..000000000 --- a/docs/Features.md +++ /dev/null @@ -1,52 +0,0 @@ -### Enable/Disable console log or change log path -You can disable, log to console, log to default log file path or specify a log path at config file, environment or command line argument. Available options: - -* console (default) - log to console -* false - Logs are disabled -* true - stop log to console and log to `/var/log/dns-proxy-server.log` file -* eg. /tmp/log.log - log to specified path - -#### Config File -```json -{ - ... - "logFile": "console" - ... -} -``` - -#### Environment - - export MG_LOG_FILE=console - -#### Command line argument - - go run dns.go -log-file=console - -### Set log level -You can change system log level at config file, environment or command line argument. Available levels: - -* CRITICAL -* ERROR -* WARNING -* NOTICE -* INFO -* DEBUG (Default) - -#### Config file -```json -{ - ... - "logLevel": "DEBUG" - ... -} -``` - -#### Environment - - export MG_LOG_LEVEL=DEBUG - -#### Command line argument - - go run dns.go -log-level=DEBUG - diff --git a/docs/README.md b/docs/README.md index 80189bab1..483380bb6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,4 +1,3 @@ -* [Building from source with docker](Compiling-the-project-from-source.md) -* [Developing with docker](Developing-at-the-project.md) -* [Generate Version](README.md) +* [Features](features.md) * [API Documentation](api) +* [Developing](developing) diff --git a/docs/developing/Compiling-the-project-from-source.md b/docs/developing/Compiling-the-project-from-source.md new file mode 100644 index 000000000..81b8640a9 --- /dev/null +++ b/docs/developing/Compiling-the-project-from-source.md @@ -0,0 +1,39 @@ +### Building from source with docker + +**DPS** uses docker to simplify the compile process + + +Generate the binaries + +```bash +$ docker-compose up prod-build-binary-dps +Starting docker-dns-server-compiler +Attaching to docker-dns-server-compiler +docker-dns-server-compiler | ok github.com/mageddo/dns-proxy-server/conf 0.008s +docker-dns-server-compiler | ? github.com/mageddo/dns-proxy-server/controller [no test files] +... +docker-dns-server-compiler | github.com/mageddo/dns-proxy-server/flags +... +docker-dns-server-compiler | _/app/src +docker-dns-server-compiler exited with code 0 +``` + +Then binaries mus be available at **build** folder + +``` +$ ls build/ +dns-proxy-server dns-proxy-server-2.5.1.tgz static +``` + +If you want you can build the docker image + +``` +$ docker-compose build prod-build-image-dps && docker-compose up prod-dps +``` + +### Used tecnologies + +* Docker +* Docker Compose +* Git +* Golang 1.9 diff --git a/docs/Developing-at-the-project.md b/docs/developing/Developing-at-the-project.md similarity index 91% rename from docs/Developing-at-the-project.md rename to docs/developing/Developing-at-the-project.md index 07d91811a..9d85a4cee 100644 --- a/docs/Developing-at-the-project.md +++ b/docs/developing/Developing-at-the-project.md @@ -2,8 +2,7 @@ Setup the environment - $ docker-compose -d compiler-dps up - $ docker exec -it gocompiler bash + $ docker-compose up -d compiler-dps && docker exec -it gocompiler-dps bash Running the application diff --git a/docs/Generate-version.md b/docs/developing/Generate-version.md similarity index 100% rename from docs/Generate-version.md rename to docs/developing/Generate-version.md diff --git a/docs/features.md b/docs/features.md new file mode 100644 index 000000000..afa75b78b --- /dev/null +++ b/docs/features.md @@ -0,0 +1,83 @@ +### Solve hostnames using wildcards + +If you register a hostname with `.` at start, then all subdomains will solve to that container/local storage entry + +Example + + docker run --rm --hostname .mageddo.com nginx:latest + +Now all **mageddo.com** subdomains will solve to that nginx container + +``` +$ nslookup site1.mageddo.com +Server: 172.17.0.4 +Address: 172.17.0.4#53 + +Non-authoritative answer: +Name: site1.mageddo.com +Address: 172.17.0.5 + +$ nslookup mageddo.com +Server: 172.17.0.4 +Address: 172.17.0.4#53 + +Non-authoritative answer: +Name: mageddo.com +Address: 172.17.0.5 + +``` + +### Enable/Disable console log or change log path +You can disable, log to console, log to default log file path or specify a log path at config file, environment or command line argument. Available options: + +* console (default) - log to console +* false - Logs are disabled +* true - stop log to console and log to `/var/log/dns-proxy-server.log` file +* eg. /tmp/log.log - log to specified path + +__Config File__ +```json +{ + ... + "logFile": "console" + ... +} +``` + +__Environment__ + + export MG_LOG_FILE=console + +__Command line argument__ + + go run dns.go -log-file=console + +### Set log level +You can change system log level using environment variable, config file, or command line argument, +DPS will consider the parameters in that order, first is more important. + +Available levels: + +* ERROR +* WARNING +* INFO +* DEBUG (Default) + +__Environment__ + + export MG_LOG_LEVEL=DEBUG + +__Config file__ + +```json +{ + ... + "logLevel": "DEBUG" + ... +} +``` + +__ Command line argument__ + + go run dns.go -log-level=DEBUG + diff --git a/events/docker/docker.go b/events/docker/docker.go index 8b7bab30d..a1e55b5b2 100644 --- a/events/docker/docker.go +++ b/events/docker/docker.go @@ -136,12 +136,13 @@ func getHostnames(inspect types.ContainerJSON) []string { func putHostnames(hostnames []string, inspect types.ContainerJSON) error { for _, host := range hostnames { - var ip string = "" - for _, network := range inspect.NetworkSettings.Networks { + var ip = "" + for k, network := range inspect.NetworkSettings.Networks { + logging.Debugf("container=%s, network=%s, ip=%s", inspect.Name, k, network.IPAddress) ip = network.IPAddress } if len(ip) == 0 { - ip = inspect.NetworkSettings.IPAddress; + ip = inspect.NetworkSettings.IPAddress if len(ip) == 0 { err := fmt.Sprintf("no network found to %s", inspect.Name) logging.Error(err) diff --git a/flags/flags.go b/flags/flags.go index b00130ebc..9f7017720 100644 --- a/flags/flags.go +++ b/flags/flags.go @@ -24,7 +24,7 @@ var ( uninstall = uninstall the service from machine `) publishServicePort = flag.Bool("service-publish-web-port", true, "Publish web port when running as service in docker mode") logToFile = flag.String("log-file", "console", "Log to file instead of console, (true=log to default log file, /tmp/log.log=log to custom log location)") - logLevel = flag.String("log-level", "DEBUG", "Log Level CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG") + logLevel = flag.String("log-level", "DEBUG", "Log Level ERROR, WARNING, INFO, DEBUG") Version = flag.Bool("version", false, "Current version") Help = flag.Bool("help", false, "This message") ) diff --git a/log/log.go b/log/log.go index 6e8755791..fd8810707 100644 --- a/log/log.go +++ b/log/log.go @@ -4,10 +4,14 @@ import ( "github.com/mageddo/go-logging" "os" "io" + "github.com/mageddo/dns-proxy-server/conf" ) func init(){ setup(os.Stdout) + logging.SetLevel(conf.LogLevel()) + logging.Warningf("status=log level changed to %d", conf.LogLevel()) + SetOutput(conf.LogFile()) } func setup(out io.Writer) { diff --git a/proxy/docker.go b/proxy/docker.go index 05d6e0a46..7deb3fc83 100644 --- a/proxy/docker.go +++ b/proxy/docker.go @@ -9,6 +9,7 @@ import ( "net" "strconv" "strings" + "fmt" ) type DockerDnsSolver struct { @@ -17,19 +18,36 @@ type DockerDnsSolver struct { func (s DockerDnsSolver) Solve(ctx context.Context, question dns.Question) (*dns.Msg, error) { - key := question.Name[:len(question.Name)-1] + questionName := question.Name[:len(question.Name)-1] + + // simple solving + var key = questionName if s.c.ContainsKey(key) { - logging.Debugf("solver=docker, status=solved-key, solver=docker, hostname=%s, ip=%+v", key, s.c.Get(key)) - return s.getMsg(key, question), nil + return s.doSolve(key, question) } - i := strings.Index(key, ".") - if i > 0 && s.c.ContainsKey(key[i:]) { - logging.Debugf("solver=docker, status=solved-key-wildcard, solver=docker, hostname=%s, ip=%+v", key, s.c.Get(key[i:])) - return s.getMsg(key[i:], question), nil + + // solving domain by wild card + key = fmt.Sprintf(".%s", questionName) + if s.c.ContainsKey(key) { + return s.doSolve(key, question) + } + + // Solving subdomains by wildcard + i := strings.Index(questionName, ".") + if i > 0 { + key = questionName[i:] + if s.c.ContainsKey(key) { + return s.doSolve(key, question) + } } return nil, errors.New("hostname not found " + key) } +func (s DockerDnsSolver) doSolve(k string, q dns.Question) (*dns.Msg, error) { + logging.Debugf("solver=docker, status=solved-key, question=%s, hostname=%s, ip=%+v", q.Name, k, s.c.Get(k)) + return s.getMsg(k, q), nil +} + func NewDockerSolver(c cache.Cache) DockerDnsSolver { return DockerDnsSolver{c} } diff --git a/vendor/github.com/mageddo/go-logging/logging.go b/vendor/github.com/mageddo/go-logging/logging.go index c52e9c860..6917c1778 100644 --- a/vendor/github.com/mageddo/go-logging/logging.go +++ b/vendor/github.com/mageddo/go-logging/logging.go @@ -10,6 +10,15 @@ import ( "github.com/mageddo/go-logging/native" ) +// in accord to https://tools.ietf.org/html/rfc5424 +const ( + ERROR = 3 + WARNING = 4 + NOTICE = 5 + INFO = 6 + DEBUG = 7 +) + type Printer interface { Printf(format string, args ...interface{}) Println(args ...interface{}) @@ -35,39 +44,58 @@ type Log interface { Printer() Printer + // specify log level + SetLevel(level int) + GetLevel() int } var l Log = New(native.NewGologPrinter(os.Stdout, "", log.LstdFlags), 3) func Debug(args ...interface{}) { - l.Debug(args...) + if isActive(l.GetLevel(), DEBUG) { + l.Debug(args...) + } } func Debugf(format string, args ...interface{}){ - l.Debugf(format, args...) + if isActive(l.GetLevel(), DEBUG) { + l.Debugf(format, args...) + } } func Info(args ...interface{}){ - l.Info(args...) + if isActive(l.GetLevel(), INFO) { + l.Info(args...) + } } func Infof(format string, args ...interface{}){ - l.Infof(format, args...) + if isActive(l.GetLevel(), INFO) { + l.Infof(format, args...) + } } func Warning(args ...interface{}){ - l.Warning(args...) + if isActive(l.GetLevel(), WARNING) { + l.Warning(args...) + } } func Warningf(format string, args ...interface{}) { - l.Warningf(format, args...) + if isActive(l.GetLevel(), WARNING) { + l.Warningf(format, args...) + } } func Error(args ...interface{}){ - l.Error(args...) + if isActive(l.GetLevel(), ERROR) { + l.Error(args...) + } } func Errorf(format string, args ...interface{}){ - l.Errorf(format, args...) + if isActive(l.GetLevel(), ERROR) { + l.Errorf(format, args...) + } } func SetOutput(w io.Writer) { @@ -86,4 +114,16 @@ func SetLog(logger Log){ // func GetLog() Log { return l -} \ No newline at end of file +} + +func SetLevel(level int){ + l.SetLevel(level) +} + +func GetLevel() int { + return l.GetLevel() +} + +func isActive(currentLevel, levelToCompare int) bool { + return currentLevel >= levelToCompare +} diff --git a/vendor/github.com/mageddo/go-logging/nativelogger.go b/vendor/github.com/mageddo/go-logging/nativelogger.go index 038ef37a6..3df3d9f8d 100644 --- a/vendor/github.com/mageddo/go-logging/nativelogger.go +++ b/vendor/github.com/mageddo/go-logging/nativelogger.go @@ -11,13 +11,14 @@ import ( type defaultLogger struct { writer Printer level int + logLevel int } func New(p Printer, level ...int) *defaultLogger { if len(level) > 0 { - return &defaultLogger{p, level[0]} + return &defaultLogger{p, level[0], DEBUG} } - return &defaultLogger{p, 2} + return &defaultLogger{p, 2, DEBUG} } func (l *defaultLogger) Debug(args ...interface{}) { @@ -60,6 +61,14 @@ func (l *defaultLogger) Printer() Printer { return l.writer } +func (l *defaultLogger) SetLevel(level int) { + l.logLevel = level +} + +func (l *defaultLogger) GetLevel() int { + return l.logLevel +} + func transformErrorInStackTrace(args []interface{}, buf *bytes.Buffer) []interface{} { size := len(args) if size > 0 { diff --git a/vendor/vendor.json b/vendor/vendor.json index 02d477fd4..1cd31b3e8 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -165,10 +165,10 @@ "revisionTime": "2018-05-06T17:23:18Z" }, { - "checksumSHA1": "ALgMULpOi2xSp6n7gvva7Wqmf3U=", + "checksumSHA1": "+mcQ9QdNtaNcWoSjVbatvwIDpO0=", "path": "github.com/mageddo/go-logging", - "revision": "8a9fe7be7eb3a469ebd798a23ca0706cfc2be869", - "revisionTime": "2018-05-06T01:37:09Z" + "revision": "31bafa27e69d4100ffe19df184be0f0831edafb8", + "revisionTime": "2018-05-31T14:18:09Z" }, { "checksumSHA1": "3a2Cl6bL2Q6uYapCm6w+t2gPrEQ=",