Skip to content

Commit

Permalink
proper conflict error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdenio committed Jan 15, 2022
1 parent 8963bfe commit f7de045
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
35 changes: 21 additions & 14 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,37 @@ func main() {

r := mux.NewRouter()

r.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("welcome to underpass"))
}).Host(*host)
r.Handle("/", http.RedirectHandler("https://github.com/cjdenio/underpass", http.StatusTemporaryRedirect)).Host(*host)

r.HandleFunc("/start", func(rw http.ResponseWriter, r *http.Request) {
subdomain := r.URL.Query().Get("subdomain")
if subdomain == "" {
subdomain, _ = gonanoid.Generate("abcdefghijklmnopqrstuvwxyz0123456789", 5)
}

c, err := upgrader.Upgrade(rw, r, nil)
if err != nil {
rw.WriteHeader(http.StatusBadRequest)
rw.Write([]byte(err.Error()))
return
}

writeMutex := sync.Mutex{}

if _, ok := tunnels[subdomain]; ok {
// Tunnel already exists

rw.WriteHeader(http.StatusConflict)
rw.Write([]byte("Tunnel is already running"))
writeMutex.Lock()
err = util.WriteMsgPack(c, models.ServerMessage{
Type: "error",
Error: fmt.Sprintf("Tunnel %s is already in use.", subdomain),
})
if err != nil {
log.Println(err)
}
writeMutex.Unlock()

c.Close()
return
}

Expand All @@ -66,15 +82,6 @@ func main() {

tunnels[subdomain] = &t

c, err := upgrader.Upgrade(rw, r, nil)
if err != nil {
rw.WriteHeader(http.StatusBadRequest)
rw.Write([]byte(err.Error()))
return
}

writeMutex := sync.Mutex{}

writeMutex.Lock()
err = util.WriteMsgPack(c, models.ServerMessage{
Type: "subdomain",
Expand Down
1 change: 1 addition & 0 deletions pkg/client/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var rootCmd = &cobra.Command{
t, err := tunnel.Connect(u.String(), fmt.Sprintf("http://localhost:%d", port))
if err != nil {
fmt.Println(err)
os.Exit(1)
return
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/client/tunnel/tunnel.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tunnel

import (
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -42,6 +43,7 @@ func Connect(url, address string) (*Tunnel, error) {
writeMutex := sync.Mutex{}

go func() {
X:
for {
var msg models.ServerMessage

Expand Down Expand Up @@ -146,6 +148,10 @@ func Connect(url, address string) (*Tunnel, error) {
if v, ok := t.activeRequests[msg.RequestID]; ok {
v.Write(msg.Data)
}
case "error":
c.Close()
closeChan <- errors.New(msg.Error)
break X
}
}
}()
Expand Down
1 change: 1 addition & 0 deletions pkg/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type ServerMessage struct {
Subdomain string `msgpack:"subdomain"`
Request Request `msgpack:"request"`
Data []byte `msgpack:"data"`
Error string `msg:"error"`
}

// Represents a message passed from the client to the server
Expand Down

0 comments on commit f7de045

Please sign in to comment.