📄 rdesktop.java
字号:
if (Options.keylayout == -1) {
System.err.println(progname + ": Invalid key layout: "
+ arg);
usage();
}
break;
case 'l':
arg = g.getOptarg();
switch (arg.charAt(0)) {
case 'd':
case 'D':
logger.setLevel(Level.DEBUG);
break;
case 'i':
case 'I':
logger.setLevel(Level.INFO);
break;
case 'w':
case 'W':
logger.setLevel(Level.WARN);
break;
case 'e':
case 'E':
logger.setLevel(Level.ERROR);
break;
case 'f':
case 'F':
logger.setLevel(Level.FATAL);
break;
default:
System.err.println(progname + ": Invalid debug level: "
+ arg.charAt(0));
usage();
}
break;
case 'n':
Options.hostname = g.getOptarg();
break;
case 'p':
Options.password = g.getOptarg();
logonflags |= Rdp.RDP_LOGON_AUTO;
break;
case 's':
Options.command = g.getOptarg();
break;
case 'u':
Options.username = g.getOptarg();
break;
case 't':
arg = g.getOptarg();
try {
Options.port = Integer.parseInt(arg);
} catch (NumberFormatException nex) {
System.err.println(progname + ": Invalid port number: "
+ arg);
usage();
}
break;
case 'T':
Options.windowTitle = g.getOptarg().replace('_', ' ');
break;
case 'r':
Options.licence_path = g.getOptarg();
break;
case '?':
default:
usage();
break;
}
}
if (fKdeHack) {
Options.height -= 46;
}
String server = null;
if (g.getOptind() < args.length) {
int colonat = args[args.length - 1].indexOf(":", 0);
if (colonat == -1) {
server = args[args.length - 1];
} else {
server = args[args.length - 1].substring(0, colonat);
Options.port = Integer.parseInt(args[args.length - 1]
.substring(colonat + 1));
}
} else {
System.err.println(progname + ": A server name is required!");
usage();
}
VChannels channels = new VChannels();
// Initialise all RDP5 channels
if (Options.use_rdp5) {
// TODO: implement all relevant channels
if (Options.map_clipboard)
channels.register(clipChannel);
}
// Now do the startup...
logger.info("properJavaRDP version " + Version.version);
if (args.length == 0)
usage();
String java = System.getProperty("java.specification.version");
logger.info("Java version is " + java);
String os = System.getProperty("os.name");
String osvers = System.getProperty("os.version");
if (os.equals("Windows 2000") || os.equals("Windows XP"))
Options.built_in_licence = true;
logger.info("Operating System is " + os + " version " + osvers);
if (os.startsWith("Linux"))
Constants.OS = Constants.LINUX;
else if (os.startsWith("Windows"))
Constants.OS = Constants.WINDOWS;
else if (os.startsWith("Mac"))
Constants.OS = Constants.MAC;
if (Constants.OS == Constants.MAC)
Options.caps_sends_up_and_down = false;
Rdp5 RdpLayer = null;
Common.rdp = RdpLayer;
RdesktopFrame window = new RdesktopFrame_Localised();
window.setClip(clipChannel);
// Configure a keyboard layout
KeyCode_FileBased keyMap = null;
try {
// logger.info("looking for: " + "/" + keyMapPath + mapFile);
InputStream istr = Rdesktop.class.getResourceAsStream("/" + keyMapPath + mapFile);
// logger.info("istr = " + istr);
if (istr == null) {
logger.debug("Loading keymap from filename");
keyMap = new KeyCode_FileBased_Localised(keyMapPath + mapFile);
} else {
logger.debug("Loading keymap from InputStream");
keyMap = new KeyCode_FileBased_Localised(istr);
}
if(istr != null) istr.close();
Options.keylayout = keyMap.getMapCode();
} catch (Exception kmEx) {
String[] msg = { (kmEx.getClass() + ": " + kmEx.getMessage()) };
window.showErrorDialog(msg);
kmEx.printStackTrace();
Rdesktop.exit(0, null, null, true);
}
logger.debug("Registering keyboard...");
if (keyMap != null)
window.registerKeyboard(keyMap);
boolean[] deactivated = new boolean[1];
int[] ext_disc_reason = new int[1];
logger.debug("keep_running = " + keep_running);
while (keep_running) {
logger.debug("Initialising RDP layer...");
RdpLayer = new Rdp5(channels);
Common.rdp = RdpLayer;
logger.debug("Registering drawing surface...");
RdpLayer.registerDrawingSurface(window);
logger.debug("Registering comms layer...");
window.registerCommLayer(RdpLayer);
loggedon = false;
readytosend = false;
logger.info("Connecting to " + server + ":" + Options.port + " ...");
if(server.equalsIgnoreCase("localhost")) server = "127.0.0.1";
if (RdpLayer != null) {
// Attempt to connect to server on port Options.port
try {
RdpLayer.connect(Options.username, InetAddress.getByName(server), logonflags, Options.domain, Options.password, Options.command, Options.directory);
// Remove to get rid of sendEvent tool
if (showTools) {
toolFrame = new SendEvent(RdpLayer);
toolFrame.show();
}
// End
if (keep_running) {
/*
* By setting encryption to False here, we have an encrypted
* login packet but unencrypted transfer of other packets
*/
if (!Options.packet_encryption)
Options.encryption = false;
logger.info("Connection successful");
// now show window after licence negotiation
RdpLayer.mainLoop(deactivated, ext_disc_reason);
if (deactivated[0])
{
/* clean disconnect */
Rdesktop.exit(0, RdpLayer, window, true);
// return 0;
}
else
{
if (ext_disc_reason[0] == exDiscReasonAPIInitiatedDisconnect
|| ext_disc_reason[0] == exDiscReasonAPIInitiatedLogoff)
{
/* not so clean disconnect, but nothing to worry about */
Rdesktop.exit(0, RdpLayer, window, true);
//return 0;
}
if(ext_disc_reason[0] >= 2){
String reason = textDisconnectReason(ext_disc_reason[0]);
String msg[] = { "Connection terminated", reason};
window.showErrorDialog(msg);
logger.warn("Connection terminated: " + reason);
Rdesktop.exit(0, RdpLayer, window, true);
}
}
keep_running = false; // exited main loop
if (!readytosend) {
// maybe the licence server was having a comms
// problem, retry?
String msg1 = "The terminal server disconnected before licence negotiation completed.";
String msg2 = "Possible cause: terminal server could not issue a licence.";
String[] msg = { msg1, msg2 };
logger.warn(msg1);
logger.warn(msg2);
window.showErrorDialog(msg);
}
} // closing bracket to if(running)
// Remove to get rid of tool window
if (showTools)
toolFrame.dispose();
// End
}catch(ConnectionException e){
String msg[] = { "Connection Exception", e.getMessage() };
window.showErrorDialog(msg);
Rdesktop.exit(0, RdpLayer, window, true);
} catch (UnknownHostException e) {
error(e,RdpLayer,window,true);
}catch(SocketException s){
if(RdpLayer.isConnected()){
logger.fatal(s.getClass().getName() + " " + s.getMessage());
//s.printStackTrace();
error(s, RdpLayer, window, true);
Rdesktop.exit(0, RdpLayer, window, true);
}
}catch (RdesktopException e) {
String msg1 = e.getClass().getName();
String msg2 = e.getMessage();
logger.fatal(msg1 + ": " + msg2);
e.printStackTrace(System.err);
if (!readytosend) {
// maybe the licence server was having a comms
// problem, retry?
String msg[] = {
"The terminal server reset connection before licence negotiation completed.",
"Possible cause: terminal server could not connect to licence server.",
"Retry?" };
boolean retry = window.showYesNoErrorDialog(msg);
if (!retry) {
logger.info("Selected not to retry.");
Rdesktop.exit(0, RdpLayer, window, true);
} else {
if (RdpLayer != null && RdpLayer.isConnected()) {
logger.info("Disconnecting ...");
RdpLayer.disconnect();
logger.info("Disconnected");
}
logger.info("Retrying connection...");
keep_running = true; // retry
continue;
}
} else {
String msg[] = { e.getMessage() };
window.showErrorDialog(msg);
Rdesktop.exit(0, RdpLayer, window, true);
}
}catch (Exception e) {
logger.warn(e.getClass().getName() + " " + e.getMessage());
e.printStackTrace();
error(e, RdpLayer, window, true);
}
} else { // closing bracket to if(!rdp==null)
logger
.fatal("The communications layer could not be initiated!");
}
}
Rdesktop.exit(0, RdpLayer, window, true);
}
/**
* Disconnects from the server connected to through rdp and destroys the
* RdesktopFrame window.
* <p>
* Exits the application iff sysexit == true, providing return value n to
* the operating system.
*
* @param n
* @param rdp
* @param window
* @param sysexit
*/
public static void exit(int n, Rdp rdp, RdesktopFrame window, boolean sysexit) {
keep_running = false;
// Remove to get rid of tool window
if ((showTools) && (toolFrame != null))
toolFrame.dispose();
// End
if (rdp != null && rdp.isConnected()) {
logger.info("Disconnecting ...");
rdp.disconnect();
logger.info("Disconnected");
}
if (window != null) {
window.setVisible(false);
window.dispose();
}
System.gc();
if (sysexit && Constants.SystemExit){
if(!Common.underApplet) System.exit(n);
}
}
/**
* Displays an error dialog via the RdesktopFrame window containing the
* customised message emsg, and reports this through the logging system.
* <p>
* The application then exits iff sysexit == true
*
* @param emsg
* @param RdpLayer
* @param window
* @param sysexit
*/
public static void customError(String emsg, Rdp RdpLayer,
RdesktopFrame window, boolean sysexit) {
logger.fatal(emsg);
String[] msg = { emsg };
window.showErrorDialog(msg);
Rdesktop.exit(0, RdpLayer, window, true);
}
/**
* Displays details of the Exception e in an error dialog via the
* RdesktopFrame window and reports this through the logger, then prints a
* stack trace.
* <p>
* The application then exits iff sysexit == true
*
* @param e
* @param RdpLayer
* @param window
* @param sysexit
*/
public static void error(Exception e, Rdp RdpLayer, RdesktopFrame window, boolean sysexit) {
try {
String msg1 = e.getClass().getName();
String msg2 = e.getMessage();
logger.fatal(msg1 + ": " + msg2);
String[] msg = { msg1, msg2 };
window.showErrorDialog(msg);
//e.printStackTrace(System.err);
} catch (Exception ex) {
logger.warn("Exception in Rdesktop.error: " + ex.getClass().getName() + ": " + ex.getMessage() );
}
Rdesktop.exit(0, RdpLayer, window, sysexit);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -