derp: move away from [32]byte key types
And some minor cleanup in the process. Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
committed by
Brad Fitzpatrick
parent
4d09316f9a
commit
259406e797
@@ -23,6 +23,7 @@ import (
|
||||
"sync"
|
||||
|
||||
"tailscale.com/derp"
|
||||
"tailscale.com/types/key"
|
||||
"tailscale.com/types/logger"
|
||||
)
|
||||
|
||||
@@ -32,7 +33,7 @@ import (
|
||||
// Recv will report the error and not retry, but subsequent calls to
|
||||
// Send/Recv will completely re-establish the connection.
|
||||
type Client struct {
|
||||
privateKey [32]byte
|
||||
privateKey key.Private
|
||||
logf logger.Logf
|
||||
closed chan struct{}
|
||||
url *url.URL
|
||||
@@ -45,7 +46,7 @@ type Client struct {
|
||||
client *derp.Client
|
||||
}
|
||||
|
||||
func NewClient(privateKey [32]byte, serverURL string, logf logger.Logf) (*Client, error) {
|
||||
func NewClient(privateKey key.Private, serverURL string, logf logger.Logf) (*Client, error) {
|
||||
u, err := url.Parse(serverURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("derphttp.NewClient: %v", err)
|
||||
@@ -146,7 +147,7 @@ func (c *Client) connect(caller string) (client *derp.Client, err error) {
|
||||
return c.client, nil
|
||||
}
|
||||
|
||||
func (c *Client) Send(dstKey [32]byte, b []byte) error {
|
||||
func (c *Client) Send(dstKey key.Public, b []byte) error {
|
||||
client, err := c.connect("derphttp.Client.Send")
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
package derphttp
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
crand "crypto/rand"
|
||||
"crypto/tls"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -13,29 +13,27 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/curve25519"
|
||||
"tailscale.com/derp"
|
||||
"tailscale.com/types/key"
|
||||
)
|
||||
|
||||
func TestSendRecv(t *testing.T) {
|
||||
const numClients = 3
|
||||
var serverPrivateKey [32]byte
|
||||
if _, err := rand.Read(serverPrivateKey[:]); err != nil {
|
||||
var serverPrivateKey key.Private
|
||||
if _, err := crand.Read(serverPrivateKey[:]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var clientPrivateKeys [][32]byte
|
||||
var clientPrivateKeys []key.Private
|
||||
for i := 0; i < numClients; i++ {
|
||||
var key [32]byte
|
||||
if _, err := rand.Read(key[:]); err != nil {
|
||||
var key key.Private
|
||||
if _, err := crand.Read(key[:]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
clientPrivateKeys = append(clientPrivateKeys, key)
|
||||
}
|
||||
var clientKeys [][32]byte
|
||||
var clientKeys []key.Public
|
||||
for _, privKey := range clientPrivateKeys {
|
||||
var key [32]byte
|
||||
curve25519.ScalarBaseMult(&key, &privKey)
|
||||
clientKeys = append(clientKeys, key)
|
||||
clientKeys = append(clientKeys, privKey.Public())
|
||||
}
|
||||
|
||||
s := derp.NewServer(serverPrivateKey, t.Logf)
|
||||
|
||||
Reference in New Issue
Block a user