agentcmd

package
v0.12.4 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AgentCmd = &cobra.Command{
	Use:   "agent",
	Short: "Start the knot agent",
	Long: `Start the knot agent and listen for incoming connections.

The agent will listen on the port specified by the --listen flag and proxy requests to the code-server instance running on the host.`,
	Args: cobra.NoArgs,
	PreRun: func(cmd *cobra.Command, args []string) {
		viper.BindPFlag("agent.endpoint", cmd.Flags().Lookup("endpoint"))
		viper.BindEnv("agent.endpoint", config.CONFIG_ENV_PREFIX+"_AGENT_ENDPOINT")

		viper.BindPFlag("agent.space_id", cmd.Flags().Lookup("space-id"))
		viper.BindEnv("agent.space_id", config.CONFIG_ENV_PREFIX+"_SPACEID")

		viper.BindPFlag("agent.port.code_server", cmd.Flags().Lookup("code-server-port"))
		viper.BindEnv("agent.port.code_server", config.CONFIG_ENV_PREFIX+"_CODE_SERVER_PORT")
		viper.SetDefault("agent.port.code_server", 0xc0de)

		viper.BindPFlag("agent.port.vnc_http", cmd.Flags().Lookup("vnc-http-port"))
		viper.BindEnv("agent.port.vnc_http", config.CONFIG_ENV_PREFIX+"_VNC_HTTP_PORT")
		viper.SetDefault("agent.port.vnc_http", "0")

		viper.BindPFlag("agent.port.ssh", cmd.Flags().Lookup("ssh-port"))
		viper.BindEnv("agent.port.ssh", config.CONFIG_ENV_PREFIX+"_SSH_PORT")
		viper.SetDefault("agent.port.ssh", "22")

		viper.BindPFlag("agent.port.tcp_port", cmd.Flags().Lookup("tcp-port"))
		viper.BindEnv("agent.port.tcp_port", config.CONFIG_ENV_PREFIX+"_TCP_PORT")

		viper.BindPFlag("agent.port.http_port", cmd.Flags().Lookup("http-port"))
		viper.BindEnv("agent.port.http_port", config.CONFIG_ENV_PREFIX+"_HTTP_PORT")

		viper.BindPFlag("agent.port.https_port", cmd.Flags().Lookup("https-port"))
		viper.BindEnv("agent.port.https_port", config.CONFIG_ENV_PREFIX+"_HTTPS_PORT")

		viper.BindPFlag("agent.update_authorized_keys", cmd.Flags().Lookup("update-authorized-keys"))
		viper.BindEnv("agent.update_authorized_keys", config.CONFIG_ENV_PREFIX+"_UPDATE_AUTHORIZED_KEYS")
		viper.SetDefault("agent.update_authorized_keys", true)

		viper.BindPFlag("agent.service_password", cmd.Flags().Lookup("service-password"))
		viper.BindEnv("agent.service_password", config.CONFIG_ENV_PREFIX+"_SERVICE_PASSWORD")
		viper.SetDefault("agent.service_password", "")

		viper.BindPFlag("agent.vscode_tunnel", cmd.Flags().Lookup("vscode-tunnel"))
		viper.BindEnv("agent.vscode_tunnel", config.CONFIG_ENV_PREFIX+"_VSCODE_TUNNEL")
		viper.SetDefault("agent.vscode_tunnel", "vscodetunnel")

		viper.BindPFlag("agent.advertise_addr", cmd.Flags().Lookup("advertise-addr"))
		viper.BindEnv("agent.advertise_addr", config.CONFIG_ENV_PREFIX+"_ADVERTISE_ADDR")
		viper.SetDefault("agent.advertise_addr", "")

		viper.BindPFlag("agent.syslog_port", cmd.Flags().Lookup("syslog-port"))
		viper.BindEnv("agent.syslog_port", config.CONFIG_ENV_PREFIX+"_SYSLOG_PORT")
		viper.SetDefault("agent.syslog_port", 514)

		viper.BindPFlag("agent.logs_port", cmd.Flags().Lookup("logs-port"))
		viper.BindEnv("agent.logs_port", config.CONFIG_ENV_PREFIX+"_LOGS_PORT")
		viper.SetDefault("agent.logs_port", 12201)

		viper.BindPFlag("agent.tls.cert_file", cmd.Flags().Lookup("cert-file"))
		viper.BindEnv("agent.tls.cert_file", config.CONFIG_ENV_PREFIX+"_CERT_FILE")
		viper.SetDefault("agent.tls.cert_file", "")

		viper.BindPFlag("agent.tls.key_file", cmd.Flags().Lookup("key-file"))
		viper.BindEnv("agent.tls.key_file", config.CONFIG_ENV_PREFIX+"_KEY_FILE")
		viper.SetDefault("agent.tls.key_file", "")

		viper.BindPFlag("agent.tls.use_tls", cmd.Flags().Lookup("use-tls"))
		viper.BindEnv("agent.tls.use_tls", config.CONFIG_ENV_PREFIX+"_USE_TLS")
		viper.SetDefault("agent.tls.use_tls", true)

		viper.BindPFlag("tls_skip_verify", cmd.Flags().Lookup("tls-skip-verify"))
		viper.BindEnv("tls_skip_verify", config.CONFIG_ENV_PREFIX+"_TLS_SKIP_VERIFY")
		viper.SetDefault("tls_skip_verify", true)

		viper.BindPFlag("resolver.nameservers", cmd.Flags().Lookup("nameserver"))
		viper.BindEnv("resolver.nameservers", config.CONFIG_ENV_PREFIX+"_NAMESERVERS")

		viper.BindPFlag("dns.listen", cmd.Flags().Lookup("dns-listen"))
		viper.BindEnv("dns.listen", config.CONFIG_ENV_PREFIX+"_DNS_LISTEN")

		viper.BindPFlag("dns.refresh_max_age", cmd.Flags().Lookup("dns-refresh-max-age"))
		viper.BindEnv("dns.refresh_max_age", config.CONFIG_ENV_PREFIX+"_MAX_AGE")
		viper.SetDefault("dns.refresh_max_age", 180)
	},
	Run: func(cmd *cobra.Command, args []string) {
		serverAddr := viper.GetString("agent.endpoint")
		spaceId := viper.GetString("agent.space_id")

		if serverAddr == "" {
			log.Fatal().Msg("server address is required")
		}

		if len(spaceId) != 36 {
			log.Fatal().Msg("space-id is required and must be a valid space ID")
		}

		if viper.GetString("dns.listen") != "" {
			dnsproxy := dnsproxy.NewDNSProxy()
			go dnsproxy.RunServer()
		}

		agent_client.ConnectAndServe(serverAddr, spaceId)
		go agent_client.ReportState(spaceId)

		if viper.GetInt("agent.syslog_port") > 0 {
			go syslogd.StartSyslogd()
		}

		if viper.GetInt("agent.logs_port") > 0 {
			go logsink.ListenAndServe()
		}

		c := make(chan os.Signal, 1)
		signal.Notify(c, os.Interrupt, syscall.SIGTERM)

		<-c

		agent_client.Shutdown()
		fmt.Println("\r")
		log.Info().Msg("agent: shutdown")
		os.Exit(0)
	},
}

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