Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Giotino committed Jun 6, 2022
1 parent 7401e97 commit ab28bae
Show file tree
Hide file tree
Showing 47 changed files with 31,235 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/node_modules/

/build/
/.next/

/.env
/game/*

schema.gql
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/node_modules/

/build/
/.next/

/.env
/game/*

!/game/.gitkeep
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"singleQuote": true,
"printWidth": 100,
"tabWidth": 2,
"semi": true
}
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM node:16

WORKDIR /usr/src/app

ADD ./package.json .
ADD ./package-lock.json .

RUN npm i

RUN npx next telemetry disable

ADD . .
RUN npm run build

ENV NODE_ENV production
RUN npm prune --production

EXPOSE 3000

CMD [ "npm", "start" ]
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Less Destructive Farm

Look at the examples in `game-archive`, you should put your node module that export a submitter with the game configuration in the `game` folder.

The package.json and the other module files should be directly inside the game folder:
- game/package.json
- game/index.js
- game/...
29 changes: 29 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: '3'

services:
app:
build: .
depends_on:
- postgres
environment:
- DB_HOST=postgres
- DB_NAME=farm
- DB_USER=postgres
- DB_PASS=postgres
- DB_DIALECT=postgres
volumes:
- ./game:/usr/src/app/game
ports:
- '3000:3000'

postgres:
image: postgres:12
environment:
- POSTGRES_DB=farm
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
- pg_data:/var/lib/postgresql/data

volumes:
pg_data:
29 changes: 29 additions & 0 deletions game-archive/http/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const got = require('got');

const SYSTEM_IP = 'http://HTTP_FLAG_SUBMISSION_URL/flags';
const TEAM_TOKEN = '';
const TIMEOUT_MS = 5000;
const teams = require('./teams.json');

module.exports = {
flagFormat: '[A-Z0-9]{31}=',
submitInterval: 120,
teams,
submitFlags: async (flags, onSubmit) => {
const tot = flags.length;
const chunkSize = 20;
for (let i = 0; i<tot-chunkSize; i+=chunkSize) {
const answer = await got.put(SYSTEM_IP, {
headers: {
'X-Team-Token': TEAM_TOKEN
},
timeout: TIMEOUT_MS,
body: JSON.stringify(flags.slice(i, i+chunkSize))
}).json();

for (const a of answer) {
await onSubmit(a.flag, a.status ? 'ACCEPTED' : 'REJECTED', a.msg.split(']')[1]);
}
}
}
};
14 changes: 14 additions & 0 deletions game-archive/http/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "game",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "UNLICENSED",
"dependencies": {
"got": "^11.8.1"
}
}
85 changes: 85 additions & 0 deletions game-archive/tcp/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const { Socket } = require('net');

const SYSTEM_IP = 'TCP_FLAG_SUBMISSION_IP';
const SYSTEM_PORT = 31337;
const TIMEOUT_MS = 5000;
const teams = require('./teams.json');

//status: 'QUEUED' | 'SKIPPED' | 'ACCEPTED' | 'REJECTED'
const responseToStatus = (response) => {
response = response.toLowerCase();
if (response.startsWith('[ok]')) return 'ACCEPTED';
if (response.startsWith('[err]')) return 'REJECTED';
return 'QUEUED';
};

module.exports = {
flagFormat: '/^\w{31}=$/',
submitInterval: 120,
teams,
submitFlags: (flags, onSubmit) =>
new Promise(async (resolve, reject) => {
const socket = new Socket();

socket.setTimeout(TIMEOUT_MS);

let buffer = '';
const untilNewline = (chunk) => {
buffer += chunk;
if (buffer.includes('\n')) {
const rows = buffer.split(/\n/g);
buffer = rows.pop();

return rows;
}
return null;
};

let submitted = 0;

socket.on('connect', () => {
// Send all flag
(async () => {
for (const flag of flags) if (!socket.destroyed) socket.write(flag + '\n');
})();
});

// Work on responses
socket.on('data', async (chunk) => {
const rows = untilNewline(chunk);
if (rows) {
for (const row of rows) {
const response = row.trim();

if (response.toLowerCase().includes('unavailable')) {
socket.end();
break;
}

console.log(response);
const status = responseToStatus(response);
console.log(status);
await onSubmit(flags[submitted++], status, response); // await or the DB is going to explode
if (submitted == flags.length) socket.end();
}
}
});

socket.on('timeout', () => {
console.log('Socket timeout');
socket.end();
});

socket.on('error', (e) => {
console.log('Socket error', e);
socket.end();
reject();
});

socket.on('close', () => {
resolve();
});

socket.connect(SYSTEM_PORT, SYSTEM_IP, () => {});
}),
};
3 changes: 3 additions & 0 deletions game-archive/tcp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added game/.gitkeep
Empty file.
2 changes: 2 additions & 0 deletions next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
7 changes: 7 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"verbose": true,
"watch": ["src/*"],
"ignore": ["node_modules", ".next", "src/pages", "src/next"],
"ext": "js ts tsx json",
"exec": "ts-node --project tsconfig.server.json src/server.ts"
}
Loading

0 comments on commit ab28bae

Please sign in to comment.