From edfc79c2ab492d9e147da0c34bd1a2a68df43a72 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Fri, 11 Apr 2025 13:55:54 -0400 Subject: handshake sending side --- handshake.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 handshake.go (limited to 'handshake.go') diff --git a/handshake.go b/handshake.go new file mode 100644 index 0000000..bbd100c --- /dev/null +++ b/handshake.go @@ -0,0 +1,49 @@ +package main + +import ( + "golang/org/x/sync/errgroup" + + "key" +) + +// handshake exchanges public keys with a remote host. +// The user is asked to verify the fingerprint of the received key +// before it is saved in the known hosts file. +func handshake(rhost string) error { + logf("initiating handshake with %s...", rhost) + var group errgroup.Group + group.Go(handshakeSend(rhost)) + group.Go(handshakeRecv(rhost)) + return group.Wait() +} + +// handshakeSend sends the local public key to a remote host. +func handshakeSend(rhost string) error { + pubkey, err := key.LoadPublicKey() + if err != nil { + return err + } + + raddr := net.JoinHostPort(rhost, port) + logf("connecting to %s...", raddr) + conn, err := net.Dial(network, raddr) + if err != nil { + return err + } + defer conn.Close() + logf("connected to %s", raddr) + + if _, err := conn.Write(pubkey[:]); err != nil { + return err + } + + logf("sent public key to %s", rhost) + return nil +} + +// handshakeRecv receives the public key of a remote host. +// The user is asked to verify the fingerprint of the key before +// it is saved to the known hosts file. +func handshakeRecv(rhost string) error { + // TODO +} -- cgit v1.2.3