Browse Source

replace all instances of panic which would crash a running daemon with log messages

Alan Shreve 12 years ago
parent
commit
73c2a61635
3 changed files with 18 additions and 6 deletions
  1. 2 1
      src/ngrok/conn/conn.go
  2. 10 2
      src/ngrok/server/http.go
  3. 6 3
      src/ngrok/server/tunnel.go

+ 2 - 1
src/ngrok/conn/conn.go

@@ -51,7 +51,8 @@ func Listen(addr *net.TCPAddr, typ string) (l *Listener, err error) {
 		for {
 			tcpConn, err := listener.AcceptTCP()
 			if err != nil {
-				panic(err)
+				log.Error("Failed to accept new TCP connection of type %s: %v", typ, err)
+				continue
 			}
 
 			c := wrapTcpConn(tcpConn, typ)

+ 10 - 2
src/ngrok/server/http.go

@@ -19,6 +19,12 @@ Authorization required
 Content-Length: %d
 
 Tunnel %s not found
+`
+
+	BadRequest = `HTTP/1.0 400 Bad Request
+Content-Length: 12
+
+Bad Request
 `
 )
 
@@ -49,14 +55,16 @@ func httpHandler(tcpConn net.Conn) {
 	defer func() {
 		// recover from failures
 		if r := recover(); r != nil {
-			conn.Warn("Failed with error %v", r)
+			conn.Warn("httpHandler failed with error %v", r)
 		}
 	}()
 
 	// read out the http request
 	req, err := conn.ReadRequest()
 	if err != nil {
-		panic(err)
+		conn.Warn("Failed to read valid http request: %v", err)
+		conn.Write([]byte(BadRequest))
+		return
 	}
 
 	// multiplex to find the right backend host

+ 6 - 3
src/ngrok/server/tunnel.go

@@ -5,7 +5,7 @@ import (
 	"fmt"
 	"net"
 	"ngrok/conn"
-	log "ngrok/log"
+	"ngrok/log"
 	"ngrok/msg"
 	"ngrok/version"
 	"time"
@@ -52,7 +52,9 @@ func newTunnel(m *msg.RegMsg, ctl *Control) (t *Tunnel) {
 		t.listener, err = net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: 0})
 
 		if err != nil {
-			panic(err)
+			t.ctl.conn.Error("Failed to create tunnel. Error binding TCP listener: %v", err)
+
+			t.ctl.stop <- &msg.RegAckMsg{Error: "Internal server error"}
 		}
 
 		go t.listenTcp(t.listener)
@@ -123,7 +125,8 @@ func (t *Tunnel) listenTcp(listener *net.TCPListener) {
 		tcpConn, err := listener.AcceptTCP()
 
 		if err != nil {
-			panic(err)
+			t.Error("Failed to accept new TCP connection: %v", err)
+			continue
 		}
 
 		conn := conn.Wrap(tcpConn, "pub")