aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--main.go37
1 files changed, 25 insertions, 12 deletions
diff --git a/main.go b/main.go
index 5f33e48..0ff7430 100644
--- a/main.go
+++ b/main.go
@@ -54,11 +54,9 @@ func main() {
func recv() error {
// Load private decryption key.
keyring := key.NewKeyring()
- boxKeypair, err := key.LoadBoxKeypair()
- if err != nil {
+ if err := loadBoxKeypair(keyring); err != nil {
return err
}
- keyring.ImportBoxKeypair(boxKeypair)
// Accept connection from remote host.
conn, err := hose_net.AcceptConnection(network, port)
@@ -66,15 +64,7 @@ func recv() error {
util.Logf("accepted connection from %s", conn.RemoteAddr())
// Load remote host's signature verification key.
- rhost, _, err := net.SplitHostPort(conn.RemoteAddr().String())
- if err != nil {
- return err
- }
- raddr, err := netip.ParseAddr(rhost)
- if err != nil {
- return err
- }
- host, err := hosts.Lookup(raddr)
+ host, err := lookupHost(conn)
if err != nil {
return err
}
@@ -92,6 +82,29 @@ func recv() error {
return err
}
+// loadBoxKeypair reads the local encryption/decryption keypair from disc and imports it into the keyring.
+func loadBoxKeypair(keyring *key.Keyring) error {
+ boxKeypair, err := key.LoadBoxKeypair()
+ if err != nil {
+ return err
+ }
+ keyring.ImportBoxKeypair(boxKeypair)
+ return nil
+}
+
+// lookupHost searches for a remote host in the known hosts file.
+func lookupHost(conn net.Conn) (hosts.Host, error) {
+ rhost, _, err := net.SplitHostPort(conn.RemoteAddr().String())
+ if err != nil {
+ return hosts.Host{}, err
+ }
+ raddr, err := netip.ParseAddr(rhost)
+ if err != nil {
+ return hosts.Host{}, err
+ }
+ return hosts.Lookup(raddr)
+}
+
// send pipes data from stdin to the remote host.
func send(rHostName string) error {
var keyCreator basic.EphemeralKeyCreator