From 8d417b84f53e65a8c77d97e1a1001dd70468106d Mon Sep 17 00:00:00 2001 From: Akilan Date: Wed, 7 Apr 2021 20:55:37 +0400 Subject: [PATCH] added possibility to create multiple ports --- server/docker/docker.go | 60 ++++++++++++++++++++++++++++++----------- server/ipfs-ssh.sh | 31 --------------------- server/rpc.go | 2 +- server/server.go | 10 ++++++- 4 files changed, 54 insertions(+), 49 deletions(-) delete mode 100644 server/ipfs-ssh.sh diff --git a/server/docker/docker.go b/server/docker/docker.go index 59c579c..2c4d9c3 100644 --- a/server/docker/docker.go +++ b/server/docker/docker.go @@ -25,6 +25,7 @@ type DockerVM struct { ID string `json:"ID"` TagName string `json:"TagName"` ImagePath string `json:"ImagePath"` + Ports []int `json:"OpenPorts"` } type ErrorLine struct { @@ -38,16 +39,23 @@ type ErrorDetail struct { var dockerRegistryUserID = "" -func BuildRunContainer() (*DockerVM,error) { +func BuildRunContainer(NumPorts int) (*DockerVM,error) { //Docker Struct Variable var RespDocker *DockerVM = new(DockerVM) + // Count: 2 allocated ports for VNC and SSH + other ports + // to open + count := NumPorts + 2 + // Gets 2 TCP ports empty - Ports, err := freeport.GetFreePorts(2) + Ports, err := freeport.GetFreePorts(count) if err != nil { return nil,err } + for i := 2; i < count; i++ { + RespDocker.Ports = append(RespDocker.Ports, Ports[i]) + } //fmt.Println(Ports[0]) // Sets Free port to Struct @@ -58,6 +66,7 @@ func BuildRunContainer() (*DockerVM,error) { RespDocker.SSHUsername = "master" RespDocker.SSHPassword = "password" RespDocker.VNCPassword = "vncpassword" + RespDocker.Ports = Ports //Default parameters RespDocker.TagName = "p2p-ubuntu" @@ -117,7 +126,7 @@ func (d *DockerVM)imageBuild(dockerClient *client.Client) error { return nil } -// Starts container and assigns IP addresses +// Starts container and assigns port numbers func (d *DockerVM)runContainer(dockerClient *client.Client) error{ ctx, _ := context.WithTimeout(context.Background(), time.Second*2000) @@ -128,21 +137,40 @@ func (d *DockerVM)runContainer(dockerClient *client.Client) error{ Volumes: map[string]struct{}{"/opt/data:/data":{}}, } - hostConfig := &container.HostConfig{ - PortBindings: nat.PortMap{ - "22/tcp": []nat.PortBinding{ - { - HostIP: "0.0.0.0", - HostPort: fmt.Sprint(d.SSHPort), - }, - }, - "6901/tcp": []nat.PortBinding{ - { - HostIP: "0.0.0.0", - HostPort: fmt.Sprint(d.VNCPort), - }, + // Port forwarding for VNC and SSH ports + PortForwarding := nat.PortMap{ + "22/tcp": []nat.PortBinding{ + { + HostIP: "0.0.0.0", + HostPort: fmt.Sprint(d.SSHPort), }, }, + "6901/tcp": []nat.PortBinding{ + { + HostIP: "0.0.0.0", + HostPort: fmt.Sprint(d.VNCPort), + }, + }, + } + + // Opening other ports (At the current moment only TCP ports can be + // opened + for i := range d.Ports { + Port, err := nat.NewPort("tcp",fmt.Sprint(d.Ports[i])) + if err != nil { + return err + } + + PortForwarding[Port] = []nat.PortBinding{ + { + HostIP: "0.0.0.0", + HostPort: fmt.Sprint(d.Ports[i]), + }, + } + } + + hostConfig := &container.HostConfig{ + PortBindings: PortForwarding, } res, err := dockerClient.ContainerCreate(ctx,config,hostConfig, diff --git a/server/ipfs-ssh.sh b/server/ipfs-ssh.sh deleted file mode 100644 index 74fce0c..0000000 --- a/server/ipfs-ssh.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env bash - -#Implementation from (https://gist.github.com/Kubuxu/0cafd6dc71114349875827c2c379fa1f) -REMOTE_HOST=${1-"YOUR REMOTE HOST HERE"} -DEFAULT_API_FILE="$HOME/.ipfs/api" -API_FILE="${IPFS_PATH-$DEFAULT_API_FILE}" - -if [ -e "$API_FILE" ]; then - echo "IPFS API is already running" - exit 1 -fi - -PORT=5001 -ssh -N -L ${PORT}:localhost:5001 $REMOTE_HOST & -SSH_PID=$! - -function cleanupAndExit() { - rm -f "$API_FILE" - echo - echo "Killing ssh." - kill "$SSH_PID" - exit -} -trap cleanupAndExit INT - -printf "/ip4/127.0.0.1/tcp/$PORT" > "$API_FILE" - -echo "Linked local API to $REMOTE_HOST." - -wait $SSH_PID -rm -f "$API_FILE" \ No newline at end of file diff --git a/server/rpc.go b/server/rpc.go index 3bf7ebc..118cc18 100644 --- a/server/rpc.go +++ b/server/rpc.go @@ -20,7 +20,7 @@ type Docker struct { // Starts container using RPC calls func (l *Listener) StartContainer( reply *Docker) error { fmt.Print("here") - vm, err := docker.BuildRunContainer() + vm, err := docker.BuildRunContainer(3) if err != nil { return err } diff --git a/server/server.go b/server/server.go index b81c154..ab001b4 100644 --- a/server/server.go +++ b/server/server.go @@ -77,8 +77,16 @@ func Server() error{ // Starts docker container in server r.GET("/startcontainer", func(c *gin.Context) { + // Get Number of ports to open + Ports := c.DefaultQuery("ports","0") + var PortsInt int - resp, err := docker.BuildRunContainer() + // Convert Get Request value to int + fmt.Sscanf(Ports, "%d", &PortsInt) + + // Creates container and returns back result to + // access container + resp, err := docker.BuildRunContainer(PortsInt) if err != nil { c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err))