forked from eflexsystems/node-samba-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
204 lines (173 loc) · 5.1 KB
/
index.js
File metadata and controls
204 lines (173 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"use strict";
const exec = require("child_process").exec;
const util = require("util");
const p = require("path");
const singleSlash = /\//g;
/*
* NT_STATUS_NO_SUCH_FILE - when trying to dir a file in a directory that *does* exist
* NT_STATUS_OBJECT_NAME_NOT_FOUND - when trying to dir a file in a directory that *does not* exist
*/
const missingFileRegex = /(NT_STATUS_OBJECT_NAME_NOT_FOUND|NT_STATUS_NO_SUCH_FILE)/im;
function wrap(str) {
return "'" + str + "'";
}
class SambaClient {
constructor(options) {
this.address = options.address;
this.username = wrap(options.username || "guest");
this.password = options.password ? wrap(options.password) : null;
this.domain = options.domain;
this.port = options.port;
// Possible values for protocol version are listed in the Samba man pages:
// https://www.samba.org/samba/docs/current/man-html/smb.conf.5.html#CLIENTMAXPROTOCOL
this.maxProtocol = options.maxProtocol;
}
getFile(path, destination, workingDir) {
const fileName = path.replace(singleSlash, "\\");
const cmdArgs = util.format("%s %s", fileName, destination);
return this.execute("get", cmdArgs, workingDir);
}
sendFile(path, destination) {
const workingDir = p.dirname(path);
const fileName = p.basename(path).replace(singleSlash, "\\");
const cmdArgs = util.format(
"%s %s",
fileName,
destination.replace(singleSlash, "\\")
);
return this.execute("put", cmdArgs, workingDir);
}
deleteFile(fileName) {
return this.execute("del", fileName, "");
}
async listFiles(fileNamePrefix, fileNameSuffix) {
try {
const cmdArgs = util.format("%s*%s", fileNamePrefix, fileNameSuffix);
const allOutput = await this.execute("dir", cmdArgs, "");
const fileList = [];
const lines = allOutput.split("\n");
for (let i = 0; i < lines.length; i++) {
const line = lines[i].toString().trim();
if (line.startsWith(fileNamePrefix)) {
const parsed = line.substring(
0,
line.indexOf(fileNameSuffix) + fileNameSuffix.length
);
fileList.push(parsed);
}
}
return fileList;
} catch (e) {
if (e.message.match(missingFileRegex)) {
return [];
} else {
throw e;
}
}
}
mkdir(remotePath, cwd) {
return this.execute(
"mkdir",
remotePath.replace(singleSlash, "\\"),
cwd !== null && cwd !== undefined ? cwd : __dirname
);
}
dir(remotePath, cwd) {
return this.execute(
"dir",
remotePath.replace(singleSlash, "\\"),
cwd !== null && cwd !== undefined ? cwd : __dirname
);
}
async fileExists(remotePath, cwd) {
try {
await this.dir(remotePath, cwd);
return true;
} catch (e) {
if (e.message.match(missingFileRegex)) {
return false;
} else {
throw e;
}
}
}
async cwd() {
const cd = await this.execute("cd", "", "");
return cd.match(/\s.{2}\s(.+?)/)[1];
}
async list(remotePath) {
const remoteDirList = [];
const remoteDirContents = await this.dir(remotePath);
for (const content of remoteDirContents.matchAll(/\s?(.+)\s{2}/g)) {
remoteDirList.push({
name: content[1].match(/\s?(.*?)\s/)[1],
type: content[1].match(/(.)\s+[0-9]/)[1],
size: content[1].match(/.\s+([0-9]+)/)[1],
modifyTime: content[1].match(/[0-9]+\s+(.+)/)[1],
});
}
return remoteDirList;
}
getSmbClientArgs(fullCmd) {
const args = ["-U", this.username];
if (!this.password) {
args.push("-N");
}
args.push("-c", fullCmd, this.address);
if (this.password) {
args.push(this.password);
}
if (this.domain) {
args.push("-W");
args.push(this.domain);
}
if (this.maxProtocol) {
args.push("--max-protocol", this.maxProtocol);
}
if (this.port) {
args.push('-p');
args.push(this.port);
}
return args;
}
execute(cmd, cmdArgs, workingDir) {
const fullCmd = wrap(util.format("%s %s", cmd, cmdArgs));
const command = [
"smbclient",
this.getSmbClientArgs(fullCmd).join(" "),
].join(" ");
const options = {
cwd: workingDir || "",
};
return new Promise((resolve, reject) => {
exec(command, options, function (err, stdout, stderr) {
const allOutput = stdout + stderr;
if (err) {
err.message += allOutput;
return reject(err);
}
return resolve(allOutput);
});
});
}
getAllShares() {
return new Promise((resolve, reject) => {
exec("smbtree -U guest -N", {}, function (err, stdout, stderr) {
const allOutput = stdout + stderr;
if (err !== null) {
err.message += allOutput;
return reject(err);
}
const shares = [];
for (const line in stdout.split(/\r?\n/)) {
const words = line.split(/\t/);
if (words.length > 2 && words[2].match(/^\s*$/) !== null) {
shares.append(words[2].trim());
}
}
return resolve(shares);
});
});
}
}
module.exports = SambaClient;