Help us shape the future of Go! Take the 2026 Go Developer Survey

commands

package
v1.3.98 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: AGPL-3.0 Imports: 22 Imported by: 0

Documentation

Overview

Package commands cmd/dmsg/pty-host/commands/confgen.go c1-net-dmsg

Package commands cmd/dmsg/pty-host/commands/root.go c1-net-dmsg

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:   dmsgclient.ExecName(),
	Short: "DMSG host for pseudoterminal command line interface",
	Long: calvin.AsciiFont("dmsgpty-host") + `
	DMSG host for pseudoterminal (pty) command line interface`,
	SilenceErrors:         true,
	SilenceUsage:          true,
	DisableSuggestions:    true,
	DisableFlagsInUseLine: true,
	RunE: func(cmd *cobra.Command, _ []string) error {

		if skFromVisor != "" {
			if !noDmsg {
				return fmt.Errorf("--sk-from-visor requires --no-dmsg (sharing PK with a running visor's dmsg client breaks the visor's dmsg-disc entry)")
			}
			if err := loadSKFromVisorConfig(skFromVisor); err != nil {
				return err
			}
		}

		conf, err := getConfig(cmd, false)
		if err != nil {
			return fmt.Errorf("failed to get config: %w", err)
		}

		if _, err := buildinfo.Get().WriteTo(stdlog.Writer()); err != nil {
			log.Printf("Failed to output build info: %v", err)
		}
		log := logging.MustGetLogger("dmsgpty-host")
		stopPProf := dmsgcmdutil.InitPProf(log, pprofMode, pprofAddr)
		defer stopPProf()
		ctx, cancel := cmdutil.SignalContext(context.Background(), log)
		defer cancel()

		pk, err := sk.PubKey()
		if err != nil {
			return fmt.Errorf("failed to derive public key from secret key: %w", err)
		}

		wl, err := pty.NewConfigWhitelist(confPath)
		if err != nil {
			return fmt.Errorf("failed to init whitelist: %w", err)
		}

		// dmsg.Client is optional. --no-dmsg skips the client entirely,
		// for the sshd-style standalone-TCP daemon shape; without it
		// we'd be racing the visor (if any) for the same dmsg-disc
		// entry under the shared PK that --sk-from-visor pulls in.
		var dmsgC *dmsg.Client
		if !noDmsg {
			dmsgC = dmsg.NewClient(pk, sk, disc.NewHTTP(conf.DmsgDisc, &http.Client{}, log), &dmsg.Config{
				MinSessions: conf.DmsgSessions,
			})
			defer func() {
				if err := dmsgC.Close(); err != nil {
					log.WithError(err).Warn("Failed to close dmsg client")
				}
			}()

			go dmsgC.Serve(ctx)
			select {
			case <-ctx.Done():
				return fmt.Errorf("failed to wait dmsg client to be ready: %w", ctx.Err())
			case <-dmsgC.Ready():
			}
		}

		host := pty.NewHost(dmsgC, wl)

		if listenFD >= 0 {
			f := os.NewFile(uintptr(listenFD), fmt.Sprintf("listen-fd-%d", listenFD))
			if f == nil {
				return fmt.Errorf("listen-fd %d is not a valid file descriptor", listenFD)
			}
			lc, err := net.FileConn(f)
			if err != nil {
				_ = f.Close()
				return fmt.Errorf("listen-fd %d net.FileConn: %w", listenFD, err)
			}

			_ = f.Close()
			log.WithField("listen_fd", listenFD).Info("dmsgpty-host: socket-activation mode, serving one session.")
			host.ServeAcceptedTCP(ctx, lc, pk, sk)
			return nil
		}

		wg := new(sync.WaitGroup)

		if conf.CLINet == "unix" {
			_ = os.Remove(conf.CLIAddr)
		}
		cliL, err := net.Listen(conf.CLINet, conf.CLIAddr)
		if err != nil {
			return fmt.Errorf("failed to serve CLI: %w", err)
		}
		log.WithField("addr", cliL.Addr()).Info("Listening for CLI connections.")
		wg.Add(1)
		go func() {
			log.WithError(host.ServeCLI(ctx, cliL)).
				Info("Stopped serving CLI.")
			wg.Done()
		}()

		if dmsgC != nil {
			log.WithField("port", conf.DmsgPort).
				Info("Listening for dmsg streams.")
			wg.Add(1)
			go func() {
				log.WithError(host.ListenAndServe(ctx, conf.DmsgPort)).
					Info("Stopped serving dmsgpty-host.")
				wg.Done()
			}()
		}

		tcpAddr := tcpListen
		if tcpAddr == "" {
			tcpAddr = conf.SshListen
		}
		if tcpAddr != "" {
			wg.Add(1)
			log.WithField("addr", tcpAddr).
				Info("Listening for direct-TCP streams (XK-noise gated).")
			go func() {
				log.WithError(host.ListenAndServeTCP(ctx, tcpAddr, pk, sk)).
					Info("Stopped serving dmsgpty-host TCP entry point.")
				wg.Done()
			}()
		}

		wg.Wait()
		return nil
	},
}

RootCmd contains commands for dmsgpty-host

Functions

func Execute

func Execute()

Execute executes the root command.

Types

This section is empty.

Jump to

Keyboard shortcuts

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