Skip to content

Commit

Permalink
refactor: add logChan to Vhost struct
Browse files Browse the repository at this point in the history
  • Loading branch information
chetan committed Nov 3, 2021
1 parent d6e63b7 commit 61b7fe6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
19 changes: 9 additions & 10 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,14 @@ func (d *Daemon) registerVhost(w http.ResponseWriter, r *http.Request) {
// disconnects.
func (d *Daemon) streamLogs(w http.ResponseWriter, r *http.Request) {
hostname := r.PostFormValue("host")
logChan := d.loggedHandler.VhostLogListeners[hostname]
if logChan == nil {
vhost := d.loggedHandler.GetVhost(hostname)
if vhost == nil {
fmt.Fprintf(w, "[*] error: host '%s' not found", hostname)
return
}

// runs forever until connection closes
d.relayLogsUntilClose(logChan, w, r.Context())
d.relayLogsUntilClose(vhost.LogChan, w, r.Context())
}

func (d *Daemon) relayLogsUntilClose(logChan chan string, w http.ResponseWriter, reqCtx context.Context) {
Expand All @@ -228,13 +228,13 @@ func (d *Daemon) relayLogsUntilClose(logChan chan string, w http.ResponseWriter,
}

// addVhost for the given binding to the LoggedHandler
func (d *Daemon) addVhost(binding string, w http.ResponseWriter) (chan string, *Vhost) {
func (d *Daemon) addVhost(binding string, w http.ResponseWriter) *Vhost {
vhost, err := CreateVhost(binding, d.enableTLS())
if err != nil {
fmt.Printf("[*] warning: failed to register new vhost `%s`\n", binding)
fmt.Printf(" %s\n", err)
w.WriteHeader(http.StatusBadRequest)
return nil, nil
return nil
}

fmt.Printf("[*] registering new vhost: %s -> %d\n", vhost.Host, vhost.Port)
Expand All @@ -245,20 +245,19 @@ func (d *Daemon) addVhost(binding string, w http.ResponseWriter) (chan string, *
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")

logChan := make(chan string, 10)
d.loggedHandler.AddVhost(vhost, logChan)
d.loggedHandler.AddVhost(vhost)
d.restartTLS()

err = addToHosts(vhost.Host)
if err != nil {
msg := fmt.Sprintf("[*] warning: failed to add %s to system hosts file: %s\n", vhost.Host, err)
fmt.Println(msg)
logChan <- msg
vhost.LogChan <- msg
}

logChan <- fmt.Sprintf("[*] added vhost: %s", binding)
vhost.LogChan <- fmt.Sprintf("[*] added vhost: %s", binding)

return logChan, vhost
return vhost
}

func (d *Daemon) hello(w http.ResponseWriter, r *http.Request) {
Expand Down
26 changes: 15 additions & 11 deletions logged_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ var defaultTLSHost = "vproxy.local"
// TODO: replace ServeMux with a proper router (chi?)
type LoggedHandler struct {
*http.ServeMux
VhostLogListeners map[string]chan string
vhostMux *VhostMux
vhostMux *VhostMux

defaultHost string
defaultCert string
Expand All @@ -30,9 +29,8 @@ type LoggedHandler struct {
// NewLoggedHandler wraps the given handler with a request/response logger
func NewLoggedHandler(vm *VhostMux) *LoggedHandler {
lh := &LoggedHandler{
ServeMux: http.NewServeMux(),
VhostLogListeners: make(map[string]chan string),
vhostMux: vm,
ServeMux: http.NewServeMux(),
vhostMux: vm,
}

lh.defaultHost = defaultTLSHost
Expand All @@ -51,13 +49,15 @@ func (lh *LoggedHandler) createDefaultCert() {
}
}

func (lh *LoggedHandler) AddVhost(vhost *Vhost, listener chan string) {
lh.VhostLogListeners[vhost.Host] = listener
func (lh *LoggedHandler) AddVhost(vhost *Vhost) {
lh.vhostMux.Servers[vhost.Host] = vhost
}

func (lh *LoggedHandler) GetVhost(host string) *Vhost {
return lh.vhostMux.Servers[host]
}

func (lh *LoggedHandler) RemoveVhost(host string) {
delete(lh.VhostLogListeners, host)
delete(lh.vhostMux.Servers, host)
}

Expand Down Expand Up @@ -104,10 +104,14 @@ func (lh *LoggedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
host := getHostName(r.Host)

l := fmt.Sprintf("%s [%s] %s [ %d ] %s %d %s", r.RemoteAddr, host, r.Method, record.status, r.URL, r.ContentLength, elapsedTime)
log.Println(l)
lh.pushLog(host, l)
}

func (lh *LoggedHandler) pushLog(host string, msg string) {
log.Println(msg)

if listener, ok := lh.VhostLogListeners[host]; ok {
listener <- l
if vhost := lh.GetVhost(host); vhost != nil {
vhost.LogChan <- msg
}
}

Expand Down
3 changes: 3 additions & 0 deletions vhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type Vhost struct {
Handler http.Handler
Cert string // TLS Certificate
Key string // TLS Private Key

LogChan chan string
}

// VhostMux is an http.Handler whose ServeHTTP forwards the request to
Expand Down Expand Up @@ -106,6 +108,7 @@ func CreateVhost(input string, useTLS bool) (*Vhost, error) {

vhost := &Vhost{
Host: hostname, ServiceHost: targetHost, Port: targetPort, Handler: proxy,
LogChan: make(chan string, 10),
}

if useTLS {
Expand Down

0 comments on commit 61b7fe6

Please sign in to comment.