@ -34,16 +34,38 @@ type Client struct {
readErr error // sticky read error
readErr error // sticky read error
}
}
func NewClient ( privateKey key . Private , nc Conn , brw * bufio . ReadWriter , logf logger . Logf ) ( * Client , error ) {
// ClientOpt is an option passed to NewClient.
noMeshKey := ""
type ClientOpt interface {
return NewMeshClient ( privateKey , nc , brw , logf , noMeshKey )
update ( * clientOpt )
}
}
// NewMeshClient is the Client constructor for trusted clients that
type clientOptFunc func ( * clientOpt )
// are a peer in a cluster mesh.
func ( f clientOptFunc ) update ( o * clientOpt ) { f ( o ) }
// clientOpt are the options passed to newClient.
type clientOpt struct {
MeshKey string
}
// MeshKey returns a ClientOpt to pass to the DERP server during connect to get
// access to join the mesh.
//
//
// An empty meshKey is equivalent to just using NewClient.
// An empty key means to not use a mesh key.
func NewMeshClient ( privateKey key . Private , nc Conn , brw * bufio . ReadWriter , logf logger . Logf , meshKey string ) ( * Client , error ) {
func MeshKey ( key string ) ClientOpt { return clientOptFunc ( func ( o * clientOpt ) { o . MeshKey = key } ) }
func NewClient ( privateKey key . Private , nc Conn , brw * bufio . ReadWriter , logf logger . Logf , opts ... ClientOpt ) ( * Client , error ) {
var opt clientOpt
for _ , o := range opts {
if o == nil {
return nil , errors . New ( "nil ClientOpt" )
}
o . update ( & opt )
}
return newClient ( privateKey , nc , brw , logf , opt )
}
func newClient ( privateKey key . Private , nc Conn , brw * bufio . ReadWriter , logf logger . Logf , opt clientOpt ) ( * Client , error ) {
c := & Client {
c := & Client {
privateKey : privateKey ,
privateKey : privateKey ,
publicKey : privateKey . Public ( ) ,
publicKey : privateKey . Public ( ) ,
@ -51,7 +73,7 @@ func NewMeshClient(privateKey key.Private, nc Conn, brw *bufio.ReadWriter, logf
nc : nc ,
nc : nc ,
br : brw . Reader ,
br : brw . Reader ,
bw : brw . Writer ,
bw : brw . Writer ,
meshKey : m eshKey,
meshKey : opt. M eshKey,
}
}
if err := c . recvServerKey ( ) ; err != nil {
if err := c . recvServerKey ( ) ; err != nil {
return nil , fmt . Errorf ( "derp.Client: failed to receive server key: %v" , err )
return nil , fmt . Errorf ( "derp.Client: failed to receive server key: %v" , err )