Giter VIP home page Giter VIP logo

remote-code-runner's Introduction

Remote Code Runner

Remote Code Runner is a simple code service for running code on remote server side. Docker is used to execute untrusted code in a sandbox environment.

Install

Environment:

  • Ubuntu Linux 18.04
  • Docker 19.x
  • Python 3.8

MAKE SURE the login user has sudo privileges and does not require entering a password.

Install all required software:

$ sudo apt install git docker.io python3.8

Get source:

$ cd /srv
$ sudo git clone https://github.com/michaelliao/remote-code-runner.git

Generate all from source:

$ cd /srv/remote-code-runner
$ sudo python3.8 generate.py

Download required docker images by warm up script (this may take a long time):

$ cd /srv/remote-code-runner/bin
$ sudo sh warm-up-docker.sh

Start server:

$ cd /srv/remote-code-runner/bin
$ sudo start-runner.sh

Usage

Using simple HTTP JSON API:

$ curl http://server-ip:8080/run -H 'Content-Type: application/json' -d '{"language":"python","code":"import math\nprint(math.pi)"}'
{"error": false, "timeout": false, "truncated": false, "output": "3.141592653589793\n"}

API input:

  • language: language name, lowercase: java, python, ruby.
  • code: language code as JSON string: import math\nprint(math.pi)

API output:

  • timeout: boolean, is execution timeout.
  • error: boolean, is error output. e.g. compile failed or syntax error.
  • truncated: boolean, is output was truncated for too many characters.
  • output: string, the output of execution.

Execution

How code are executed on the remote server side:

  1. Http server runner.py got language name and code from API;
  2. Write code into a temperary directory like /tmp/remote-code-runner/1;
  3. Execute command like sudo docker run -t --rm -w /app -v /tmp/dir:/app <image-tag> python3 main.py;
  4. Write output into API response;
  5. Clean up temperary directory.

Limitation

  • Multiple files are not supported.
  • There is no way to read input from console. Any user input code will cause timeout.

Security

Remote code runner should only be served in private network. User identity check, rate limit must be done in application server or reverse proxy like Nginx.

Extension

How to add a new language:

  1. Add configuration in config.json:
{
    ...
    "languages": {
        ...
        "node": {
            "file": "main.js",
            "image": "node:13.12-slim",
            "command": "node main.js"
        }
    }
}

The key node is the language name.

  1. Make sure image is downloaded on local:
$ sudo docker run -t --rm node:13.12-slim ls
  1. Restart start-runner.sh.

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.