Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"moment-timezone": "^0.5.31"
},
"devDependencies": {
"@types/node": "^18.19.103",
"@types/node": "18.11.18",
"nodemon": "^2.0.4",
"ts-node": "^9.0.0",
"tslint": "^6.1.0",
Expand Down
74 changes: 46 additions & 28 deletions src/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Packet from "../protocol/Packet";
import Request from "../protocol/smb2/Request";
import Response from "../protocol/smb2/Response";
import Header from "../protocol/smb2/Header";
import HeaderFlag from "../protocol/smb2/HeaderFlag";
import StatusCode from "../protocol/smb2/StatusCode";
import Smb2PacketType from "../protocol/smb2/PacketType";
import Session, { AuthenticateOptions } from "./Session";
Expand Down Expand Up @@ -34,6 +35,7 @@ class Client extends EventEmitter {
responseCallbackMap = new Map<bigint, (response: Response) => void>();

connected: boolean = false;
signingKey?: Buffer;

port: number = 445;

Expand All @@ -47,13 +49,15 @@ class Client extends EventEmitter {

constructor(
public host: string,
public options: Options = {}
public options: Options = {},
) {
super();

if (typeof this.options.port === "number") this.port = this.options.port;
if (typeof this.options.connectTimeout === "number") this.connectTimeout = this.options.connectTimeout;
if (typeof this.options.requestTimeout === "number") this.requestTimeout = this.options.requestTimeout;
if (typeof this.options.connectTimeout === "number")
this.connectTimeout = this.options.connectTimeout;
if (typeof this.options.requestTimeout === "number")
this.requestTimeout = this.options.requestTimeout;
}

async connect() {
Expand All @@ -70,10 +74,7 @@ class Client extends EventEmitter {
this.connectTimeoutId = setTimeout(() => {
reject(new Error("connect_timeout"));
}, this.connectTimeout);
this.socket.connect(
this.port,
this.host
);
this.socket.connect(this.port, this.host);
this.socket.once("connect", () => {
resolve();
});
Expand All @@ -99,9 +100,9 @@ class Client extends EventEmitter {
{
messageId,
clientId: this._id,
...header
...header,
},
body
body,
);
}

Expand All @@ -113,18 +114,17 @@ class Client extends EventEmitter {
async send(request: Request) {
if (!this.connected) throw new Error("not_connected");

const buffer = request.serialize();
const buffer = this.serializeRequest(request);
this.socket.write(buffer);

const messageId = request.header.messageId;
const sendPromise = new Promise<Response>((resolve, reject) => {
const requestTimeoutId = setTimeout(
() => {
const err = new Error(`request_timeout: ${structureUtil.parseEnumValue(Smb2PacketType, request.header.type)}(${messageId})`);
reject(err);
},
this.requestTimeout
);
const requestTimeoutId = setTimeout(() => {
const err = new Error(
`request_timeout: ${structureUtil.parseEnumValue(Smb2PacketType, request.header.type)}(${messageId})`,
);
reject(err);
}, this.requestTimeout);

this.requestTimeoutIdMap.set(messageId, requestTimeoutId);

Expand Down Expand Up @@ -161,23 +161,39 @@ class Client extends EventEmitter {
return response;
}

private serializeRequest(request: Request) {
if (!this.signingKey || !request.header.sessionId) {
return request.serialize();
}

request.header.flags = (request.header.flags || 0) | HeaderFlag.Signed;
request.header.signature = Buffer.alloc(16);

const unsignedBuffer = request.serialize();
const signature = crypto
.createHmac("sha256", this.signingKey)
.update(unsignedBuffer.slice(4))
.digest()
.slice(0, 16);

request.header.signature = signature;
return request.serialize();
}

onData = (buffer: Buffer) => {
if (this.responseRestChunk) {
buffer = Buffer.concat([this.responseRestChunk, buffer]);
this.responseRestChunk = undefined;
}

const {
chunks,
restChunk
} = Packet.getChunks(buffer);
const { chunks, restChunk } = Packet.getChunks(buffer);
this.responseRestChunk = restChunk;

for (const chunk of chunks) {
const response = Response.parse(chunk);
this.onResponse(response);
}
}
};

onResponse(response: Response) {
if (
Expand All @@ -198,15 +214,15 @@ class Client extends EventEmitter {

onError = (err: Error) => {
console.error(err);
}
};

onClose = (hadError: boolean) => {
this.connected = false;
}
};

async echo() {
return await this.request({
type: Smb2PacketType.Echo
type: Smb2PacketType.Echo,
});
}

Expand All @@ -233,18 +249,20 @@ class Client extends EventEmitter {
private registerSession(session: Session) {
session
.once("authenticate", () => this.sessions.push(session))
.once("logoff", () => this.sessions.splice(this.sessions.indexOf(session), 1));
.once("logoff", () =>
this.sessions.splice(this.sessions.indexOf(session), 1),
);
}

async close() {
if (!this.connected) return;

await Promise.all(this.sessions.map(x => x.logoff()));
await Promise.all(this.sessions.map((x) => x.logoff()));

this.destroySocket();

this.connected = false;
}
}

export default Client;
export default Client;
Loading