about summary refs log tree commit diff
path: root/src/container.go
diff options
context:
space:
mode:
authormaride <maride@darknebu.la>2018-08-23 11:46:23 +0200
committermaride <maride@darknebu.la>2018-08-23 11:46:23 +0200
commit8b65f91699cd474563c0abacc726a3d47961a78f (patch)
tree5d811fe2e5b0ddc7ecafdb9c1ee0343d91fa768e /src/container.go
parent82c922d557f6628043ab771cdf10e4da9546347d (diff)
Add VPN container and access
Diffstat (limited to 'src/container.go')
-rw-r--r--src/container.go40
1 files changed, 18 insertions, 22 deletions
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