Code Monkey home page Code Monkey logo

codespace's Introduction

๐ŸŽ‰ Codespaces

I was curious about how Codeforces worked, so created my own version! Welcome to Codespacesโ€”your social problem solving playground! ๐Ÿง‘โ€๐Ÿ’ปโœจ

aww

โœจ Features

  • โœ๏ธ Create Your Own Problems: Feeling creative? You can also create and manage your own coding problems! ๐ŸŽจ

  • ๐Ÿ” Codeforces Problem Scraping: Users can just enter a Codeforces problem link and watch the magic happen! The problem statement and test cases are automatically parsed and shared across all the users int the room. ๐ŸŒŸ

  • ๐Ÿš€ Code Evaluation: Evaluate codes submissions on the fly in a safe and secure sandboxed environment!

  • ๐Ÿ‘ฉ๐Ÿ‘จโ€๐Ÿ’ป Collaborative Coding: Team up with your friends and solve problems together in real-time with up to 10 users.

  • ๐ŸŽค Group Voice Chat: Integrated group voice chat for seamless collaboration using WebRTC.

  • ๐Ÿ“ฆ Problem Packages: Easily store and manage problem packages.

  • ๐Ÿงฎ๐Ÿ–ผ๏ธ Math and Image Rendering: Supports rendering math equations and images (or any media) in problem statements.

  • โšก Caching: Lightning-fast caching for problem statements and packages using Redis.

Can be used to host contests completely locally(for the most part)

๐ŸŽฌ Demo

Check out the demo here. ๐Ÿฟ

Hosting is down at the moment cause I lost access to my EC2 instance :(

๐Ÿ—๏ธ System Architecture

Architecture

Here's a high-level architecture of our cute (not so)little monolithic system. ๐Ÿฅฐ

๐Ÿ“ Submission Handling

  • Backend pulls the test data from database if its not cached

  • Every new submission triggers a new Docker container (Alpine Linux).

  • The container:

    • Compiles and runs the code in a sandbox in a secure way
    • Sends a verdict (AC/WA/CE/TLE/RTE).
    • Programs have a 2-second time limit and 256mb memory limit; if they don't finish in time, they get a TLE (Time Limit Exceeded) verdict. โฐ
    • Alongside the time and memory limit inside the container, the container has no access to the internet and has a limit of atmost 100 processes (to protect against fork bombs)

Container architecture

Container

This is what's inside the container

High level overview of what happens in each container:

  • Compile the code and put it in a sandboxed environment with the input data.

  • Change the password for the root user to something (truly)random so that untrusted code can't access the root userspace.

  • Lock down the enviroment by setting permissions such that the code can't access anything outside of it,the code only has access to the input file.

  • We run the code with a time limit of 2 seconds.

  • After and if the code execution is complete, the generated output is then sent out to the root user for verification, if something goes wrong with the execution, then the sandbox gets terminated instantly and root user returns the verdict.

  • If the code is executed successfully, the root user sends out the verdict by comparing the program output and expected output

Check this for implementation

Note:

You are welcome and encouraged to try to hack this system as it would help me improve upon it in the next iteration and it wouldn't have reached this stage if people hadn't exploited it in the past.

If you find any bugs/vulnerabilites please open an issue and let me know as it's quite unpredictable what people would do to a system when they get their hands on it,especially when they can run codes on it.

Wait a second, why did you make this again ?

idk, why do you do things ?, I like it, its just curiosity, I have no endgoal with this, I just like things to be as elegant and as fast as I can make them and this give me a way try novel things.

๐Ÿ”ง Implementation Details

  • ๐ŸŽค Group Voice Chat: Implemented using WebRTC to reduce server load. Chat with your whole group while you code!

  • ๐Ÿณ Docker API: Used to spawn containers on the fly for code evaluation.

  • ๐Ÿค Collaborative Features: Utilized WebSockets (Socket.IO) for real-time collaboration.

  • โ› Web scraping: Used BeautifulSoup with a custom scraper to scrape problems from codeforces.

  • ๐Ÿ–๏ธ Sandboxing: Implemented sandboxing inside the docker container by setting up non privileged users to prevent untrusted codes from accessing the root userspace.

  • ๐Ÿ“ฆ Problem Packages: Stored in MongoDB for easy management.

    • Includes:
      • Problem Statements (Interpreted)
      • Sample test data
      • Main test data (for evaluating submissions)
      • Expected outputs for the main tests
  • โšก Redis Caching: Cached problem packages and test data to limit database hits.

  • ๐Ÿงฎ Math Rendering: Used KaTeX for rendering math equations.

  • ๐Ÿšซ Rate Limiting: Added to each compilation API call to prevent abuse.

๐ŸŒ Supported Languages

  • Only supports C++ for now. ๐Ÿ–‹๏ธ

Current objectives

  • Making submission system more scalable and efficient with better auto scaling(thinking about making this thing its own microservice, would take more time but would be very kawaii)

  • Making the system multi-node from one node, I'll also introduce a new architecture which ofc I won't be renting a cluster or whatever(I'm broke), instead let's try the hacky way, since ec2 instances are available in free tier, I'll just use those as nodes, and kubernetes would just scale shit up and down as needed, nice

  • Fix a security issue which got introduced in the last update(ignorant of me to think that compiling untrusted code is safe)

๐Ÿš€ Usage

To use this project, follow these steps: (Ensure that Redis and Docker are installed before following these steps)

  1. Clone the repository:

    git clone https://github.com/nubskr/codespace.git
    cd codespace
  2. Install dependencies:

    npm install
  3. Pull Container images and config docker and setup environment variables

    sudo ./setup.sh
    
    cd ./server
    vim .env
    
    MONGODB_URI=''
    AWS_ACCESS_KEY_ID=''
    AWS_SECRET_ACCESS_KEY=''
    AWS_REGION=''
    S3_BUCKET_NAME=''
    CONCURRENT_SCRAPING_WORKERS=''
    CONCURRENT_SUBMISSION_WORKERS=''
  4. Start the servers:

    npm start
    cd ..
    cd ./frontend
    npm start
  5. Open the application in your browser.

Prerequisites

  • You need Docker and Redis installed in the backend to use the submission functionality.
  • The ./Docker directory contains the Docker image I made to evaluate the submissions.

codespace's People

Contributors

dependabot[bot] avatar nubskr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

sirjo41 hazu-7

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.