feat: add daemon mode

This commit is contained in:
Nicolas Chatelain
2025-03-18 21:14:23 +01:00
parent b843007b18
commit 24387a6de0
4 changed files with 32 additions and 6 deletions
+9
View File
@@ -54,6 +54,15 @@ func authMiddleware() gin.HandlerFunc {
}
}
func GetAPIUrl() string {
targetDomain := config.Config.GetString("web.listen")
scheme := "http"
if config.Config.GetBool("web.tls.enabled") {
scheme = "https"
}
return fmt.Sprintf("%s://%s", scheme, targetDomain)
}
func StartLigoloApi() {
if config.Config.GetBool("web.debug") {
gin.SetMode(gin.DebugMode)
+2 -1
View File
@@ -156,6 +156,7 @@ func InitConfig(configFile string) {
Config.AddConfigPath("$HOME/.ligolo-proxy")
Config.AddConfigPath("/etc/ligolo-proxy")
logrus.Infof("Loading configuration file %s", configFile)
if err := Config.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
logrus.Warn("daemon configuration file not found. Creating a new one...")
@@ -198,7 +199,7 @@ func InitConfig(configFile string) {
Config.SetDefault("web.secret", hex.EncodeToString(secret))
if firstStart {
Config.SetDefault("interface.ligolo.routes", []string{"10.254.0.0/24", "10.255.0.0/24"})
Config.SetDefault("interface.ligolosample.routes", []string{"10.254.0.0/24", "10.255.0.0/24"})
Config.SetDefault("agent.deadbeefcafe.interface", "ligolo")
Config.SetDefault("agent.deadbeefcafe.autobind", false)
}
+15 -5
View File
@@ -34,6 +34,7 @@ func main() {
var versionFlag = flag.Bool("version", false, "show the current version")
var hideBanner = flag.Bool("nobanner", false, "don't show banner on startup")
var configFile = flag.String("config", "", "the config file to use")
var daemonMode = flag.Bool("daemon", false, "run as daemon mode (no CLI)")
flag.Usage = func() {
fmt.Printf("Ligolo-ng %s / %s / %s\n", version, commit, date)
@@ -62,7 +63,7 @@ func main() {
allowDomains = strings.Split(*domainWhitelist, ",")
}
if !*hideBanner {
if !*hideBanner && !*daemonMode {
app.App.SetPrintASCIILogo(func(a *grumble.App) {
a.Println(" __ _ __ ")
a.Println(" / / (_)___ _____ / /___ ____ ____ _")
@@ -140,12 +141,21 @@ func main() {
}
}()
if *daemonMode && !config.Config.GetBool("web.enabled") {
logrus.Warning("daemon mode enabled but web.enabled is false!")
}
if config.Config.GetBool("web.enabled") {
logrus.Info("Starting Ligolo-ng Web")
logrus.Infof("Starting Ligolo-ng Web, API URL is set to: %s", app.GetAPIUrl())
go app.StartLigoloApi()
}
// Grumble doesn't like cli args
os.Args = []string{}
grumble.Main(app.App)
if *daemonMode {
proxyController.WaitForFinished()
} else {
// Grumble doesn't like cli args
os.Args = []string{}
grumble.Main(app.App)
}
}
+6
View File
@@ -16,6 +16,7 @@ type Controller struct {
Network string
Connection chan net.Conn
startchan chan error
donechan chan error
ControllerConfig
}
@@ -33,6 +34,10 @@ func (c *Controller) WaitForReady() error {
return <-c.startchan
}
func (c *Controller) WaitForFinished() error {
return <-c.donechan
}
func (c *Controller) GetSelfCertificateSignature() (*tls.Certificate, error) {
if c.CertManagerConfig.EnableSelfcert {
return c.tlsConfig.GetCertificate(nil)
@@ -91,4 +96,5 @@ func (c *Controller) ListenAndServe() {
c.Connection <- conn
}
}
c.donechan <- nil
}