server

package
v0.4.11 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2024 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CommonDirCmd = &cobra.Command{
	Use:   "common-dir",
	Short: "Manage ACME common directory for a host",
	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
		}

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

		webServer, _, err := findWebServerHost(serverName, log)

		if err != nil {
			return err
		}

		processManager, err := webServer.GetProcessManager()

		if err != nil {
			return err
		}

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

		commonDirManager, err := commondir.GetCommonDirManager(webServer, webServerReverter, log)

		if err != nil {
			return err
		}

		if enableCommonDir {
			err = commonDirManager.EnableCommonDir(serverName)
		} else if disableCommonDir {
			err = commonDirManager.DisableCommonDir(serverName)
		} else {
			fmt.Printf("Common directory status for host %s: %t\n", serverName, commonDirManager.IsCommonDirEnabled(serverName))

			return nil
		}

		if err != nil {
			if rErr := webServerReverter.Rollback(); rErr != nil {
				log.Error(fmt.Sprintf("failed to rallback webserver configuration on common directory switching: %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 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, originEnabledConfigFilePath, 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, filepath.Dir(originEnabledConfigFilePath)); 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 GenerateTokenCmd = &cobra.Command{
	Use:   "generate-token",
	Short: "Generate new token",
	RunE: func(cmd *cobra.Command, args []string) error {
		conf, err := config.GetConfig()

		if err != nil {
			return err
		}

		data, err := os.ReadFile(conf.ConfigFilePath)

		if err != nil {
			return err
		}

		confMap := make(map[string]interface{})
		err = yaml.Unmarshal(data, confMap)

		if err != nil {
			return err
		}

		randomUuid, err := uuid.NewRandom()

		if err != nil {
			return err
		}

		token := randomUuid.String()
		confMap["Token"] = token
		data, err = yaml.Marshal(confMap)

		if err != nil {
			return err
		}

		err = os.WriteFile(conf.ConfigFilePath, data, 0644)

		if err != nil {
			return err
		}

		fmt.Printf("Token: %s\n", token)

		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 IssueCertificateCmd = &cobra.Command{
	Use:   "issue-cert",
	Short: "Secure domain with a certificate",
	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
		}

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

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

		webServer, vhost, err := findWebServerHost(serverName, log)

		if err != nil {
			return err
		}

		certManager, err := certificates.GetCertificateManager(config, log)

		if err != nil {
			return err
		}

		certData := agentintegration.CertificateIssueRequestData{
			Email:         email,
			ServerName:    serverName,
			DocRoot:       vhost.DocRoot,
			WebServer:     webServer.GetCode(),
			ChallengeType: certificates.HttpChallengeTypeCode,
			Subjects:      aliases,
			Assign:        assign,
		}
		cert, err := certManager.Issue(certData)

		if err != nil {
			return err
		}

		data, err := json.MarshalIndent(cert, "", " ")

		if err != nil {
			return err
		}

		fmt.Println(string(data))

		return nil
	},
}
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