📄 javatunnelclient.java
字号:
package javatunnel;import org.apache.commons.cli.*;import org.apache.commons.lang.NumberUtils;/** * *JavaTunnel * *Copyright (C) 2002 Andr閟 Ederra * *This program is free software; you can redistribute it and/or *modify it under the terms of the GNU General Public License *as published by the Free Software Foundation; either version 2 *of the License, or (at your option) any later version. * *This program is distributed in the hope that it will be useful, *but WITHOUT ANY WARRANTY; without even the implied warranty of *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *GNU General Public License for more details. * *You should have received a copy of the GNU General Public License *along with this program; if not, write to the Free Software *Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Main client class that parses the commandline options and starts the JavaTunnel client core*/public class JavaTunnelClient { static String usage = " java javatunnel.JavaTunnelClient -mode amode -l aport -d destinationip -dport aport [-p proxyip] [-pport proxyport] [-s javatunnelserverip] [-sport javatunnelserverport] [-h]"; static String usageExample = "java javatunnel.JavaTunnelClient -mode Redirect -l 8080 -d 192.168.0.100 -dport 80"; public static Configuration configuration = new Configuration(); /** * JavaTunnel commandLine entrypoint. * @param args */ public static void main(String[] args) { //Create commandline options: mode,destination,destinationPort,proxy,proxyPort,server,severPort and localPort Options commandLineOptions = createCommandLineOptions(); //Parse the commandline and fill the Configuration object with the parameters configuration = parseCommandLine(commandLineOptions, args); Engine.cleanHostsFile(); Engine engine=EngineFactory.createEngine(configuration); engine.listen(); }/** * Method createCommandLineOptions. * Creates a Options(apache commons CLI Option object) containing the commandline parameter logic * @return Options * */ private static Options createCommandLineOptions() { Options commandLineOptions = new Options(); Option modeOption = new Option( "mode", true, "Type of operation mode of the client.\nAvailables modes: SSLProxy, Redirect and HTTPTunnel"); modeOption.setRequired(true); modeOption.setArgs(1); modeOption.setArgName("MODE"); Option destinationOption = new Option( "d", "destinationAddress", true, "Destination ip address"); destinationOption.setRequired(true); destinationOption.setArgs(1); destinationOption.setArgName("destionation ip"); Option destinationPortOption = new Option("dport", "destinationPort", true, "Destination port"); destinationPortOption.setRequired(true); destinationPortOption.setArgs(1); destinationPortOption.setArgName("destionation port"); Option proxyAddressOption = new Option("p", "proxyAddress", true, "Proxy ip address"); proxyAddressOption.setRequired(false); proxyAddressOption.setArgs(1); proxyAddressOption.setArgName("proxy ip"); Option proxyPortOption = new Option("pport", "proxyPort", true, "Proxy port"); proxyPortOption.setRequired(false); proxyPortOption.setArgs(1); proxyPortOption.setArgName("proxy port"); Option serverAddressOption = new Option( "s", "serverAddress", true, "JavaTunnel server ip address"); serverAddressOption.setRequired(false); serverAddressOption.setArgs(1); serverAddressOption.setArgName("server ip"); Option serverPortOption = new Option("sport", "serverPort", true, "JavaTunnel server port"); serverPortOption.setRequired(false); serverPortOption.setArgs(1); serverPortOption.setArgName("server port"); Option localPortOption = new Option( "l", "localPort", true, "Local port where JavaTunnel is listening"); localPortOption.setRequired(true); localPortOption.setArgs(1); localPortOption.setArgName("local port"); Option manageHostsOption = new Option("hosts", "help", true, "Manage system host file configuration"); manageHostsOption.setArgs(0); manageHostsOption.setRequired(false); Option helpOption = new Option("h", "help", true, "JavaTunnel usage"); helpOption.setArgs(0); helpOption.setRequired(false); commandLineOptions.addOption(modeOption); commandLineOptions.addOption(destinationOption); commandLineOptions.addOption(destinationPortOption); commandLineOptions.addOption(proxyAddressOption); commandLineOptions.addOption(proxyPortOption); commandLineOptions.addOption(serverAddressOption); commandLineOptions.addOption(serverPortOption); commandLineOptions.addOption(localPortOption); commandLineOptions.addOption(helpOption); commandLineOptions.addOption(manageHostsOption); return commandLineOptions; } /** * Method parseCommandLine. * Process the commandLine parameters according to a some commandline options * @param commandLineOptions Apache-commons CLI options object representing the commandLine logic * @param args CommandLine parameters String array * @return Configuration A javatunnel configuration object encapsulating the configuration */ private static Configuration parseCommandLine( Options commandLineOptions, String[] args) { HelpFormatter help = new HelpFormatter(); try { CommandLine commandLine = new GnuParser().parse(commandLineOptions, args); if (commandLine.hasOption("h")) { help.printHelp( 70, usage, "JavaTunnel commandline help.", commandLineOptions, "\nExample:" + usageExample); System.exit(1); } configuration.setMode(commandLine.getOptionValue("mode")); String localPort = commandLine.getOptionValue("l"); if (NumberUtils.isDigits(localPort)) configuration.setLocalPort(Integer.parseInt(localPort)); configuration.setDestinationAddress( commandLine.getOptionValue("d")); if (commandLine.hasOption("dport")) { String destinationPort = commandLine.getOptionValue("dport"); if (NumberUtils.isDigits(destinationPort)) { configuration.setDestinationPort( Integer.parseInt(destinationPort)); } } if (commandLine.hasOption("p")) { configuration.setProxyAddress(commandLine.getOptionValue("p")); } if (commandLine.hasOption("pport")) { String proxyPort = commandLine.getOptionValue("pport"); if (NumberUtils.isDigits(proxyPort)) { configuration.setProxyPort(Integer.parseInt(proxyPort)); } } if (commandLine.hasOption("s")) { configuration.setServerAddress(commandLine.getOptionValue("s")); } if (commandLine.hasOption("sport")) { String serverPort = commandLine.getOptionValue("sport"); if (NumberUtils.isDigits(serverPort)) { configuration.setServerPort(Integer.parseInt(serverPort)); } } if (commandLine.hasOption("hosts")) { configuration.setManageHostsFile(true); } return configuration; } catch (ParseException pe) { System.out.println("\n\t***Invalid parameters.***\n"); help.printHelp( 70, usage, "JavaTunnel commandline help.", commandLineOptions, "\nExample:" + usageExample); System.out.println(pe); System.exit(1); return null; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -