From 8b65f91699cd474563c0abacc726a3d47961a78f Mon Sep 17 00:00:00 2001 From: maride Date: Thu, 23 Aug 2018 11:46:23 +0200 Subject: Add VPN container and access --- src/container.go | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) (limited to 'src/container.go') diff --git a/src/container.go b/src/container.go index 73912bf..5b2075b 100644 --- a/src/container.go +++ b/src/container.go @@ -1,12 +1,15 @@ package main import ( - "context" - "github.com/docker/docker/client" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types" "fmt" "time" + "github.com/docker/docker/api/types/network" +) + +const ( + VPNNetworkName = "vpn-network" ) type ChallengeContainer struct { @@ -15,33 +18,26 @@ type ChallengeContainer struct { IP string } -var ( - dockerCtx context.Context - dockerCli *client.Client -) - // Starts the container and returns its address and containerID if successful func (cc ChallengeContainer) startContainer() (address string, containerID string, err error) { - // Set up our context if there is none already set up - if dockerCtx == nil { - dockerCtx = context.Background() - } - - // Set up our Docker CLI connection if there is not already one - if dockerCli == nil { - dockerCli, err = client.NewEnvClient() - - if err != nil { - return "", "", err - } - } + // Set up our context and Docker CLI connection + setupContext() + setupDockerCLI() + // Set up network + setupNetwork() // Create container resp, err := dockerCli.ContainerCreate(dockerCtx, &container.Config{ Image: cc.Challenge.Container, Env: []string{fmt.Sprintf("FLAG=%s", cc.Challenge.Flag)}, Tty: false, - }, nil, nil, "") + }, nil, &network.NetworkingConfig{ + EndpointsConfig: map[string]*network.EndpointSettings{ + VPNNetworkName: { + NetworkID: vpnNetworkID, + }, + }, + }, "") if err != nil { return "", "", err @@ -60,7 +56,7 @@ func (cc ChallengeContainer) startContainer() (address string, containerID strin } // Return IP, Container ID and error - return inspectJSON.NetworkSettings.IPAddress, resp.ID,nil + return inspectJSON.NetworkSettings.Networks[VPNNetworkName].IPAddress, resp.ID,nil } // Stops the container with a timeout of one second -- cgit 1.4.1 From cbb1ab24a40b5e84adcd44a4d0d4d7fdd9f49f5f Mon Sep 17 00:00:00 2001 From: maride Date: Sun, 2 Sep 2018 01:49:26 +0200 Subject: Abort on errors --- src/container.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/container.go') diff --git a/src/container.go b/src/container.go index 5b2075b..c9a918f 100644 --- a/src/container.go +++ b/src/container.go @@ -1,15 +1,15 @@ package main import ( - "github.com/docker/docker/api/types/container" - "github.com/docker/docker/api/types" "fmt" - "time" + "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/network" + "time" ) const ( - VPNNetworkName = "vpn-network" + VPNNetworkName = "circus-vpnnet" ) type ChallengeContainer struct { @@ -24,7 +24,11 @@ func (cc ChallengeContainer) startContainer() (address string, containerID strin setupContext() setupDockerCLI() // Set up network - setupNetwork() + err = setupNetwork() + + if err != nil { + return "", "", err + } // Create container resp, err := dockerCli.ContainerCreate(dockerCtx, &container.Config{ -- cgit 1.4.1