Skip to content

Commit

Permalink
Fix containers
Browse files Browse the repository at this point in the history
  • Loading branch information
W2Wizard committed May 11, 2024
1 parent 5777292 commit 4ca7247
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 42 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,5 @@ dist
# Finder (MacOS) folder config
.DS_Store

build
build
dockerapi.d.ts
6 changes: 3 additions & 3 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

EXCEPTIONS: ANY ORGANIZATION REPRESENTING 42 (Central) IS NOT ALLOWED TO USE THIS
UNLESS EXPLICIT PERMISSION IS GIVEN! ANY CODE COPIED, USED, ... FROM BEFORE OR AFTER THIS LICENSE WILL BE MET WITH A LEGAL RESPONSE!

HOWEVER THE ENTITY KNOWN AS CODAM CODING COLLEGE IS ALLOWED TO USE THIS SOFTWARE.
UNLESS EXPLICIT PERMISSION IS GIVEN! ANY CODE COPIED, USED, ... FROM BEFORE OR AFTER THIS LICENSE
WITH THE EXCEPTIONS OF CODAM CODING COLLEGE AND ACTIVE STUDENTS (NONE STAFF) OF THE 42 NETWORK.
INVIDIVUAL 42 CAMPUSES WITH THE EXCEPTION OF PARIS ARE ALLOWED TO USE THIS SOFTWARE
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ curl -XPOST -H "Content-type: application/json" -d '{
"branch": "master",
"commit": "67dc80a"
}
}' 'http://localhost:3001/evaluate/git/libc'
}' 'http://localhost:3001/evaluate/git/lib'
```

```bash
# For single files
# Note: This example will not work as the code is invalid
# Fork bombs won't work on the server
curl -XPOST -H "Content-type: application/json" -d '{
"data": {
"args": [],
Expand Down
Binary file modified bun.lockb
Binary file not shown.
6 changes: 3 additions & 3 deletions docker/git/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ RUN apt-get update && apt-get install -y \
COPY start.sh /usr/local/bin/init.sh
RUN chmod 755 /usr/local/bin/init.sh

RUN addgroup --gid 1337 nxtdmy
RUN adduser --shell /bin/bash runner && adduser runner nxtdmy
#RUN addgroup --gid 1337 nxtdmy
#RUN adduser --shell /bin/bash runner && adduser runner nxtdmy

USER runner
USER bun
ENTRYPOINT [ "init.sh" ]
10 changes: 6 additions & 4 deletions docker/single/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ RUN apt-get update && apt-get install -y \
build-essential \
&& rm -rf /var/lib/apt/lists/*

RUN useradd runner --shell /bin/bash --create-home

COPY start.sh .
COPY entry.sh /usr/local/bin/entry.sh
ENTRYPOINT [ "entry.sh" ]
COPY ./start.sh .
COPY ./entry.sh /usr/local/bin/entry.sh
RUN chmod +x /usr/local/bin/entry.sh

#USER bun
ENTRYPOINT [ "/usr/local/bin/entry.sh" ]
4 changes: 2 additions & 2 deletions docker/single/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -e
ID=$(xxd -l 16 -ps /dev/urandom | tr -d " \n")
Dir="/tmp/$ID"
File="$dir/user.c"
Home="/home/runner/"
Home="/home/bun/"

# Build
#==============================================================================
Expand All @@ -24,4 +24,4 @@ IFS=';' read -ra ARGS <<< "$CODE_ARGS"
#==============================================================================

echo "[+] Running file..."
su - runner -s /bin/bash -c "pwd; ls -l; ./a.out ${ARGS[@]}"
su - bun -s /bin/bash -c "pwd; ls -l; ./a.out ${ARGS[@]}"
2 changes: 1 addition & 1 deletion projects/balls/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
ID=$(xxd -l 16 -ps /dev/urandom | tr -d " \n")
ProjectDIR="/tmp/$ID/project"
ObjectsDIR="/tmp/$ID/objects"
Home="/home/runner/"
Home="/home/bun/"

# Functions
#==============================================================================
Expand Down
67 changes: 40 additions & 27 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@
// See README in the root of the project for license details.
// ============================================================================

import Container from './docker/container'
import Git from './git'
import { AutoRouter, json, type IRequest, type ResponseHandler, error, status, StatusError } from 'itty-router'
import type { FileBody, GitBody } from './types'
import { $ } from 'bun'
import Single from './single'
import Git from "./git";
import {
AutoRouter,
json,
type IRequest,
type ResponseHandler,
error,
StatusError,
} from "itty-router";
import type { FileBody, GitBody } from "./types";
import Single from "./single";

// Middleware
// ============================================================================

const withHeaders: ResponseHandler = (response: IRequest) => {
response.headers.set('X-Server', Bun.env.SERVER ?? 'robopeer')
response.headers.set('X-Powered-By', 'itty-router')
response.headers.set('X-Runtime', `Bun ${Bun.version}`)
}
response.headers.set("X-Server", Bun.env.SERVER ?? "robopeer");
response.headers.set("X-Powered-By", "itty-router");
response.headers.set("X-Runtime", `Bun ${Bun.version}`);
};

// ============================================================================

Expand All @@ -26,27 +31,35 @@ const router = AutoRouter({
finally: [withHeaders],
format: json,
catch: error,
})
});

router.post('/evaluate/git/:project', async (req) => {
let body: GitBody = await req.json()
.catch(() => { throw new StatusError(400, 'Invalid JSON body.') })
router.post("/evaluate/git/:project", async (req) => {
let body: GitBody = await req
.json()
.catch(() => {
throw new StatusError(400, "Invalid JSON body.");
})
.then((data) => data);

if (!body.data.branch || !body.data.commit || !body.data.repo) {
throw new StatusError(400, 'Invalid JSON body.')
throw new StatusError(400, "Invalid JSON body.");
}

if (!await Bun.file(`./projects/${req.params.project}/index.test.ts`).exists()) {
throw new StatusError(404, 'Project not found.')
if (
!(await Bun.file(`./projects/${req.params.project}/index.test.ts`).exists())
) {
throw new StatusError(404, "Project not found.");
}

return await Git.run(req.params.project, body)
})
return await Git.run(req.params.project, body);
});

router.post('/evaluate/code', async (req) => {
let body: FileBody = await req.json()
.catch(() => { throw new StatusError(400, 'Invalid JSON body.') })
router.post("/evaluate/code", async (req) => {
let body: FileBody = await req
.json()
.catch(() => {
throw new StatusError(400, "Invalid JSON body.");
})
.then((data) => data);

if (
Expand All @@ -55,14 +68,14 @@ router.post('/evaluate/code', async (req) => {
!body.data.flags ||
!body.data.lang
) {
throw new StatusError(400, 'Invalid JSON body.')
throw new StatusError(400, "Invalid JSON body.");
}

return await Single.run(body)
})
return await Single.run(body);
});

console.log(`Running: https://localhost:${Bun.env.PORT ?? 8080}`);
console.log(`Running: http://localhost:${Bun.env.PORT ?? 8080}`);

// ============================================================================

export default router
export default router;

0 comments on commit 4ca7247

Please sign in to comment.