Skip to content

Commit edf8775

Browse files
committed
fix: npe when replacing vhost
1 parent cd7e268 commit edf8775

4 files changed

Lines changed: 50 additions & 9 deletions

File tree

daemon.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ func (d *Daemon) removeVhost(w http.ResponseWriter, r *http.Request) {
286286
func (d *Daemon) doRemoveVhost(vhost *Vhost, w http.ResponseWriter) {
287287
fmt.Printf("[*] removing vhost: %s -> %d\n", vhost.Host, vhost.ServicePort)
288288
fmt.Fprintf(w, "removing vhost: %s -> %d\n", vhost.Host, vhost.ServicePort)
289-
vhost.Close()
290289
d.loggedHandler.RemoveVhost(vhost.Host)
291290
d.saveVhosts()
292291
}
@@ -349,7 +348,6 @@ func (d *Daemon) addVhost(binding string, w http.ResponseWriter) *Vhost {
349348
// remove any existing vhost
350349
if v := d.loggedHandler.GetVhost(vhost.Host); v != nil {
351350
fmt.Printf("[*] removing existing vhost: %s -> %d\n", v.Host, v.ServicePort)
352-
v.Close()
353351
d.loggedHandler.RemoveVhost(vhost.Host)
354352
}
355353

daemon_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"net/http/httptest"
77
"os"
8+
"path"
89
"strings"
910
"testing"
1011

@@ -22,13 +23,30 @@ func setup() error {
2223
fmt.Println("using temp dir:", temp)
2324
os.Setenv("CERT_PATH", temp)
2425
os.Setenv("CAROOT_PATH", temp)
26+
os.Setenv("CAROOT", temp)
2527
err = InitTrustStore()
2628
if err != nil {
2729
return err
2830
}
2931
return nil
3032
}
3133

34+
func reset() {
35+
os.Remove(path.Join(temp, "vhosts.json"))
36+
}
37+
38+
func listTempDir() {
39+
files, err := os.ReadDir(temp)
40+
if err != nil {
41+
fmt.Println("error reading temp dir:", err)
42+
return
43+
}
44+
fmt.Println("temp dir contents:")
45+
for _, f := range files {
46+
fmt.Println(" -", f.Name())
47+
}
48+
}
49+
3250
func teardown() {
3351
os.RemoveAll(temp)
3452
}
@@ -48,6 +66,7 @@ func TestMain(m *testing.M) {
4866
}
4967

5068
func TestListClients(t *testing.T) {
69+
reset()
5170
request, _ := http.NewRequest("GET", "/events/next/", nil)
5271
response := httptest.NewRecorder()
5372

@@ -79,14 +98,18 @@ func TestListClients(t *testing.T) {
7998
}
8099

81100
func TestAddRemoveVhost(t *testing.T) {
82-
101+
reset()
83102
vhostMux := CreateVhostMux([]string{}, true)
84103
lh := NewLoggedHandler(vhostMux)
104+
assert.Equal(t, 0, len(lh.vhostMux.Servers))
105+
85106
d := NewDaemon(lh, "", 0, 0)
86107

87108
r := httptest.NewRecorder()
88109
d.addVhost("foo:8000", r)
89110
assert.Equal(t, 1, len(lh.vhostMux.Servers))
111+
assert.NotNil(t, lh.vhostMux.Servers["foo"], "has vhost for foo")
112+
assert.NotNil(t, lh.vhostMux.Servers["foo"].Handler, "has handler for foo")
90113

91114
v := d.loggedHandler.GetVhost("foo")
92115
d.doRemoveVhost(v, r)

logged_handler.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ func (lh *LoggedHandler) GetVhost(host string) *Vhost {
5858
}
5959

6060
func (lh *LoggedHandler) RemoveVhost(host string) {
61-
delete(lh.vhostMux.Servers, host)
61+
vhost := lh.vhostMux.Servers[host]
62+
if vhost != nil {
63+
vhost.Close()
64+
delete(lh.vhostMux.Servers, host)
65+
}
6266
}
6367

6468
// DumpServers to the given writer
@@ -114,11 +118,7 @@ func (lh *LoggedHandler) pushLog(host string, msg string) {
114118
fmt.Println(msg)
115119

116120
if vhost := lh.GetVhost(host); vhost != nil {
117-
vhost.logChan <- msg // push to buffer
118-
for _, logChan := range vhost.listeners {
119-
// push to client listeners
120-
logChan <- msg
121-
}
121+
vhost.PushLog(msg)
122122
}
123123
}
124124

vhost.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log"
77
"net/http"
88
"net/url"
9+
"runtime/debug"
910
"strconv"
1011
"strings"
1112

@@ -27,6 +28,7 @@ type Vhost struct {
2728
logRing *deque.Deque[string] `json:"-"`
2829
logChan LogListener `json:"-"`
2930
listeners []LogListener `json:"-"`
31+
closed bool `json:"-"`
3032
}
3133

3234
type LogListener chan string
@@ -52,6 +54,7 @@ func (v *VhostMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
5254
defer func() {
5355
if val := recover(); val != nil {
5456
log.Printf("Error proxying request `%s` to `%s`: %v", originalURL, r.URL, val)
57+
log.Printf("%s", debug.Stack())
5558
w.WriteHeader(http.StatusServiceUnavailable)
5659
fmt.Fprintf(w, "Error proxying request `%s` to `%s`: %v", originalURL, r.URL, val)
5760
}
@@ -121,6 +124,8 @@ func CreateVhost(input string, useTLS bool) (*Vhost, error) {
121124
}
122125
}
123126

127+
vhost.Init()
128+
124129
return vhost, nil
125130
}
126131

@@ -167,6 +172,10 @@ func (v *Vhost) BufferAsString() string {
167172
}
168173

169174
func (v *Vhost) Close() {
175+
if v.closed {
176+
return
177+
}
178+
v.closed = true
170179
if v.logChan != nil {
171180
close(v.logChan)
172181
}
@@ -175,6 +184,17 @@ func (v *Vhost) Close() {
175184
}
176185
}
177186

187+
func (v *Vhost) PushLog(msg string) {
188+
if v.closed {
189+
return
190+
}
191+
v.logChan <- msg // push to buffer
192+
for _, logChan := range v.listeners {
193+
// push to client listeners
194+
logChan <- msg
195+
}
196+
}
197+
178198
func (v *Vhost) populateLogBuffer() {
179199
for line := range v.logChan {
180200
if v.logRing.Len() < 10 {

0 commit comments

Comments
 (0)