Lots of progress
This commit is contained in:
190
main.go
190
main.go
@@ -2,9 +2,19 @@ package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"git.nevets.tech/Steven/ezconf"
|
||||
"github.com/go-git/go-billy/v5"
|
||||
"github.com/go-git/go-billy/v5/memfs"
|
||||
@@ -12,12 +22,7 @@ import (
|
||||
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
||||
"github.com/go-git/go-git/v5/storage/memory"
|
||||
"github.com/google/go-github/v55/github"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"github.com/makifdb/pidfile"
|
||||
)
|
||||
|
||||
var config *ezconf.Configuration
|
||||
@@ -33,57 +38,175 @@ var creds *http.BasicAuth
|
||||
|
||||
var repo *git.Repository
|
||||
|
||||
var ctx context.Context
|
||||
var cancel context.CancelFunc
|
||||
var wg sync.WaitGroup
|
||||
|
||||
//TODO create logic for gh vs gt repos
|
||||
|
||||
func main() {
|
||||
|
||||
devFlag := flag.Bool("dev", false, "Developer Mode")
|
||||
|
||||
configFile := flag.String("config", "/etc/certman/certman.conf", "Configuration file")
|
||||
|
||||
newDomainFlag := flag.String("new-domain", "example.com", "Domain to create new configs and directories for")
|
||||
newDomainDirFlag := flag.String("new-domain-dir", "/opt/certs/example.com", "Directory that certs will be stored in")
|
||||
|
||||
installFlag := flag.Bool("install", false, "Install Certman")
|
||||
modeFlag := flag.String("mode", "client", "CertManager Mode [server, client]")
|
||||
thinInstallFlag := flag.Bool("t", false, "Thin Install (skip creating dirs)")
|
||||
|
||||
newKeyFlag := flag.Bool("newkey", false, "Generate new encryption key")
|
||||
|
||||
reloadFlag := flag.Bool("reload", false, "Reload configs")
|
||||
|
||||
daemonFlag := flag.Bool("d", false, "Daemon Mode")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
if *devFlag {
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
if *newDomainFlag != "example.com" {
|
||||
fmt.Printf("Creating new domain %s\n", *newDomainFlag)
|
||||
createNewDomainConfig(*newDomainFlag)
|
||||
createNewDomainCertsDir(*newDomainFlag)
|
||||
createNewDomainCertsDir(*newDomainFlag, *newDomainDirFlag)
|
||||
fmt.Println("Successfully created domain entry for " + *newDomainFlag + "\nUpdate config file as needed in /etc/certman/domains/" + *newDomainFlag + ".conf")
|
||||
os.Exit(0)
|
||||
}
|
||||
if *installFlag {
|
||||
makeDirs()
|
||||
config = ezconf.NewConfiguration("/etc/certman/certman.conf", defaultConfig)
|
||||
if !*thinInstallFlag {
|
||||
makeDirs()
|
||||
}
|
||||
config = ezconf.NewConfiguration(*configFile, strings.ReplaceAll(defaultConfig, "{mode}", *modeFlag))
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if *newKeyFlag {
|
||||
key, err := GenerateKey()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf(key)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if *reloadFlag {
|
||||
pidBytes, err := os.ReadFile("/var/run/certman.pid")
|
||||
if err != nil {
|
||||
fmt.Printf("Error getting PID from /var/run/certman.pid: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
pidStr := strings.TrimSpace(string(pidBytes))
|
||||
daemonPid, err := strconv.Atoi(pidStr)
|
||||
if err != nil {
|
||||
fmt.Printf("Error converting PID string to int (%s): %v\n", pidStr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
proc, err := os.FindProcess(daemonPid)
|
||||
if err != nil {
|
||||
fmt.Printf("Error finding process with PID %d: %v\n", daemonPid, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
err = proc.Signal(syscall.SIGHUP)
|
||||
if err != nil {
|
||||
fmt.Printf("Error sending SIGHUP to PID %d: %v\n", daemonPid, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if *daemonFlag {
|
||||
err := pidfile.CreateOrUpdatePIDFile("/var/run/certman.pid")
|
||||
if err != nil {
|
||||
fmt.Println("Error creating pidfile")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
ctx, cancel = context.WithCancel(context.Background())
|
||||
|
||||
// Check if main config exists
|
||||
if _, err := os.Stat("/etc/certman/certman.conf"); os.IsNotExist(err) {
|
||||
if _, err := os.Stat(*configFile); os.IsNotExist(err) {
|
||||
fmt.Println("Main config file not found, please run 'certman --install', then properly configure /etc/certman/certman.conf.")
|
||||
os.Exit(1)
|
||||
} else if err != nil {
|
||||
fmt.Printf("Error opening /etc/certman/certman.conf: %v\n", err)
|
||||
fmt.Printf("Error opening %s: %v\n", *configFile, err)
|
||||
}
|
||||
config = ezconf.LoadConfiguration(*configFile)
|
||||
|
||||
// Setup SIGINT and SIGTERM listeners
|
||||
sigChannel := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChannel, syscall.SIGINT, syscall.SIGTERM)
|
||||
defer signal.Stop(sigChannel)
|
||||
|
||||
// Task loop
|
||||
go func() {
|
||||
reloadSigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(reloadSigChan, syscall.SIGHUP)
|
||||
defer signal.Stop(reloadSigChan)
|
||||
|
||||
}()
|
||||
ticker := time.NewTicker(5 * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
wg.Add(1)
|
||||
if config.GetAsString("App.mode") == "server" {
|
||||
fmt.Println("Starting CertManager in server mode...")
|
||||
// Server Task loop
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
fmt.Println("Shutting down server")
|
||||
return
|
||||
case <-reloadSigChan:
|
||||
{
|
||||
fmt.Println("Reloading configs...")
|
||||
}
|
||||
case <-ticker.C:
|
||||
{
|
||||
fmt.Println("Tick!")
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
} else if config.GetAsString("App.mode") == "client" {
|
||||
fmt.Println("Starting CertManager in client mode...")
|
||||
// Client Task loop
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
fmt.Println("Shutting down client")
|
||||
return
|
||||
case <-reloadSigChan:
|
||||
{
|
||||
fmt.Println("Reloading configs...")
|
||||
}
|
||||
case <-ticker.C:
|
||||
{
|
||||
fmt.Println("Tick!")
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
} else {
|
||||
fmt.Println("Invalid operating mode \"" + config.GetAsString("App.mode") + "\"")
|
||||
}
|
||||
|
||||
// Cleanup on stop
|
||||
sig := <-sigChannel
|
||||
fmt.Printf("Program terminated with %v\n", sig)
|
||||
|
||||
close()
|
||||
stop()
|
||||
wg.Wait()
|
||||
}
|
||||
}
|
||||
|
||||
func close() {
|
||||
|
||||
func stop() {
|
||||
cancel()
|
||||
}
|
||||
|
||||
func maindis() {
|
||||
@@ -141,7 +264,6 @@ func maindis() {
|
||||
{
|
||||
url := createGiteaRepo()
|
||||
repo, workTree = cloneRepo(url)
|
||||
fixUpdateSh()
|
||||
cmd = exec.Command("lego", legoNewSiteArgs...)
|
||||
}
|
||||
case "renew":
|
||||
@@ -161,7 +283,6 @@ func maindis() {
|
||||
{
|
||||
url := createGiteaRepo()
|
||||
repo, workTree = cloneRepo(url)
|
||||
fixUpdateSh()
|
||||
addAndPushCerts()
|
||||
os.Exit(0)
|
||||
}
|
||||
@@ -202,30 +323,3 @@ func maindis() {
|
||||
}
|
||||
addAndPushCerts()
|
||||
}
|
||||
|
||||
func fixUpdateSh() {
|
||||
oldUpdateSh, err := fs.Open("update.sh")
|
||||
if err != nil {
|
||||
fmt.Printf("Error opening update.sh: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
contentBytes, err := io.ReadAll(oldUpdateSh)
|
||||
if err != nil {
|
||||
fmt.Printf("Error reading update.sh: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
content := string(contentBytes)
|
||||
strings.ReplaceAll(content, "<>", domain)
|
||||
updateSh, err := fs.Create("update.sh")
|
||||
_, err = updateSh.Write([]byte(content))
|
||||
err = updateSh.Close()
|
||||
if err != nil {
|
||||
fmt.Printf("Error writing update.sh: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
_, err = workTree.Add("update.sh")
|
||||
if err != nil {
|
||||
fmt.Printf("Error adding update.sh: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user