Skip to content

Commit

Permalink
Allow for server to support TLS if desired (#17)
Browse files Browse the repository at this point in the history
* Allow for server to support TLS if desired

* Updated readme to cover TLS changes
  • Loading branch information
rokett authored Oct 20, 2022
1 parent 9ef4786 commit b7ce3e4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ Next run the example main:
go run cmd/guac/guac.go
```

Now you can connect with [the example Vue app](https://github.com/wwt/guac-vue). By default guac will try to connect to a guacd instance at `127.0.0.1:4822`. If you need to configure something different, you can do so by configuring environment variables; see the configurable parameters below.
Now you can connect with [the example Vue app](https://github.com/wwt/guac-vue). By default, guac will try to connect to a guacd instance at `127.0.0.1:4822`. If you need to configure something different, you can do so by configuring environment variables; see the configurable parameters below.

Guac listens on `http://0.0.0.0:4567`. If you have a need for the connection to Guac to be secure, you will need to pass a certificate and keyfile to it using the `CERT_PATH` and `CERT_KEY_PATH` environment variables; it will then listen on `https://0.0.0.0:4567`. The secure connection uses TLS 1.3.

## Configurable parameters
| Environment Variable | Description | Default Value | Required? |
| -------------------- | ----------------------------------------------- | -------------- | ----------|
| `GUACD_ADDRESS` | The address and port that guacd is listening on | 127.0.0.1:4822 | No |
| Environment Variable | Description | Default Value | Required? |
| -------------------- | -------------------------------------------------------------------------------------------------------- | -------------- | ----------|
| `CERT_PATH` | Full path, including filename, to a certificate file in order for guac to listen on HTTPS (TLS 1.3) | | No |
| `CERT_KEY_PATH` | Full path, including filename, to the certificate keyfile in order for guac to listen on HTTPS (TLS 1.3) | | No |
| `GUACD_ADDRESS` | The address and port that guacd is listening on | 127.0.0.1:4822 | No |

## Acknowledgements

Expand Down
57 changes: 51 additions & 6 deletions cmd/guac/guac.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
Expand All @@ -15,12 +15,30 @@ import (
)

var (
guacdAddr = "127.0.0.1:4822"
certPath string
certKeyPath string
guacdAddr = "127.0.0.1:4822"
)

func main() {
logrus.SetLevel(logrus.DebugLevel)

if os.Getenv("CERT_PATH") != "" {
certPath = os.Getenv("CERT_PATH")
}

if os.Getenv("CERT_KEY_PATH") != "" {
certKeyPath = os.Getenv("CERT_KEY_PATH")
}

if certPath != "" && certKeyPath == "" {
logrus.Fatal("You must set the CERT_KEY_PATH environment variable to specify the full path to the certificate keyfile")
}

if certPath == "" && certKeyPath != "" {
logrus.Fatal("You must set the CERT_PATH environment variable to specify the full path to the certificate file")
}

if os.Getenv("GUACD_ADDRESS") != "" {
guacdAddr = os.Getenv("GUACD_ADDRESS")
}
Expand Down Expand Up @@ -62,18 +80,45 @@ func main() {
}
})

logrus.Println("Serving on http://0.0.0.0:4567")
tlsCfg := tls.Config{}
if certPath != "" {
cert, err := tls.LoadX509KeyPair(certPath, certKeyPath)
if err != nil {
logrus.Fatalf("Unable to load certificate keypair: %s\n", err)
}

tlsCfg.MinVersion = tls.VersionTLS13
tlsCfg.Certificates = []tls.Certificate{cert}
tlsCfg.CurvePreferences = []tls.CurveID{
tls.X25519,
tls.CurveP256,
tls.CurveP384,
}
}

s := &http.Server{
Addr: "0.0.0.0:4567",
Handler: mux,
ReadTimeout: guac.SocketTimeout,
WriteTimeout: guac.SocketTimeout,
MaxHeaderBytes: 1 << 20,
TLSConfig: &tlsCfg,
}
err := s.ListenAndServe()
if err != nil {
fmt.Println(err)

if certPath != "" {
logrus.Println("Serving on https://0.0.0.0:4567")

err := s.ListenAndServeTLS("", "")
if err != nil {
logrus.Fatal(err)
}
} else {
logrus.Println("Serving on http://0.0.0.0:4567")

err := s.ListenAndServe()
if err != nil {
logrus.Fatal(err)
}
}
}

Expand Down

0 comments on commit b7ce3e4

Please sign in to comment.