Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TLS support for API #352

Merged
merged 2 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -623,11 +623,19 @@ api:
base_path: "/rtc" # default "", API prefix for serve on suburl (/api => /rtc/api)
static_dir: "www" # default "", folder for static files (custom web interface)
origin: "*" # default "", allow CORS requests (only * supported)
tls_listen: ":1985" # default "", HTTPS port
tls_cert: | # default "". PEM-encoded fullchain certificate for https
-----BEGIN CERTIFICATE-----
.....
-----END CERTIFICATE-----
tls_private_key: | # default "". PEM-encoded private key for https
-----BEGIN PRIVATE KEY-----
.....
-----END PRIVATE KEY-----
```

**PS:**

- go2rtc doesn't provide HTTPS. Use [Nginx](https://nginx.org/) or [Ngrok](#module-ngrok) or [Home Assistant Add-on](#go2rtc-home-assistant-add-on) for this tasks
- MJPEG over WebSocket plays better than native MJPEG because Chrome [bug](https://bugs.chromium.org/p/chromium/issues/detail?id=527446)
- MP4 over WebSocket was created only for Apple iOS because it doesn't support MSE and native MP4

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

import (
"crypto/tls"
"encoding/json"
"github.com/AlexxIT/go2rtc/internal/app"
"github.com/rs/zerolog"
Expand All @@ -21,6 +22,9 @@ func Init() {
BasePath string `yaml:"base_path"`
StaticDir string `yaml:"static_dir"`
Origin string `yaml:"origin"`
TLSListen string `yaml:"tls_listen"`
TLSCert string `yaml:"tls_cert"`
TLSPrivateKey string `yaml:"tls_private_key"`
} `yaml:"api"`
}

Expand Down Expand Up @@ -75,6 +79,37 @@ func Init() {
log.Fatal().Err(err).Msg("[api] serve")
}
}()

// Initialize the HTTPS server
if cfg.Mod.TLSListen != "" {
tlsConfig := &tls.Config{}
if cfg.Mod.TLSCert != "" && cfg.Mod.TLSPrivateKey != "" {
tlsListener, err := net.Listen("tcp", cfg.Mod.TLSListen)
if err != nil {
log.Fatal().Err(err).Msg("[api] tls listen")
return
}
log.Info().Str("addr", cfg.Mod.TLSListen).Msg("[api] tls listen")

cert, err := tls.X509KeyPair([]byte(cfg.Mod.TLSCert), []byte(cfg.Mod.TLSPrivateKey))
if err != nil {
print(cfg.Mod.TLSCert)
log.Fatal().Err(err).Msg("[api] tls load cert/key")
return
}
tlsConfig.Certificates = []tls.Certificate{cert}

tlsServer := &http.Server{
Handler: Handler,
TLSConfig: tlsConfig,
}
go func() {
if err := tlsServer.ServeTLS(tlsListener, "", ""); err != nil {
log.Fatal().Err(err).Msg("[api] tls serve")
}
}()
}
}
}

var Handler http.Handler
Expand Down
Loading