-
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.
- Loading branch information
1 parent
75ae5fe
commit bad2738
Showing
4 changed files
with
87 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package flaghandlerservice | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"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() { | ||
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() { | ||
challenges := mongo.FetchDocs(context.Background(), mongo.ChallengesCollection, bson.M{}) | ||
for _, challenge := range challenges { | ||
go flagUpdater(challenge) | ||
} | ||
} | ||
|
||
func flagUpdater(challenge bson.M) { | ||
flagValue := String(int(configs.FlagConfig.FlagLength)) | ||
var challengeStruct types.Challenge | ||
challengeBytes, _ := bson.Marshal(challenge) | ||
bson.Unmarshal(challengeBytes, challengeStruct) | ||
var flag = &types.Flag{} | ||
flag.Value = flagValue | ||
flag.TeamID = challengeStruct.TeamID | ||
flag.ChallengeID = challengeStruct.ID | ||
flag.CreatedAt = time.Now() | ||
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 String(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() | ||
} |