Skip to content

Commit

Permalink
Enhancement: Launcher re-generate certificate if expired or less than…
Browse files Browse the repository at this point in the history
… 1 day to and starting the server
  • Loading branch information
luskaner committed Dec 21, 2024
1 parent 145f27d commit cb63c1a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 deletions.
16 changes: 9 additions & 7 deletions common/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ func CertificatePairFolder(executablePath string) string {
return folder
}

func HasCertificatePair(executablePath string) bool {
parentDir := CertificatePairFolder(executablePath)
func CertificatePair(executablePath string) (ok bool, parentDir string, cert string) {
parentDir = CertificatePairFolder(executablePath)
if parentDir == "" {
return false
return
}
if _, err := os.Stat(filepath.Join(parentDir, Cert)); os.IsNotExist(err) {
return false
cert = filepath.Join(parentDir, Cert)
if _, err := os.Stat(cert); os.IsNotExist(err) {
return
}
if _, err := os.Stat(filepath.Join(parentDir, Key)); os.IsNotExist(err) {
return false
return
}
return true
ok = true
return
}
8 changes: 4 additions & 4 deletions launcher/internal/cmdUtils/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ func (c *Config) StartServer(executable string, args []string, stop bool, canTru
if executable != serverExecutablePath {
fmt.Println("Found server executable path:", serverExecutablePath)
}
if !common.HasCertificatePair(serverExecutablePath) {

if exists, certificateFolder, cert := common.CertificatePair(serverExecutablePath); !exists || server.CertificateSoonExpired(cert) {
if !canTrustCertificate {
fmt.Println("serverStart is true and canTrustCertificate is false. Certificate pair is missing. Generate your own certificates manually.")
errorCode = internal.ErrServerCertMissing
fmt.Println("serverStart is true and canTrustCertificate is false. Certificate pair is missing or soon expired. Generate your own certificates manually.")
errorCode = internal.ErrServerCertMissingExpired
return
}
certificateFolder := common.CertificatePairFolder(serverExecutablePath)
if certificateFolder == "" {
fmt.Println("Cannot find certificate folder of the server. Make sure the folder structure of the server is correct.")
errorCode = internal.ErrServerCertDirectory
Expand Down
2 changes: 1 addition & 1 deletion launcher/internal/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
ErrServerExecutable
ErrServerConnectSecure
ErrServerUnreachable
ErrServerCertMissing
ErrServerCertMissingExpired
ErrServerCertDirectory
ErrServerCertCreate
ErrServerStart
Expand Down
27 changes: 26 additions & 1 deletion launcher/internal/server/ssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package server
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"github.com/luskaner/ageLANServer/common"
"github.com/luskaner/ageLANServer/launcher-common/executor/exec"
"net"
"os"
"path/filepath"
"time"
)

func TlsConfig(insecureSkipVerify bool) *tls.Config {
Expand Down Expand Up @@ -56,6 +58,29 @@ func GenerateCertificatePair(certificateFolder string) (result *exec.Result) {
if _, err := os.Stat(exePath); err != nil {
return nil
}
result = exec.Options{File: exePath, Wait: true, ExitCode: true}.Exec()
result = exec.Options{File: exePath, Wait: true, Args: []string{"-r"}, ExitCode: true}.Exec()
return
}

func CertificateSoonExpired(cert string) bool {
if cert == "" {
return true
}

certPEM, err := os.ReadFile(cert)
if err != nil {
return true
}

block, _ := pem.Decode(certPEM)
if block == nil {
return true
}

crt, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return true
}

return time.Now().Add(24 * time.Hour).After(crt.NotAfter)
}
8 changes: 5 additions & 3 deletions server-genCert/internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ var (
fmt.Println("Failed to determine certificate pair folder")
os.Exit(internal.ErrCertDirectory)
}
if !replace && common.HasCertificatePair(serverExe) {
fmt.Println("Already have certificate pair and force is false, set force to true or delete it manually.")
os.Exit(internal.ErrCertCreateExisting)
if !replace {
if exists, _, _ := common.CertificatePair(serverExe); exists {
fmt.Println("Already have certificate pair and force is false, set force to true or delete it manually.")
os.Exit(internal.ErrCertCreateExisting)
}
}
if !internal.GenerateCertificatePair(serverFolder) {
fmt.Println("Could not generate certificate pair.")
Expand Down

0 comments on commit cb63c1a

Please sign in to comment.