|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/ctfer-io/go-ctfd/api" |
| 9 | +) |
| 10 | + |
| 11 | +func main() { |
| 12 | + url := "http://localhost:8000" |
| 13 | + apiKey := "ctfd_xxx" |
| 14 | + |
| 15 | + // Connecting to existing CTFd |
| 16 | + fmt.Println("[+] Getting initial nonce and session values") |
| 17 | + nonce, session, err := api.GetNonceAndSession(url) |
| 18 | + if err != nil { |
| 19 | + log.Fatalf("Getting nonce and session: %s", err) |
| 20 | + } |
| 21 | + cli := api.NewClient(url, nonce, session, apiKey) |
| 22 | + |
| 23 | + // Create a user bracket |
| 24 | + fmt.Println("[+] Creating user bracket") |
| 25 | + bu, err := cli.PostBrackets(&api.PostBracketsParams{ |
| 26 | + Name: "Juniors", |
| 27 | + Description: "Brackets for 14-25 years old players.", |
| 28 | + Type: "users", |
| 29 | + }) |
| 30 | + if err != nil { |
| 31 | + log.Fatalf("Creating bracket: %s", err) |
| 32 | + } |
| 33 | + |
| 34 | + // Create a user |
| 35 | + fmt.Println("[+] Creating user") |
| 36 | + user, err := cli.PostUsers(&api.PostUsersParams{ |
| 37 | + Name: "player1", |
| 38 | + |
| 39 | + Password: "password", |
| 40 | + BracketID: ptr(strconv.Itoa(bu.ID)), |
| 41 | + Type: "user", |
| 42 | + Fields: []api.Field{}, |
| 43 | + }) |
| 44 | + if err != nil { |
| 45 | + log.Fatalf("Creating user: %s", err) |
| 46 | + } |
| 47 | + fmt.Printf("Created user %d\n", user.ID) |
| 48 | + |
| 49 | + // Create a team bracket |
| 50 | + fmt.Println("[+] Creating team bracket") |
| 51 | + bt, err := cli.PostBrackets(&api.PostBracketsParams{ |
| 52 | + Name: "Students", |
| 53 | + Description: "Brackets for students", |
| 54 | + Type: "teams", |
| 55 | + }) |
| 56 | + if err != nil { |
| 57 | + log.Fatalf("Creating bracket: %s", err) |
| 58 | + } |
| 59 | + |
| 60 | + // Create a team |
| 61 | + team, err := cli.PostTeams(&api.PostTeamsParams{ |
| 62 | + Name: "students", |
| 63 | + |
| 64 | + Password: "password", |
| 65 | + BracketID: ptr(strconv.Itoa(bt.ID)), |
| 66 | + Fields: []api.Field{}, |
| 67 | + }) |
| 68 | + if err != nil { |
| 69 | + log.Fatalf("Creating team: %s", err) |
| 70 | + } |
| 71 | + fmt.Printf("Created team %d\n", team.ID) |
| 72 | + |
| 73 | + if _, err := cli.PostTeamMembers(team.ID, &api.PostTeamsMembersParams{ |
| 74 | + UserID: user.ID, |
| 75 | + }); err != nil { |
| 76 | + log.Fatalf("Adding user %d in team %d: %s", user.ID, team.ID, err) |
| 77 | + } |
| 78 | + fmt.Printf("Added user %d in team %d\n", user.ID, team.ID) |
| 79 | +} |
| 80 | + |
| 81 | +func ptr[T any](t T) *T { |
| 82 | + return &t |
| 83 | +} |
0 commit comments