Skip to content

Commit

Permalink
Add template package, update models, and enhance challenge management
Browse files Browse the repository at this point in the history
WIP: Should print a functioning yaml compose, still doesn't for now, mostly due to placeholder not being filled + type issue making some parameters list of strings instead of simple strings
  • Loading branch information
Lolozendev committed Nov 3, 2024
1 parent 3efac7a commit 09f9206
Show file tree
Hide file tree
Showing 13 changed files with 533 additions and 33 deletions.
80 changes: 69 additions & 11 deletions challengemanager/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// This doesn't returns any value, it just logs the errors and warnings because in a near future , it will handle the errors and warnings on its own.

package challengemanager

import (
Expand All @@ -9,43 +11,49 @@ import (
"strings"

"github.com/Lolozendev/CTFManager/internal"
"github.com/Lolozendev/CTFManager/internal/constants"
"go.uber.org/zap"
)

const (
ChallengesPath string = "/challenges"
ChallengesFirstNetworkPosition int = 11
ChallengesLastNetworkPosition int = 249
ChallengesPath string = constants.ChallengesPath
ChallengesFirstNetworkPosition int = constants.ChallengesFirstNetworkPosition
ChallengesLastNetworkPosition int = constants.ChallengesLastNetworkPosition
)

var (
ChallengesNameRegexp *regexp.Regexp = regexp.MustCompile(`(?P<DIGIT>\d{1,3}|x)-\w*$`)
logger *zap.SugaredLogger = internal.GetLogger()
)

func normalizeChallengeName(name string) {
func normalizeChallengeName(name string) bool {
foundError := false
if !ChallengesNameRegexp.MatchString(name) {
logger.Error("Error: ", name, " does not match the expected format")
if regexp.MustCompile(`\w*`).MatchString(name) {
logger.Info("Renaming ", name, " to a disabled challenge")
var newname = "x-" + name
newname := "x-" + name
os.Rename(ChallengesPath+"/"+name, ChallengesPath+"/"+newname)
} else {
logger.Error("Error: cannot rename ", name, " to a disabled challenge, make sure the challenge name is alphanumeric only")
//TODO: Add a way to Normalize the name to remove special characters
foundError = true
}
}
return foundError
}

func CheckChallengeNames() {
func checkChallengeNames() bool {
foundError := false
entries, err := os.ReadDir(ChallengesPath)
if err != nil {
logger.Error("Error: ", err)
}

for _, entry := range entries {
normalizeChallengeName(entry.Name())
foundError = normalizeChallengeName(entry.Name())
}
return foundError
}

func checkDuplicatesAndHoles() (map[int]string, error) {
Expand All @@ -54,7 +62,7 @@ func checkDuplicatesAndHoles() (map[int]string, error) {
logger.Error("Error: ", err)
}

var actualChallenges = make(map[int]string, 0)
actualChallenges := make(map[int]string, 0)

for _, entry := range entries {
digit, name, _ := strings.Cut(entry.Name(), "-")
Expand Down Expand Up @@ -100,11 +108,21 @@ func checkChallengeStructure(path string) bool {
return false
}
}
if _, err := os.Stat(path + "/.env"); err != nil {
if errors.Is(err, fs.ErrNotExist) {
logger.Error("Error: ", path, ` does not have a .env file,
please create one even if it is empty`)
return false
} else {
logger.Error("Unkown error: ", err)
return false
}
}
return true
}

func CheckChallengesStructure() bool {
var foundError bool = false
func checkChallengesStructure() bool {
foundError := false
entries, err := os.ReadDir(ChallengesPath)
if err != nil {
logger.Error("Error: ", err)
Expand Down Expand Up @@ -155,10 +173,50 @@ func CheckChallengeDirectory() bool {
return false
}

if checkChallengeNames() {
return false
}

_, err = checkDuplicatesAndHoles()
if err != nil {
return false
}

return CheckChallengesStructure()
return checkChallengesStructure()
}

func GetChallenges() []string {
entries, err := os.ReadDir(ChallengesPath)
if err != nil {
logger.Error("Error: ", err)
}

challenges := make([]string, 0)

for _, entry := range entries {
challenges = append(challenges, entry.Name())
}
return challenges
}

func GetActivatedChallenges() []string {
challenges := GetChallenges()
activatedChallenges := make([]string, 0)
for _, challenge := range challenges {
if !strings.HasPrefix(challenge, "x-") {
activatedChallenges = append(activatedChallenges, challenge)
}
}
return activatedChallenges
}

func GetDisabledChallenges() []string {
challenges := GetChallenges()
disabledChallenges := make([]string, 0)
for _, challenge := range challenges {
if strings.HasPrefix(challenge, "x-") {
disabledChallenges = append(disabledChallenges, challenge)
}
}
return disabledChallenges
}
24 changes: 21 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
package main

import (
"fmt"
"os"

"github.com/Lolozendev/CTFManager/internal"
"github.com/Lolozendev/CTFManager/serializer"
)

func main() {
logger := internal.GetLogger()
defer logger.Sync()
logger.Info("Hello, World!")
logger.Warn("This is a warning!")
logger.Error("This is an error!")

logger.Info("only usable command for now is 'team create <teamname>'")

if len(os.Args) < 4 {
logger.Error("Error: Missing arguments")
return
}

logger.Info("creating team ", os.Args[3])

serialized, err := serializer.SerializeTeam(1, os.Args[3], []string{"user1", "user2"})
if err != nil {
logger.Error("Error: ", err)
return
}

fmt.Println(serialized)
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module github.com/Lolozendev/CTFManager

go 1.23.2

require go.uber.org/zap v1.27.0
require (
go.uber.org/zap v1.27.0
gopkg.in/yaml.v3 v3.0.1
)

require go.uber.org/multierr v1.11.0 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
18 changes: 18 additions & 0 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package constants

// This holds the constants for the paths of the challenges and teams used in multiple part of the project
const (
ChallengesPath string = "/challenges"
ChallengesFirstNetworkPosition int = 11
ChallengesLastNetworkPosition int = 249
TeamsPath string = "/equipes"
TeamsFirstNetworkPosition int = 1
TeamsLastNetworkPosition int = 254
DnsMasqTemplatePath string = "/dnsconf/dnsmasq.template"

//placeholders
TeamNamePlaceholder string = "<TEAM_NAME>"
TeamNumberPlaceholder string = "<TEAM_NUMBER>"
ChallengeNamePlaceholder string = "<CHALLENGE_NAME>"
ChallengeNumberPlaceholder string = "<CHALLENGE_NUMBER>"
)
10 changes: 5 additions & 5 deletions internal/model/network/network.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package network

type Network struct {
TeamNetwork TeamNetwork `yaml:"{ TEAM_NAME }-Network"`
TeamNetwork TeamNetwork `yaml:"<TEAM_NUMBER>-Network"`
}

type TeamNetwork struct {
Expand All @@ -19,8 +19,8 @@ type Config struct {

/*
networks:
{ TEAM_NAME }-Network:
driver: bridge
ipam:
config:
<TEAM_NAME>-Network:
driver: bridge
ipam:
config: 10.0.<TEAM_NUMBER>.0/24
*/
14 changes: 12 additions & 2 deletions internal/model/services/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@ package services
type Challenge struct {
Name string
Build string `yaml:"build"`
ContainerName string `yaml:"{ TEAM_NAME }-{ CHALLENGE_NAME }"`
ContainerName string `yaml:"<TEAM_NAME>-<CHALLENGE_NAME>"`
EnvFile string `yaml:"env_file,omitempty"`
Networks ChallengeNetworks `yaml:"networks"`
}

type ChallengeNetworks struct {
TeamNetwork ChallengeTeamNetwork `yaml:"{ TEAM_NAME }-Network"`
TeamNetwork ChallengeTeamNetwork `yaml:"<TEAM_NAME>-Network"`
}

type ChallengeTeamNetwork struct {
Ipv4Address string `yaml:"ipv4_address"`
}

/*
<CHALLENGE_NAME>:
build: /challenges/<CHALLENGE_NUMBER>-<CHALLENGE_NAME>
container_name: <TEAM_NAME>-<CHALLENGE_NAME>
env_file: /challenges/<CHALLENGE_NUMBER>-<CHALLENGE_NAME>/.env
networks:
TEAM_NAME-Network:
ipv4_address: 10.0.<TEAM_NUMBER>.<CHALLENGE_NUMBER>
*/
6 changes: 3 additions & 3 deletions internal/model/services/dnsmasq.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type Dnsmasq struct {
}

type DnsmasqNetworks struct {
TeamNetwork DnsmasqTeamNetwork `yaml:"{ TEAM_NAME }-Network"`
TeamNetwork DnsmasqTeamNetwork `yaml:"<TEAM_NAME>-Network"`
}

type DnsmasqTeamNetwork struct {
Expand All @@ -23,6 +23,6 @@ dnsmasq:
cap_add:
- NET_ADMIN
networks:
team_network:
ipv4_address: 10.0.{ TEAM_NUMBER }.253
<TEAM_NAME>-Network:
ipv4_address: 10.0.<TEAM_NUMBER>.253
*/
17 changes: 9 additions & 8 deletions internal/model/services/wiregard.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package services

type Wireguard struct {
ServerAddress string
Image string `yaml:"image"`
ContainerName string `yaml:"container_name"`
Ports []string `yaml:"ports"`
Expand All @@ -11,7 +12,7 @@ type Wireguard struct {
}

type WiregardNetwork struct {
TeamNetwork WiregardTeamNetwork `yaml:"{ TEAM_NAME }-Network"`
TeamNetwork WiregardTeamNetwork `yaml:"<TEAM_NAME>-Network"`
}

type WiregardTeamNetwork struct {
Expand All @@ -23,21 +24,21 @@ wireguard:
image: linuxserver/wireguard
container_name: wireguard
ports:
- "50{ TEAM_NUMBER }:51820/udp"
- "50<TEAM_NUMBER>:51820/udp"
environment:
- PUID=1000
- PGID=1000
- TZ=Europe/Paris
- PEERS={ TEAM_MEMBERS }
- PEERDNS=10.0.{ TEAM_NUMBER }.253,1.1.1.1
- ALLOWEDIPS=10.0.{ TEAM_NUMBER }.0/24
- SERVERURL=127.0.0.1
- SERVERPORT=50{ TEAM_NUMBER }
- PEERS=<TEAM_MEMBERS>
- PEERDNS=10.0.<TEAM_NUMBER>.253,1.1.1.1
- ALLOWEDIPS=10.0.<TEAM_NUMBER>.0/24
- SERVERURL=127.0.0.1 //replace with the server's IP
- SERVERPORT=50<TEAM_NUMBER>
volumes:
- ./config:/config
cap_add:
- NET_ADMIN
networks:
team_network:
ipv4_address: 10.0.{ TEAM_NUMBER }.252
ipv4_address: 10.0.<TEAM_NUMBER>.252
*/
1 change: 1 addition & 0 deletions internal/model/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Member struct {

type Team struct {
Name string
Number int
Members []Member
Network network.Network `yaml:"networks"`
Services services.Services `yaml:"services"`
Expand Down
6 changes: 6 additions & 0 deletions internal/template/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package template

type Placeholder struct {
TeamName string
ChallengeName string
}
Loading

0 comments on commit 09f9206

Please sign in to comment.