aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2025-04-14 19:44:25 -0400
committerSam Anthony <sam@samanthony.xyz>2025-04-14 19:44:25 -0400
commited00b0e3d9b8a3304ceb4f1d847e929c45b6332f (patch)
tree152846b8d8f4c47227075d6a245f6a1022207a07
parent0ec4fba0bded082b243a3285abc878d24855e1f2 (diff)
downloadhose-ed00b0e3d9b8a3304ceb4f1d847e929c45b6332f.zip
create handshake package
-rw-r--r--handshake/handshake.go (renamed from handshake.go)26
-rw-r--r--main.go3
2 files changed, 16 insertions, 13 deletions
diff --git a/handshake.go b/handshake/handshake.go
index c12d8e7..07b3ef4 100644
--- a/handshake.go
+++ b/handshake/handshake.go
@@ -1,4 +1,4 @@
-package main
+package handshake
import (
"bufio"
@@ -20,6 +20,9 @@ import (
)
const (
+ port = "60322"
+ network = "tcp"
+
timeout = 1 * time.Minute
retryInterval = 500 * time.Millisecond
)
@@ -33,10 +36,9 @@ const (
sigPublicKey = "Public signature verification key"
)
-// handshake exchanges public keys with a remote host.
-// The user is asked to verify the received key
-// before it is saved in the known hosts file.
-func handshake(rhost string) error {
+// Handshake exchanges public keys with a remote host.
+// The user is asked to verify the received keys before they are saved in the known hosts file.
+func Handshake(rhost string) error {
util.Logf("initiating handshake with %s...", rhost)
errs := make(chan error, 2)
@@ -44,13 +46,13 @@ func handshake(rhost string) error {
group, ctx := errgroup.WithContext(context.Background())
group.Go(func() error {
- if err := handshakeSend(rhost); err != nil {
+ if err := send(rhost); err != nil {
errs <- err
}
return nil
})
group.Go(func() error {
- if err := handshakeRecv(rhost); err != nil {
+ if err := receive(rhost); err != nil {
errs <- err
}
return nil
@@ -65,8 +67,8 @@ func handshake(rhost string) error {
}
}
-// handshakeSend sends the local public box (encryption) key to a remote host.
-func handshakeSend(rhost string) error {
+// send sends the local public box (encryption) key to a remote host.
+func send(rhost string) error {
util.Logf("loading public encryption key...")
pubBoxkey, err := key.LoadBoxPublicKey()
if err != nil {
@@ -107,9 +109,9 @@ func dialWithTimeout(network, address string, timeout time.Duration) (net.Conn,
}
}
-// handshakeRecv receives the public key of a remote host.
-// The user is asked to verify the key before it is saved to the known hosts file.
-func handshakeRecv(rhost string) error {
+// receive receives the public keys of a remote host.
+// The user is asked to verify the keys before they are saved to the known hosts file.
+func receive(rhost string) error {
// Listen for connection.
laddr := net.JoinHostPort("", port)
ln, err := net.Listen(network, laddr)
diff --git a/main.go b/main.go
index 161a21b..b27d1e8 100644
--- a/main.go
+++ b/main.go
@@ -7,6 +7,7 @@ import (
"net"
"os"
+ "git.samanthony.xyz/hose/handshake"
"git.samanthony.xyz/hose/util"
)
@@ -25,7 +26,7 @@ var (
func main() {
flag.Parse()
if *handshakeHost != "" {
- if err := handshake(*handshakeHost); err != nil {
+ if err := handshake.Handshake(*handshakeHost); err != nil {
util.Eprintf("%v\n", err)
}
} else if *recvFlag {