Skip to content

Commit b7ce3e4

Browse files
authored
Allow for server to support TLS if desired (#17)
* Allow for server to support TLS if desired * Updated readme to cover TLS changes
1 parent 9ef4786 commit b7ce3e4

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ Next run the example main:
2222
go run cmd/guac/guac.go
2323
```
2424

25-
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.
25+
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.
26+
27+
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.
2628

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

3236
## Acknowledgements
3337

cmd/guac/guac.go

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package main
22

33
import (
4+
"crypto/tls"
45
"encoding/json"
5-
"fmt"
66
"io"
77
"net"
88
"net/http"
@@ -15,12 +15,30 @@ import (
1515
)
1616

1717
var (
18-
guacdAddr = "127.0.0.1:4822"
18+
certPath string
19+
certKeyPath string
20+
guacdAddr = "127.0.0.1:4822"
1921
)
2022

2123
func main() {
2224
logrus.SetLevel(logrus.DebugLevel)
2325

26+
if os.Getenv("CERT_PATH") != "" {
27+
certPath = os.Getenv("CERT_PATH")
28+
}
29+
30+
if os.Getenv("CERT_KEY_PATH") != "" {
31+
certKeyPath = os.Getenv("CERT_KEY_PATH")
32+
}
33+
34+
if certPath != "" && certKeyPath == "" {
35+
logrus.Fatal("You must set the CERT_KEY_PATH environment variable to specify the full path to the certificate keyfile")
36+
}
37+
38+
if certPath == "" && certKeyPath != "" {
39+
logrus.Fatal("You must set the CERT_PATH environment variable to specify the full path to the certificate file")
40+
}
41+
2442
if os.Getenv("GUACD_ADDRESS") != "" {
2543
guacdAddr = os.Getenv("GUACD_ADDRESS")
2644
}
@@ -62,18 +80,45 @@ func main() {
6280
}
6381
})
6482

65-
logrus.Println("Serving on http://0.0.0.0:4567")
83+
tlsCfg := tls.Config{}
84+
if certPath != "" {
85+
cert, err := tls.LoadX509KeyPair(certPath, certKeyPath)
86+
if err != nil {
87+
logrus.Fatalf("Unable to load certificate keypair: %s\n", err)
88+
}
89+
90+
tlsCfg.MinVersion = tls.VersionTLS13
91+
tlsCfg.Certificates = []tls.Certificate{cert}
92+
tlsCfg.CurvePreferences = []tls.CurveID{
93+
tls.X25519,
94+
tls.CurveP256,
95+
tls.CurveP384,
96+
}
97+
}
6698

6799
s := &http.Server{
68100
Addr: "0.0.0.0:4567",
69101
Handler: mux,
70102
ReadTimeout: guac.SocketTimeout,
71103
WriteTimeout: guac.SocketTimeout,
72104
MaxHeaderBytes: 1 << 20,
105+
TLSConfig: &tlsCfg,
73106
}
74-
err := s.ListenAndServe()
75-
if err != nil {
76-
fmt.Println(err)
107+
108+
if certPath != "" {
109+
logrus.Println("Serving on https://0.0.0.0:4567")
110+
111+
err := s.ListenAndServeTLS("", "")
112+
if err != nil {
113+
logrus.Fatal(err)
114+
}
115+
} else {
116+
logrus.Println("Serving on http://0.0.0.0:4567")
117+
118+
err := s.ListenAndServe()
119+
if err != nil {
120+
logrus.Fatal(err)
121+
}
77122
}
78123
}
79124

0 commit comments

Comments
 (0)