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

commands

package
v1.3.101 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package commands cmd/dmsg/self-ping/commands/self-ping.go c1-net-dmsg

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:   dmsgclient.ExecName(),
	Short: "DMSG self-ping: dial own PK through a specific server",
	Long: `Creates a temporary dmsg client, connects to the specified dmsg server,
then dials its own public key through that server.

If the noise handshake completes successfully the round-trip latency is printed
and the command exits 0. If anything fails, the error is printed and the
command exits 1.

The --server flag is required and must be in the format pk@ip:port, e.g.:

  skywire dmsg self-ping --server 02a2d4c3...@45.79.124.73:30082`,
	SilenceErrors:         true,
	SilenceUsage:          true,
	DisableSuggestions:    true,
	DisableFlagsInUseLine: true,
	RunE: func(_ *cobra.Command, _ []string) error {
		log := logging.MustGetLogger("dmsg-self-ping")

		srvEntry, err := parseServerEntry(serverAddr)
		if err != nil {
			return fmt.Errorf("invalid --server value: %w", err)
		}

		pk, sk := cipher.GenerateKeyPair()
		log.WithField("tmp_pk", pk.String()).Debug("Generated temporary keypair")

		entries := direct.GetAllEntries(cipher.PubKeys{pk}, []*disc.Entry{srvEntry})
		dClient := direct.NewClient(entries, log)

		ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
		defer cancel()

		cfg := &dmsg.Config{MinSessions: 1}
		dmsgC, stop, err := direct.StartDmsg(ctx, log, pk, sk, dClient, cfg)
		if err != nil {
			return fmt.Errorf("failed to connect to dmsg server %s: %w", srvEntry.Server.Address, err)
		}
		defer stop()

		lis, err := dmsgC.Listen(selfPingPort)
		if err != nil {
			return fmt.Errorf("failed to listen on dmsg port %d: %w", selfPingPort, err)
		}
		defer lis.Close()

		acceptErrCh := make(chan error, 1)
		go func() {
			stream, aerr := lis.AcceptStream()
			if aerr != nil {
				acceptErrCh <- aerr
				return
			}
			stream.Close()
			acceptErrCh <- nil
		}()

		start := time.Now()
		dialStream, err := dmsgC.DialStream(ctx, dmsg.Addr{PK: pk, Port: selfPingPort})
		if err != nil {
			return fmt.Errorf("self-dial failed via server %s: %w", srvEntry.Server.Address, err)
		}
		latency := time.Since(start)
		dialStream.Close()

		if aerr := <-acceptErrCh; aerr != nil {
			return fmt.Errorf("accept side of self-dial failed: %w", aerr)
		}

		cmdutil.Printf("DMSG self-ping OK\n")
		cmdutil.Printf("  server:  %s @ %s\n", srvEntry.Static.String(), srvEntry.Server.Address)
		cmdutil.Printf("  own pk:  %s\n", pk.String())
		cmdutil.Printf("  latency: %v\n", latency.Round(time.Millisecond))
		return nil
	},
}

RootCmd is the self-ping command.

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