Skip to content

A simple online compiler, supports c, c++, java, golang, python and js

License

Notifications You must be signed in to change notification settings

CLOUDERHEM/OnlineCompiler

Repository files navigation

在线编译器

一款简单的在线编译器后台, 支持c, c++, java, python, golangjavascript, 后续会支持更多语言

原理

  1. 使用JNI调用C代码
  2. 通过Linux内核安全机制 seccomp限制syscall, 例如禁止 clone, fork, kill, socket, vfork 等系统调用

安装

建议在docker中使用, 非docker环境中使用会存在syscall被限制导致程序不能正常被执行(此时需要将被限制的syscall添加到seccomp_rules白名单中)

  1. 执行sandbox/src/main/c中的脚本, 生成sandbox.so文件到/usr/lib/sandbox/sandbox.so
  2. 后端使用springboot , 通过启动类启动

接口示例

Run 提交代码

URL: /just/run

Method: POST

Args:

  • language : c = 1, c++ = 2, java = 3, python = 4, javascript = 5, golang = 6
  • code : your code
  • input : 运行输入

Request示例:

求 1 ~ 100 的和

{
    "language": 2,
    "code": "#include <iostream>\nusing namespace std;\nint main() {\n    int a, b;\n    cin >> a >> b;\n    int res = 0;\n    while (a <= b) {\n        res += a;\n        a++;\n    }\n    cout << res << endl;\n    return 0;\n}",
    "input": "1 100"
}

Response示例:

{
    "code": 200,
    "msg": "等待执行结果",
    "data": {
        "id": "7ce2e89a-5a04-4810-aadb-d6e957c9aa13",
        "status": null,
        "result": null,
        "timestamp": 1656135776537
    }
}

Query 查询执行结果

URL: /just/query

Method: GET

Args:

  • id 任务的id

Request示例:

curl --location --request GET 127.0.0.1:8080/just/query?id=7ce2e89a-5a04-4810-aadb-d6e957c9aa13

Response示例:

  • data.status : 没有任务 = -1, 运行结束 = 0, 仍在运行 = 1
  • data.result.status : 运行成功 = 0, 编译错误 = 1, 运行出错 = 2
  • data.result.output : 运行的输出
{
    "code": 200,
    "msg": "运行成功 ",
    "data": {
        "id": "7ce2e89a-5a04-4810-aadb-d6e957c9aa13",
        "status": 0,
        "result": {
            "cpuTime": 0,
            "signal": 0,
            "memory": 1626112,
            "exitCode": 0,
            "result": 0,
            "error": 0,
            "realTime": 42,
            "status": 0,
            "output": "5050\n",
            "runMsg": null
        },
        "timestamp": 1656138213151
    }
}

附加

  • sandbox源于: QingdaoU/OnlineJudge
  • sandbox在某些系统上可能存在syscall 权限问题, 使用dmesg -T查看系统调用, 在sandbox中的c/src/rulessyscalls_whitelist中放行该syscall
  • 在某些机器或系统上memoryrealTime需求可能很大, 可以在compiler/config/language/configs 调整为适合大小

About

A simple online compiler, supports c, c++, java, golang, python and js

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published