Skip to content

Commit

Permalink
Merge pull request #17 from Tarnadas/dev
Browse files Browse the repository at this point in the history
2.2.0
  • Loading branch information
Mario Reder authored Jan 28, 2019
2 parents d3ed431 + 044ecef commit 6a387fc
Show file tree
Hide file tree
Showing 13 changed files with 757 additions and 75 deletions.
32 changes: 28 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
language: node_js

node_js:
- "8.9.4"
- 8.15.0

env:
- CXX=g++-4.8
global:
- CXX=g++-4.8
- PACKAGE_VERSION="2.2.0"

addons:
apt:
Expand All @@ -17,6 +18,29 @@ jobs:
include:
- stage: Test
script: jest --coverage --coverageReporters=text-lcov | ./node_modules/coveralls/bin/coveralls.js
name: "Unit Tests"
- script: yarn lint
name: "Lint"
- stage: Build
script: yarn build

name: "Build"
- stage: Deploy
script: yarn compile
name: "Compile & Deploy"
before_deploy:
- cd compile
- tar czf net64plus-server_$(echo $PACKAGE_VERSION)_win32-x64.tar.gz net64plus-server_$(echo $PACKAGE_VERSION)_win32-x64.exe
- tar czf net64plus-server_$(echo $PACKAGE_VERSION)_linux-x64.tar.gz net64plus-server_$(echo $PACKAGE_VERSION)_linux-x64
deploy:
provider: releases
api_key:
secure: vV+znDhyrpJzGHr8D7GwzYvq36elrDzDYIOF8b9hjjRIuQzLbI+SD7YieQBh6079EHM/CNquBlIBZUba9qI6g+XE6vHQn02WcU6KprE1u5XHMntPqpsoHfLzEkd5lAiLQ98izHKaaLL6hMXzwMuwkt1anI0kWbO6Y4/vWPGZw3xlyR+LJCj+SsDq4QeX1i1YMpTlBaDPRae+tDHckOX102n7KkeNEzwo9X4T7Tctr1dbQDeVuupv7CyMDcaB437F9X0yWRdieA95BcAzfw/iUqghJnDBhXjuTDgWa5YoMLmpvM9Un1AOSwEZgGTram4wc61fMVASIhWfu9QHvrSnEQd7t9XhdJZ+uMAwUdrLB7vC1BaeL4i38E+QnUWuN9xGnZCmpioy3ed9+WqKRh9cem/kXevuKwxDcc7WVxu+QV2pK7Zq2yRXi9N4m5xpmaGOkCW56Z4Iu5uSXDA+OxWX6w/r4B1PN+L0Ju+zLcoaragennA2azILZo4R9EEAXZN8Wh44STbnH7bQvsARtveIETW+v/HAqWTGfBFHXDT3Wy6iwy+TQSAoEKEQAKMcqin6bCWpe9iQM0ioVRcshxSzkt2ZnOlu4lX2kwXBVY/L9hEcRhkz1q9cjUY0CFNWJEdxRZXCwfpWD/ys6hfTFPuTIaNI7iOt59FvFexaoGjOO+c=
file:
- net64plus-server_$(echo $PACKAGE_VERSION)_win32-x64.tar.gz
- net64plus-server_$(echo $PACKAGE_VERSION)_linux-x64.tar.gz
skip_cleanup: true
draft: true
name: "Net64+ Server $(echo $PACKAGE_VERSION)"
on:
repo: Tarnadas/net64plus-server
branch: master
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
3 changes: 2 additions & 1 deletion build.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const nexe = require('nexe')
const fs = require('fs')
const path = require('path')
const pjson = require('./package.json')

const supportedPlatforms = [
[ 'win32', 'windows', 'x64' ],
Expand Down Expand Up @@ -28,7 +29,7 @@ function build (platform, platformName, arch) {

nexe.compile({
input: buildPath,
output: path.join('compile', `net64plus-server_${platform}-${arch}`),
output: path.join('compile', `net64plus-server_${pjson.version}_${platform}-${arch}`),
target: `${platformName}-${arch}-8.15.0`,
native: {
uws: {
Expand Down
16 changes: 16 additions & 0 deletions changelog-gen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const standardChangelog = require('standard-changelog')
const pjson = require('./package.json')
const fs = require('fs')
const path = require('path')

const changelogPath = path.join(__dirname, 'changelog')
if (!fs.existsSync(changelogPath)) {
fs.mkdirSync(changelogPath)
}

const changelogFilePath = path.join(changelogPath, `${pjson.version}.md`)
const fileStream = fs.createWriteStream(changelogFilePath, {
encoding: 'utf8'
})
standardChangelog()
.pipe(fileStream)
9 changes: 9 additions & 0 deletions changelog/2.2.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# [2.2.0](https://github.com/Tarnadas/net64plus-server/compare/2.1.1...2.2.0) (2019-01-27)


### Features

* automatic port checking and command args ([f0afc2e](https://github.com/Tarnadas/net64plus-server/commit/f0afc2e))



8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "net64plus-server",
"version": "2.1.2",
"version": "2.2.0",
"compatVersion": "1.0",
"description": "Net64+ Dedicated Server",
"main": "dist/index.js",
Expand All @@ -11,7 +11,8 @@
"build:compile": "webpack --env=compile",
"compile": "npm run build:compile && node ./build",
"lint": "eslint src/ --ext ts",
"test": "jest"
"test": "jest",
"changelog": "node ./changelog-gen.js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -54,6 +55,7 @@
]
},
"dependencies": {
"argparse": "^1.0.10",
"axios": "^0.17.1",
"escape-html": "^1.0.3",
"farmhash": "^2.0.5",
Expand All @@ -62,6 +64,7 @@
"xss": "^1.0.3"
},
"devDependencies": {
"@types/argparse": "^1.0.35",
"@types/jest": "^22.2.0",
"@types/uws": "^0.13.1",
"awesome-typescript-loader": "^3.4.1",
Expand All @@ -87,6 +90,7 @@
"nexe": "^2.0.0-rc.23",
"node-loader": "^0.6.0",
"standard": "^10.0.3",
"standard-changelog": "^2.0.6",
"ts-jest": "^22.0.4",
"typescript": "^2.9",
"typescript-eslint-parser": "^13.0.0",
Expand Down
54 changes: 54 additions & 0 deletions src/Arguments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { ArgumentParser } from 'argparse'

import * as fs from 'fs'
import * as path from 'path'

import { Settings, DEFAULT_SETTINGS } from './models/Settings.model'

export class Arguments {
public settings: Settings

private parser = new ArgumentParser({
addHelp: true,
description: 'Net64+ server'
})

constructor () {
let settings: Settings | undefined
let settingsPath = path.join(__dirname, '../settings.json')
try {
settings = JSON.parse(fs.readFileSync(settingsPath, {
encoding: 'utf8'
}))
} catch (err) {
fs.writeFileSync(settingsPath, JSON.stringify(DEFAULT_SETTINGS))
console.info('Failed to find or parse settings.json file. Using default settings instead and created a settings.json just for you.')
}

this.parser.addArgument([ '--port', '-P' ], {
type: (int: string) => parseInt(int)
})
this.parser.addArgument([ '--gamemode', '-g' ])
this.parser.addArgument([ '--disableGamemodeVote', '-G' ], {
action: 'storeFalse'
})
this.parser.addArgument([ '--passwordRequired', '-pr' ], {
action: 'storeTrue'
})
this.parser.addArgument([ '--password', '-p' ])
this.parser.addArgument([ '--name', '-n' ])
this.parser.addArgument([ '--domain', '-D' ])
this.parser.addArgument([ '--description', '-d' ])
this.parser.addArgument([ '--enableWebHook', '-w' ], {
action: 'storeTrue'
})
this.parser.addArgument([ '--apiKey', '-k' ])
const parsed = this.parser.parseArgs() as Settings

this.settings = {} as any
Object.entries(DEFAULT_SETTINGS).forEach(([ key, defaultValue ]: [ string, any ]) => {
// @ts-ignore
this.settings[key] = parsed[key] || (settings ? settings[key] : null) || defaultValue
})
}
}
7 changes: 4 additions & 3 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,16 @@ export class Client {

private onDisconnect (): void {
this.identity.startDeleteTimeout()
webSocketServer.removePlayer(this.id)
delete webSocketServer.clients[this.id]
if (this.player) {
webSocketServer.removePlayer(this.id)
}
if (this.connectionTimeout) {
clearTimeout(this.connectionTimeout)
}
if (this.afkTimeout) {
clearInterval(this.afkTimeout)
}
const activeUsers = webSocketServer.clients.filter(client => client).length
console.info(`Active users: ${activeUsers}/24`)
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/WebHook.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ describe('WebHook', () => {
const expectedHeaders = {
headers: {
Authorization: 'APIKEY apiKey'
}
},
responseType: 'json',
timeout: 10000
}

expect(mockAxios.post).toHaveBeenCalledWith(URL_API, expect.objectContaining(expectedBody), expectedHeaders)
Expand Down
18 changes: 13 additions & 5 deletions src/WebHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { webSocketServer } from './globals'
import { Settings } from './models/Settings.model'
import { Server } from './models/Server.model'

const URL_LIST = 'https://smmdb.ddns.net/net64'
export const URL_API = 'https://smmdb.ddns.net/api/net64server'

const apiKey = Symbol('apiKey')
Expand All @@ -19,6 +18,8 @@ export class WebHook {

private compatVersion: string

private id?: string

private ip: string

private port: number
Expand All @@ -41,6 +42,8 @@ export class WebHook {

private apiKey: string

private isDedicated: boolean

constructor (
{ name, domain, description, port, passwordRequired, apiKey }: Omit<Settings, 'gamemode'>,
{ ip, country, countryCode, latitude, longitude }: Server
Expand All @@ -58,22 +61,27 @@ export class WebHook {
this.longitude = longitude
this.passwordRequired = passwordRequired
this.apiKey = apiKey!
this.isDedicated = !process.env.IS_EXECUTABLE
this.loop()
}

private loop = async () => {
try {
const body = Object.assign({}, this)
body.toJSON = this.toJSON
await axios.post(
URL_API,
const url = `${URL_API}${this.id ? `?id=${this.id}` : ''}`
const res = (await axios.post(
url,
body,
{
timeout: 10000,
headers: this.apiKey ? {
'Authorization': `APIKEY ${this.apiKey}`
} : {}
} : {},
responseType: 'json'
}
)
)).data
this.id = res.id
} catch (err) {
if (err.response && err.response.status === 401) {
console.error('Your API key seems to be wrong. Please check your settings!\nWebHook was disabled now')
Expand Down
38 changes: 18 additions & 20 deletions src/WebSocketServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class WebSocketServer {

private command: Command

private ip: string
private ip?: string

private port: number

Expand All @@ -57,54 +57,52 @@ export class WebSocketServer {

private description: string

private countryCode: string
private countryCode?: string

public readonly passwordRequired: boolean

public readonly password: string

constructor (
{ port, gamemode, enableGamemodeVote, passwordRequired, password, name, domain, description }: Settings,
server?: Server
{ port, gamemode, enableGamemodeVote, passwordRequired, password, name, domain, description }: Settings
) {
this.gameMode = gamemode
this.ip = server ? server.ip : ''
this.port = port
this.name = name
this.domain = domain
this.description = description
this.countryCode = server ? server.countryCode : 'LAN'
this.passwordRequired = passwordRequired
this.password = password
this.command = new Command(enableGamemodeVote)
this.onConnection = this.onConnection.bind(this)
this.server = new WSServer({ port: this.port }, () => {
console.info(`\nNet64+ ${process.env.VERSION} server successfully started!\nAccepting connections on Port ${this.port}`)
if (passwordRequired) {
console.info('Password protection enabled')
}
if (process.env.IS_EXECUTABLE) {
console.info('Connect locally via direct connect 127.0.0.1\nTo accept external connections, your Port must be forwarded.\nTo join via LAN, others must use your LAN IP address: win + "cmd" > ipconfig > IPv4 Address or via Hamachi network and IP')
console.info('\nThis is a precompiled version of the Net64+ server. It has the limitation, that it cannot be displayed on the public server list. It is only meant to be used for user servers!\n')
}
})
this.server.on('connection', this.onConnection)
this.server = new WSServer({ port: this.port })
this.metaData = new MetaData()
this.clients = []
this.players = []
}

public start (server?: Server) {
this.ip = server ? server.ip : ''
this.countryCode = server ? server.countryCode : 'LAN'
console.info(`\nNet64+ ${process.env.VERSION} server successfully started!`)
this.server!.on('connection', this.onConnection)
if (this.passwordRequired) {
console.info('Password protection enabled')
}
}

public addPlayer (player: Player): void {
const playerId = player.client.id
this.players[playerId] = player
if (!this.playerWithToken) {
this.grantNewServerToken(player)
}
this.broadcastPlayerListMessage()
const activeUsers = this.players.filter(players => players).length
console.info(`Active users: ${activeUsers}/24`)
}

public removePlayer (clientId: number): void {
delete this.clients[clientId]
const playerToRemove = this.players[clientId]
let shouldGrantNewToken = false
if (playerToRemove === this.playerWithToken) {
Expand All @@ -116,6 +114,8 @@ export class WebSocketServer {
this.grantNewServerToken()
}
this.broadcastPlayerListMessage()
const activeUsers = this.players.filter(player => player).length
console.info(`Active users: ${activeUsers}/24`)
}

private broadcastPlayerListMessage (): void {
Expand Down Expand Up @@ -404,8 +404,6 @@ export class WebSocketServer {
console.info(`A new client connected and received ID: ${id}`)
}
this.clients[id] = new Client(id, ws)
const activeUsers = this.clients.filter(client => client).length
console.info(`Active users: ${activeUsers}/24`)
}

private getNextClientId (): number | null {
Expand Down
Loading

0 comments on commit 6a387fc

Please sign in to comment.