forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
389 lines (337 loc) · 12.8 KB
/
Client.java
File metadata and controls
389 lines (337 loc) · 12.8 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
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package common;
import java.awt.Frame;
import java.awt.ScrollPane;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.net.InetSocketAddress;
import java.util.Arrays;
import rdpclient.RdpClient;
import streamer.Element;
import streamer.Pipeline;
import streamer.PipelineImpl;
import streamer.SocketWrapper;
import streamer.SocketWrapperImpl;
import streamer.apr.AprSocketWrapperImpl;
import streamer.bco.BcoSocketWrapperImpl;
import streamer.ssl.SSLState;
import vncclient.VncClient;
import common.opt.IntOption;
import common.opt.Option;
import common.opt.OptionParser;
import common.opt.StringEnumerationOption;
import common.opt.StringOption;
public class Client {
enum Protocol {
NONE, VNC, RDP, HYPERV
}
// Common options
private final Option help = new Option() {
{
name = "--help";
alias = "-h";
description = "Show this help text.";
}
};
private final Option debugLink = new Option() {
{
name = "--debug-link";
alias = "-DL";
description = "Print debugging messages when packets are transferred via links.";
}
};
private final Option debugElement = new Option() {
{
name = "--debug-element";
alias = "-DE";
description = "Print debugging messages when packets are received or sent by elements.";
}
};
private final Option debugPipeline = new Option() {
{
name = "--debug-pipeline";
alias = "-DP";
description = "Print debugging messages in pipelines.";
}
};
private final StringOption hostName = new StringOption() {
{
name = "--host";
alias = "-n";
aliases = new String[] {"--host-name"};
required = true;
description = "Name or IP address of host to connect to.";
}
};
private final IntOption canvasWidth = new IntOption() {
{
name = "--width";
alias = "-W";
value = 1024;
description = "Width of canvas.";
}
};
private final IntOption canvasHeight = new IntOption() {
{
name = "--height";
alias = "-H";
value = 768;
description = "Height of canvas.";
}
};
// Protocol specific options
private final IntOption vncPort = new IntOption() {
{
name = "--port";
alias = "-p";
value = 5901;
description = "Port of VNC display server to connect to. Calculate as 5900 + display number, e.g. 5900 for display #0, 5901 for display #1, and so on.";
}
};
private final IntOption rdpPort = new IntOption() {
{
name = "--port";
alias = "-p";
value = 3389;
description = "Port of RDP server to connect to.";
}
};
private final IntOption hyperVPort = new IntOption() {
{
name = "--port";
alias = "-p";
value = 2179;
description = "Port of HyperV server to connect to.";
}
};
private final StringOption password = new StringOption() {
{
name = "--password";
alias = "-P";
required = true;
description = "Password to use.";
}
};
private final StringOption rdpPassword = new StringOption() {
{
name = "--password";
alias = "-P";
required = false;
description = "Password to use. If omitted, then login screen will be shown.";
}
};
private final StringOption userName = new StringOption() {
{
name = "--user";
alias = "-U";
value = "Administrator";
description = "User name to use.";
}
};
private final StringOption domain = new StringOption() {
{
name = "--domain";
alias = "-D";
value = "Workgroup";
description = "NTLM domain to login into.";
}
};
private final StringOption hyperVInstanceId = new StringOption() {
{
name = "--instance";
alias = "-i";
required = true;
description = "HyperV instance ID to use.";
}
};
private final StringEnumerationOption sslImplementation = new StringEnumerationOption() {
{
name = "--ssl-implementation";
alias = "-j";
value = "apr";
choices = new String[] {"jre", "apr", "bco"};
description = "Select SSL engine to use: JRE standard implementation, Apache Portable Runtime native library, BonuncyCastle.org implementation.";
}
};
private final Option[] commonOptions = new Option[] {help, debugLink, debugElement, debugPipeline, hostName, canvasWidth, canvasHeight};
private final Option[] vncOptions = new Option[] {vncPort, password};
private final Option[] rdpOptions = new Option[] {sslImplementation, rdpPort, domain, userName, rdpPassword};
private final Option[] hyperVOptions = new Option[] {sslImplementation, hyperVPort, hyperVInstanceId, domain, userName, password};
private static Frame frame;
private static SocketWrapper socket;
private static ScrollPane scroller;
private static ScreenDescription screen;
private static BufferedImageCanvas canvas;
private InetSocketAddress address;
private void help() {
System.out.println("Usage: \n java common.Client vnc|rdp|hyperv OPTIONS\n");
System.out.println(Option.toHelp("Common options", commonOptions));
System.out.println(Option.toHelp("VNC options", vncOptions));
System.out.println(Option.toHelp("RDP options", rdpOptions));
System.out.println(Option.toHelp("HyperV options", hyperVOptions));
}
public void runClient(String[] args) {
try {
Protocol protocol = parseOptions(args);
if (protocol == Protocol.NONE)
return;
System.setProperty("streamer.Link.debug", "" + debugLink.used);
System.setProperty("streamer.Element.debug", "" + debugElement.used);
System.setProperty("streamer.Pipeline.debug", "" + debugPipeline.used);
SSLState sslState = new SSLState();
// Create socket wrapper
if ("jre".equals(sslImplementation.value)) {
socket = new SocketWrapperImpl("socket", sslState);
} else if ("apr".equals(sslImplementation.value)) {
socket = new AprSocketWrapperImpl("socket", sslState);
} else if ("bco".equals(sslImplementation.value)) {
socket = new BcoSocketWrapperImpl("socket", sslState);
} else {
throw new RuntimeException("Unexpected option value: \"" + sslImplementation.value + "\". " + sslImplementation.help());
}
screen = new ScreenDescription();
canvas = new BufferedImageCanvas(canvasWidth.value, canvasHeight.value);
screen.addSizeChangeListener(new SizeChangeListener() {
@Override
public void sizeChanged(int width, int height) {
if (canvas != null) {
canvas.setCanvasSize(width, height);
if (scroller != null)
scroller.setSize(canvas.getWidth(), canvas.getHeight());
}
}
});
assemblePipeline(setMainElementAndAddressBasedOnProtocol(protocol, sslState));
frame = createVncClientMainWindow(canvas, protocol.toString() + " " + hostName.value, new WindowAdapter() {
@Override
public void windowClosing(WindowEvent evt) {
shutdown();
}
});
try {
// Connect socket to remote server and run main loop(s)
socket.connect(address);
} finally {
shutdown();
}
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
protected static void assemblePipeline(Element main) {
Pipeline pipeline = new PipelineImpl("Client");
pipeline.add(socket, main);
pipeline.link("socket", main.getId(), "socket");
pipeline.validate();
}
private Element setMainElementAndAddressBasedOnProtocol(Protocol protocol, SSLState sslState) {
Element main;
switch (protocol) {
case VNC:
address = new InetSocketAddress(hostName.value, vncPort.value);
main = new VncClient("client", password.value, screen, canvas);
break;
case RDP:
address = new InetSocketAddress(hostName.value, rdpPort.value);
main = new RdpClient("client", hostName.value, domain.value, userName.value, rdpPassword.value, null, screen, canvas, sslState);
break;
case HYPERV:
address = new InetSocketAddress(hostName.value, hyperVPort.value);
main = new RdpClient("client", hostName.value, domain.value, userName.value, password.value, hyperVInstanceId.value, screen, canvas, sslState);
break;
default:
address = null;
main = null;
}
return main;
}
private Protocol parseOptions(String[] args) {
String protocolName = (args.length > 0) ? args[0] : "";
Protocol protocol;
Option[] options;
try {
protocol = Protocol.valueOf(protocolName);
} catch (IllegalArgumentException e) {
protocol = Protocol.NONE;
}
switch (protocol) {
case VNC:
options = join(commonOptions, vncOptions);
break;
case RDP:
options = join(commonOptions, rdpOptions);
break;
case HYPERV:
options = join(commonOptions, hyperVOptions);
break;
default:
help();
return Protocol.NONE;
}
// Parse all options for given protocol
String[] arguments = OptionParser.parseOptions(args, 1, options);
if (arguments.length > 0) {
System.err.println("[Client] ERROR: Arguments are not allowed here. Check command syntax. Extra arguments: \"" + Arrays.toString(arguments) + "\".");
help();
return Protocol.NONE;
}
if (help.used) {
help();
return Protocol.NONE;
}
return protocol;
}
protected static void shutdown() {
if (frame != null) {
frame.setVisible(false);
frame.dispose();
}
if (socket != null)
socket.shutdown();
}
protected static Frame createVncClientMainWindow(BufferedImageCanvas canvas, String title, WindowListener windowListener) {
// Create AWT windows
Frame frame = new Frame(title + " - RDP");
// Use scrolling pane to support screens, which are larger than ours
scroller = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
scroller.add(canvas);
scroller.setSize(canvas.getWidth(), canvas.getHeight());
frame.add(scroller);
frame.pack();
frame.setVisible(true);
frame.addWindowListener(windowListener);
return frame;
}
/**
* Join two arrays with options and return new array.
*/
private Option[] join(Option[] a1, Option[] a2) {
// Extend first array
Option[] result = Arrays.copyOf(a1, a1.length + a2.length);
// Append second array to first
for (int i = 0, p = a1.length; i < a2.length; i++, p++)
result[p] = a2[i];
return result;
}
public static void main(String args[]) {
// *DEBUG*/System.setProperty("javax.net.debug", "ssl");
// * DEBUG */System.setProperty("javax.net.debug", "ssl:record:packet");
new Client().runClient(args);
}
}