diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2025-04-11 13:55:54 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2025-04-11 13:55:54 -0400 |
| commit | edfc79c2ab492d9e147da0c34bd1a2a68df43a72 (patch) | |
| tree | 229bfe2d52fb7f71688a88a94fb8904e54860431 | |
| parent | 84b7e4b1c020c493c605d116570d3d8e5c0b3256 (diff) | |
| download | hose-edfc79c2ab492d9e147da0c34bd1a2a68df43a72.zip | |
handshake sending side
| -rw-r--r-- | handshake.go | 49 | ||||
| -rw-r--r-- | main.go | 14 |
2 files changed, 58 insertions, 5 deletions
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 +} @@ -16,18 +16,22 @@ const ( ) var ( - r = flag.Bool("r", false, "receive") - rhost = flag.String("s", "", "send to remote host") + recvFlag = flag.Bool("r", false, "receive") + sendHost = flag.String("s", "", "send to remote host") ) func main() { flag.Parse() - if *r { + if *handshakeHost { + if err := handshake(*handshakeHost); err != nil { + eprintf("%v\n", err) + } + } else if *recvFlag { if err := recv(); err != nil { eprintf("%v\n", err) } - } else if *rhost != "" { - if err := send(*rhost); err != nil { + } else if *sendHost != "" { + if err := send(*sendHost); err != nil { eprintf("%v\n", err) } } else { |