-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwpse.cpp
More file actions
110 lines (98 loc) · 3.1 KB
/
wpse.cpp
File metadata and controls
110 lines (98 loc) · 3.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
#include <node.h>
#include <v8.h>
#include <windows.h>
#include <string>
#define BUFFER_SIZE 1024
using namespace v8;
Handle<Value> sexec(const Arguments& args) {
HandleScope scope;
Handle<Object> ret = Object::New();
Local<String> lstrStdOut = String::Empty();
Local<String> lstrStdErr = String::Empty();
Local<String> lstrCmd = String::Empty();
HANDLE hOutReadPipe = NULL;
HANDLE hOutWritePipe = NULL;
HANDLE hErrReadPipe = NULL;
HANDLE hErrWritePipe = NULL;
STARTUPINFOA si;
PROCESS_INFORMATION pi;
SECURITY_ATTRIBUTES sa;
DWORD dwRead = 0;
BOOL bReadSuccess = false;
CHAR buffer[BUFFER_SIZE];
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
ZeroMemory(&sa, sizeof(sa));
sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
if (!(CreatePipe(&hOutReadPipe, &hOutWritePipe, &sa, 0) && CreatePipe(&hErrReadPipe, &hErrWritePipe, &sa, 0))) {
return ThrowException(String::New("Pipe not created"));
}
if (!(SetHandleInformation(hOutReadPipe, HANDLE_FLAG_INHERIT, 0) && SetHandleInformation(hOutReadPipe, HANDLE_FLAG_INHERIT, 0))) {
return ThrowException(String::New("SetHandleInformation failed"));
}
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.hStdOutput = hOutWritePipe;
si.hStdInput = NULL;
si.hStdError = hErrWritePipe;
for (int i = 0; i < args.Length(); i++) {
lstrCmd = String::Concat(lstrCmd, args[i]->ToString());
if (i < (args.Length() - 1)) {
lstrCmd = String::Concat(lstrCmd, String::New(" "));
}
}
if (!CreateProcess(NULL, // No module name (use command line)
*String::Utf8Value(lstrCmd), // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
TRUE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
return ThrowException(String::New("Process exit with error"));
}
CloseHandle(hOutWritePipe);
CloseHandle(hErrWritePipe);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
ret->Set(
String::NewSymbol("target"),
lstrCmd
);
for (;;) {
bReadSuccess = ReadFile(hOutReadPipe, buffer, BUFFER_SIZE, &dwRead, NULL);
if (!bReadSuccess || dwRead == 0) {
break;
}
lstrStdOut = String::Concat(lstrStdOut, String::New(buffer, (int)dwRead));
}
CloseHandle(hOutReadPipe);
ret->Set(
String::NewSymbol("out"),
lstrStdOut
);
for (;;) { //Copy, paste... Shame on me!
bReadSuccess = ReadFile(hErrReadPipe, buffer, BUFFER_SIZE, &dwRead, NULL);
if (!bReadSuccess || dwRead == 0) {
break;
}
lstrStdErr = String::Concat(lstrStdErr, String::New(buffer, (int)dwRead));
}
CloseHandle(hErrReadPipe);
ret->Set(
String::NewSymbol("err"),
lstrStdErr
);
return scope.Close(ret);
}
void init(Handle<Object> target) {
target->Set(String::New("exec"), FunctionTemplate::New(sexec)->GetFunction());
}
NODE_MODULE(pse, init);