func TestGetServiceCurrentStatus(t *testing.T) { db, err := openDB() defer db.Close() round := 1 team := 2 service := 3 status1 := steward.Status{Round: round, TeamID: team, ServiceID: service, State: steward.StatusUP} steward.PutStatus(db.db, status1) halfStatus := steward.Status{Round: round, TeamID: team, ServiceID: service, State: steward.StatusUnknown} state, err := steward.GetState(db.db, halfStatus) if err != nil { log.Fatalln("Get state failed:", err) } if state != steward.StatusUP { log.Fatalln("Get states invalid") } }
func collectTeamResult(db *sql.DB, team steward.Team, services []steward.Service) (tr TeamResult, err error) { tr.ID = team.ID tr.Name = team.Name rr, err := steward.GetLastResult(db, team.ID) if err != nil { // At game start, no result exist rr = steward.RoundResult{AttackScore: 0, DefenceScore: 0} } tr.Attack = rr.AttackScore tr.Defence = rr.DefenceScore advisory, err := steward.GetAdvisoryScore(db, team.ID) if err != nil { tr.Advisory = 0 } else { tr.Advisory = advisory } round, err := steward.CurrentRound(db) if err != nil { // At game start, no round exist return tr, nil } for _, svc := range services { s := steward.Status{round.ID, team.ID, svc.ID, -1} state, err := steward.GetState(db, s) if err != nil { // Try to get status from previous round s.Round-- state, err = steward.GetState(db, s) if err != nil { state = steward.StatusDown } } tr.Status = append(tr.Status, state) } return }
func checkServicesStatus(db *sql.DB, round int, teams []steward.Team, services []steward.Service, status steward.ServiceState) { for _, team := range teams { for _, svc := range services { halfStatus := steward.Status{Round: round, TeamID: team.ID, ServiceID: svc.ID, State: -1} state, err := steward.GetState(db, halfStatus) if err != nil { log.Fatalln("Get state failed:", err) } if state != status { log.Fatalln("One of service status is", state, "instead", status) } } } }
func handler(conn net.Conn, db *sql.DB, priv *rsa.PrivateKey, attackFlow chan scoreboard.Attack) { addr := conn.RemoteAddr().String() defer conn.Close() fmt.Fprint(conn, greetingMsg) flag, err := bufio.NewReader(conn).ReadString('\n') if err != nil { log.Println("Read error:", err) } flag = strings.Trim(flag, "\n") log.Printf("\tGet flag %s from %s", flag, addr) valid, err := vexillary.ValidFlag(flag, priv.PublicKey) if err != nil { log.Println("\tValidate flag failed:", err) } if !valid { fmt.Fprint(conn, invalidFlagMsg) return } exist, err := steward.FlagExist(db, flag) if err != nil { log.Println("\tExist flag check failed:", err) fmt.Fprint(conn, internalErrorMsg) return } if !exist { fmt.Fprint(conn, flagDoesNotExistMsg) return } flg, err := steward.GetFlagInfo(db, flag) if err != nil { log.Println("\tGet flag info failed:", err) fmt.Fprint(conn, internalErrorMsg) return } captured, err := steward.AlreadyCaptured(db, flg.ID) if err != nil { log.Println("\tAlready captured check failed:", err) fmt.Fprint(conn, internalErrorMsg) return } if captured { fmt.Fprint(conn, alreadyCapturedMsg) return } team, err := teamByAddr(db, addr) if err != nil { log.Println("\tGet team by ip failed:", err) fmt.Fprint(conn, invalidTeamMsg) return } if flg.TeamID == team.ID { log.Printf("\tTeam %s try to send their flag", team.Name) fmt.Fprint(conn, flagYoursMsg) return } round, err := steward.CurrentRound(db) if round.ID != flg.Round { log.Printf("\t%s try to send flag from past round", team.Name) fmt.Fprint(conn, flagExpiredMsg) return } roundEndTime := round.StartTime.Add(round.Len) if time.Now().After(roundEndTime) { log.Printf("\t%s try to send flag from finished round", team.Name) fmt.Fprint(conn, flagExpiredMsg) return } halfStatus := steward.Status{flg.Round, team.ID, flg.ServiceID, steward.StatusUnknown} state, err := steward.GetState(db, halfStatus) if state != steward.StatusUP { log.Printf("\t%s service not ok, cannot capture", team.Name) fmt.Fprint(conn, serviceNotUpMsg) return } err = steward.CaptureFlag(db, flg.ID, team.ID) if err != nil { log.Println("\tCapture flag failed:", err) fmt.Fprint(conn, internalErrorMsg) return } go func() { attack := scoreboard.Attack{ Attacker: team.ID, Victim: flg.TeamID, Service: flg.ServiceID, Timestamp: time.Now().Unix(), } select { case attackFlow <- attack: default: _ = <-attackFlow attackFlow <- attack } }() fmt.Fprint(conn, capturedMsg) }