Skip to content

Commit

Permalink
ping响应后断开连接
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Jul 14, 2022
1 parent d57501a commit d09b6f1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export class Client extends EventEmitter {
return this._closed
}

public get remoteAddress(): string {
return `${this.socket.remoteAddress}:${this.socket.remotePort}`
}

public get state(): States {
return this._state
}
Expand Down Expand Up @@ -175,6 +179,7 @@ export class Client extends EventEmitter {
}

public async responsePing(backend: Backend): Promise<void> {
this.logger.trace('response ping')
const response = JSON.stringify({
version: {
name: backend.version,
Expand Down
24 changes: 17 additions & 7 deletions src/proxy-server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Logger from 'bunyan'
import {isWorker, worker} from 'cluster'
import {EventEmitter} from 'events'
import {pick} from 'lodash'
import {createServer, Server, Socket} from 'net'
import {Container} from 'typedi'
import {Backend, IBackend} from './backend'
Expand All @@ -26,7 +27,7 @@ export class ProxyServer extends EventEmitter {
private host?: string,
) {
super()
const loggerOptions: Logger.LoggerOptions = {name: 'server', port, host}
const loggerOptions: Logger.LoggerOptions = {name: 'server', port, host, level: this.config.loglevel}
if (isWorker) {
loggerOptions.worker = worker.id
}
Expand Down Expand Up @@ -81,12 +82,17 @@ export class ProxyServer extends EventEmitter {
})
try {
const nextState = await client.awaitHandshake()
this.logger.debug({nextState}, 'handshake success')
const backend = await this.getBackend(client.host)
if (!backend) return client.close(`${client.host} not found`)
if (nextState !== EnumHandShakeState.login && backend.handlePing) {
await client.responsePing(backend)
} else {
if (nextState === EnumHandShakeState.login) {
switch (nextState) {
case EnumHandShakeState.status:
if (backend.handlePing) {
await client.responsePing(backend)
client.kill()
}
break
case EnumHandShakeState.login:
if (client.username && this.isUsernameBanned(client.username)) {
this.logger.warn({
ip: socket.remoteAddress, username: client.username,
Expand All @@ -101,9 +107,13 @@ export class ProxyServer extends EventEmitter {
client.close(this.config.message.bannedUUID)
return
}
}
await client.pipeToBackend(backend, nextState)
break
default:
this.logger.warn({client: pick(client, 'remoteAddress')}, `unknown handshake state ${nextState}`)
client.close(`unknown handshake state ${nextState}`)
}

await client.pipeToBackend(backend, nextState)
} catch (err) {
this.logger.error(err)
client.close(err.message)
Expand Down

0 comments on commit d09b6f1

Please sign in to comment.