From 8db7c4741cb49211ee4d253508fc8ca910e9be60 Mon Sep 17 00:00:00 2001 From: Tim Vallancourt Date: Tue, 29 Oct 2024 12:27:57 -0700 Subject: [PATCH 1/3] fix packet --- src/connection.ts | 3 ++- src/login7-payload.ts | 16 ++++++++++------ src/token/handler.ts | 4 ++-- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/connection.ts b/src/connection.ts index d810acb16..4beb650c2 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -873,6 +873,7 @@ export interface ConnectionOptions { interface RoutingData { server: string; port: number; + login7server: string; } /** @@ -2484,7 +2485,7 @@ class Connection extends EventEmitter { } payload.hostname = this.config.options.workstationId || os.hostname(); - payload.serverName = this.routingData ? this.routingData.server : this.config.server; + payload.serverName = this.routingData ? this.routingData.login7server : this.config.server; payload.appName = this.config.options.appName || 'Tedious'; payload.libraryName = libraryName; payload.language = this.config.options.language; diff --git a/src/login7-payload.ts b/src/login7-payload.ts index 01b85f94c..899d0b583 100644 --- a/src/login7-payload.ts +++ b/src/login7-payload.ts @@ -251,15 +251,12 @@ class Login7Payload { } // (ibUnused / ibExtension): 2-byte - offset = fixedData.writeUInt16LE(dataOffset, offset); + const extensionOffsetHeaderOffset = offset; + offset = fixedData.writeUInt16LE(0, offset); + // (cchUnused / cbExtension): 2-byte - const extensions = this.buildFeatureExt(); offset = fixedData.writeUInt16LE(4, offset); - const extensionOffset = Buffer.alloc(4); - extensionOffset.writeUInt32LE(dataOffset += 4, 0); - dataOffset += extensions.length; - buffers.push(extensionOffset, extensions); // ibCltIntName: 2-byte offset = fixedData.writeUInt16LE(dataOffset, offset); @@ -365,6 +362,13 @@ class Login7Payload { fixedData.writeUInt32LE(0, offset); } + fixedData.writeUInt16LE(dataOffset, extensionOffsetHeaderOffset); + + const extensions = this.buildFeatureExt(); + const extensionOffset = Buffer.alloc(4); + extensionOffset.writeUInt32LE(dataOffset + 4, 0); + buffers.push(extensionOffset, extensions); + const data = Buffer.concat(buffers); data.writeUInt32LE(data.length, 0); return data; diff --git a/src/token/handler.ts b/src/token/handler.ts index 0c259a723..5eb97f8b4 100644 --- a/src/token/handler.ts +++ b/src/token/handler.ts @@ -246,7 +246,7 @@ export class Login7TokenHandler extends TokenHandler { declare connection: Connection; declare fedAuthInfoToken: FedAuthInfoToken | undefined; - declare routingData: { server: string, port: number } | undefined; + declare routingData: { server: string, port: number, login7server: string } | undefined; declare loginAckReceived: boolean; @@ -340,7 +340,7 @@ export class Login7TokenHandler extends TokenHandler { const [ server ] = token.newValue.server.split('\\'); this.routingData = { - server, port: token.newValue.port + server, port: token.newValue.port, login7server: token.newValue.server }; } From 08f8110f4045f8f80272ff9c7b38ac2a142d81e5 Mon Sep 17 00:00:00 2001 From: Tim Vallancourt Date: Tue, 29 Oct 2024 17:12:01 -0700 Subject: [PATCH 2/3] instance --- src/connection.ts | 4 ++-- src/token/handler.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/connection.ts b/src/connection.ts index 4beb650c2..75e32252b 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -873,7 +873,7 @@ export interface ConnectionOptions { interface RoutingData { server: string; port: number; - login7server: string; + instance: string; } /** @@ -2485,7 +2485,7 @@ class Connection extends EventEmitter { } payload.hostname = this.config.options.workstationId || os.hostname(); - payload.serverName = this.routingData ? this.routingData.login7server : this.config.server; + payload.serverName = this.routingData ? `${this.routingData.server}\\${this.routingData.instance}` : this.config.server; payload.appName = this.config.options.appName || 'Tedious'; payload.libraryName = libraryName; payload.language = this.config.options.language; diff --git a/src/token/handler.ts b/src/token/handler.ts index 5eb97f8b4..832e5bd4c 100644 --- a/src/token/handler.ts +++ b/src/token/handler.ts @@ -246,7 +246,7 @@ export class Login7TokenHandler extends TokenHandler { declare connection: Connection; declare fedAuthInfoToken: FedAuthInfoToken | undefined; - declare routingData: { server: string, port: number, login7server: string } | undefined; + declare routingData: { server: string, port: number, instance: string } | undefined; declare loginAckReceived: boolean; @@ -337,10 +337,10 @@ export class Login7TokenHandler extends TokenHandler { onRoutingChange(token: RoutingEnvChangeToken) { // Removes instance name attached to the redirect url. E.g., redirect.db.net\instance1 --> redirect.db.net - const [ server ] = token.newValue.server.split('\\'); + const [ server, instance ] = token.newValue.server.split('\\'); this.routingData = { - server, port: token.newValue.port, login7server: token.newValue.server + server, port: token.newValue.port, instance }; } From a951cb537f9e29dd97e16e11911e2c2bb654729d Mon Sep 17 00:00:00 2001 From: Tim Vallancourt Date: Wed, 4 Dec 2024 16:39:54 -0800 Subject: [PATCH 3/3] trace id --- src/prelogin-payload.ts | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/prelogin-payload.ts b/src/prelogin-payload.ts index bcc1fbc5e..cff5232b5 100644 --- a/src/prelogin-payload.ts +++ b/src/prelogin-payload.ts @@ -1,8 +1,10 @@ import { sprintf } from 'sprintf-js'; import WritableTrackingBuffer from './tracking-buffer/writable-tracking-buffer'; +import { randomBytes } from 'crypto'; const optionBufferSize = 20; +const traceIdSize = 36; const TOKEN = { VERSION: 0x00, @@ -10,6 +12,7 @@ const TOKEN = { INSTOPT: 0x02, THREADID: 0x03, MARS: 0x04, + TRACEID: 0x05, FEDAUTHREQUIRED: 0x06, TERMINATOR: 0xFF }; @@ -73,6 +76,9 @@ class PreloginPayload { declare mars: number; declare marsString: string; + + declare traceId: Buffer; + declare fedAuthRequired: number; constructor(bufferOrOptions: Buffer | Options = { encrypt: false, version: { major: 0, minor: 0, build: 0, subbuild: 0 } }) { @@ -93,6 +99,7 @@ class PreloginPayload { this.createInstanceOption(), this.createThreadIdOption(), this.createMarsOption(), + this.createTraceIdOption(), this.createFedAuthOption() ]; @@ -171,6 +178,17 @@ class PreloginPayload { }; } + createTraceIdOption() { + const buffer = new WritableTrackingBuffer(traceIdSize); + // generate a random series of bytes to use as the TraceID + // used for debugging purposes + buffer.writeBuffer(randomBytes(traceIdSize)); + return { + token: TOKEN.TRACEID, + data: buffer.data + }; + } + createFedAuthOption() { const buffer = new WritableTrackingBuffer(optionBufferSize); buffer.writeUInt8(0x01); @@ -203,6 +221,9 @@ class PreloginPayload { case TOKEN.MARS: this.extractMars(dataOffset); break; + case TOKEN.TRACEID: + this.extractTraceId(dataOffset); + break; case TOKEN.FEDAUTHREQUIRED: this.extractFedAuth(dataOffset); break; @@ -239,20 +260,25 @@ class PreloginPayload { this.marsString = marsByValue[this.mars]; } + extractTraceId(offset: number) { + this.traceId = this.data.subarray(offset, traceIdSize); + } + extractFedAuth(offset: number) { this.fedAuthRequired = this.data.readUInt8(offset); } toString(indent = '') { return indent + 'PreLogin - ' + sprintf( - 'version:%d.%d.%d.%d, encryption:0x%02X(%s), instopt:0x%02X, threadId:0x%08X, mars:0x%02X(%s)', + 'version:%d.%d.%d.%d, encryption:0x%02X(%s), instopt:0x%02X, threadId:0x%08X, mars:0x%02X(%s), traceId:%s', this.version.major, this.version.minor, this.version.build, this.version.subbuild, this.encryption ? this.encryption : 0, this.encryptionString ? this.encryptionString : '', this.instance ? this.instance : 0, this.threadId ? this.threadId : 0, this.mars ? this.mars : 0, - this.marsString ? this.marsString : '' + this.marsString ? this.marsString : '', + this.traceId ? this.traceId.toString('hex') : '', ); } }