aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--handshake/handshake.go2
-rw-r--r--handshake/receive.go14
-rw-r--r--handshake/send.go2
-rw-r--r--main.go14
-rw-r--r--net/net.go19
5 files changed, 25 insertions, 26 deletions
diff --git a/handshake/handshake.go b/handshake/handshake.go
index e5e3c37..d4d1ab4 100644
--- a/handshake/handshake.go
+++ b/handshake/handshake.go
@@ -9,7 +9,7 @@ import (
)
const (
- port = "60322"
+ port = 60322
network = "tcp"
timeout = 1 * time.Minute
diff --git a/handshake/receive.go b/handshake/receive.go
index 986b967..b2a0aa7 100644
--- a/handshake/receive.go
+++ b/handshake/receive.go
@@ -12,6 +12,7 @@ import (
"git.samanthony.xyz/hose/hosts"
"git.samanthony.xyz/hose/key"
+ hose_net "git.samanthony.xyz/hose/net"
"git.samanthony.xyz/hose/util"
)
@@ -27,7 +28,7 @@ var errVerifyKey = errors.New("host key verification failed")
// 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 {
- conn, err := acceptConnection()
+ conn, err := hose_net.AcceptConnection(network, port)
if err != nil {
return err
}
@@ -56,17 +57,6 @@ func receive(rhost string) error {
return hosts.Add(hosts.Host{raddr, rBoxPubKey, rSigPubKey})
}
-func acceptConnection() (net.Conn, error) {
- laddr := net.JoinHostPort("", port)
- ln, err := net.Listen(network, laddr)
- if err != nil {
- return nil, err
- }
- defer ln.Close()
- util.Logf("listening on %s", laddr)
- return ln.Accept()
-}
-
func receiveKeys(conn net.Conn) (key.BoxPublicKey, key.SigPublicKey, error) {
// Receive public box (encryption) key from remote host.
var rBoxPubKey key.BoxPublicKey
diff --git a/handshake/send.go b/handshake/send.go
index 444db70..95c8f48 100644
--- a/handshake/send.go
+++ b/handshake/send.go
@@ -31,7 +31,7 @@ func loadKeys() (key.BoxPublicKey, key.SigPublicKey, error) {
}
func sendKeys(rhost string, boxPubKey key.BoxPublicKey, sigPubKey key.SigPublicKey) error {
- raddr := net.JoinHostPort(rhost, port)
+ raddr := net.JoinHostPort(rhost, fmt.Sprintf("%d", port))
util.Logf("connecting to %s...", raddr)
conn, err := dialWithTimeout(network, raddr, timeout)
if err != nil {
diff --git a/main.go b/main.go
index 148f672..5f33e48 100644
--- a/main.go
+++ b/main.go
@@ -2,7 +2,6 @@ package main
import (
"flag"
- "fmt"
"github.com/keybase/saltpack"
"github.com/keybase/saltpack/basic"
"github.com/tonistiigi/units"
@@ -14,6 +13,7 @@ import (
"git.samanthony.xyz/hose/handshake"
"git.samanthony.xyz/hose/hosts"
"git.samanthony.xyz/hose/key"
+ hose_net "git.samanthony.xyz/hose/net"
"git.samanthony.xyz/hose/util"
)
@@ -61,17 +61,7 @@ func recv() error {
keyring.ImportBoxKeypair(boxKeypair)
// Accept connection from remote host.
- laddr := net.JoinHostPort("", fmt.Sprintf("%d", port))
- ln, err := net.Listen(network, laddr)
- if err != nil {
- return err
- }
- defer ln.Close()
- util.Logf("listening on %s", laddr)
- conn, err := ln.Accept()
- if err != nil {
- return err
- }
+ conn, err := hose_net.AcceptConnection(network, port)
defer conn.Close()
util.Logf("accepted connection from %s", conn.RemoteAddr())
diff --git a/net/net.go b/net/net.go
new file mode 100644
index 0000000..13a1891
--- /dev/null
+++ b/net/net.go
@@ -0,0 +1,19 @@
+package net
+
+import (
+ "fmt"
+ std_net "net"
+
+ "git.samanthony.xyz/hose/util"
+)
+
+func AcceptConnection(network string, port uint16) (std_net.Conn, error) {
+ laddr := std_net.JoinHostPort("", fmt.Sprintf("%d", port))
+ ln, err := std_net.Listen(network, laddr)
+ if err != nil {
+ return nil, err
+ }
+ defer ln.Close()
+ util.Logf("listening on %s", laddr)
+ return ln.Accept()
+}