mirror of https://github.com/jetkvm/kvm.git
feat: simple TLS support (#247)
This commit is contained in:
commit
c4348c7eb4
5
main.go
5
main.go
|
@ -35,6 +35,8 @@ func Main() {
|
||||||
StartNativeCtrlSocketServer()
|
StartNativeCtrlSocketServer()
|
||||||
StartNativeVideoSocketServer()
|
StartNativeVideoSocketServer()
|
||||||
|
|
||||||
|
initPrometheus()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
err = ExtractAndRunNativeBin()
|
err = ExtractAndRunNativeBin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -67,6 +69,9 @@ func Main() {
|
||||||
}()
|
}()
|
||||||
//go RunFuseServer()
|
//go RunFuseServer()
|
||||||
go RunWebServer()
|
go RunWebServer()
|
||||||
|
if config.TLSMode != "" {
|
||||||
|
go RunWebSecureServer()
|
||||||
|
}
|
||||||
// If the cloud token isn't set, the client won't be started by default.
|
// If the cloud token isn't set, the client won't be started by default.
|
||||||
// However, if the user adopts the device via the web interface, handleCloudRegister will start the client.
|
// However, if the user adopts the device via the web interface, handleCloudRegister will start the client.
|
||||||
if config.CloudToken != "" {
|
if config.CloudToken != "" {
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
package kvm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version"
|
||||||
|
"github.com/prometheus/common/version"
|
||||||
|
)
|
||||||
|
|
||||||
|
var promHandler http.Handler
|
||||||
|
|
||||||
|
func initPrometheus() {
|
||||||
|
// A Prometheus metrics endpoint.
|
||||||
|
version.Version = builtAppVersion
|
||||||
|
prometheus.MustRegister(versioncollector.NewCollector("jetkvm"))
|
||||||
|
}
|
5
web.go
5
web.go
|
@ -12,10 +12,7 @@ import (
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version"
|
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
"github.com/prometheus/common/version"
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -90,8 +87,6 @@ func setupRouter() *gin.Engine {
|
||||||
r.POST("/device/setup", handleSetup)
|
r.POST("/device/setup", handleSetup)
|
||||||
|
|
||||||
// A Prometheus metrics endpoint.
|
// A Prometheus metrics endpoint.
|
||||||
version.Version = builtAppVersion
|
|
||||||
prometheus.MustRegister(versioncollector.NewCollector("jetkvm"))
|
|
||||||
r.GET("/metrics", gin.WrapH(promhttp.Handler()))
|
r.GET("/metrics", gin.WrapH(promhttp.Handler()))
|
||||||
|
|
||||||
// Protected routes (allows both password and noPassword modes)
|
// Protected routes (allows both password and noPassword modes)
|
||||||
|
|
|
@ -0,0 +1,132 @@
|
||||||
|
package kvm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/ecdsa"
|
||||||
|
"crypto/elliptic"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/tls"
|
||||||
|
"crypto/x509"
|
||||||
|
"crypto/x509/pkix"
|
||||||
|
"encoding/pem"
|
||||||
|
"log"
|
||||||
|
"math/big"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
WebSecureListen = ":443"
|
||||||
|
WebSecureSelfSignedDefaultDomain = "jetkvm.local"
|
||||||
|
WebSecureSelfSignedDuration = 365 * 24 * time.Hour
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
tlsCerts = make(map[string]*tls.Certificate)
|
||||||
|
tlsCertLock = &sync.Mutex{}
|
||||||
|
)
|
||||||
|
|
||||||
|
// RunWebSecureServer runs a web server with TLS.
|
||||||
|
func RunWebSecureServer() {
|
||||||
|
r := setupRouter()
|
||||||
|
|
||||||
|
server := &http.Server{
|
||||||
|
Addr: WebSecureListen,
|
||||||
|
Handler: r,
|
||||||
|
TLSConfig: &tls.Config{
|
||||||
|
// TODO: cache certificate in persistent storage
|
||||||
|
GetCertificate: func(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
||||||
|
hostname := WebSecureSelfSignedDefaultDomain
|
||||||
|
if info.ServerName != "" {
|
||||||
|
hostname = info.ServerName
|
||||||
|
} else {
|
||||||
|
hostname = strings.Split(info.Conn.LocalAddr().String(), ":")[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Infof("TLS handshake for %s, SupportedProtos: %v", hostname, info.SupportedProtos)
|
||||||
|
|
||||||
|
cert := createSelfSignedCert(hostname)
|
||||||
|
|
||||||
|
return cert, nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
logger.Infof("Starting websecure server on %s", RunWebSecureServer)
|
||||||
|
err := server.ListenAndServeTLS("", "")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func createSelfSignedCert(hostname string) *tls.Certificate {
|
||||||
|
if tlsCert := tlsCerts[hostname]; tlsCert != nil {
|
||||||
|
return tlsCert
|
||||||
|
}
|
||||||
|
tlsCertLock.Lock()
|
||||||
|
defer tlsCertLock.Unlock()
|
||||||
|
|
||||||
|
logger.Infof("Creating self-signed certificate for %s", hostname)
|
||||||
|
|
||||||
|
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to generate private key: %v", err)
|
||||||
|
}
|
||||||
|
keyUsage := x509.KeyUsageDigitalSignature
|
||||||
|
|
||||||
|
notBefore := time.Now()
|
||||||
|
notAfter := notBefore.AddDate(1, 0, 0)
|
||||||
|
|
||||||
|
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
||||||
|
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("Failed to generate serial number: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
dnsName := hostname
|
||||||
|
ip := net.ParseIP(hostname)
|
||||||
|
if ip != nil {
|
||||||
|
dnsName = WebSecureSelfSignedDefaultDomain
|
||||||
|
}
|
||||||
|
|
||||||
|
template := x509.Certificate{
|
||||||
|
SerialNumber: serialNumber,
|
||||||
|
Subject: pkix.Name{
|
||||||
|
CommonName: hostname,
|
||||||
|
Organization: []string{"JetKVM"},
|
||||||
|
},
|
||||||
|
NotBefore: notBefore,
|
||||||
|
NotAfter: notAfter,
|
||||||
|
|
||||||
|
KeyUsage: keyUsage,
|
||||||
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
||||||
|
BasicConstraintsValid: true,
|
||||||
|
|
||||||
|
DNSNames: []string{dnsName},
|
||||||
|
IPAddresses: []net.IP{},
|
||||||
|
}
|
||||||
|
|
||||||
|
if ip != nil {
|
||||||
|
template.IPAddresses = append(template.IPAddresses, ip)
|
||||||
|
}
|
||||||
|
|
||||||
|
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("Failed to create certificate: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cert := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
|
||||||
|
if cert == nil {
|
||||||
|
logger.Errorf("Failed to encode certificate")
|
||||||
|
}
|
||||||
|
|
||||||
|
tlsCert := &tls.Certificate{
|
||||||
|
Certificate: [][]byte{derBytes},
|
||||||
|
PrivateKey: priv,
|
||||||
|
}
|
||||||
|
tlsCerts[hostname] = tlsCert
|
||||||
|
|
||||||
|
return tlsCert
|
||||||
|
}
|
Loading…
Reference in New Issue