summaryrefslogtreecommitdiff
path: root/protocols/Skype/src/skype_runtime.cpp
blob: bd6fe46a496fc70a400486113b7f8c1d8f6b04f1 (plain)
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
#include "skype.h"

#include "aes\aes.h" 

#include "..\..\..\skypekit\key.h"

char *CSkypeProto::LoadKeyPair()
{
	HRSRC hResource = FindResource(g_hInstance, MAKEINTRESOURCE(IDR_KEY), L"BIN");
	if (hResource == NULL)
		return NULL;

	HGLOBAL hLoadedResource = LoadResource(g_hInstance, hResource);
	if (hLoadedResource == NULL)
		return NULL;

	LPVOID pLockedResource = LockResource(hLoadedResource);
	if (pLockedResource == NULL)
		return NULL;

	int length = ::SizeofResource(g_hInstance, hResource);
	if (length == 0)
		return NULL;

	char* pData = (char*)_alloca(length + 1);
    ::memcpy(pData, pLockedResource, length);
    pData[length] = 0;

	unsigned decodedLen;
	mir_ptr<BYTE> tmpD((BYTE*)::mir_base64_decode(pData, &decodedLen));
	BYTE *result = (BYTE*)::mir_alloc(decodedLen+17);

	aes_context ctx;
	char *key = (char*)::mir_base64_decode(MY_KEY, NULL);
	::aes_set_key(&ctx, (BYTE*)key, 128);
	::mir_free(key);

	for (unsigned i = 0; i < decodedLen; i += 16)
		aes_decrypt(&ctx, &tmpD[i], &result[i]);

	result[decodedLen] = 0;
	return (char *)result;
}

int CSkypeProto::StartSkypeRuntime(const wchar_t *profileName)
{
	STARTUPINFO cif = {0};
	cif.cb = sizeof(STARTUPINFO);
	cif.dwFlags = STARTF_USESHOWWINDOW;
	cif.wShowWindow = SW_HIDE;	

	wchar_t	fileName[MAX_PATH];
	::GetModuleFileName(g_hInstance, fileName, MAX_PATH);

	wchar_t *skypeKitPath = ::wcsrchr(fileName, '\\');
	if (skypeKitPath != NULL)
		*skypeKitPath = 0;
	::swprintf(fileName, SIZEOF(fileName), L"%s\\%s", fileName, L"SkypeKit.exe");

	PROCESSENTRY32 entry;
	entry.dwSize = sizeof(PROCESSENTRY32);

	// todo: rework
	HANDLE snapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
	if (::Process32First(snapshot, &entry) == TRUE)
	{
		while (::Process32Next(snapshot, &entry) == TRUE)
		{
			if (::wcsicmp(entry.szExeFile, L"SkypeKit.exe") == 0 || ::wcsicmp(entry.szExeFile, L"Skype.exe") == 0)
			{
				this->skypeKitPort += rand() % 1000 + 8963;
				break;
			}
		}
	}
	::CloseHandle(snapshot);

	wchar_t param[128];
	VARST dbPath( _T("%miranda_userdata%\\SkypeKit"));
	::swprintf(param, SIZEOF(param), L"-p -P %d -f \"%s\"", this->skypeKitPort, dbPath);
	int startingrt = ::CreateProcess(
		fileName, param,
		NULL, NULL, FALSE,
		CREATE_NEW_CONSOLE,
		NULL, NULL, &cif, &this->skypeKitProcessInfo);

	return startingrt;
}

BOOL CSkypeProto::SafeTerminateProcess(HANDLE hProcess, UINT uExitCode)
{
    DWORD dwTID, dwCode, dwErr = 0;
    HANDLE hProcessDup = INVALID_HANDLE_VALUE;
    HANDLE hRT = NULL;
    HINSTANCE hKernel = ::GetModuleHandle(L"Kernel32");
    BOOL bSuccess = FALSE;

    BOOL bDup = ::DuplicateHandle(
		::GetCurrentProcess(), 
		hProcess, 
		GetCurrentProcess(), 
		&hProcessDup, 
		PROCESS_ALL_ACCESS, 
		FALSE, 
		0);

    // Detect the special case where the process is 
    // already dead...
    if (::GetExitCodeProcess((bDup) ? hProcessDup : hProcess, &dwCode) && (dwCode == STILL_ACTIVE))
    {
        FARPROC pfnExitProc;
           
        pfnExitProc = GetProcAddress(hKernel, "ExitProcess");

        hRT = ::CreateRemoteThread(
			(bDup) ? hProcessDup : hProcess, 
			NULL, 
			0, 
			(LPTHREAD_START_ROUTINE)pfnExitProc,
			(PVOID)uExitCode, 0, &dwTID);

        if ( hRT == NULL )
            dwErr = GetLastError();
    }
    else
        dwErr = ERROR_PROCESS_ABORTED;

    if (hRT)
    {
        // Must wait process to terminate to 
        // guarantee that it has exited...
        ::WaitForSingleObject((bDup) ? hProcessDup : hProcess, INFINITE);

        ::CloseHandle(hRT);
        bSuccess = TRUE;
    }

    if ( bDup )
        ::CloseHandle(hProcessDup);

    if ( !bSuccess )
        ::SetLastError(dwErr);

    return bSuccess;
}

void CSkypeProto::StopSkypeRuntime()
{
	//DWORD dwExitCode = 0;
	//this->SafeTerminateProcess(this->skypeKitProcessInfo.hProcess, 0);
	//::PostThreadMessage(this->skypeKitProcessInfo.dwThreadId, WM_CLOSE, 0, 0);
	//::WaitForSingleObject(this->skypeKitProcessInfo.hProcess, 1500);

	DWORD dwExitCode = 0;
	::GetExitCodeProcess(this->skypeKitProcessInfo.hProcess, &dwExitCode);
	if (dwExitCode == STILL_ACTIVE)
		//::TerminateProcess(this->skypeKitProcessInfo.hProcess, 0); // Zero is the exit code
		this->SafeTerminateProcess(this->skypeKitProcessInfo.hProcess, 0);

	::CloseHandle(this->skypeKitProcessInfo.hThread);
	::CloseHandle(this->skypeKitProcessInfo.hProcess);
}