Giter VIP home page Giter VIP logo

glimpse's Introduction

Glimpse

Glimpse is a microservice that allows for remote code execution, written in Python.

Current list of supported languages:

  • Java
  • C++
  • Python
  • C
  • JavaScript
  • GoLang

Getting Started

Glimpse is not yet deployed, but will be soon. In the meantime, you can spin it up yourself locally.

  1. Run the following setup script: chmod +x setup.sh && ./setup.sh. This will install all necessary Python dependencies and build the Docker image.
  2. To run the FastAPI app, use the following command: python3 -m uvicorn api:app --host 0.0.0.0 --port 8000 --workers <int:num_workers>.
    • num_workers will decide the number of web workers that are running the API in parallel. When developing locally, no more than 2 are needed.
  3. Send a request to the endpoint like so:
fetch('<endpoint_url>/run-code-pool', {
    method: 'POST',
    headers: {
        "Content-Type": "application/json"
    },
    body: JSON.stringify({
        "language": "py", // also accepts "java", "js", "cpp", "c", "go"
        "code": "print(\"Hello, world!\")",
        "input": ""  // optional parameter
    })
}).then((resp) => resp.json())
.then((data) => console.log(data));

Or use our friendly neighborhood cURL CLI tool:

curl -X POST '<endpoint_url>/run-code-pool' \
    -H 'Content-Type: application/json' \
    -d '{"language": "py", "code": "print(\"Hello, world!\")", "input": ""}'

The output from the endpoint will be as follows:

{
    "output": "Hello, world!\n",
    "error": "",
    "language": "py",
    "info": "python3 --version"
}

How It Works

Glimpse allows code to be executed via a web request. You can run code and display code results in environment like Node, in a React application, etc. It works by sending instructions to a Docker container about how to compile and/or execute code coming in, carrying out those instructions, and saving the result before replacing the code-contaminated container.

v2.

The current iteration of Glimpse recognizes that untrusted code should not be ran outside of a containerized environment. What's more, code submissions should run in their own container.

We now maintain a scalable container "pool" of pre-warmed containers that are all able to execute code in any of the supported languages. When a code submission is submitted via our endpoint, a few things happen:

  • A pre-warmed container is selected from the pool, and we create a record of that submission in the container.
  • That code file is executed in the container, and the output is recorded.
  • That container is discarded, and a new one takes its place.
v1.

The first iteration of Glimpse ran in a single Docker container, which has all the necessary software installed to compile and run files from supported languages.

When a request was made to Lantern's run_code endpoint, the corresponding method run_code was called, and the following steps occurred in order:

  1. A submission file was generated with a unique UUID and the proper extension.
  2. A set of compilation and execution command line arguments were generated, instructing the container how to execute the code parameter that was passed in.
  3. The code was compiled (if a compilation step is necessary) and executed, and the resulting std_out and std_err streams were captured and returned as a Response.

Glimpse takes advantage of FastAPI's support for async Python to compile and execute code processing requests in "parallel" (thanks, GIL).

API Docs

/run-code-pool

Runs untrusted code using a Docker Container pool and returns the output.

Input:

{
    "language": str,  // py, js, java, cpp, c, go
    "code": str,  // Stringified code block
    "input": str,  // User input (for print() statements)
}

Output:

{
    "output": str, // Stringified output of the code snippet
    "error": str, // Any errors that occured during program compilation or execution
    "language": str, // same as input
    "info": str, // Command to get version information from server
}

Limitations

Glimpse is not meant to be used to run I/O operations in most languages. If one were to try to run the command input() with Glimpse, it would eventually timeout. All I/O interactions must be spoofed on the client-side, and passed to the endpoint using the input body param.

glimpse's People

Contributors

daviskeene avatar

Watchers

 avatar

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.