Commit 58203317 authored by nanahira's avatar nanahira

first

parent c00c537a
Pipeline #13866 passed with stages
in 3 minutes and 59 seconds
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
/data
/output
/config.yaml
.git*
Dockerfile
.dockerignore
/tests
webpack.config.js
dist/*
build/*
*.js
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
/data
/output
/config.yaml
stages:
- install
- build
- deploy
variables:
GIT_DEPTH: "1"
npm_ci:
stage: install
tags:
- linux
script:
- npm ci
artifacts:
paths:
- node_modules
.build_base:
stage: build
tags:
- linux
dependencies:
- npm_ci
build:
extends: .build_base
script: npm run build
artifacts:
paths:
- dist/
upload_to_minio:
stage: deploy
dependencies:
- build
tags:
- linux
script:
- aws s3 --endpoint=https://minio.momobako.com:9000 sync --delete dist/ s3://nanahira/path
only:
- master
stages:
- build
- combine
- deploy
variables:
GIT_DEPTH: "1"
CONTAINER_TEST_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
CONTAINER_TEST_ARM_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-arm
CONTAINER_TEST_X86_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-x86
CONTAINER_RELEASE_IMAGE: $CI_REGISTRY_IMAGE:latest
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
build-x86:
stage: build
tags:
- docker
script:
- TARGET_IMAGE=$CONTAINER_TEST_X86_IMAGE
- docker build --pull -t $TARGET_IMAGE .
- docker push $TARGET_IMAGE
build-arm:
stage: build
tags:
- docker-arm
script:
- TARGET_IMAGE=$CONTAINER_TEST_ARM_IMAGE
- docker build --pull -t $TARGET_IMAGE .
- docker push $TARGET_IMAGE
combine:
stage: combine
tags:
- docker
script:
- TARGET_IMAGE=$CONTAINER_TEST_IMAGE
- SOURCE_IMAGE_2=$CONTAINER_TEST_ARM_IMAGE
- SOURCE_IMAGE_1=$CONTAINER_TEST_X86_IMAGE
- docker pull $SOURCE_IMAGE_1
- docker pull $SOURCE_IMAGE_2
- docker manifest create $TARGET_IMAGE --amend $SOURCE_IMAGE_1 --amend
$SOURCE_IMAGE_2
- docker manifest push $TARGET_IMAGE
deploy_latest:
stage: deploy
tags:
- docker
script:
- TARGET_IMAGE=$CONTAINER_RELEASE_IMAGE
- SOURCE_IMAGE=$CONTAINER_TEST_IMAGE
- docker pull $SOURCE_IMAGE
- docker tag $SOURCE_IMAGE $TARGET_IMAGE
- docker push $TARGET_IMAGE
only:
- master
deploy_tag:
stage: deploy
tags:
- docker
script:
- TARGET_IMAGE=$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
- SOURCE_IMAGE=$CONTAINER_TEST_IMAGE
- docker pull $SOURCE_IMAGE
- docker tag $SOURCE_IMAGE $TARGET_IMAGE
- docker push $TARGET_IMAGE
only:
- tags
/install-npm.sh
.git*
/data
/output
/config.yaml
.idea
.dockerignore
Dockerfile
/src
/coverage
/tests
/dist/tests
{
"singleQuote": true,
"trailingComma": "all"
}
\ No newline at end of file
FROM node:lts-bullseye-slim as base
LABEL Author="Nanahira <nanahira@momobako.com>"
RUN apt update && apt -y install python3 build-essential && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /var/log/*
WORKDIR /usr/src/app
COPY ./package*.json ./
FROM base as builder
RUN npm ci && npm cache clean --force
COPY . ./
RUN npm run build
FROM base
ENV NODE_ENV production
RUN npm ci && npm cache clean --force
COPY --from=builder /usr/src/app/dist ./dist
CMD [ "npm", "start" ]
The MIT License (MIT)
Copyright (c) 2021 Nanahira
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# Minecraft Stub
Minecraft stub server
\ No newline at end of file
Minecraft stub server
## Environment
- `PORT` - Minecraft server port
- `MC_VERSION` - Minecraft version to use
- `MC_MOTD` - Minecraft server MOTD
- `MC_MAX` - Minecraft server max players
- `MC_CURRENT_MIN` - Minecraft server current min players
- `MC_CURRENT_MAX` - Minecraft server current max players
- `MC_CURRENT_PLAYERS` - Minecraft server current players
- `MC_REFRESH_COUNT` - Minecraft server ping refresh period count
- `MC_KICK_MESSAGE` - Minecraft server kick message
## Path
- `/usr/src/app/icon.png` - Minecraft server icon
icon.png

2.81 KB

import net from 'net';
import { createLogger } from 'bunyan';
import { parsePacket, encode } from './src/decode';
import { PingGenerator } from './src/ping';
const server = net.createServer();
const log = createLogger({ name: 'minecraft-stub' });
const generator = new PingGenerator();
server.on('error', (err) => {
log.error('Server error', err);
});
server.on('connection', (socket) => {
let buffer = Buffer.alloc(0);
socket.on('data', async (data) => {
buffer = Buffer.concat([buffer, data]);
const baseBuffer = buffer.slice(buffer.indexOf(0x00) + 1, buffer.length);
const baseHostname =
parsePacket.hostname(baseBuffer).length <= 10
? parsePacket.hostname(baseBuffer).length + 10
: parsePacket.hostname(baseBuffer).length;
if (buffer.length < baseHostname && buffer.length > 10) return;
try {
const player = parsePacket.player(buffer, baseBuffer);
const protocol = parsePacket.protocol(baseBuffer);
const hostname = parsePacket.hostname(baseBuffer);
const port = parsePacket.port(baseBuffer);
const state = parsePacket.state(baseBuffer);
// Perhaps someone can change the logging message? :/
switch (state) {
case 1:
/*
Response handshake with server status
https://wiki.vg/Server_List_Ping#Handshake
*/
log.info(
`Handshake is initiated with hostname ${hostname} and port ${port}`,
);
const config = await generator.generate(protocol);
socket.write(encode(config));
break;
case 2:
/*
Response when client tries to connect
https://wiki.vg/Protocol#Disconnect_.28login.29
*/
log.info(`${player} is trying to connect to ${hostname}:${port}`);
socket.write(encode(generator.kickMessage(player)));
break;
default:
log.info(`Responding to ping`);
socket.write(buffer);
break;
}
} catch (error) {
log.error(error);
} finally {
buffer = Buffer.alloc(0);
}
});
});
const port = parseInt(process.env.PORT || '25565', 10);
server.listen(port, null, () => {
log.info(`Fake Minecraft Server Listening in ${port}`);
});
#!/bin/bash
npm install --save-dev \
@types/node \
typescript \
'@typescript-eslint/eslint-plugin@^4.28.2' \
'@typescript-eslint/parser@^4.28.2 '\
'eslint@^7.30.0' \
'eslint-config-prettier@^8.3.0' \
'eslint-plugin-prettier@^3.4.0' \
prettier \
jest \
@types/jest \
ts-jest \
rimraf
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"name": "myproject",
"description": "myproject-desc",
"version": "1.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"lint": "eslint --fix .",
"build": "rimraf dist && tsc",
"test": "jest --passWithNoTests",
"start": "node dist/index.js | bunyan"
},
"repository": {
"type": "git",
"url": "https://code.mycard.moe/3rdeye/myproject.git"
},
"author": "Nanahira <nanahira@momobako.com>",
"license": "MIT",
"keywords": [],
"bugs": {
"url": "https://code.mycard.moe/3rdeye/myproject/issues"
},
"homepage": "https://code.mycard.moe/3rdeye/myproject",
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "tests",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
},
"devDependencies": {
"@types/bunyan": "^1.8.8",
"@types/jest": "^28.1.1",
"@types/node": "^18.0.0",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^3.4.1",
"jest": "^28.1.1",
"prettier": "^2.7.1",
"rimraf": "^3.0.2",
"ts-jest": "^28.0.5",
"typescript": "^4.7.3"
},
"dependencies": {
"bunyan": "^1.8.15",
"make-random": "^3.0.2"
}
}
const leb = {
encode: (value) => {
value |= 0;
const result = [];
while (true) {
const byte = value & 0x7f;
value >>= 7;
if (
(value === 0 && (byte & 0x40) === 0) ||
(value === -1 && (byte & 0x40) !== 0)
) {
result.push(byte);
return Buffer.from(result);
}
result.push(byte | 0x80);
}
},
decode: (input) => {
let result = 0;
let shift = 0;
while (true) {
const byte = input.shift();
result |= (byte & 0x7f) << shift;
shift += 7;
if ((0x80 & byte) === 0) {
if (shift < 32 && (byte & 0x40) !== 0) {
return result | (~0 << shift);
}
return result;
}
}
},
};
export const parsePacket = {
hostname: (buffer: Buffer) => {
const val1 = 3;
const val2 =
buffer.indexOf(0x00) == -1 ? buffer.length - 3 : buffer.indexOf(0x00) - 4;
const result = buffer.slice(val1, val2).toString('utf8');
return result;
},
port: (buffer: Buffer) => {
const val1 =
buffer.indexOf(0x00) == -1 ? buffer.length - 3 : buffer.indexOf(0x00) - 4;
const val2 =
buffer.indexOf(0x00) == -1 ? buffer.length - 1 : buffer.indexOf(0x00) - 2;
const result = buffer.slice(val1, val2).readUInt16BE();
return result;
},
protocol: (buffer: Buffer) => {
return leb.decode(Array.prototype.slice.call(buffer.slice(0, 2)));
},
player: (rawBuffer: Buffer, buffer: Buffer) => {
if (buffer.indexOf(0x00) == -1) return '';
const val1 = buffer.indexOf(0x00) + 2;
const val2 = rawBuffer.length;
const result = buffer.slice(val1, val2).toString('utf8');
return result;
},
state: (buffer) => {
const val1 =
buffer.indexOf(0x00) == -1 ? buffer.length - 1 : buffer.indexOf(0x00) - 2;
const val2 =
buffer.indexOf(0x00) == -1 ? buffer.length : buffer.indexOf(0x00) - 1;
const result = buffer.slice(val1, val2).readInt8();
return result;
},
};
export function encode(data: any) {
data = Buffer.from(JSON.stringify(data), 'utf-8');
return Buffer.concat([
leb.encode(data.byteLength + leb.encode(data.byteLength).byteLength + 1),
Buffer.alloc(1),
leb.encode(data.byteLength),
data,
]);
}
import fs from 'fs';
import { randomEnglish, randomUUID } from 'make-random';
export interface Ping {
version: Version;
players: Players;
description: Description;
favicon: string;
}
export interface Description {
text: string;
}
export interface Players {
max: number;
online: number;
sample: Sample[];
}
export interface Sample {
name: string;
id: string;
}
export interface Version {
name: string;
protocol: number;
}
export class PingGenerator {
private icon: string;
private players: Players;
private max = 20;
private currentMax = 10;
private currentMin = 5;
private refreshPeriod = 100;
private pointer = 0;
async refreshPlayers() {
this.pointer = 0;
this.players = {
max: this.max,
online:
Math.floor(Math.random() * (this.currentMax - this.currentMin + 1)) +
this.currentMin,
sample: [
{
name: await randomEnglish(1),
id: await randomUUID(),
},
],
};
}
async getPlayers() {
if (!this.players || ++this.pointer >= this.refreshPeriod) {
await this.refreshPlayers();
}
return this.players;
}
async generate(protocol: number): Promise<Ping> {
return {
version: {
name: process.env.MC_VERSION || '1.12.2',
protocol,
},
players: await this.getPlayers(),
description: {
text: process.env.MC_MOTD || 'A Minecraft server',
},
favicon: this.icon ? `data:image/png;base64,${this.icon}` : undefined,
};
}
load;
constructor() {
if (process.env.MC_MAX) {
this.max = parseInt(process.env.MC_MAX);
}
if (process.env.MC_CURRENT_MAX) {
this.currentMax = parseInt(process.env.MC_CURRENT_MAX);
}
if (process.env.MC_CURRENT_MIN) {
this.currentMin = parseInt(process.env.MC_CURRENT_MIN);
}
if (process.env.MC_REFRESH_COUNT) {
this.refreshPeriod = parseInt(process.env.MC_REFRESH_COUNT);
}
fs.promises
.readFile('./icon.png', 'base64')
.then((data) => {
this.icon = data;
})
.catch();
}
kickMessage(name: string) {
const message =
process.env.MC_KICK_MESSAGE ||
'You ({name}) are not allowed to join this server.';
return message.replaceAll('{name}', name);
}
}
describe('Sample test.', () => {
it('should pass', () => {
expect(true).toBe(true);
});
});
{
"compilerOptions": {
"outDir": "dist",
"module": "commonjs",
"target": "es2021",
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"declaration": true,
"sourceMap": true
},
"compileOnSave": true,
"allowJs": true,
"include": [
"*.ts",
"src/**/*.ts",
"test/**/*.ts",
"tests/**/*.ts"
]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment