Skip to content

Commit

Permalink
feat: Adding instance specific debug logging
Browse files Browse the repository at this point in the history
fix: Allowing auth to be set by an environment variable
  • Loading branch information
krotik committed Sep 22, 2019
1 parent 9c2595f commit 154b491
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 91 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [1.2.1](https://devt.de///compare/v1.2.0...v1.2.1) (2019-09-21)
## [1.3.0](https://devt.de///compare/v1.2.0...v1.3.0) (2019-09-22)


### Features

* Adding instance specific debug logging ([77d225c](https://devt.de///commit/77d225c))



Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Note: By default you can only reach the streams via localhost. Use the -host par
### Command line options
The main DudelDu executable has the following command line options:
```
DudelDu 0.0.0
DudelDu x.x.x
Usage of ./dudeldu [options] <playlist>
-? Show this help message
-auth string
Expand All @@ -65,15 +65,19 @@ Usage of ./dudeldu [options] <playlist>
-fqs int
Frame queue size (default 10000)
-host string
Server hostname to listen on (default "localhost")
Server hostname to listen on (default "127.0.0.1")
-loop
Loop playlists
-port string
Server port to listen on (default "9091")
-pp string
Prefix all paths with a string
-shuffle
Shuffle playlists
-tps int
Thread pool size (default 10)
Authentication can also be defined via the environment variable: DUDELDU_AUTH="<user>:<pass>"
```

Building DudelDu
Expand Down
6 changes: 3 additions & 3 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (drh *DefaultRequestHandler) checkAuth(bufStr string, clientString string)

b, err := base64.StdEncoding.DecodeString(res[1])
if err != nil {
Print("Invalid request (cannot decode authentication): ", bufStr)
drh.logger.PrintDebug("Invalid request (cannot decode authentication): ", bufStr)
return auth, bufStr, false
}

Expand All @@ -77,7 +77,7 @@ func (drh *DefaultRequestHandler) checkAuth(bufStr string, clientString string)
// Authorize request

if auth != drh.auth && drh.auth != "" {
printDebug("Wrong authentication:", auth)
drh.logger.PrintDebug("Wrong authentication:", auth)
return auth, bufStr, false
}

Expand All @@ -89,7 +89,7 @@ func (drh *DefaultRequestHandler) checkAuth(bufStr string, clientString string)

// No authorization

printDebug("No authentication found")
drh.logger.PrintDebug("No authentication found")
return auth, bufStr, false

} else if bufStr == "" && hasAuth {
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/run_demo.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
../../dudeldu -loop -shuffle -debug -auth web:web demo_playlist.dpl
DUDELDU_AUTH="web:web" ../../dudeldu -loop -shuffle -debug demo_playlist.dpl
59 changes: 25 additions & 34 deletions requesthandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"bytes"
"fmt"
"io"
"log"
"math"
"net"
"regexp"
Expand Down Expand Up @@ -59,16 +58,6 @@ requestOffsetPattern is the pattern which is used to extract the requested offse
*/
var requestOffsetPattern = regexp.MustCompile("(?im)^Range: bytes=([0-9]+)-.*$")

/*
Print logger method. Using a custom type so it can be customized.
*/
var Print = log.Print

/*
DebugOutput is a flag to enable additional debugging output
*/
var DebugOutput = false

/*
DefaultRequestHandler data structure
*/
Expand All @@ -81,24 +70,35 @@ type DefaultRequestHandler struct {
shuffle bool // Flag if the playlist should be shuffled
auth string // Required (basic) authentication string - may be empty
authPeers *datautil.MapCache // Peers which have been authenticated
logger DebugLogger // Logger for debug output
}

/*
NewDefaultRequestHandler creates a new default request handler object.
*/
func NewDefaultRequestHandler(pf PlaylistFactory, loop bool, shuffle bool, auth string) *DefaultRequestHandler {
func NewDefaultRequestHandler(pf PlaylistFactory, loop bool,
shuffle bool, auth string) *DefaultRequestHandler {

drh := &DefaultRequestHandler{
PlaylistFactory: pf,
loop: loop,
LoopTimes: -1,
shuffle: shuffle,
auth: auth,
authPeers: datautil.NewMapCache(0, peerNoAuthTimeout),
logger: nil,
}
drh.ServeRequest = drh.defaultServeRequest
return drh
}

/*
SetDebugLogger sets the debug logger for this request handler.
*/
func (drh *DefaultRequestHandler) SetDebugLogger(logger DebugLogger) {
drh.logger = logger
}

/*
HandleRequest handles requests from streaming clients. It tries to extract
the path and if meta data is supported. Once a request has been successfully
Expand All @@ -107,7 +107,7 @@ finishes.
*/
func (drh *DefaultRequestHandler) HandleRequest(c net.Conn, nerr net.Error) {

printDebug("Handling request from: ", c.RemoteAddr())
drh.logger.PrintDebug("Handling request from: ", c.RemoteAddr())

defer func() {
c.Close()
Expand All @@ -116,13 +116,13 @@ func (drh *DefaultRequestHandler) HandleRequest(c net.Conn, nerr net.Error) {
// Check if there was an error

if nerr != nil {
Print(nerr)
drh.logger.PrintDebug(nerr)
return
}

buf, err := drh.decodeRequestHeader(c)
if err != nil {
Print(err)
drh.logger.PrintDebug(err)
return
}

Expand All @@ -137,7 +137,7 @@ func (drh *DefaultRequestHandler) HandleRequest(c net.Conn, nerr net.Error) {
clientString, _, _ = net.SplitHostPort(c.RemoteAddr().String())
}

printDebug("Client:", c.RemoteAddr(), " Request:", bufStr)
drh.logger.PrintDebug("Client:", c.RemoteAddr(), " Request:", bufStr)

if i := strings.Index(bufStr, "\r\n\r\n"); i >= 0 {
var auth string
Expand Down Expand Up @@ -186,7 +186,7 @@ func (drh *DefaultRequestHandler) HandleRequest(c net.Conn, nerr net.Error) {
}
}

Print("Invalid request: ", bufStr)
drh.logger.PrintDebug("Invalid request: ", bufStr)
}

/*
Expand Down Expand Up @@ -231,7 +231,7 @@ func (drh *DefaultRequestHandler) defaultServeRequest(c net.Conn, path string, m
var currentPlaying string
var err error

printDebug("Serve request path:", path, " Metadata support:", metaDataSupport, " Offset:", offset)
drh.logger.PrintDebug("Serve request path:", path, " Metadata support:", metaDataSupport, " Offset:", offset)

pl := drh.PlaylistFactory.Playlist(path, drh.shuffle)
if pl == nil {
Expand All @@ -249,20 +249,20 @@ func (drh *DefaultRequestHandler) defaultServeRequest(c net.Conn, path string, m
for {
for !pl.Finished() {

if DebugOutput {
if drh.logger.IsDebugOutputEnabled() {
playingString := fmt.Sprintf("%v - %v", pl.Title(), pl.Artist())

if playingString != currentPlaying {
currentPlaying = playingString
printDebug("Written bytes: ", writtenBytes)
printDebug("Sending: ", currentPlaying)
drh.logger.PrintDebug("Written bytes: ", writtenBytes)
drh.logger.PrintDebug("Sending: ", currentPlaying)
}
}

// Check if there were any errors

if err != nil {
Print(err)
drh.logger.PrintDebug(err)
return
}

Expand All @@ -282,7 +282,7 @@ func (drh *DefaultRequestHandler) defaultServeRequest(c net.Conn, path string, m
}
}

printDebug("Serve request path:", path, " complete")
drh.logger.PrintDebug("Serve request path:", path, " complete")
}

/*
Expand Down Expand Up @@ -315,13 +315,13 @@ func (drh *DefaultRequestHandler) prepareFrame(c net.Conn, pl Playlist, frameOff
if frame == nil {

if !pl.Finished() {
Print(fmt.Sprintf("Empty frame for: %v - %v (Error: %v)", pl.Title(), pl.Artist(), err))
drh.logger.PrintDebug(fmt.Sprintf("Empty frame for: %v - %v (Error: %v)", pl.Title(), pl.Artist(), err))
}

} else if err != nil {

if err != ErrPlaylistEnd {
Print(fmt.Sprintf("Error while retrieving playlist data: %v", err))
drh.logger.PrintDebug(fmt.Sprintf("Error while retrieving playlist data: %v", err))
}

err = nil
Expand Down Expand Up @@ -458,12 +458,3 @@ func (drh *DefaultRequestHandler) writeUnauthorized(c net.Conn) error {

return err
}

/*
printDebug will print additional debug output if `DebugOutput` is enabled.
*/
func printDebug(v ...interface{}) {
if DebugOutput {
Print(v...)
}
}
Loading

0 comments on commit 154b491

Please sign in to comment.