Skip to content

Commit

Permalink
feat: saner docker volumes and flag/env handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bchmnn committed Jul 5, 2024
1 parent f668c76 commit 1779da0
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 67 deletions.
14 changes: 7 additions & 7 deletions checker/src/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async def putflag0(
except ConnectionClosedError:
raise MumbleException("Connection was closed")
except InvalidStatusCode:
raise MumbleException("Connection was closed")
raise MumbleException("Invalid status code")

return username

Expand Down Expand Up @@ -98,7 +98,7 @@ async def getflag0(
except ConnectionClosedError:
raise MumbleException("Connection was closed")
except InvalidStatusCode:
raise MumbleException("Connection was closed")
raise MumbleException("Invalid status code")


@checker.exploit(0)
Expand Down Expand Up @@ -137,7 +137,7 @@ async def exploit0(
except ConnectionClosedError:
raise MumbleException("Connection was closed")
except InvalidStatusCode:
raise MumbleException("Connection was closed")
raise MumbleException("Invalid status code")

match = re.findall(r"FLAG\s*([A-Za-z0-9\+\=\/]+)\s*OK", response)
if len(match) == 0:
Expand Down Expand Up @@ -172,7 +172,7 @@ async def putnoise0(
except ConnectionClosedError:
raise MumbleException("Connection was closed")
except InvalidStatusCode:
raise MumbleException("Connection was closed")
raise MumbleException("Invalid status code")

await db.set("noise_id", i)

Expand Down Expand Up @@ -207,7 +207,7 @@ async def getnoise0(
except ConnectionClosedError:
raise MumbleException("Connection was closed")
except InvalidStatusCode:
raise MumbleException("Connection was closed")
raise MumbleException("Invalid status code")


@checker.havoc(0)
Expand Down Expand Up @@ -283,7 +283,7 @@ async def getflag1(
except ConnectionClosedError:
raise MumbleException("Connection was closed")
except InvalidStatusCode:
raise MumbleException("Connection was closed")
raise MumbleException("Invalid Status Code")


@checker.exploit(1)
Expand Down Expand Up @@ -334,7 +334,7 @@ async def exploit1(
except ConnectionClosedError:
raise MumbleException("Connection was closed")
except InvalidStatusCode:
raise MumbleException("Connection was closed")
raise MumbleException("Invalid status code")


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion service/Dockerfile.backend
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ COPY ./image ./image

EXPOSE 6969/tcp

ENTRYPOINT ["./backend/out/main", "-i", "./image", "-k", "/apikey/key.txt", "-f", "/devenvs", "-t", "/devenvs-tmp"]
ENTRYPOINT ["./backend/out/main", "-i", "./image"]

13 changes: 13 additions & 0 deletions service/backend/controller/devenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
Expand All @@ -27,6 +28,18 @@ type DevenvController struct {
}

func NewDevenvController(docker *service.DockerService, devenvFilesPath string, devenvFilesTmpPath string) DevenvController {

err := util.MakeDirIfNotExists(devenvFilesPath)

if err != nil {
log.Fatal(err)
}

err = util.MakeDirIfNotExists(devenvFilesTmpPath)
if err != nil {
log.Fatal(err)
}

return DevenvController{
Docker: docker,
Upgrader: websocket.Upgrader{
Expand Down
11 changes: 2 additions & 9 deletions service/backend/database/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package database

import (
"log"
"os"

"replme/model"

Expand All @@ -12,15 +11,9 @@ import (

var DB *gorm.DB

func Connect() {
sqlitePath := os.Getenv("REPL_SQLITE")

if sqlitePath == "" {
log.Fatal("No Sqlitepath")
}

func Connect(dbPath string) {
var err error
DB, err = gorm.Open(sqlite.Open(sqlitePath), &gorm.Config{})
DB, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{})

if err != nil {
log.Fatal("Failed to connect to DB:", err)
Expand Down
83 changes: 72 additions & 11 deletions service/backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,88 @@ import (

func main() {
var imagePath string
var imageTag string
var dbPath string
var apiKeyPath string
var devenvFiles string
var devenvFilesTmp string
var devenvsPath string
var devenvsTmpPath string
var containerLogsPath string

flag.StringVar(&imagePath, "i", "", "Image directory (required)")
flag.StringVar(&apiKeyPath, "k", "", "Apikey file (required)")
flag.StringVar(&devenvFiles, "f", "", "Devenv files (required)")
flag.StringVar(&devenvFilesTmp, "t", "", "Devenv files tmp (required)")
flag.StringVar(&imagePath, "i", "", "Image dir (required)")
flag.StringVar(&imagePath, "n", "", "Image tag (required), env: REPL_IMG_TAG")
flag.StringVar(&dbPath, "d", "", "Database file (required), env: REPL_SQLITE")
flag.StringVar(&apiKeyPath, "k", "", "Apikey file (required), env: REPL_API_KEY")
flag.StringVar(&devenvsPath, "f", "", "Devenv files dir (required), env: REPL_DEVENVS")
flag.StringVar(&devenvsTmpPath, "t", "", "Tmp devenv files dir (required), env: REPL_DEVENVS_TMP")
flag.StringVar(&containerLogsPath, "l", "", "Container logs dir (required), env: REPL_CONTAINER_LOGS")

flag.Parse()

if imagePath == "" || apiKeyPath == "" {
if imagePath == "" {
flag.Usage()
os.Exit(1)
}

if imageTag == "" {
imageTagEnv := os.Getenv("REPL_IMG_TAG")
if imageTagEnv == "" {
flag.Usage()
os.Exit(1)
}
imageTag = imageTagEnv
}

if dbPath == "" {
dbPathEnv := os.Getenv("REPL_SQLITE")
if dbPathEnv == "" {
flag.Usage()
os.Exit(1)
}
dbPath = dbPathEnv
}

if apiKeyPath == "" {
apiKeyPathEnv := os.Getenv("REPL_API_KEY")
if apiKeyPathEnv == "" {
flag.Usage()
os.Exit(1)
}
apiKeyPath = apiKeyPathEnv
}

if devenvsPath == "" {
devenvsPathEnv := os.Getenv("REPL_DEVENVS")
if devenvsPathEnv == "" {
flag.Usage()
os.Exit(1)
}
devenvsPath = devenvsPathEnv
}

if devenvsTmpPath == "" {
devenvsTmpPathEnv := os.Getenv("REPL_DEVENVS_TMP")
if devenvsTmpPathEnv == "" {
flag.Usage()
os.Exit(1)
}
devenvsTmpPath = devenvsTmpPathEnv
}

// REPL_CONTAINER_LOGS

if containerLogsPath == "" {
containerLogsPathTmp := os.Getenv("REPL_CONTAINER_LOGS")
if containerLogsPathTmp == "" {
flag.Usage()
os.Exit(1)
}
containerLogsPath = containerLogsPathTmp
}

apiKey := util.ApiKey(apiKeyPath)
imageTag := "ptwhy"

docker := service.Docker(apiKey)
docker.BuildImage(imagePath, imageTag)
docker := service.Docker(apiKey, imagePath, imageTag, containerLogsPath)
docker.BuildImage()

server.Init(&docker, devenvFiles, devenvFilesTmp)
server.Init(&docker, dbPath, devenvsPath, devenvsTmpPath)
}
4 changes: 2 additions & 2 deletions service/backend/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/gin-gonic/gin"
)

func NewRouter(docker *service.DockerService, devenvFilesPath string, devenvFilesTmpPath string) *gin.Engine {
func NewRouter(docker *service.DockerService, dbPath string, devenvFilesPath string, devenvFilesTmpPath string) *gin.Engine {

logLevel, exists := os.LookupEnv("REPL_LOG")
if !exists {
Expand All @@ -26,7 +26,7 @@ func NewRouter(docker *service.DockerService, devenvFilesPath string, devenvFile
util.LoggerInit(logLevel)

util.SLogger.Info("Connecting to DB ..")
database.Connect()
database.Connect(dbPath)
util.SLogger.Info("Migrating DB ..")
database.Migrate()

Expand Down
4 changes: 2 additions & 2 deletions service/backend/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"replme/util"
)

func Init(docker *service.DockerService, devenvFilesPath string, devenvFilesTmpPath string) {
engine := NewRouter(docker, devenvFilesPath, devenvFilesTmpPath)
func Init(docker *service.DockerService, dbPath string, devenvFilesPath string, devenvFilesTmpPath string) {
engine := NewRouter(docker, dbPath, devenvFilesPath, devenvFilesTmpPath)
util.SLogger.Infof("Server is running on port 6969")
engine.Run(":6969")
}
2 changes: 1 addition & 1 deletion service/backend/service/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Cleanup(docker *DockerService, replState *ReplStateService, devenvFilesPath
}

func (cleanup *CleanupService) DoCleanup() {
containers, err := cleanup.Docker.GetContainers("ptwhy")
containers, err := cleanup.Docker.GetContainers(cleanup.Docker.ImgTag)

if err != nil {
return
Expand Down
Loading

0 comments on commit 1779da0

Please sign in to comment.