Пример #1
0
        public void HackFeedBack(HackFeedback hfb)
        {
            if (Online.FindIndex(x => x.Token == Context.ConnectionId) < 0)
            {
                return;
            }
            var hack = DbContext.Hacks.Find(hfb.HackID);

            hack.Result = hfb.Result;
            hack.Hint   = hfb.Hint;
            DbContext.SaveChanges();
            if (hack.Result == Entity.HackResult.Success)
            {
                var hash = Helpers.Security.SHA1(hack.InputData);
                var tc   = (from t in DbContext.TestCases where t.Hash == hash select t).FirstOrDefault();
                if (tc == null)
                {
                    tc = new Entity.TestCase
                    {
                        Input     = hack.InputData,
                        Hash      = Helpers.Security.SHA1(hack.InputData),
                        Type      = Entity.TestCaseType.Custom,
                        ProblemID = hack.Status.ProblemID,
                        Output    = hfb.Output
                    };
                    DbContext.TestCases.Add(tc);
                }
                DbContext.JudgeTasks.Add(new Entity.JudgeTask
                {
                    StatusID    = hack.StatusID,
                    Result      = hfb.JudgeResult,
                    TimeUsage   = hfb.TimeUsage,
                    MemoryUsage = hfb.MemoryUsage,
                    Hint        = hfb.Hint,
                    TestCaseID  = tc.ID
                });
                hack.Status.Result = Entity.JudgeResult.Hacked;
                DbContext.SaveChanges();
                SignalR.CodeCombHub.context.Clients.Group(hack.Defender.Username).onHacked(new Models.View.HackResult(hack, true));
                SignalR.CodeCombHub.context.Clients.Group(hack.Hacker.Username).onHackFinished(new Models.View.HackResult(hack));

                SignalR.CodeCombHub.context.Clients.All.onStatusChanged(new Models.View.Status(hack.Status));
                if (DateTime.Now >= hack.Status.Problem.Contest.Begin)
                {
                    SignalR.CodeCombHub.context.Clients.All.onStandingsChanged(hack.Status.Problem.Contest.ID, new Models.View.Standing(hack.Defender, hack.Status.Problem.Contest));
                    SignalR.CodeCombHub.context.Clients.All.onStandingsChanged(hack.Status.Problem.Contest.ID, new Models.View.Standing(hack.Hacker, hack.Status.Problem.Contest));
                }
            }
            else
            {
                SignalR.CodeCombHub.context.Clients.Group(hack.Hacker.Username).onHackFinished(new Models.View.HackResult(hack));
                if (DateTime.Now >= hack.Status.Problem.Contest.Begin && DateTime.Now < hack.Status.Problem.Contest.End)
                {
                    SignalR.CodeCombHub.context.Clients.All.onStandingsChanged(hack.Status.Problem.Contest.ID, new Models.View.Standing(hack.Hacker, hack.Status.Problem.Contest));
                }
            }
        }
Пример #2
0
        public ActionResult TestCaseUpload(int id)
        {
            var problem = DbContext.Problems.Find(id);
            var contest = problem.Contest;
            var user    = ViewBag.CurrentUser == null ? new Entity.User() : (Entity.User)ViewBag.CurrentUser;

            if (user.Role < Entity.UserRole.Master && !(from cm in contest.Managers select cm.UserID).Contains(user.ID))
            {
                return(RedirectToAction("Message", "Shared", new { msg = "您无权执行本操作!" }));
            }
            var file      = Request.Files[0];
            var timestamp = Helpers.String.ToTimeStamp(DateTime.Now);
            var filename  = timestamp + ".zip";
            var dir       = Server.MapPath("~") + @"\Temp\";

            file.SaveAs(dir + filename);
            Helpers.Zip.UnZip(dir + filename, dir + timestamp);
            System.IO.File.Delete(dir + filename);
            var files = System.IO.Directory.GetFiles(dir + timestamp);

            foreach (var f in files)
            {
                if (System.IO.Path.GetExtension(f) == ".in")
                {
                    var testcase = new Entity.TestCase();
                    testcase.Input = System.IO.File.ReadAllText(f);
                    var outfile = f.Substring(0, f.Length - 3) + ".out";
                    var exist   = false;
                    if (System.IO.File.Exists(outfile))
                    {
                        testcase.Output = System.IO.File.ReadAllText(outfile);
                        exist           = true;
                    }
                    outfile = f.Substring(0, f.Length - 3) + ".ans";
                    if (System.IO.File.Exists(outfile))
                    {
                        testcase.Output = System.IO.File.ReadAllText(outfile);
                        exist           = true;
                    }
                    if (!exist)
                    {
                        continue;
                    }
                    testcase.ProblemID = id;
                    testcase.Type      = TestCaseType.Overall;
                    testcase.Hash      = Helpers.Security.SHA1(testcase.Input);
                    DbContext.TestCases.Add(testcase);
                }
            }
            DbContext.SaveChanges();
            System.IO.Directory.Delete(dir + timestamp, true);
            return(RedirectToAction("TestCases", "Problem", new { id = problem.ID }));
        }
Пример #3
0
        private void Run()
        {
            string ExecuteFile = WorkDirectory + "\\" + Task.Record.ID + "\\Main";

            switch (Task.Record.Language)
            {
            case Entity.ProgrammingLanguage.Java:
                ExecuteFile += ".class";
                break;

            case Entity.ProgrammingLanguage.Python27:
            case Entity.ProgrammingLanguage.Python33:
                ExecuteFile += ".py";
                break;

            case Entity.ProgrammingLanguage.Ruby:
                ExecuteFile += ".rb";
                break;

            default:
                ExecuteFile += ".exe";
                break;
            }
            if (!System.IO.File.Exists(ExecuteFile))
            {
                throw new Exception("Compiled file is not found.");
            }

            bool needDownloading;

            if (System.IO.File.Exists(WorkDirectory + "\\" + Task.TestCaseID + ".in") && System.IO.File.Exists(WorkDirectory + "\\" + Task.TestCaseID + ".out") && System.IO.File.Exists(WorkDirectory + "\\" + Task.TestCaseID + ".hash.in") && System.IO.File.Exists(WorkDirectory + "\\" + Task.TestCaseID + ".hash.out"))
            {
                var InputHash         = System.IO.File.ReadAllBytes(WorkDirectory + "\\" + Task.TestCaseID + ".in.hash");
                var OutputHash        = System.IO.File.ReadAllBytes(WorkDirectory + "\\" + Task.TestCaseID + ".out.hash");
                var correctInputHash  = CenterServer.GetInputHash(Task.TestCaseID.Value);
                var correctOutputHash = CenterServer.GetOutputHash(Task.TestCaseID.Value);
                needDownloading = !InputHash.SequenceEqual(correctInputHash) || !OutputHash.SequenceEqual(correctOutputHash);
            }
            else
            {
                needDownloading = true;
            }

            if (needDownloading)
            {
                Entity.TestCase testCase = CenterServer.GetTestCase(Task.TestCaseID.Value);
                System.IO.File.WriteAllBytes(WorkDirectory + "\\" + Task.TestCaseID + ".in.hash", testCase.InputHash);
                System.IO.File.WriteAllBytes(WorkDirectory + "\\" + Task.TestCaseID + ".out.hash", testCase.OutputHash);
                System.IO.File.WriteAllBytes(WorkDirectory + "\\" + Task.TestCaseID + ".in", testCase.Input);
                System.IO.File.WriteAllBytes(WorkDirectory + "\\" + Task.TestCaseID + ".out", testCase.Output);
            }

            Runner Runner = new Runner();

            Runner.Identity = Identity;
            Runner.RunnerInfo.CenaCoreDirectory = Environment.CurrentDirectory + "\\Core\\CenaPlus.Core.exe";
            Runner.RunnerInfo.StdInFile         = "..\\" + Task.TestCaseID + ".in";
            Runner.RunnerInfo.StdOutFile        = Task.TestCaseID + ".user";
            Runner.RunnerInfo.TimeLimit         = Task.Problem.TimeLimit;
            Runner.RunnerInfo.MemoryLimit       = Convert.ToInt32(Task.Problem.MemoryLimit / 1024);
            Runner.RunnerInfo.HighPriorityTime  = 1000;
            Runner.RunnerInfo.WorkingDirectory  = WorkDirectory + "\\" + Task.Record.ID;
            switch (Task.Record.Language)
            {
            case Entity.ProgrammingLanguage.Java:
                Runner.RunnerInfo.Cmd = Bll.ConfigHelper.Java;
                break;

            case Entity.ProgrammingLanguage.Python27:
                Runner.RunnerInfo.Cmd = Bll.ConfigHelper.Python27; break;

            case Entity.ProgrammingLanguage.Python33:
                Runner.RunnerInfo.Cmd = Bll.ConfigHelper.Python33; break;

            case Entity.ProgrammingLanguage.Ruby:
                Runner.RunnerInfo.Cmd = Bll.ConfigHelper.Ruby; break;

            default:
                Runner.RunnerInfo.Cmd     = "Main.exe";
                Runner.RunnerInfo.APIHook = "..\\safe.dll";
                break;
            }
            Runner.Start();
            if (Runner.RunnerResult.TimeUsed > Task.Problem.TimeLimit)
            {
                Feedback = new Entity.TaskFeedback_Run
                {
                    MemUsage     = Runner.RunnerResult.PagedSize,
                    RecordID     = Task.Record.ID,
                    RecordStatus = Entity.RecordStatus.TimeLimitExceeded,
                    TestCaseID   = Task.TestCaseID.Value,
                    TimeUsage    = Runner.RunnerResult.TimeUsed
                };
                return;
            }
            var mem = Runner.RunnerResult.PagedSize;

            if (Task.Record.Language == Entity.ProgrammingLanguage.Java)
            {
                mem = Runner.RunnerResult.WorkingSetSize;
            }
            if (mem > Task.Problem.MemoryLimit / 1024)
            {
                Feedback = new Entity.TaskFeedback_Run
                {
                    MemUsage     = Runner.RunnerResult.PagedSize,
                    RecordID     = Task.Record.ID,
                    RecordStatus = Entity.RecordStatus.MemoryLimitExceeded,
                    TestCaseID   = Task.TestCaseID.Value,
                    TimeUsage    = Runner.RunnerResult.TimeUsed
                };
                return;
            }
            if (Task.Record.Language == Entity.ProgrammingLanguage.C && Runner.RunnerResult.ExitCode != 0 && Runner.RunnerResult.ExitCode != 1 || Runner.RunnerResult.ExitCode != 0 && Task.Record.Language != Entity.ProgrammingLanguage.C)
            {
                Feedback = new Entity.TaskFeedback_Run
                {
                    MemUsage     = Runner.RunnerResult.PagedSize,
                    RecordID     = Task.Record.ID,
                    RecordStatus = Entity.RecordStatus.RuntimeError,
                    TestCaseID   = Task.TestCaseID.Value,
                    TimeUsage    = Runner.RunnerResult.TimeUsed
                };
                return;
            }
            //spj
            Runner = null;
            GC.Collect();
            Runner          = new Runner();
            Runner.Identity = Identity;
            Runner.RunnerInfo.CenaCoreDirectory = Environment.CurrentDirectory + "\\Core\\CenaPlus.Core.exe";
            Runner.RunnerInfo.TimeLimit         = 2000;
            Runner.RunnerInfo.MemoryLimit       = 128 * 1024;
            if (Task.Problem.Spj == null)
            {
                Runner.RunnerInfo.WorkingDirectory = WorkDirectory;
                Runner.RunnerInfo.Cmd        = String.Format("spj.exe {0} {1} {2}", Task.TestCaseID + ".out", Task.Record.ID + "\\" + Task.TestCaseID + ".user", Task.TestCaseID + ".in");
                Runner.RunnerInfo.StdOutFile = Task.Record.ID + "\\" + Task.TestCaseID + ".validator";
            }
            else
            {
                var CustomSPJ = WorkDirectory + "\\spj" + Task.Problem.ID + "\\Main";
                switch (Task.Record.Language)
                {
                case Entity.ProgrammingLanguage.Java:
                    CustomSPJ += ".class";
                    break;

                case Entity.ProgrammingLanguage.Python27:
                case Entity.ProgrammingLanguage.Python33:
                    CustomSPJ += ".py";
                    break;

                case Entity.ProgrammingLanguage.Ruby:
                    CustomSPJ += ".rb";
                    break;

                default:
                    CustomSPJ += ".exe";
                    break;
                }
                if (!System.IO.File.Exists(CustomSPJ))
                {
                    throw new Exception("Custom spj not found.");
                }
                Runner.RunnerInfo.WorkingDirectory = WorkDirectory + "\\spj" + Task.Problem.ID;
                Runner.RunnerInfo.StdOutFile       = "..\\" + Task.TestCaseID + ".validator";
                if (CenaPlus.Judge.Compiler.RunEXE.Contains((Entity.ProgrammingLanguage)Task.Problem.ValidatorLanguage))
                {
                    Runner.RunnerInfo.Cmd = "Main.exe";
                }
                else
                {
                    Runner.RunnerInfo.Cmd = GetCommandLine((Entity.ProgrammingLanguage)Task.Problem.ValidatorLanguage);
                }
                Runner.RunnerInfo.Cmd += String.Format(" ..\\{0} ..\\{1}\\{2} ..\\{3}", Task.TestCaseID + ".out", Task.Record.ID, Task.Record.ID + "\\" + Task.TestCaseID + ".user", Task.TestCaseID + ".in");
            }
            Runner.Start();
            spjOutput = WorkDirectory + "\\" + Task.Record.ID + "\\" + Task.TestCaseID + ".validator";
            if (Runner.RunnerResult.ExitCode != 0 && Runner.RunnerResult.ExitCode != 1 && Runner.RunnerResult.ExitCode != 2 || Runner.RunnerResult.TimeUsed > 2000)
            {
                Feedback = new Entity.TaskFeedback_Run
                {
                    MemUsage     = Runner.RunnerResult.PagedSize,
                    RecordID     = Task.Record.ID,
                    RecordStatus = Entity.RecordStatus.ValidatorError,
                    TestCaseID   = Task.TestCaseID.Value,
                    TimeUsage    = Runner.RunnerResult.TimeUsed
                };
                return;
            }
            else
            {
                var Result = (Entity.RecordStatus)Runner.RunnerResult.ExitCode;
                Feedback = new Entity.TaskFeedback_Run
                {
                    MemUsage     = Runner.RunnerResult.PagedSize,
                    RecordID     = Task.Record.ID,
                    RecordStatus = Result,
                    TestCaseID   = Task.TestCaseID.Value,
                    TimeUsage    = Runner.RunnerResult.TimeUsed
                };
                return;
            }
        }
Пример #4
0
        private void Hack()
        {
            if (!System.IO.Directory.Exists(WorkDirectory + "\\hack" + Task.Hack.ID))
            {
                System.IO.Directory.CreateDirectory(WorkDirectory + "\\hack" + Task.Hack.ID);
            }
            Entity.TestCase HackData = new Entity.TestCase();
            if (Task.Hack.DatamakerLanguage != null)
            {
                CenaPlus.Judge.Compiler Compiler = new Compiler();
                Compiler.CompileInfo.Source    = Task.Hack.DataOrDatamaker;
                Compiler.Identity              = Identity;
                Compiler.CompileInfo.Language  = (Entity.ProgrammingLanguage)Task.Hack.DatamakerLanguage;
                Compiler.CompileInfo.Arguments = GetCommandLine(Compiler.CompileInfo.Language);
                if (Compiler.CompileInfo.Language == Entity.ProgrammingLanguage.Java)
                {
                    Compiler.CompileInfo.Arguments = Bll.ConfigHelper.Javac;
                }
                Compiler.CompileInfo.TimeLimit         = 1000;
                Compiler.CompileInfo.WorkingDirectory  = WorkDirectory + "\\hack" + Task.Hack.ID;
                Compiler.CompileInfo.CenaCoreDirectory = Environment.CurrentDirectory + "\\Core\\CenaPlus.Core.exe";
                Compiler.EnvironmentVariables          = EnvironmentVariables;
                Compiler.Start();
                if (Compiler.CompileResult.CompileFailed)
                {
                    Feedback = new Entity.TaskFeedback_Hack
                    {
                        HackData       = null,
                        HackID         = Task.Hack.ID,
                        HackStatus     = Entity.HackStatus.DatamakerError,
                        CompilerOutput = Compiler.CompileResult.CompilerOutput
                    };
                    return;
                }
                else
                {
                    CenaPlus.Judge.Runner Runner = new Runner();
                    Runner.Identity = Compiler.Identity;
                    Runner.RunnerInfo.CenaCoreDirectory = Environment.CurrentDirectory + "\\Core\\CenaPlus.Core.exe";
                    Runner.RunnerInfo.TimeLimit         = Task.Problem.TimeLimit;
                    Runner.RunnerInfo.MemoryLimit       = (int)(Task.Problem.MemoryLimit / 1024);
                    Runner.RunnerInfo.StdOutFile        = WorkDirectory + "\\hack" + Task.Hack.ID + "\\HackData.txt";
                    Runner.RunnerInfo.WorkingDirectory  = WorkDirectory + "\\hack" + Task.Hack.ID;
                    Runner.RunnerInfo.HighPriorityTime  = 1000;
                    if (CenaPlus.Judge.Compiler.RunEXE.Contains(Compiler.CompileInfo.Language))
                    {
                        Runner.RunnerInfo.Cmd = "Main.exe";
                    }
                    else
                    {
                        Runner.RunnerInfo.Cmd = GetCommandLine(Compiler.CompileInfo.Language);
                    }
                    Runner.Start();
                    if (Runner.RunnerResult.ExitCode != 0 || Runner.RunnerResult.TimeUsed > Task.Problem.TimeLimit)
                    {
                        Feedback = new Entity.TaskFeedback_Hack
                        {
                            HackData   = null,
                            HackID     = Task.Hack.ID,
                            HackStatus = Entity.HackStatus.DatamakerError
                        };
                        return;
                    }
                    else
                    {
                        HackData.Input = System.IO.File.ReadAllBytes(WorkDirectory + "\\hack" + Task.Hack.ID + "\\HackData.txt");
                    }
                }
            }
            else
            {
                HackData.Input = System.Text.Encoding.Default.GetBytes(Task.Hack.DataOrDatamaker);//TODO: Default Encode?
                System.IO.File.WriteAllBytes(WorkDirectory + "\\hack" + Task.Hack.ID + "\\HackData.txt", HackData.Input);
            }

            if (!System.IO.File.Exists(WorkDirectory + "\\" + Task.Record.ID + "\\Main" + CenaPlus.Judge.Compiler.GetExtension(Task.Record.Language)))//这里的Record是被Hack的Record
            {
                Compile();
            }
            else if (Task.Problem.Spj != null && System.IO.File.Exists(WorkDirectory + "\\spj" + Task.Problem.ID + "\\Main" + CenaPlus.Judge.Compiler.GetExtension((Entity.ProgrammingLanguage)Task.Problem.SpjLanguage)) == false)
            {
                Compile();
            }
            else if (Task.Problem.Std != null && System.IO.File.Exists(WorkDirectory + "\\std" + Task.Problem.ID + "\\Main" + CenaPlus.Judge.Compiler.GetExtension((Entity.ProgrammingLanguage)Task.Problem.StdLanguage)) == false)
            {
                Compile();
            }
            else if (Task.Problem.Std != null && System.IO.File.Exists(WorkDirectory + "\\range" + Task.Problem.ID + "\\Main" + CenaPlus.Judge.Compiler.GetExtension((Entity.ProgrammingLanguage)Task.Problem.ValidatorLanguage)) == false)
            {
                Compile();
            }

            //TODO: Hack Logic
            //data range validation
            CenaPlus.Judge.Runner runRange = new Runner();
            runRange.Identity = Identity;
            runRange.RunnerInfo.CenaCoreDirectory = Environment.CurrentDirectory + "\\Core\\CenaPlus.Core.exe";
            runRange.RunnerInfo.APIHook           = "..\\safe.dll";
            runRange.RunnerInfo.MemoryLimit       = (int)(Task.Problem.MemoryLimit / 1024);
            runRange.RunnerInfo.HighPriorityTime  = 1000;
            runRange.RunnerInfo.TimeLimit         = Task.Problem.TimeLimit;
            runRange.RunnerInfo.StdInFile         = "..\\hack" + Task.Hack.ID + "\\HackData.txt";
            runRange.RunnerInfo.WorkingDirectory  = WorkDirectory + "\\range" + Task.Problem.ID;
            if (CenaPlus.Judge.Compiler.RunEXE.Contains((Entity.ProgrammingLanguage)Task.Problem.ValidatorLanguage))
            {
                runRange.RunnerInfo.Cmd = "Main.exe";
            }
            else
            {
                runRange.RunnerInfo.Cmd = GetCommandLine((Entity.ProgrammingLanguage)Task.Problem.ValidatorLanguage);
            }
            runRange.Start();
            if (runRange.RunnerResult.ExitCode != 0)
            {
                Feedback = new Entity.TaskFeedback_Hack
                {
                    HackData   = null,
                    HackID     = Task.Hack.ID,
                    HackStatus = Entity.HackStatus.BadData
                };
                return;
            }
            //run std
            CenaPlus.Judge.Runner runStd = new Runner();
            runStd.Identity = Identity;
            runStd.RunnerInfo.CenaCoreDirectory = Environment.CurrentDirectory + "\\Core\\CenaPlus.Core.exe";
            runStd.RunnerInfo.APIHook           = "..\\safe.dll";
            runStd.RunnerInfo.HighPriorityTime  = 1000;
            runStd.RunnerInfo.MemoryLimit       = (int)(Task.Problem.MemoryLimit / 1024);
            runStd.RunnerInfo.TimeLimit         = Task.Problem.TimeLimit;
            runStd.RunnerInfo.StdInFile         = "..\\hack" + Task.Hack.ID + "\\HackData.txt";
            runStd.RunnerInfo.StdOutFile        = "..\\hack" + Task.Hack.ID + "\\StdOutput.txt";
            runStd.RunnerInfo.WorkingDirectory  = WorkDirectory + "\\std" + Task.Problem.ID;
            if (CenaPlus.Judge.Compiler.RunEXE.Contains((Entity.ProgrammingLanguage)Task.Problem.StdLanguage))
            {
                runStd.RunnerInfo.Cmd = "Main.exe";
            }
            else
            {
                runStd.RunnerInfo.Cmd = GetCommandLine((Entity.ProgrammingLanguage)Task.Problem.StdLanguage);
            }
            runStd.Start();
            if (runStd.RunnerResult.ExitCode != 0 || runStd.RunnerResult.TimeUsed > Task.Problem.TimeLimit)
            {
                Feedback = new Entity.TaskFeedback_Hack
                {
                    HackData   = null,
                    HackID     = Task.Hack.ID,
                    HackStatus = Entity.HackStatus.BadData
                };
                return;
            }
            //run hackee
            CenaPlus.Judge.Runner runHackee = new Runner();
            runHackee.Identity = Identity;
            runHackee.RunnerInfo.CenaCoreDirectory = Environment.CurrentDirectory + "\\Core\\CenaPlus.Core.exe";
            runHackee.RunnerInfo.APIHook           = "..\\safe.dll";
            runHackee.RunnerInfo.HighPriorityTime  = 1000;
            runHackee.RunnerInfo.MemoryLimit       = (int)(Task.Problem.MemoryLimit / 1024);
            runHackee.RunnerInfo.TimeLimit         = Task.Problem.TimeLimit;
            runHackee.RunnerInfo.StdInFile         = "..\\hack" + Task.Hack.ID + "\\HackData.txt";
            runHackee.RunnerInfo.StdOutFile        = "..\\hack" + Task.Hack.ID + "\\HackeeOutput.txt";
            runHackee.RunnerInfo.WorkingDirectory  = WorkDirectory + "\\" + Task.Record.ID;
            if (CenaPlus.Judge.Compiler.RunEXE.Contains((Entity.ProgrammingLanguage)Task.Record.Language))
            {
                runHackee.RunnerInfo.Cmd = "Main.exe";
            }
            else
            {
                runHackee.RunnerInfo.Cmd = GetCommandLine((Entity.ProgrammingLanguage)Task.Record.Language);
            }
            runHackee.Start();
            HackData.Output = System.IO.File.ReadAllBytes(WorkDirectory + "\\hack" + Task.Hack.ID + "\\StdOutput.txt");
            if (runHackee.RunnerResult.ExitCode != 0 || runHackee.RunnerResult.TimeUsed > Task.Problem.TimeLimit)
            {
                Feedback = new Entity.TaskFeedback_Hack
                {
                    HackData   = HackData,
                    HackID     = Task.Hack.ID,
                    HackStatus = Entity.HackStatus.Success
                };
                return;
            }
            else
            {
                CenaPlus.Judge.Runner runSpj = new Runner();
                runSpj.Identity = Identity;
                runSpj.RunnerInfo.CenaCoreDirectory = Environment.CurrentDirectory + "\\Core\\CenaPlus.Core.exe";
                runSpj.RunnerInfo.APIHook           = "..\\safe.dll";
                runSpj.RunnerInfo.HighPriorityTime  = 1000;
                runSpj.RunnerInfo.MemoryLimit       = (int)(Task.Problem.MemoryLimit / 1024);
                runSpj.RunnerInfo.TimeLimit         = Task.Problem.TimeLimit;
                //runSpj.RunnerInfo.StdInFile = "..\\hack" + Task.Hack.ID + "\\HackData.txt";
                //runSpj.RunnerInfo.StdOutFile = "..\\hack" + Task.Hack.ID + "\\StdOutput.txt";
                if (Task.Problem.SpjLanguage == null)
                {
                    runSpj.RunnerInfo.WorkingDirectory = WorkDirectory;
                    runSpj.RunnerInfo.Cmd = String.Format("spj.exe {0} {1} {2}", "hack" + Task.Hack.ID + "\\StdOutput.txt", "hack" + Task.Hack.ID + "\\UserOutput.txt", "hack" + Task.Hack.ID + "\\HackData.txt");
                }
                else
                {
                    runSpj.RunnerInfo.WorkingDirectory = WorkDirectory + "\\spj" + Task.Problem.ID;
                    if (CenaPlus.Judge.Compiler.RunEXE.Contains((Entity.ProgrammingLanguage)Task.Problem.SpjLanguage))
                    {
                        runSpj.RunnerInfo.Cmd = "Main.exe";
                    }
                    else
                    {
                        runSpj.RunnerInfo.Cmd = GetCommandLine((Entity.ProgrammingLanguage)Task.Problem.SpjLanguage);
                    }
                    runSpj.RunnerInfo.Cmd += String.Format(" {0} {1} {2}", "..\\hack" + Task.Hack.ID + "\\StdOutput.txt", "..\\hack" + Task.Hack.ID + "\\UserOutput.txt", "..\\hack" + Task.Hack.ID + "\\HackData.txt");
                }
                runSpj.Start();
                if (runSpj.RunnerResult.ExitCode < 0 || runSpj.RunnerResult.ExitCode > 3 || runSpj.RunnerResult.TimeUsed > Task.Problem.TimeLimit)
                {
                    Feedback = new Entity.TaskFeedback_Hack
                    {
                        HackData   = null,
                        HackID     = Task.Hack.ID,
                        HackStatus = Entity.HackStatus.BadData
                    };
                    return;
                }
                else
                {
                    if (runSpj.RunnerResult.ExitCode == 0)
                    {
                        Feedback = new Entity.TaskFeedback_Hack
                        {
                            HackData   = null,
                            HackID     = Task.Hack.ID,
                            HackStatus = Entity.HackStatus.Success
                        };
                        return;
                    }
                    else
                    {
                        Feedback = new Entity.TaskFeedback_Hack
                        {
                            HackData   = HackData,
                            HackID     = Task.Hack.ID,
                            HackStatus = Entity.HackStatus.Failure
                        };
                        return;
                    }
                }
            }
        }
Пример #5
0
        private void Hack()
        {
            if (!System.IO.Directory.Exists(WorkDirectory + "\\hack" + Task.Hack.ID))
            {
                System.IO.Directory.CreateDirectory(WorkDirectory + "\\hack" + Task.Hack.ID);
            }
            Entity.TestCase HackData = new Entity.TestCase();
            if (Task.Hack.DatamakerLanguage != null)
            {
                CenaPlus.Judge.Compiler Compiler = new Compiler();
                Compiler.CompileInfo.Source = Task.Hack.DataOrDatamaker;
                Compiler.Identity = Identity;
                Compiler.CompileInfo.Language = (Entity.ProgrammingLanguage)Task.Hack.DatamakerLanguage;
                Compiler.CompileInfo.Arguments = GetCommandLine(Compiler.CompileInfo.Language);
                if (Compiler.CompileInfo.Language == Entity.ProgrammingLanguage.Java)
                    Compiler.CompileInfo.Arguments = Bll.ConfigHelper.Javac;
                Compiler.CompileInfo.TimeLimit = 1000;
                Compiler.CompileInfo.WorkingDirectory = WorkDirectory + "\\hack" + Task.Hack.ID;
                Compiler.CompileInfo.CenaCoreDirectory = Environment.CurrentDirectory + "\\Core\\CenaPlus.Core.exe";
                Compiler.EnvironmentVariables = EnvironmentVariables;
                Compiler.Start();
                if (Compiler.CompileResult.CompileFailed)
                {
                    Feedback = new Entity.TaskFeedback_Hack
                    {
                        HackData = null,
                        HackID = Task.Hack.ID,
                        HackStatus = Entity.HackStatus.DatamakerError,
                        CompilerOutput = Compiler.CompileResult.CompilerOutput
                    };
                    return;
                }
                else
                {
                    CenaPlus.Judge.Runner Runner = new Runner();
                    Runner.Identity = Compiler.Identity;
                    Runner.RunnerInfo.CenaCoreDirectory = Environment.CurrentDirectory + "\\Core\\CenaPlus.Core.exe";
                    Runner.RunnerInfo.TimeLimit = Task.Problem.TimeLimit;
                    Runner.RunnerInfo.MemoryLimit = (int)(Task.Problem.MemoryLimit / 1024);
                    Runner.RunnerInfo.StdOutFile = WorkDirectory + "\\hack" + Task.Hack.ID + "\\HackData.txt";
                    Runner.RunnerInfo.WorkingDirectory = WorkDirectory + "\\hack" + Task.Hack.ID;
                    Runner.RunnerInfo.HighPriorityTime = 1000;
                    if (CenaPlus.Judge.Compiler.RunEXE.Contains(Compiler.CompileInfo.Language))
                    {
                        Runner.RunnerInfo.Cmd = "Main.exe";
                    }
                    else
                    {
                        Runner.RunnerInfo.Cmd = GetCommandLine(Compiler.CompileInfo.Language);
                    }
                    Runner.Start();
                    if (Runner.RunnerResult.ExitCode != 0 || Runner.RunnerResult.TimeUsed > Task.Problem.TimeLimit)
                    {
                        Feedback = new Entity.TaskFeedback_Hack
                        {
                            HackData = null,
                            HackID = Task.Hack.ID,
                            HackStatus = Entity.HackStatus.DatamakerError
                        };
                        return;
                    }
                    else
                    {
                        HackData.Input = System.IO.File.ReadAllBytes(WorkDirectory + "\\hack" + Task.Hack.ID + "\\HackData.txt");
                    }
                }
            }
            else
            {
                HackData.Input = System.Text.Encoding.Default.GetBytes(Task.Hack.DataOrDatamaker);//TODO: Default Encode?
                System.IO.File.WriteAllBytes(WorkDirectory + "\\hack" + Task.Hack.ID + "\\HackData.txt", HackData.Input);
            }

            if (!System.IO.File.Exists(WorkDirectory + "\\" + Task.Record.ID + "\\Main" + CenaPlus.Judge.Compiler.GetExtension(Task.Record.Language)))//这里的Record是被Hack的Record
            {
                Compile();
            }
            else if (Task.Problem.Spj != null && System.IO.File.Exists(WorkDirectory + "\\spj" + Task.Problem.ID + "\\Main" + CenaPlus.Judge.Compiler.GetExtension((Entity.ProgrammingLanguage)Task.Problem.SpjLanguage)) == false)
            {
                Compile();
            }
            else if (Task.Problem.Std != null && System.IO.File.Exists(WorkDirectory + "\\std" + Task.Problem.ID + "\\Main" + CenaPlus.Judge.Compiler.GetExtension((Entity.ProgrammingLanguage)Task.Problem.StdLanguage)) == false)
            {
                Compile();
            }
            else if (Task.Problem.Std != null && System.IO.File.Exists(WorkDirectory + "\\range" + Task.Problem.ID + "\\Main" + CenaPlus.Judge.Compiler.GetExtension((Entity.ProgrammingLanguage)Task.Problem.ValidatorLanguage)) == false)
            {
                Compile();
            }

            //TODO: Hack Logic
            //data range validation
            CenaPlus.Judge.Runner runRange = new Runner();
            runRange.Identity = Identity;
            runRange.RunnerInfo.CenaCoreDirectory = Environment.CurrentDirectory + "\\Core\\CenaPlus.Core.exe";
            runRange.RunnerInfo.APIHook = "..\\safe.dll";
            runRange.RunnerInfo.MemoryLimit = (int)(Task.Problem.MemoryLimit / 1024);
            runRange.RunnerInfo.HighPriorityTime = 1000;
            runRange.RunnerInfo.TimeLimit = Task.Problem.TimeLimit;
            runRange.RunnerInfo.StdInFile = "..\\hack" + Task.Hack.ID + "\\HackData.txt";
            runRange.RunnerInfo.WorkingDirectory = WorkDirectory + "\\range" + Task.Problem.ID;
            if (CenaPlus.Judge.Compiler.RunEXE.Contains((Entity.ProgrammingLanguage)Task.Problem.ValidatorLanguage))
            {
                runRange.RunnerInfo.Cmd = "Main.exe";
            }
            else
            {
                runRange.RunnerInfo.Cmd = GetCommandLine((Entity.ProgrammingLanguage)Task.Problem.ValidatorLanguage);
            }
            runRange.Start();
            if (runRange.RunnerResult.ExitCode != 0)
            {
                Feedback = new Entity.TaskFeedback_Hack
                {
                    HackData = null,
                    HackID = Task.Hack.ID,
                    HackStatus = Entity.HackStatus.BadData
                };
                return;
            }
            //run std
            CenaPlus.Judge.Runner runStd = new Runner();
            runStd.Identity = Identity;
            runStd.RunnerInfo.CenaCoreDirectory = Environment.CurrentDirectory + "\\Core\\CenaPlus.Core.exe";
            runStd.RunnerInfo.APIHook = "..\\safe.dll";
            runStd.RunnerInfo.HighPriorityTime = 1000;
            runStd.RunnerInfo.MemoryLimit = (int)(Task.Problem.MemoryLimit / 1024);
            runStd.RunnerInfo.TimeLimit = Task.Problem.TimeLimit;
            runStd.RunnerInfo.StdInFile = "..\\hack" + Task.Hack.ID + "\\HackData.txt";
            runStd.RunnerInfo.StdOutFile = "..\\hack" + Task.Hack.ID + "\\StdOutput.txt";
            runStd.RunnerInfo.WorkingDirectory = WorkDirectory + "\\std" + Task.Problem.ID;
            if (CenaPlus.Judge.Compiler.RunEXE.Contains((Entity.ProgrammingLanguage)Task.Problem.StdLanguage))
            {
                runStd.RunnerInfo.Cmd = "Main.exe";
            }
            else
            {
                runStd.RunnerInfo.Cmd = GetCommandLine((Entity.ProgrammingLanguage)Task.Problem.StdLanguage);
            }
            runStd.Start();
            if (runStd.RunnerResult.ExitCode != 0 || runStd.RunnerResult.TimeUsed > Task.Problem.TimeLimit)
            {
                Feedback = new Entity.TaskFeedback_Hack
                {
                    HackData = null,
                    HackID = Task.Hack.ID,
                    HackStatus = Entity.HackStatus.BadData
                };
                return;
            }
            //run hackee
            CenaPlus.Judge.Runner runHackee = new Runner();
            runHackee.Identity = Identity;
            runHackee.RunnerInfo.CenaCoreDirectory = Environment.CurrentDirectory + "\\Core\\CenaPlus.Core.exe";
            runHackee.RunnerInfo.APIHook = "..\\safe.dll";
            runHackee.RunnerInfo.HighPriorityTime = 1000;
            runHackee.RunnerInfo.MemoryLimit = (int)(Task.Problem.MemoryLimit / 1024);
            runHackee.RunnerInfo.TimeLimit = Task.Problem.TimeLimit;
            runHackee.RunnerInfo.StdInFile = "..\\hack" + Task.Hack.ID + "\\HackData.txt";
            runHackee.RunnerInfo.StdOutFile = "..\\hack" + Task.Hack.ID + "\\HackeeOutput.txt";
            runHackee.RunnerInfo.WorkingDirectory = WorkDirectory + "\\" + Task.Record.ID;
            if (CenaPlus.Judge.Compiler.RunEXE.Contains((Entity.ProgrammingLanguage)Task.Record.Language))
            {
                runHackee.RunnerInfo.Cmd = "Main.exe";
            }
            else
            {
                runHackee.RunnerInfo.Cmd = GetCommandLine((Entity.ProgrammingLanguage)Task.Record.Language);
            }
            runHackee.Start();
            HackData.Output = System.IO.File.ReadAllBytes(WorkDirectory + "\\hack" + Task.Hack.ID + "\\StdOutput.txt");
            if (runHackee.RunnerResult.ExitCode != 0 || runHackee.RunnerResult.TimeUsed > Task.Problem.TimeLimit)
            {
                Feedback = new Entity.TaskFeedback_Hack
                {
                    HackData = HackData,
                    HackID = Task.Hack.ID,
                    HackStatus = Entity.HackStatus.Success
                };
                return;
            }
            else
            {
                CenaPlus.Judge.Runner runSpj = new Runner();
                runSpj.Identity = Identity;
                runSpj.RunnerInfo.CenaCoreDirectory = Environment.CurrentDirectory + "\\Core\\CenaPlus.Core.exe";
                runSpj.RunnerInfo.APIHook = "..\\safe.dll";
                runSpj.RunnerInfo.HighPriorityTime = 1000;
                runSpj.RunnerInfo.MemoryLimit = (int)(Task.Problem.MemoryLimit / 1024);
                runSpj.RunnerInfo.TimeLimit = Task.Problem.TimeLimit;
                //runSpj.RunnerInfo.StdInFile = "..\\hack" + Task.Hack.ID + "\\HackData.txt";
                //runSpj.RunnerInfo.StdOutFile = "..\\hack" + Task.Hack.ID + "\\StdOutput.txt";
                if (Task.Problem.SpjLanguage == null)
                {
                    runSpj.RunnerInfo.WorkingDirectory = WorkDirectory;
                    runSpj.RunnerInfo.Cmd = String.Format("spj.exe {0} {1} {2}", "hack" + Task.Hack.ID + "\\StdOutput.txt", "hack" + Task.Hack.ID + "\\UserOutput.txt", "hack" + Task.Hack.ID + "\\HackData.txt");
                }
                else
                {
                    runSpj.RunnerInfo.WorkingDirectory = WorkDirectory + "\\spj" + Task.Problem.ID;
                    if (CenaPlus.Judge.Compiler.RunEXE.Contains((Entity.ProgrammingLanguage)Task.Problem.SpjLanguage))
                    {
                        runSpj.RunnerInfo.Cmd = "Main.exe";
                    }
                    else
                    {
                        runSpj.RunnerInfo.Cmd = GetCommandLine((Entity.ProgrammingLanguage)Task.Problem.SpjLanguage);
                    }
                    runSpj.RunnerInfo.Cmd += String.Format(" {0} {1} {2}", "..\\hack" + Task.Hack.ID + "\\StdOutput.txt", "..\\hack" + Task.Hack.ID + "\\UserOutput.txt", "..\\hack" + Task.Hack.ID + "\\HackData.txt");
                }
                runSpj.Start();
                if (runSpj.RunnerResult.ExitCode < 0 || runSpj.RunnerResult.ExitCode > 3 || runSpj.RunnerResult.TimeUsed > Task.Problem.TimeLimit)
                {
                    Feedback = new Entity.TaskFeedback_Hack
                    {
                        HackData = null,
                        HackID = Task.Hack.ID,
                        HackStatus = Entity.HackStatus.BadData
                    };
                    return;
                }
                else
                {
                    if (runSpj.RunnerResult.ExitCode == 0)
                    {
                        Feedback = new Entity.TaskFeedback_Hack
                        {
                            HackData = null,
                            HackID = Task.Hack.ID,
                            HackStatus = Entity.HackStatus.Success
                        };
                        return;
                    }
                    else
                    {
                        Feedback = new Entity.TaskFeedback_Hack
                        {
                            HackData = HackData,
                            HackID = Task.Hack.ID,
                            HackStatus = Entity.HackStatus.Failure
                        };
                        return;
                    }
                }
            }
        }
Пример #6
0
 public TestCase(Entity.TestCase testcase)
 {
     ID     = testcase.ID;
     Input  = testcase.Input;
     Output = testcase.Output;
 }
Пример #7
0
        public void HackFeedBack(HackFeedback hfb)
        {
            if (Online.FindIndex(x => x.Token == Context.ConnectionId) < 0)
                return;
            var hack = DbContext.Hacks.Find(hfb.HackID);
            hack.Result = hfb.Result;
            hack.Hint = hfb.Hint;
            DbContext.SaveChanges();
            if (hack.Result == Entity.HackResult.Success)
            {
                var hash = Helpers.Security.SHA1(hack.InputData);
                var tc = (from t in DbContext.TestCases where t.Hash == hash select t).FirstOrDefault();
                if (tc == null)
                {
                    tc = new Entity.TestCase
                    {
                        Input = hack.InputData,
                        Hash = Helpers.Security.SHA1(hack.InputData),
                        Type = Entity.TestCaseType.Custom,
                        ProblemID = hack.Status.ProblemID,
                        Output = hfb.Output
                    };
                    DbContext.TestCases.Add(tc);
                }
                DbContext.JudgeTasks.Add(new Entity.JudgeTask
                {
                    StatusID = hack.StatusID,
                    Result = hfb.JudgeResult,
                    TimeUsage = hfb.TimeUsage,
                    MemoryUsage = hfb.MemoryUsage,
                    Hint = hfb.Hint,
                    TestCaseID = tc.ID
                });
                hack.Status.Result = Entity.JudgeResult.Hacked;
                DbContext.SaveChanges();
                SignalR.CodeCombHub.context.Clients.Group(hack.Defender.Username).onHacked(new Models.View.HackResult(hack, true));
                SignalR.CodeCombHub.context.Clients.Group(hack.Hacker.Username).onHackFinished(new Models.View.HackResult(hack));

                SignalR.CodeCombHub.context.Clients.All.onStatusChanged(new Models.View.Status(hack.Status));
                if (DateTime.Now >= hack.Status.Problem.Contest.Begin)
                {
                    SignalR.CodeCombHub.context.Clients.All.onStandingsChanged(hack.Status.Problem.Contest.ID, new Models.View.Standing(hack.Defender, hack.Status.Problem.Contest));
                    SignalR.CodeCombHub.context.Clients.All.onStandingsChanged(hack.Status.Problem.Contest.ID, new Models.View.Standing(hack.Hacker, hack.Status.Problem.Contest));
                }
            }
            else
            {
                SignalR.CodeCombHub.context.Clients.Group(hack.Hacker.Username).onHackFinished(new Models.View.HackResult(hack));
                if (DateTime.Now >= hack.Status.Problem.Contest.Begin && DateTime.Now < hack.Status.Problem.Contest.End)
                {
                    SignalR.CodeCombHub.context.Clients.All.onStandingsChanged(hack.Status.Problem.Contest.ID, new Models.View.Standing(hack.Hacker, hack.Status.Problem.Contest));
                }
            }
        }