public static KeyValuePair <User, int[]>[] RegisterVoters(string host, int[][] votes, User[] candidateUsers) { var fakeTasks = votes.Take(candidateUsers.Length).Zip(candidateUsers, (vote, voter) => new { vote, voter, task = Task.FromResult(voter.Cookies) }); var newVotersTasks = votes.Skip(candidateUsers.Length).Select(vote => { var voter = UsersManager.GenRandomUser(); return(new { vote, voter, task = ElectroClient.RegUserAsync(host, Program.PORT, voter.Login, voter.Pass, voter.PublicMessage, voter.PrivateMessage) }); }).ToArray(); log.InfoFormat("Registering {0} new voters", newVotersTasks.Length); var voterTasks = fakeTasks.Concat(newVotersTasks).ToArray(); KeyValuePair <User, int[]>[] voters; try { voters = voterTasks.Select(arg => { arg.voter.Cookies = arg.task.Result; return(new KeyValuePair <User, int[]>(arg.voter, arg.vote)); }).ToArray(); } catch (AggregateException e) { throw new ServiceException(ExitCode.DOWN, string.Format("Failed to reg {0} voters in parallel: {1}", newVotersTasks.Length, e.Flatten())); } log.Info("Voters registered"); return(voters); }
public static int ProcessPut(string host, string id, string flag) { log.Info("Processing Vuln2.Put"); var candidateUsers = RegisterCandidates(host, flag.Distinct().Select(c => UsersManager.GenUser(null, c.ToString())).OrderBy(user => user.Login).ToArray()); var election = StartElection(host, candidateUsers[0], false, nominateTimeInSec, voteTimeInSec); var privateKey = election.PrivateKeyForCandidates; var sw = Stopwatch.StartNew(); election = NominateUsers(host, election, candidateUsers.Skip(1).ToArray()); sw.Stop(); var tts = nominateTimeInSec * 1000 - sw.ElapsedMilliseconds; if (tts > 0) { log.InfoFormat("Sleeping for {0}ms before voting (nomination duration is {1}s)", tts, nominateTimeInSec); Thread.Sleep((int)tts); } var votes = GenVotes(flag, election); var voters = RegisterVoters(host, votes, candidateUsers); log.InfoFormat("Voting by {0} voters", voters.Length); foreach (var voter in voters) { ElectroClient.Vote(host, Program.PORT, voter.Key.Cookies, election.Id, HomoCrypto.EncryptVector(voter.Value, election.PublicKey)); } log.Info("Voted"); var state = new Vuln2State { ElectionId = election.Id.ToString(), Voter = voters[0].Key, PrivateKey = privateKey }; log.Info("Flag put"); Console.Out.WriteLine(Convert.ToBase64String(Encoding.UTF8.GetBytes(state.ToJsonString()))); return((int)ExitCode.OK); }