func TestAlreadyCaptured(t *testing.T) {

	db, err := openDB()

	defer db.Close()

	flg1 := steward.Flag{ID: 1, Flag: "f", Round: 1, TeamID: 1,
		ServiceID: 1, Cred: "1:2"}
	flg2 := steward.Flag{ID: 2, Flag: "b", Round: 1, TeamID: 1,
		ServiceID: 1, Cred: "1:2"}

	err = steward.CaptureFlag(db.db, flg1.ID, 20)

	captured, err := steward.AlreadyCaptured(db.db, flg1.ID)
	if err != nil {
		log.Fatalln("Already captured check failed:", err)
	}

	if !captured {
		log.Fatalln("Captured flag is not captured")
	}

	captured, err = steward.AlreadyCaptured(db.db, flg2.ID)
	if err != nil {
		log.Fatalln("Already captured check failed:", err)
	}

	if captured {
		log.Fatalln("Not captured flag is captured")
	}
}
Exemple #2
0
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)
}