server

package
v0.4.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 3, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DeployCertificateCmd = &cobra.Command{
	Use:   "deploy-cert",
	Short: "Deploy certificate to a domain",
	RunE: func(cmd *cobra.Command, args []string) error {
		config, err := config.GetConfig()

		if err != nil {
			return err
		}

		log, err := logger.NewLogger(config)

		if err != nil {
			return err
		}

		supportedWebServerCodes := webserver.GetSupportedWebServers()

		if webServerCode == "" {
			return fmt.Errorf("webserver is not specified")
		}

		if serverName == "" {
			return fmt.Errorf("domain is not specified")
		}

		if !slices.Contains(supportedWebServerCodes, webServerCode) {
			return fmt.Errorf("invalid webserver code %s", webServerCode)
		}

		webServer, err := webserver.GetWebServer(webServerCode, map[string]string{})

		if err != nil {
			return err
		}

		processManager, err := webServer.GetProcessManager()

		if err != nil {
			return err
		}

		vhost, err := webServer.GetVhostByName(serverName)

		if err != nil {
			return err
		}

		webServerReverter := &reverter.Reverter{
			HostMng: webServer.GetVhostManager(),
			Logger:  log,
		}

		if vhost == nil {
			return fmt.Errorf("could not find virtual host '%s'", serverName)
		}

		deployer, err := deploy.GetCertificateDeployer(webServer, webServerReverter, log)

		if err != nil {
			return err
		}

		sslConfigFilePath, err := deployer.DeployCertificate(vhost, certPath, certKeyPath)

		if err != nil {
			if rErr := webServerReverter.Rollback(); rErr != nil {
				log.Error(fmt.Sprintf("failed to rallback webserver configuration on cert deploy: %v", rErr))
			}

			return err
		}

		if err = webServer.GetVhostManager().Enable(sslConfigFilePath); err != nil {
			if rErr := webServerReverter.Rollback(); rErr != nil {
				log.Error(fmt.Sprintf("failed to rallback webserver configuration on host enabling: %v", rErr))
			}

			return err
		}

		if err = processManager.Reload(); err != nil {
			if rErr := webServerReverter.Rollback(); rErr != nil {
				log.Error(fmt.Sprintf("failed to rallback webserver configuration on webserver reload: %v", rErr))
			}

			return err
		}

		if err = webServerReverter.Commit(); err != nil {
			if rErr := webServerReverter.Rollback(); rErr != nil {
				log.Error(fmt.Sprintf("failed to commit webserver configuration: %v", rErr))
			}
		}

		return nil
	},
}
View Source
var HostsCmd = &cobra.Command{
	Use:   "hosts",
	Short: "Show virtual hosts of web servers",
	RunE: func(cmd *cobra.Command, args []string) error {
		conf, err := config.GetConfig()

		if err != nil {
			return err
		}

		log, err := logger.NewLogger(conf)

		if err != nil {
			return err
		}

		supportedWebServerCodes := webserver.GetSupportedWebServers()
		webServerCodes := supportedWebServerCodes

		if webServerCode != "" {
			if !slices.Contains(supportedWebServerCodes, webServerCode) {
				return fmt.Errorf("invalid webserver code %s", webServerCode)
			}

			webServerCodes = []string{webServerCode}
		}

		var vhosts []agentintegration.VirtualHost

		for _, webServerCode := range webServerCodes {
			webServer, err := webserver.GetWebServer(webServerCode, map[string]string{})

			if err != nil {
				log.Info(fmt.Sprintf("failed to get %s webserver: %v", webServerCode, err))

				continue
			}

			hosts, err := webServer.GetVhosts()

			if err != nil {
				return err
			}

			vhosts = append(vhosts, hosts...)
		}

		if isJson {
			output, err := json.Marshal(vhosts)

			if err != nil {
				return err
			}

			return writeOutput(cmd, string(output))
		}

		var outputParts []string

		for _, host := range vhosts {
			output, err := yaml.Marshal(host)

			if err != nil {
				return err
			}

			outputParts = append(outputParts, string(output))
		}

		return writeOutput(cmd, strings.Join(outputParts, "\n"))
	},
}
View Source
var RootCmd = &cobra.Command{
	Use:   "r2dtools",
	Short: "R2DTools agent",
	Run: func(cmd *cobra.Command, args []string) {
		cmd.Usage()
	},
}
View Source
var ServeCmd = &cobra.Command{
	Use:   "serve",
	Short: "Starts TCP server",
	RunE: func(cmd *cobra.Command, args []string) error {
		config, err := config.GetConfig()

		if err != nil {
			return err
		}

		logger, err := logger.NewLogger(config)

		if err != nil {
			return err
		}

		certificatesHandler, err := certificates.GetHandler(config, logger)

		if err != nil {
			return err
		}

		router := router.Router{}
		router.RegisterHandler("main", &server.MainHandler{
			Config: config,
			Logger: logger,
		})
		router.RegisterHandler("certificates", certificatesHandler)

		server := &server.Server{
			Port:   config.Port,
			Router: router,
			Logger: logger,
			Config: config,
		}

		if err := server.Serve(); err != nil {
			return err
		}

		return nil
	},
}
View Source
var VersionCmd = &cobra.Command{
	Use:   "version",
	Short: "Show agent version",
	RunE: func(cmd *cobra.Command, args []string) error {
		config, err := config.GetConfig()

		if err != nil {
			return err
		}

		version, err := agent.GetAgentVersion(config)

		if err != nil {
			return err
		}

		fmt.Println(version)

		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL