forked from sztupy/adbfsplugin
/
adbhandler.cpp
404 lines (362 loc) · 12 KB
/
adbhandler.cpp
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include "StdAfx.h"
#include "adbhandler.h"
#include "adbfsplugin.h"
using namespace std;
/* -------------------------------
---- Some helper functions ----
------------------------------- */
#define EPOCH_DIFF 0x019DB1DED53E8000LL /* 116444736000000000 nsecs */
#define RATE_DIFF 10000000 /* 100 nsecs */
/* Convert a UNIX time_t into a Windows filetime_t */
__int64 unixTimeToFileTime(unsigned int utime) {
__int64 tconv = ((__int64)utime * RATE_DIFF) + EPOCH_DIFF;
return tconv;
}
/* Convert a Windows filetime_t into a UNIX time_t */
unsigned int fileTimeToUnixTime(__int64 ftime) {
unsigned int tconv = (unsigned int)((ftime - EPOCH_DIFF) / RATE_DIFF);
return (time_t)tconv;
}
string trim( string const& str, const char* sepSet)
{
std::string::size_type const first = str.find_first_not_of(sepSet);
return ( first==std::string::npos ) ? std::string() : str.substr(first, str.find_last_not_of(sepSet)-first+1);
}
/* Get adbfsplugin directory, and replace dll with adb.exe */
LPSTR __adb__filename = NULL;
LPSTR GetAdbFileName() {
if (__adb__filename) return __adb__filename;
__adb__filename = new char[1024];
GetModuleFileName( GetModuleHandle("adbfsplugin.wfx"), __adb__filename, 1024 );
LPSTR filename = PathFindFileName(__adb__filename);
strcpy_s(filename,16,"adb.exe");
return __adb__filename;
}
// quote a string for usage in bash
wstring QuoteString(wstring str) {
wstring result = L"'";
for (auto i = str.begin(); i!= str.end(); i++) {
if (*i==L'\'') {
result.append(L"'\\''");
} else {
result.push_back(*i);
}
}
result.append(L"'");
return result;
}
unsigned char base64table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
unsigned char base64table2[257] = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\x3E~~~\x3F\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D~~~\x00~~~\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19~~~~~~\x1A\x1B\x1C\x1D\x1E\x1F\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
// base64 decode 4 characters to 3 characters
// returns the bytes decoded (might be less because of padding)
int decode64(const char* input, char* output) {
if ((input[3]=='=') && (input[2]=='=') && (input[0]=='=') && (input[1]=='=')) return 0;
unsigned int n = (base64table2[input[0]] << 18) | (base64table2[input[1]] << 12) | (base64table2[input[2]] << 6) | (base64table2[input[3]]);
output[0] = (unsigned char)(n>>16);
output[1] = (unsigned char)((n>>8) & 0xFF);
output[2] = (unsigned char)(n & 0xFF);
return 1 + ((input[3]!='=')?1:0) + ((input[2]!='=')?1:0);
}
int encode64(const char* input, char* output) {
unsigned int n = ((unsigned char)input[0] << 16) | ((unsigned char)input[1] << 8) | (unsigned char)input[2];
output[0] = base64table[n>>18];
output[1] = base64table[(n>>12) & (0x3F)];
output[2] = base64table[(n>>6) & (0x3F)];
output[3] = base64table[(n) & (0x3F)];
return 4;
}
/* ---------------------------
---- Adb Communicator -----
--------------------------- */
AdbCommunicator* AdbCommunicator::_global_adb = 0;
AdbCommunicator::~AdbCommunicator() {
Close();
LogProc(PluginNumber, MSGTYPE_DISCONNECT, "Closing plugin");
}
AdbCommunicator::AdbCommunicator() {
PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;
ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
siStartInfo.cb = sizeof(STARTUPINFO);
LogProc(PluginNumber, MSGTYPE_DETAILS, "Starting ADB Server: ");
LogProc(PluginNumber, MSGTYPE_DETAILS, GetAdbFileName());
BOOL retval = CreateProcess(GetAdbFileName(),"adb.exe start-server",NULL,NULL,TRUE,CREATE_NO_WINDOW|CREATE_UNICODE_ENVIRONMENT,NULL,NULL,&siStartInfo,&piProcInfo);
if (!retval) {
throw wstring(L"<0000 - Could not start ADB server>");
}
s = INVALID_SOCKET;
_needsu = true;
actbufsize=0;
actbufpos=0;
}
void AdbCommunicator::Close() {
LogProc(PluginNumber, MSGTYPE_DISCONNECT, "Closing connection /");
closesocket(s);
s = INVALID_SOCKET;
actbufsize=0;
actbufpos=0;
}
void AdbCommunicator::SendStringToServer(char* str) {
if (send(s, str, strlen(str), 0) == SOCKET_ERROR) {
Close();
throw wstring(L"<0009 - could not switch to usb mode>");
}
// get result
char recbuf[5];
recbuf[4]='\0';
int bytesRead = recv(s, recbuf, 4, MSG_WAITALL);
if ((bytesRead==SOCKET_ERROR) || (bytesRead!=4)) {
Close();
throw wstring(L"<000A - no ack data from adb server>");
}
if (_strcmpi("FAIL",recbuf)==0) {
// cleanup
recv(s,recbuf,4,MSG_WAITALL);
int datalen;
sscanf_s(recbuf,"%x",&datalen);
char* data = new char[datalen+1];
recv(s,data,datalen,MSG_WAITALL);
Close();
throw wstring(L"<000B - FAIL response from adb server>");
} else if (_strcmpi("OKAY",recbuf)!=0) {
Close();
throw wstring(L"<000C - Bad response from adb server>");
}
}
void AdbCommunicator::ReConnect() {
LogProc(PluginNumber, MSGTYPE_CONNECT, "CONNECT /");
struct addrinfo *result = NULL, *ptr = NULL, hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
if (getaddrinfo("127.0.0.1", "5037", &hints, &result)!=0) {
throw wstring(L"<0007 - localhost not found>");
}
ptr = result;
s = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (s==INVALID_SOCKET) {
throw wstring(L"<0006 - socket initialization failed>");
}
if (connect(s, ptr->ai_addr, ptr->ai_addrlen) == SOCKET_ERROR) {
Close();
throw wstring(L"<0008 - could not connect to local adb server>");
}
// switch to usb mode
// TODO: multiple devices support
SendStringToServer("0012host:transport-usb");
// start shell
SendStringToServer("0006shell:");
if (_needsu) {
// TODO: very hacky
Sleep(500); // let the shell start
CleanBuffer(false); // remove everything in buffer
char buf[4] = "su\n";
send(s,buf,3,0);
Sleep(50); // small timeout for the echo
CleanBuffer(false); // remove echo
CleanBuffer(true); // wait for root
// TODO: should check whether root failed or not
}
}
void AdbCommunicator::CleanBuffer(bool timeout) {
TIMEVAL timeval;
timeval.tv_sec = 0;
timeval.tv_usec = 0;
fd_set set;
FD_ZERO(&set);
FD_SET(s, &set);
// cleans input buffer
actbufsize=0;
actbufpos=0;
actbufpospoint=actbuf;
while (select(0, &set, NULL, NULL, (timeout)?NULL:&timeval)!=0) {
recv(s,actbuf,BUF_SIZE,0);
if (timeout) return;
}
}
void AdbCommunicator::PushCommandW(wstring command) {
if (s==INVALID_SOCKET) {
ReConnect();
Sleep(500); // wait for the shell to start
}
CleanBuffer(false);
// add some garbage data to determine where sending starts and where it stops
command = L"echo \"===adbfsplugin<--\" ;" + command + L" ; echo \"===adbfsplugin-->\"";
// convert utf-16 command to utf-8
int sizeneeded = WideCharToMultiByte(CP_UTF8,0,command.c_str(),-1,NULL,0,NULL,NULL);
char* comm = new char[sizeneeded+3];
WideCharToMultiByte(CP_UTF8,0,command.c_str(),-1,comm,sizeneeded+3,NULL,NULL);
sizeneeded = strlen(comm);
comm[sizeneeded]='\n';
comm[sizeneeded+1]='\0';
if (send(s,comm,sizeneeded+1,0) == SOCKET_ERROR) {
Close();
throw wstring(L"<000D - Command send failed>");
}
// throw out initial garbage
string* line = ReadLine();
while ((line!=NULL) && (*line != "===adbfsplugin<--")) {
delete line;
line = ReadLine();
}
if (line) delete line;
};
int AdbCommunicator::ReadBuf(void) {
actbufpos++;
actbufpospoint++;
if (actbufsize<=actbufpos) {
actbufsize = recv(s,actbuf,BUF_SIZE,0);
if (actbufsize!=SOCKET_ERROR) {
actbufpos=0;
actbufpospoint = actbuf;
} else {
actbufpos=0;
actbufpospoint=actbuf;
actbufsize=0;
return SOCKET_ERROR;
}
}
return actbufsize-actbufpos;
}
int AdbCommunicator::PutData(const char * data, int length) {
return send(s,data,length,0);
}
string* AdbCommunicator::ReadLine() {
string input = "";
DWORD bytesRead;
int state=0; // check for prompt state
bytesRead = ReadBuf();
int size=0;
while ((bytesRead!=SOCKET_ERROR) && (bytesRead!=0) && (*actbufpospoint!='\n') && ((size != 17) || (input != "===adbfsplugin-->"))) {
size++;
input.push_back(*actbufpospoint);
bytesRead = ReadBuf();
}
//LogProc(PluginNumber,MSGTYPE_DETAILS,(char*)input.c_str());
if (bytesRead==SOCKET_ERROR) {
Close();
int d = WSAGetLastError();
throw wstring(L"Socket Error");
}
if (input.empty() || input=="===adbfsplugin-->") {
return NULL;
}
return new string(trim(input," \t\r\n"));
}
wstring* AdbCommunicator::ReadLineW() {
string* input = ReadLine();
if (input==NULL) return NULL;
int wide = MultiByteToWideChar(CP_UTF8, 0, input->c_str(), input->length()+1, NULL, 0);
LPWSTR output = new wchar_t[wide];
MultiByteToWideChar(CP_UTF8, 0, input->c_str(), input->length()+1, output, wide);
delete input;
return new wstring(output,wide-1);
}
/* ---------------------------
---- FileData Helpers -----
--------------------------- */
void FillStat(wstring directory, list<FileData*>* fd) {
try {
wstring command = L"busybox stat -c \"%a -%F- %g %u %s %X %Y %Z %N\" ";
for (auto i = fd->begin(); i != fd->end(); i++) {
command.append(L" ");
command.append(QuoteString(directory+(*i)->name));
}
AdbCommunicator::instance()->PushCommandW(command);
wstring* line = AdbCommunicator::instance()->ReadLineW();
auto i = fd->begin();
while ((line!=NULL) && (i!=fd->end())) {
(*i)->cache_name = directory+(*i)->name;
int mode = 0;;
WCHAR type[100];
WCHAR name[1024];
ZeroMemory(type,sizeof(type));
ZeroMemory(name,sizeof(name));
DWORD gid = 0;
DWORD uid = 0;
unsigned __int64 size = 0;
DWORD time1 = 0,time2 = 0,time3 = 0;
int res = swscanf_s(line->c_str(),L"%o -%[a-zA-Z ]- %u %u %I64i %u %u %u %[^\n]",&mode,type,countof(type),&gid,&uid,&size,&time1,&time2,&time3,name,countof(name));
if (res<9) {
(*i)->alt_name = L"<0005 - stat failed>";
} else {
(*i)->accessTime = time1;
(*i)->modificationTime = time2;
(*i)->changeTime = time3;
(*i)->uid = uid;
(*i)->gid = gid;
(*i)->mode = mode;
(*i)->size = size;
(*i)->type = OTHER;
if (wcscmp(type,L"directory")==0) {
(*i)->type = DIRECTORY;
} else if (wcscmp(type, L"symbolic link")==0) {
(*i)->type = LINK;
} else if (wcscmp(type, L"regular file")==0) {
(*i)->type = REGFILE;
}
(*i)->alt_name = name;
}
line = AdbCommunicator::instance()->ReadLineW();
i++;
}
} catch (wstring e) {
}
}
void GetStat(WIN32_FIND_DATAW* fs, FileData* fd) {
memset(fs,0,sizeof(WIN32_FIND_DATAW));
wcslcpy(fs->cFileName,fd->name.c_str(),countof(fs->cFileName)-1);
fs->dwFileAttributes = 0x80000000;
if (fd->type == DIRECTORY) {
fs->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
}
fs->dwReserved0 = fd->mode;
fs->nFileSizeHigh = fd->size >> 32;
fs->nFileSizeLow = fd->size & 0x0FFFFFFFF;
}
list<FileData*>* DirList(wstring filename) {
auto* result = new list<FileData*>();
try {
AdbCommunicator::instance()->PushCommandW((wstring(L"busybox ls --color=never -1 ") + QuoteString(filename)).c_str());
wstring* line = AdbCommunicator::instance()->ReadLineW();
while (line!=NULL) {
result->push_back(new FileData(*line));
delete line;
line = AdbCommunicator::instance()->ReadLineW();
}
} catch (wstring e) {
result->push_back(new FileData(e));
}
// Get stat in batches of 10 files
auto* l = new list<FileData*>();
for (auto i = result->begin(); i!= result->end(); i++) {
l->push_back(*i);
if (l->size()>10) {
FillStat(filename,l);
l->clear();
}
}
if (!l->empty()) {
FillStat(filename,l);
}
delete l;
return result;
}
bool RunCommand(wstring comm)
{
try {
AdbCommunicator::instance()->PushCommandW(comm);
wstring* line = AdbCommunicator::instance()->ReadLineW();
while (line!=NULL) {
LogProcW(PluginNumber, MSGTYPE_DETAILS, (WCHAR*)line->c_str());
delete line;
line = AdbCommunicator::instance()->ReadLineW();
}
return true;
} catch (wstring e) {
LogProcW(PluginNumber, MSGTYPE_IMPORTANTERROR, (WCHAR*)e.c_str());
return false;
}
}