diff options
-rw-r--r-- | Dockerfile | 17 | ||||
-rw-r--r-- | hosted/background.png | bin | 0 -> 44640 bytes | |||
-rw-r--r-- | hosted/index.html | 44 | ||||
-rw-r--r-- | hosted/login.html | 48 | ||||
-rw-r--r-- | hosted/register.html | 45 | ||||
-rw-r--r-- | src/access.go | 7 | ||||
-rw-r--r-- | src/docker.go | 69 | ||||
-rw-r--r-- | src/http.go | 86 | ||||
-rw-r--r-- | src/main.go | 18 |
9 files changed, 334 insertions, 0 deletions
diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8e52428 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM golang + +# Install libraries +RUN go get \ + github.com/gorilla/mux \ + +# Create workdir +RUN mkdir /workdir +WORKDIR /workdir + +# Copy our sources +COPY src /workdir/src +COPY hosted /workdir/hosted +# and build them +RUN go build -o /workdir/register src/*.go + +ENTRYPOINT [ "/workdir/register" ] diff --git a/hosted/background.png b/hosted/background.png new file mode 100644 index 0000000..3654637 --- /dev/null +++ b/hosted/background.png Binary files differdiff --git a/hosted/index.html b/hosted/index.html new file mode 100644 index 0000000..699c7d0 --- /dev/null +++ b/hosted/index.html @@ -0,0 +1,44 @@ +<!DOCTYPE html> +<html> + <head> + <meta charset="utf-8"> + <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> + <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> + </head> + <body> + <nav class="navbar navbar-expand-md navbar-dark bg-dark mb-4"> + <a class="navbar-brand" href="/">Companion</a> + <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation"> + <span class="navbar-toggler-icon"></span> + </button> + <div class="collapse navbar-collapse" id="navbarCollapse"> + <ul class="navbar-nav mr-auto"> + <li class="nav-item active"> + <a class="nav-link" href="/">Home <span class="sr-only">(current)</span></a> + </li> + <li class="nav-item"> + <a class="nav-link" href="/register">Register</a> + </li> + </ul> + </div> + </nav> + <main class="container" role="main"> + <div class="jumbotron"> + <h1 class="display-4">Hello.</h1> + <h3>I'm here to register you.</h3> + <p>I was created so you can obtain a companion the most simple + way possible.</p> + <p>"What is a companion?" you might ask yourself. Well, your + companion is there to start, stop and reset challenge containers + for you. It also takes your flags checking if they are valid or + not. + <p>Please note that I'm not in scope. Please don't hack me. + However, if you accidentally find bugs in my code, ... report + them!</p> + <hr> + <a class="btn btn-primary btn-lg" href="/register" + role="button">Get a companion!</a> + </div> + </main> + </body> +</html> diff --git a/hosted/login.html b/hosted/login.html new file mode 100644 index 0000000..6105549 --- /dev/null +++ b/hosted/login.html @@ -0,0 +1,48 @@ +<!DOCTYPE html> +<html> + <head> + <meta charset="utf-8"> + <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> + <style type="text/css"> + body { + background-image: url("/files/background.png"); + } + .vertical-center { + min-height: 100%; /* Fallback for browsers do NOT support vh unit */ + min-height: 100vh; /* These two lines are counted as one :-) */ + + display: flex; + align-items: center; + } + </style> + </head> + <body> + <div class="container"> + <div class="row"> + <div class="col"></div> + <div class="col"> + <div class="vertical-center"> + <div class="card card-body"> + <h1> + Companion + </h1> + + <form method="POST"> + <div class="form-group"> + <label for="nameInput">Name</label> + <input type="text" class="form-control" id="nameInput" placeholder="Name" name="username" autofocus> + </div> + <div class="form-group"> + <label for="accesscodeInput">Access Code</label> + <input type="password" class="form-control" id="accesscodeInput" placeholder="Access Code" name="accesscode"> + </div> + <button type="submit" class="btn btn-primary">Login</button> + </form> + </div> + </div> + </div> + <div class="col"></div> + </div> + </div> + </body> +</html> diff --git a/hosted/register.html b/hosted/register.html new file mode 100644 index 0000000..373b64e --- /dev/null +++ b/hosted/register.html @@ -0,0 +1,45 @@ +<!DOCTYPE html> +<html> + <head> + <meta charset="utf-8"> + <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> + <style type="text/css"> + body { + background-image: url("/files/background.png"); + } + .vertical-center { + min-height: 100%; /* Fallback for browsers do NOT support vh unit */ + min-height: 100vh; /* These two lines are counted as one :-) */ + + display: flex; + align-items: center; + } + </style> + </head> + <body> + <div class="container"> + <div class="row"> + <div class="col"></div> + <div class="col"> + <div class="vertical-center"> + <div class="card card-body"> + <h1> + Obtain a Companion + </h1> + + <form method="POST"> + <div class="form-group"> + <label for="nameInput">Please enter your + Name:</label> + <input type="text" class="form-control" id="nameInput" placeholder="Name" name="username" autofocus> + </div> + <button type="submit" class="btn btn-primary">Proceed</button> + </form> + </div> + </div> + </div> + <div class="col"></div> + </div> + </div> + </body> +</html> diff --git a/src/access.go b/src/access.go new file mode 100644 index 0000000..e43d5aa --- /dev/null +++ b/src/access.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func newAccessCode() string { + return fmt.Sprintf("%d", 4) +} diff --git a/src/docker.go b/src/docker.go new file mode 100644 index 0000000..846a77e --- /dev/null +++ b/src/docker.go @@ -0,0 +1,69 @@ +package main + +import ( + "context" + "log" + "os" + + "github.com/docker/docker/api/types/container" + "github.com/docker/docker/api/types/network" + "github.com/docker/docker/client" +) + +const ( + fixedDockerVersion = "1.38" +) + +var ( + dockerCtx context.Context + dockerCLI *client.Client +) + +func setupContext() { + if dockerCtx == nil { + dockerCtx = context.Background() + } +} + +func setupDockerCLI() (err error) { + os.Setenv("DOCKER_API_VERSION", "1.27") + if dockerCLI == nil { + dockerCLI, err = client.NewEnvClient() + } + return err +} + +func spawnCompanion(username string) string { + log.Println("Spwaning a new companion container") + // setup the context and the docker cli connection + setupContext() + setupDockerCLI() + + Config := &container.Config{ + // docker socket + // companion seed + Image: "circus-companion:latest", + // + // username + // accesscode + // sessionsalt + // vpnremote address + // vpnrempte port + Cmd: []string{"-username", username, "-accesscode", accesscode}, + } + + HostConfig := &container.HostConfig{ + // ports (nat.PortMap) + } + + NetworkingConfig := &network.NetworkingConfig{ + // network + } + + _, err := dockerCLI.ContainerCreate(dockerCtx, Config, HostConfig, NetworkingConfig, "") + if err != nil { + panic(err) + } + + return accesscode +} diff --git a/src/http.go b/src/http.go new file mode 100644 index 0000000..a9b4240 --- /dev/null +++ b/src/http.go @@ -0,0 +1,86 @@ +package main + +import ( + "flag" + "fmt" + "io/ioutil" + "net/http" + "strings" + + "github.com/gorilla/mux" +) + +var ( + port *int // port the http server listens on + usernames []string // list of usernames +) + +func registerHTTPFlags() { + port = flag.Int("port", 8081, "The port the http server should listen on") +} + +func setupHTTPServer() http.Server { + r := mux.NewRouter() + + r.HandleFunc("/", indexHandler) + r.HandleFunc("/register", registerGetHandler).Methods("GET") + r.HandleFunc("/register", registerPostHandler).Methods("POST") + + return http.Server{ + Addr: fmt.Sprintf("0.0.0.0:%d", *port), + Handler: r, + } +} + +// Host of the index file +func indexHandler(w http.ResponseWriter, r *http.Request) { + readFileToReponse(w, "/index.html") +} + +// Read register page +func registerGetHandler(w http.ResponseWriter, r *http.Request) { + readFileToReponse(w, "/register.html") +} + +// Process a registration +func registerPostHandler(w http.ResponseWriter, r *http.Request) { + r.ParseForm() + + username := r.Form.Get("username") + + // test if the username has already been chosen + if !isUniq(username) { + w.Write([]byte("Username has already been taken.")) + return + } + + // add the new username to the list of usernames + usernames = append(usernames, username) + + // generate a new accesscode + accesscode := newAccessCode() + + // redirect the user to the front page + http.Redirect(w, r, "/", http.StatusTemporaryRedirect) +} + +func isUniq(username string) bool { + for _, user := range usernames { + if username == user { + return false + } + } + return true +} + +func readFileToReponse(w http.ResponseWriter, path string) { + requestedFile := strings.Replace(path, "..", "", -1) + + contents, readError := ioutil.ReadFile(fmt.Sprintf("hosted/%s", requestedFile)) + + if readError != nil { + w.Write([]byte(fmt.Sprintf("unable to read %s", requestedFile))) + } else { + w.Write([]byte(contents)) + } +} diff --git a/src/main.go b/src/main.go new file mode 100644 index 0000000..57007e8 --- /dev/null +++ b/src/main.go @@ -0,0 +1,18 @@ +package main + +import ( + "flag" + "log" +) + +func main() { + // Set up flags + registerHTTPFlags() + flag.Parse() + + // Set up the http server + log.Printf("Running the HTTP server on port %d", *port) + httpServer := setupHTTPServer() + + log.Fatalln(httpServer.ListenAndServe()) +} |