2022-03-22 11:03:15 -04:00
|
|
|
package coordinator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2022-04-11 04:38:03 -04:00
|
|
|
"github.com/edgelesssys/constellation/cli/file"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/edgelesssys/constellation/coordinator/atls"
|
2022-04-22 05:06:55 -04:00
|
|
|
"github.com/edgelesssys/constellation/coordinator/attestation/simulator"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/edgelesssys/constellation/coordinator/core"
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/kms"
|
2022-03-25 11:05:17 -04:00
|
|
|
"github.com/edgelesssys/constellation/coordinator/peer"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/edgelesssys/constellation/coordinator/pubapi"
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/pubapi/pubproto"
|
2022-04-11 04:38:03 -04:00
|
|
|
"github.com/edgelesssys/constellation/coordinator/state"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/edgelesssys/constellation/coordinator/store"
|
2022-04-28 03:49:15 -04:00
|
|
|
"github.com/edgelesssys/constellation/coordinator/util/grpcutil"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/edgelesssys/constellation/coordinator/util/testdialer"
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/vpnapi"
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/vpnapi/vpnproto"
|
2022-04-11 04:38:03 -04:00
|
|
|
"github.com/spf13/afero"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/goleak"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
goleak.VerifyTestMain(m,
|
|
|
|
// https://github.com/census-instrumentation/opencensus-go/issues/1262
|
|
|
|
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCoordinator tests the integration of packages core, pubapi, and vpnapi. It activates
|
|
|
|
// a coordinator and some nodes and (virtually) sends a packet over the fake VPN.
|
|
|
|
func TestCoordinator(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2022-04-13 06:39:55 -04:00
|
|
|
nodeIPs := []string{"192.0.2.11", "192.0.2.12", "192.0.2.13"}
|
|
|
|
coordinatorIP := "192.0.2.1"
|
|
|
|
bindPort := "9000"
|
2022-03-22 11:03:15 -04:00
|
|
|
logger := zaptest.NewLogger(t)
|
|
|
|
dialer := testdialer.NewBufconnDialer()
|
|
|
|
netw := newNetwork()
|
|
|
|
|
|
|
|
// spawn 4 peers: 1 designated coordinator and 3 nodes
|
2022-04-13 06:39:55 -04:00
|
|
|
coordServer, coordPAPI, _ := spawnPeer(require, logger.Named("coord"), dialer, netw, net.JoinHostPort(coordinatorIP, bindPort))
|
2022-03-22 11:03:15 -04:00
|
|
|
defer coordPAPI.Close()
|
|
|
|
defer coordServer.GracefulStop()
|
2022-04-13 06:39:55 -04:00
|
|
|
nodeServer1, nodePAPI1, nodeVPN1 := spawnPeer(require, logger.Named("node1"), dialer, netw, net.JoinHostPort(nodeIPs[0], bindPort))
|
2022-03-22 11:03:15 -04:00
|
|
|
defer nodePAPI1.Close()
|
|
|
|
defer nodeServer1.GracefulStop()
|
2022-04-13 06:39:55 -04:00
|
|
|
nodeServer2, nodePAPI2, nodeVPN2 := spawnPeer(require, logger.Named("node2"), dialer, netw, net.JoinHostPort(nodeIPs[1], bindPort))
|
2022-03-22 11:03:15 -04:00
|
|
|
defer nodePAPI2.Close()
|
|
|
|
defer nodeServer2.GracefulStop()
|
2022-04-13 06:39:55 -04:00
|
|
|
nodeServer3, nodePAPI3, nodeVPN3 := spawnPeer(require, logger.Named("node3"), dialer, netw, net.JoinHostPort(nodeIPs[2], bindPort))
|
2022-03-22 11:03:15 -04:00
|
|
|
defer nodePAPI3.Close()
|
|
|
|
defer nodeServer3.GracefulStop()
|
|
|
|
|
2022-04-13 06:39:55 -04:00
|
|
|
require.NoError(activateCoordinator(require, dialer, coordinatorIP, bindPort, nodeIPs))
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
// send something from node 1 to node 2
|
|
|
|
|
|
|
|
nodeIP1, err := nodeVPN1.GetInterfaceIP()
|
|
|
|
require.NoError(err)
|
|
|
|
nodeIP2, err := nodeVPN2.GetInterfaceIP()
|
|
|
|
require.NoError(err)
|
|
|
|
assert.NotEqual(nodeIP1, nodeIP2)
|
|
|
|
|
|
|
|
nodeVPN1.send(nodeIP2, "foo")
|
|
|
|
assert.Nil(nodeVPN3.recv())
|
|
|
|
pa := nodeVPN2.recv()
|
|
|
|
require.NotNil(pa)
|
|
|
|
assert.Equal(nodeIP1, pa.src)
|
|
|
|
assert.Equal("foo", pa.data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestConcurrent is supposed to detect data races when run with -race.
|
|
|
|
func TestConcurrent(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2022-04-26 10:19:22 -04:00
|
|
|
nodeIPs := []string{"192.0.2.11", "192.0.2.12"}
|
2022-04-13 06:39:55 -04:00
|
|
|
coordinatorIP := "192.0.2.1"
|
|
|
|
bindPort := "9000"
|
2022-03-22 11:03:15 -04:00
|
|
|
logger := zaptest.NewLogger(t)
|
|
|
|
dialer := testdialer.NewBufconnDialer()
|
|
|
|
netw := newNetwork()
|
|
|
|
|
|
|
|
// spawn peers
|
2022-04-13 06:39:55 -04:00
|
|
|
coordServer, coordPAPI, _ := spawnPeer(require, logger.Named("coord"), dialer, netw, net.JoinHostPort(coordinatorIP, bindPort))
|
2022-03-22 11:03:15 -04:00
|
|
|
defer coordPAPI.Close()
|
|
|
|
defer coordServer.GracefulStop()
|
2022-04-13 06:39:55 -04:00
|
|
|
nodeServer1, nodePAPI1, _ := spawnPeer(require, logger.Named("node1"), dialer, netw, net.JoinHostPort(nodeIPs[0], bindPort))
|
2022-03-22 11:03:15 -04:00
|
|
|
defer nodePAPI1.Close()
|
|
|
|
defer nodeServer1.GracefulStop()
|
2022-04-13 06:39:55 -04:00
|
|
|
nodeServer2, nodePAPI2, _ := spawnPeer(require, logger.Named("node2"), dialer, netw, net.JoinHostPort(nodeIPs[1], bindPort))
|
2022-03-22 11:03:15 -04:00
|
|
|
defer nodePAPI2.Close()
|
|
|
|
defer nodeServer2.GracefulStop()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2022-04-26 10:19:22 -04:00
|
|
|
// This test is a rather rough check for concurrency errors in the pubapi. To this end, various funcs of the pubapi
|
|
|
|
// are called concurrently. As a minimal verification, returned errors are checked.
|
|
|
|
// The coverage of this test alone isn't sufficient. Not all funcs of the pubapi are tested, and arguments are constant.
|
|
|
|
// In the future, we should have something more sophisticated.
|
|
|
|
|
|
|
|
actCoord := func(retErr chan error) {
|
2022-03-22 11:03:15 -04:00
|
|
|
defer wg.Done()
|
2022-04-26 10:19:22 -04:00
|
|
|
retErr <- activateCoordinator(require, dialer, coordinatorIP, bindPort, nodeIPs)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-26 08:26:45 -04:00
|
|
|
actNode := func(papi *pubapi.API) {
|
2022-03-22 11:03:15 -04:00
|
|
|
defer wg.Done()
|
2022-04-26 10:19:22 -04:00
|
|
|
// actNode is called on already activated nodes, so this will fail due to wrong state.
|
2022-04-26 08:26:45 -04:00
|
|
|
assert.Error(papi.ActivateAsNode(nil))
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
updNode := func(papi *pubapi.API, noerr bool) {
|
|
|
|
defer wg.Done()
|
|
|
|
_, err := papi.TriggerNodeUpdate(context.Background(), &pubproto.TriggerNodeUpdateRequest{})
|
|
|
|
if noerr {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getState := func(papi *pubapi.API) {
|
|
|
|
defer wg.Done()
|
2022-04-26 10:19:22 -04:00
|
|
|
// GetState should always succeed, regardless of what happened to the peer before.
|
2022-03-22 11:03:15 -04:00
|
|
|
_, err := papi.GetState(context.Background(), &pubproto.GetStateRequest{})
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
join := func(papi *pubapi.API) {
|
|
|
|
defer wg.Done()
|
2022-04-26 10:19:22 -04:00
|
|
|
// For now, we always pass an empty JoinClusterRequest, so JoinCluster
|
|
|
|
// is expected to fail even if the peer is in the required state.
|
2022-03-22 11:03:15 -04:00
|
|
|
_, err := papi.JoinCluster(context.Background(), &pubproto.JoinClusterRequest{})
|
|
|
|
assert.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// activate coordinator and make some other calls concurrently
|
|
|
|
wg.Add(16)
|
2022-04-26 10:19:22 -04:00
|
|
|
actCoordErrs := make(chan error, 2)
|
|
|
|
go actCoord(actCoordErrs)
|
|
|
|
go actCoord(actCoordErrs)
|
|
|
|
// updNode on unactivated node should fail.
|
|
|
|
// updNode on Coordinator should fail.
|
|
|
|
// updNode on Node should succeed, but we don't know whether the node is already activated or not, so we can't expect no error.
|
2022-03-22 11:03:15 -04:00
|
|
|
go updNode(coordPAPI, false)
|
|
|
|
go updNode(coordPAPI, false)
|
|
|
|
go updNode(nodePAPI1, false)
|
|
|
|
go updNode(nodePAPI1, false)
|
|
|
|
go updNode(nodePAPI2, false)
|
|
|
|
go updNode(nodePAPI2, false)
|
|
|
|
go getState(coordPAPI)
|
|
|
|
go getState(coordPAPI)
|
|
|
|
go getState(nodePAPI1)
|
|
|
|
go getState(nodePAPI1)
|
|
|
|
go getState(nodePAPI2)
|
|
|
|
go getState(nodePAPI2)
|
|
|
|
go join(coordPAPI)
|
|
|
|
go join(coordPAPI)
|
|
|
|
wg.Wait()
|
2022-04-26 10:19:22 -04:00
|
|
|
actCoord1HasErr := <-actCoordErrs != nil
|
|
|
|
actCoord2HasErr := <-actCoordErrs != nil
|
|
|
|
require.NotEqual(actCoord1HasErr, actCoord2HasErr, "exactly one actCoord call should succeed")
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
// make some concurrent calls on the activated peers
|
|
|
|
wg.Add(26)
|
2022-04-26 10:19:22 -04:00
|
|
|
go actCoord(actCoordErrs)
|
|
|
|
go actCoord(actCoordErrs)
|
2022-04-26 08:26:45 -04:00
|
|
|
go actNode(coordPAPI)
|
|
|
|
go actNode(coordPAPI)
|
|
|
|
go actNode(nodePAPI1)
|
|
|
|
go actNode(nodePAPI1)
|
|
|
|
go actNode(nodePAPI2)
|
|
|
|
go actNode(nodePAPI2)
|
2022-03-22 11:03:15 -04:00
|
|
|
go updNode(coordPAPI, false)
|
|
|
|
go updNode(coordPAPI, false)
|
|
|
|
go updNode(nodePAPI1, true)
|
|
|
|
go updNode(nodePAPI1, true)
|
|
|
|
go updNode(nodePAPI2, true)
|
|
|
|
go updNode(nodePAPI2, true)
|
|
|
|
go getState(coordPAPI)
|
|
|
|
go getState(coordPAPI)
|
|
|
|
go getState(nodePAPI1)
|
|
|
|
go getState(nodePAPI1)
|
|
|
|
go getState(nodePAPI2)
|
|
|
|
go getState(nodePAPI2)
|
|
|
|
go join(coordPAPI)
|
|
|
|
go join(coordPAPI)
|
|
|
|
go join(nodePAPI1)
|
|
|
|
go join(nodePAPI1)
|
|
|
|
go join(nodePAPI2)
|
|
|
|
go join(nodePAPI2)
|
|
|
|
wg.Wait()
|
2022-04-26 10:19:22 -04:00
|
|
|
// One Coordinator is already activated, following both activation calls will fail now.
|
|
|
|
assert.Error(<-actCoordErrs)
|
|
|
|
assert.Error(<-actCoordErrs)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-28 03:49:15 -04:00
|
|
|
func spawnPeer(require *require.Assertions, logger *zap.Logger, netDialer *testdialer.BufconnDialer, netw *network, endpoint string) (*grpc.Server, *pubapi.API, *fakeVPN) {
|
2022-03-22 11:03:15 -04:00
|
|
|
vpn := newVPN(netw, endpoint)
|
2022-04-22 05:06:55 -04:00
|
|
|
cor, err := core.NewCore(vpn, &core.ClusterFake{}, &core.ProviderMetadataFake{}, &core.CloudControllerManagerFake{}, &core.CloudNodeManagerFake{}, &core.ClusterAutoscalerFake{}, &core.EncryptedDiskFake{}, logger, simulator.OpenSimulatedTPM, fakeStoreFactory{}, file.NewHandler(afero.NewMemMapFs()))
|
2022-03-22 11:03:15 -04:00
|
|
|
require.NoError(err)
|
2022-04-11 04:38:03 -04:00
|
|
|
require.NoError(cor.AdvanceState(state.AcceptingInit, nil, nil))
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
getPublicAddr := func() (string, error) {
|
|
|
|
return "192.0.2.1", nil
|
|
|
|
}
|
2022-04-28 03:49:15 -04:00
|
|
|
dialer := grpcutil.NewDialer(&core.MockValidator{}, netDialer)
|
|
|
|
vapiServer := &fakeVPNAPIServer{logger: logger.Named("vpnapi"), core: cor, dialer: netDialer}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-04-28 03:49:15 -04:00
|
|
|
papi := pubapi.New(logger, cor, dialer, vapiServer, getPublicAddr, nil)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
tlsConfig, err := atls.CreateAttestationServerTLSConfig(&core.MockIssuer{})
|
|
|
|
require.NoError(err)
|
|
|
|
server := grpc.NewServer(grpc.Creds(credentials.NewTLS(tlsConfig)))
|
|
|
|
pubproto.RegisterAPIServer(server, papi)
|
|
|
|
|
2022-04-28 03:49:15 -04:00
|
|
|
listener := netDialer.GetListener(endpoint)
|
2022-03-22 11:03:15 -04:00
|
|
|
go server.Serve(listener)
|
|
|
|
|
|
|
|
return server, papi, vpn
|
|
|
|
}
|
|
|
|
|
2022-04-28 03:49:15 -04:00
|
|
|
func activateCoordinator(require *require.Assertions, dialer netDialer, coordinatorIP, bindPort string, nodeIPs []string) error {
|
2022-03-22 11:03:15 -04:00
|
|
|
ctx := context.Background()
|
2022-04-13 06:39:55 -04:00
|
|
|
conn, err := dialGRPC(ctx, dialer, net.JoinHostPort(coordinatorIP, bindPort))
|
2022-03-22 11:03:15 -04:00
|
|
|
require.NoError(err)
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
client := pubproto.NewAPIClient(conn)
|
|
|
|
stream, err := client.ActivateAsCoordinator(ctx, &pubproto.ActivateAsCoordinatorRequest{
|
2022-04-13 06:39:55 -04:00
|
|
|
NodePublicIps: nodeIPs,
|
|
|
|
MasterSecret: []byte("Constellation"),
|
|
|
|
KmsUri: kms.ClusterKMSURI,
|
|
|
|
StorageUri: kms.NoStoreURI,
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
for {
|
|
|
|
_, err := stream.Recv()
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-28 03:49:15 -04:00
|
|
|
func dialGRPC(ctx context.Context, dialer netDialer, target string) (*grpc.ClientConn, error) {
|
2022-03-22 11:03:15 -04:00
|
|
|
tlsConfig, err := atls.CreateAttestationClientTLSConfig([]atls.Validator{&core.MockValidator{}})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return grpc.DialContext(ctx, target,
|
|
|
|
grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
|
|
|
|
return dialer.DialContext(ctx, "tcp", addr)
|
|
|
|
}),
|
|
|
|
grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeStoreFactory struct{}
|
|
|
|
|
|
|
|
func (fakeStoreFactory) New() (store.Store, error) {
|
|
|
|
return store.NewStdStore(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeVPNAPIServer struct {
|
|
|
|
logger *zap.Logger
|
|
|
|
core vpnapi.Core
|
|
|
|
dialer *testdialer.BufconnDialer
|
|
|
|
listener net.Listener
|
|
|
|
server *grpc.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *fakeVPNAPIServer) Listen(endpoint string) error {
|
|
|
|
api := vpnapi.New(v.logger, v.core)
|
|
|
|
v.server = grpc.NewServer()
|
|
|
|
vpnproto.RegisterAPIServer(v.server, api)
|
|
|
|
v.listener = v.dialer.GetListener(endpoint)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *fakeVPNAPIServer) Serve() error {
|
|
|
|
return v.server.Serve(v.listener)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *fakeVPNAPIServer) Close() {
|
|
|
|
if v.server != nil {
|
|
|
|
v.server.GracefulStop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type network struct {
|
|
|
|
packets map[string][]packet
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNetwork() *network {
|
|
|
|
return &network{packets: make(map[string][]packet)}
|
|
|
|
}
|
|
|
|
|
|
|
|
type packet struct {
|
|
|
|
src string
|
|
|
|
data string
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeVPN struct {
|
|
|
|
peers map[string]string // vpnIP -> publicIP
|
|
|
|
netw *network
|
|
|
|
publicIP string
|
|
|
|
interfaceIP string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newVPN(netw *network, publicEndpoint string) *fakeVPN {
|
|
|
|
publicIP, _, err := net.SplitHostPort(publicEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return &fakeVPN{
|
|
|
|
peers: make(map[string]string),
|
|
|
|
netw: netw,
|
|
|
|
publicIP: publicIP,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 11:26:17 -04:00
|
|
|
func (*fakeVPN) Setup(privKey []byte) error {
|
|
|
|
return nil
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-13 03:17:05 -04:00
|
|
|
func (*fakeVPN) GetPrivateKey() ([]byte, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 11:26:17 -04:00
|
|
|
func (*fakeVPN) GetPublicKey() ([]byte, error) {
|
2022-03-22 11:03:15 -04:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *fakeVPN) GetInterfaceIP() (string, error) {
|
|
|
|
return v.interfaceIP, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *fakeVPN) SetInterfaceIP(ip string) error {
|
|
|
|
v.interfaceIP = ip
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *fakeVPN) AddPeer(pubKey []byte, publicIP string, vpnIP string) error {
|
|
|
|
v.peers[vpnIP] = publicIP
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *fakeVPN) RemovePeer(pubKey []byte) error {
|
|
|
|
panic("dummy")
|
|
|
|
}
|
|
|
|
|
2022-03-25 11:05:17 -04:00
|
|
|
func (v *fakeVPN) UpdatePeers(peers []peer.Peer) error {
|
|
|
|
for _, peer := range peers {
|
2022-04-13 06:39:55 -04:00
|
|
|
if err := v.AddPeer(peer.VPNPubKey, peer.PublicIP, peer.VPNIP); err != nil {
|
2022-03-25 11:05:17 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
func (v *fakeVPN) send(dst string, data string) {
|
|
|
|
pubdst := v.peers[dst]
|
|
|
|
packets := v.netw.packets
|
|
|
|
packets[pubdst] = append(packets[pubdst], packet{src: v.publicIP, data: data})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *fakeVPN) recv() *packet {
|
|
|
|
packets := v.netw.packets
|
|
|
|
queue := packets[v.publicIP]
|
|
|
|
if len(queue) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
packet := queue[0]
|
|
|
|
packets[v.publicIP] = queue[1:]
|
|
|
|
for vpnIP, pubIP := range v.peers {
|
|
|
|
if pubIP == packet.src {
|
|
|
|
packet.src = vpnIP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &packet
|
|
|
|
}
|
2022-04-28 03:49:15 -04:00
|
|
|
|
|
|
|
type netDialer interface {
|
|
|
|
DialContext(ctx context.Context, network, address string) (net.Conn, error)
|
|
|
|
}
|