74 lines
2.9 KiB
Go
74 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"syscall"
|
|
"time"
|
|
|
|
"sea-vhost/internal/isolation"
|
|
)
|
|
|
|
func main() {
|
|
socket := env("SEA_AGENT_SOCKET", "/run/sea-vhost/agent.sock")
|
|
root := env("SEA_TENANT_ROOT", "/www/wwwroot/seavHost/vHost")
|
|
backupRoot := env("SEA_BACKUP_ROOT", "/www/backup/sea-vhost")
|
|
legacyRoot := env("SEA_LEGACY_SITE_ROOT", "/www/wwwroot")
|
|
fileOwner := env("SEA_FILE_OWNER", "sea-vhost")
|
|
nodePIDRoot := env("SEA_NODE_PID_ROOT", "/www/server/nodejs/vhost/pids")
|
|
nginxVhostRoot := env("SEA_NGINX_VHOST_ROOT", "/www/server/panel/vhost/nginx")
|
|
nginxBinary := env("SEA_NGINX_BINARY", "/www/server/nginx/sbin/nginx")
|
|
purePWBinary := env("SEA_PURE_PW_BINARY", "/www/server/pure-ftpd/bin/pure-pw")
|
|
phpStateRoot := env("SEA_PHP_IDENTITY_STATE", "/var/lib/sea-vhost-agent/php")
|
|
phpRunRoot := env("SEA_PHP_IDENTITY_RUN", "/run/sea-vhost-php")
|
|
usageStatePath := env("SEA_USAGE_STATE", "/var/lib/sea-vhost-agent/traffic.json")
|
|
nginxLogRoot := env("SEA_NGINX_LOG_ROOT", "/www/wwwlogs")
|
|
certificateRoot := env("SEA_CERT_ROOT", "/www/server/panel/vhost/cert")
|
|
pureFTPConfig := env("SEA_PURE_FTP_CONFIG", "/www/server/pure-ftpd/etc/pure-ftpd.conf")
|
|
pureFTPPem := env("SEA_PURE_FTP_PEM", "/www/server/pure-ftpd/etc/pure-ftpd.pem")
|
|
if err := os.MkdirAll(filepath.Dir(socket), 0750); err != nil {
|
|
slog.Error("cannot create socket directory", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
_ = os.Remove(socket)
|
|
listener, err := net.Listen("unix", socket)
|
|
if err != nil {
|
|
slog.Error("cannot listen", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
defer listener.Close()
|
|
if err = os.Chmod(socket, 0660); err != nil {
|
|
slog.Error("cannot secure socket", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
agent := isolation.Agent{Root: root, BackupRoot: backupRoot, LegacyRoot: legacyRoot, FileOwner: fileOwner, NodePIDRoot: nodePIDRoot, NginxVhostRoot: nginxVhostRoot, NginxBinary: nginxBinary, PurePWBinary: purePWBinary, PHPStateRoot: phpStateRoot, PHPRunRoot: phpRunRoot, UsageStatePath: usageStatePath, NginxLogRoot: nginxLogRoot, CertificateRoot: certificateRoot, PureFTPConfig: pureFTPConfig, PureFTPPem: pureFTPPem, InternalToken: os.Getenv("SEA_INTERNAL_AUTH_TOKEN")}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
if os.Getenv("SEA_NODE_STATELESS") != "true" {
|
|
go agent.RunRuntimeBackupExpiry(ctx)
|
|
}
|
|
server := &http.Server{Handler: agent.Handler(), ReadHeaderTimeout: 5 * time.Second, ReadTimeout: 30 * time.Second, WriteTimeout: 2 * time.Hour, IdleTimeout: 30 * time.Second}
|
|
go func() {
|
|
if err := server.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
slog.Error("agent failed", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
}()
|
|
stop := make(chan os.Signal, 1)
|
|
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
|
|
<-stop
|
|
_ = server.Close()
|
|
}
|
|
func env(name, fallback string) string {
|
|
if value := os.Getenv(name); value != "" {
|
|
return value
|
|
}
|
|
return fallback
|
|
}
|