-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
213 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,8 @@ const ( | |
ValueKey = "value" | ||
|
||
IDKey = "id" | ||
|
||
SubmittedBy = "submittedby" | ||
|
||
Flag = "flag" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package flaghandlerservice | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"sync" | ||
"time" | ||
|
||
"github.com/jasonlvhit/gocron" | ||
"github.com/sdslabs/katana/configs" | ||
"github.com/sdslabs/katana/lib/mongo" | ||
"github.com/sdslabs/katana/types" | ||
"go.mongodb.org/mongo-driver/bson" | ||
) | ||
|
||
func Ticker(wg sync.WaitGroup) { | ||
log.Println("Ticker") | ||
defer wg.Done() | ||
handler() | ||
gocron.Every(uint64(configs.FlagConfig.TickPeriod)).Second().Do(handler) | ||
<-gocron.Start() | ||
// gocron.Every(uint64(configs.FlagConfig.TickPeriod)).Second().Do(flaggetter) todo:trigger setter here | ||
// <-gocron.Start() | ||
} | ||
|
||
func handler() { | ||
log.Print("Handler triggered -> ", time.Now()) | ||
challenges := mongo.FetchChallenges(context.Background(), mongo.ChallengesCollection, bson.M{}) | ||
teams := mongo.FetchTeams(context.Background(), mongo.TeamsCollection, bson.M{}) | ||
time := time.Now() | ||
for _, team := range teams { | ||
for _, challenge := range challenges { | ||
go flagUpdater(challenge, team, time) | ||
} | ||
} | ||
} | ||
|
||
func flagUpdater(challenge types.Challenge, team types.CTFTeam, triggerTime time.Time) { | ||
flagValue := random(configs.FlagConfig.FlagLength) | ||
var flag = &types.Flag{} | ||
flag.Value = flagValue | ||
flag.ChallengeID = challenge.ID | ||
flag.CreatedAt = triggerTime | ||
flag.TeamID = team.Index | ||
if _, err := mongo.InsertOne(context.Background(), mongo.FlagsCollection, flag); err != nil { | ||
log.Println(err) | ||
} else { | ||
log.Println("Trigger setter") | ||
//todo : trigger flag setter here | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package flaghandlerservice | ||
|
||
import ( | ||
"bytes" | ||
"crypto/rand" | ||
"encoding/binary" | ||
) | ||
|
||
func Bytes(n int) []byte { | ||
b := make([]byte, n) | ||
_, err := rand.Read(b) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return b | ||
} | ||
|
||
var defLetters = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") | ||
|
||
func random(n int, letters ...string) string { | ||
var letterRunes []rune | ||
if len(letters) == 0 { | ||
letterRunes = defLetters | ||
} else { | ||
letterRunes = []rune(letters[0]) | ||
} | ||
|
||
var bb bytes.Buffer | ||
bb.Grow(n) | ||
l := uint32(len(letterRunes)) | ||
// on each loop, generate one random rune and append to output | ||
for i := 0; i < n; i++ { | ||
bb.WriteRune(letterRunes[binary.BigEndian.Uint32(Bytes(4))%l]) | ||
} | ||
return bb.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.