diff options
Diffstat (limited to 'src')
-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 |
4 files changed, 180 insertions, 0 deletions
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()) +} |