-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclickvmconsole.cpp
More file actions
238 lines (224 loc) · 6.57 KB
/
Copy pathclickvmconsole.cpp
File metadata and controls
238 lines (224 loc) · 6.57 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
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
// clickvmconsole.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <Wtsapi32.h>
#include <algorithm>
#include <direct.h>
#include <iostream>
#include <string>
#include <io.h>
#include <windows.h>
#include <ctime>
#include <WinSock.h>
#include "simpleDll.h"
using namespace std;
#include <stdlib.h>
#pragma comment(lib, "WtsApi32.lib")
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib,"simpleDll.lib")
extern void wrlog(char *filename,const char *p,bool writetime);
int tmp_sender(string ip,string port)
{
WSADATA wsa;
//初始化套接字DLL
if(WSAStartup(MAKEWORD(2,2),&wsa)!=0)
{
MessageBox(NULL, "网络无响应,请检查网络是否畅通?","",MB_OK);
wrlog("CloudTerm\\cthandler.log","socket initialize failed.\n",true);
return -1;
}
//创建套接字
SOCKET sock;
if((sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET)
{
MessageBox(NULL, "网络无响应,请检查网络是否畅通?","",MB_OK);
wrlog("CloudTerm\\cthandler.log","create socket failed.\n",true);
return -1;
}
struct sockaddr_in serverAddress;
memset(&serverAddress,0,sizeof(sockaddr_in));
serverAddress.sin_family=AF_INET;
serverAddress.sin_addr.S_un.S_addr = inet_addr(ip.c_str());
serverAddress.sin_port = htons(atoi(port.c_str()));
//建立和服务器的连接
if(connect(sock,(sockaddr*)&serverAddress,sizeof(serverAddress))==SOCKET_ERROR)
{
MessageBox(NULL, "网络无响应,请检查网络是否畅通?","",MB_OK);
wrlog("CloudTerm\\cthandler.log","connect failed.\n",true);
return -1;
}
//printf("Message from %s: %s\n",inet_ntoa(serverAddress.sin_addr),buf);
return sock;
}
string GetModuleDir()
{
HMODULE module = GetModuleHandle(0);
char pFileName[MAX_PATH];
GetModuleFileName(module, pFileName, MAX_PATH);
string csFullPath(pFileName);
int nPos = csFullPath.rfind('\\');
if( nPos == string::npos )
return string("");
else
return csFullPath.substr(0, nPos);
}
void ConvertUtf8ToGBK(string& strUtf8)
{
int len=MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8.c_str(), -1, NULL,0);
WCHAR* wszGBK = new WCHAR[len+1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8.c_str(), -1, wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK=new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte (CP_ACP, 0, wszGBK, -1, szGBK, len, NULL,NULL);
strUtf8 = szGBK;
delete[] szGBK;
delete[] wszGBK;
}
void VdProcess(char *cRecv)
{
string vnc_path;
vnc_path = GetModuleDir() + "\\if\\cacvd_old.exe";
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
string sRecv(cRecv);
char szAppname[64], szIp[64], szPort[16], szPassword[256];
ZeroMemory(szAppname, sizeof(szAppname));
ZeroMemory(szIp, sizeof(szIp));
ZeroMemory(szPort, sizeof(szPort));
ZeroMemory(szPassword, sizeof(szPassword));
int nIndex(-1);
(nIndex = sRecv.find("|setting=")) != string::npos? nIndex += strlen("|setting="):
wrlog("CloudTerm\\cthandler.log","vd command error.",true);
wrlog("CloudTerm\\cthandler.log","cRecv:",true);
wrlog("CloudTerm\\cthandler.log",cRecv,true);
int i = 0;
while (cRecv[nIndex] != '*')
{
szAppname[i++] = cRecv[nIndex++];
}
szAppname[i]='\0';
wrlog("CloudTerm\\cthandler.log",szAppname,true);
string sAppname(szAppname);
if (szAppname[0] == '\0')
{
//memcpy(szAppname, "cacrd", strlen("cacrd"));
sAppname = "cacvd";
}
else
{
ConvertUtf8ToGBK(sAppname);
}
//wrlog("CloudTerm\\cthandler.log",szAppname,true);
wrlog("CloudTerm\\cthandler.log",sAppname.c_str(),true);
nIndex++;
i = 0;
while (cRecv[nIndex] != '*')
{
szIp[i++] = cRecv[nIndex++];
}
nIndex++;
i = 0;
while (cRecv[nIndex] != '|')
{
szPort[i++] = cRecv[nIndex++];
}
//if string is empty
if (szPort[0] == '\0')
{
memcpy(szPort, "5901", strlen("5901"));
}
string scmd;
scmd = vnc_path + " -title " + "\"" + sAppname + "\" " + szIp + ":" + szPort;
string logmsg("启动程序及传入参数:");
logmsg+=scmd;
//wrlog("CloudTerm\\cthandler.log",logmsg.c_str(),true);
if( !CreateProcess( NULL, // No module name (use command line)
const_cast <char *>(scmd.c_str()), // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
CREATE_NO_WINDOW, // 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
)
{
MessageBox(NULL, "网络无响应,请检查网络是否畅通?","",MB_OK);
char szError[256];
sprintf(szError, "CreateProcess failed (%d).", GetLastError());
wrlog("CloudTerm\\cthandler.log",szError,true);
}
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
wrlog("CloudTerm\\cthandler.log","vdcomplete",true);
}
int _tmain(int argc, _TCHAR* argv[])
{
if(argc<2)
{
MessageBox(NULL,"param error","error",0);
return -1;
}
string arg(argv[1]);
string logmsg="接收参数:";
logmsg+=argv[1];
string ccip(argv[2]);
wrlog("CloudTerm\\cthandler.log",logmsg.c_str(),true);
wrlog("CloudTerm\\cthandler.log",ccip.c_str(),true);
//接命令就发cc
//虚拟机控制台实现
int loc;
if ((loc = arg.find("|vmid")) != string::npos)
{
string SendInfo = arg;
SOCKET Sock = tmp_sender(ccip, "50000");
if(Sock==-1)
{return -1;}
if(send(Sock,"if=clickvmconsole\n",strlen("if=clickvmconsole\n"),0)==SOCKET_ERROR)
{
MessageBox(NULL, "网络无响应,请检查网络是否畅通?","",MB_OK);
wrlog("CloudTerm\\cthandler.log","send data failed.",true);
return -1;
}
int bytes;
char cRecv[1024];
memset(cRecv,0,1024);
if((bytes = recv(Sock, cRecv, 1024, 0)) == SOCKET_ERROR)
{
MessageBox(NULL, "网络无响应,请检查网络是否畅通?","",MB_OK);
wrlog("CloudTerm\\cthandler.log","recive data failed.",true);
return -1;
}
if (!strncmp(cRecv, "ok", 2))
{
//wrlog("CloudTerm\\cthandler.log",SendInfo.c_str(),true);
if(send(Sock,SendInfo.c_str(),strlen(SendInfo.c_str()),0)==SOCKET_ERROR)
{
MessageBox(NULL, "网络无响应,请检查网络是否畅通?","",MB_OK);
wrlog("CloudTerm\\cthandler.log","send data failed.",true);
GetLastError();
return -1;
}
if((bytes = recv(Sock, cRecv, 1024, 0)) == SOCKET_ERROR)
{
MessageBox(NULL, "网络无响应,请检查网络是否畅通?","",MB_OK);
wrlog("CloudTerm\\cthandler.log","recive data failed.",true);
return -1;
}
}
closesocket(Sock);
if (strstr(cRecv, "|infotype=vd|") != NULL)
{
VdProcess(cRecv);
}
}
return 0;
}