Skip to content

Commit

Permalink
Merge pull request #352 from skrashevich/patch-listen-tls
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Jun 29, 2023
2 parents 7bd42eb + c94d1e2 commit ac5bcda
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
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"
"fmt"
"github.com/AlexxIT/go2rtc/internal/app"
Expand All @@ -22,6 +23,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 @@ -74,6 +78,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")
}
}()
}
}
}

const (
Expand Down

0 comments on commit ac5bcda

Please sign in to comment.