📄 engine.java
字号:
package javatunnel;import java.util.*;import java.io.*;import java.net.*;/** * * 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. */public abstract class Engine { static String pathToHostsFile; ArrayList incomingHandlers = new ArrayList(); ArrayList outcomingHandlers = new ArrayList(); boolean terminate = false; Object lock = new Object(); ServerSocket serverSocket; abstract public void listen(); public void stop() { terminate = true; //The only way to exit the accept state of a serverSocket(not using // NIO) try { serverSocket.close(); } catch (IOException ioe) { //I dont care, im killing the socket anyway ioe.printStackTrace(); } synchronized (lock) { for (int i = 0; i < incomingHandlers.size(); i++) { ((DataHandler) incomingHandlers.get(i)).stopHandler(); ((DataHandler) outcomingHandlers.get(i)).stopHandler(); } } } void addToHostFile(String address) { //Check if the address is a name or a ip //If the address is a ip it makes no sense to modify the hosts file InetAddress ipAddress = null; try { ipAddress = InetAddress.getByName(address); if (ipAddress.getHostAddress().equals(address)) { //The address is an ip not a name, return return; } } catch (UnknownHostException uhe) { System.out.println("Unable to resolv: " + address); } File hostsFile = new File(pathToHostsFile); try { FileWriter writer = new FileWriter(hostsFile, true); writer.write("\n# JavaTunnel -- DO NOT EDIT OR CORRUPTION MAY HAPPEN -- " + address); writer.write("\n127.0.0.1\t" + address); writer.flush(); writer.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } public static String identifyHostsSystemFilePath() { //identify os family (Windows or Linux) String osName = System.getProperty("os.name").toLowerCase(); boolean isWindows = false; boolean isUnix = false; boolean isMac = false; boolean is9XorME = false; boolean isAtLeast2K = false; //Check for: //Linux (linux) //Solaris(solaris or sunos) //FreeBSD (freebsd or free/bsd) //Aix (aix) //Iris (irix) //HP-UX(mpe/ix or hp-ux) //Other unix(unix) //Other bsd(bsd) //Windows (windows) //Mac (mac) if (osName.indexOf("linux") != -1 || osName.indexOf("solaris") != -1 || osName.indexOf("sunos") != -1 || osName.indexOf("solaris") != -1 || osName.indexOf("freebsd") != -1 || osName.indexOf("free/bsd") != -1 || osName.indexOf("aix") != -1 || osName.indexOf("irix") != -1 || osName.indexOf("mpe/ix") != -1 || osName.indexOf("hp-ux") != -1 || osName.indexOf("unix") != -1 || osName.indexOf("bsd") != -1) { isUnix = true; } if (osName.indexOf("windows") != -1) { isWindows = true; if (osName.indexOf("95") != -1 || osName.indexOf("98") != -1 || osName.indexOf("me") != -1) is9XorME = true; else isAtLeast2K = true; } if (osName.indexOf("mac") != -1) { isMac = true; } String pathToHostsFile = null; if (isUnix) pathToHostsFile = "/etc/hosts"; if (is9XorME) pathToHostsFile = "c:\\windows\\hosts"; if (isAtLeast2K) pathToHostsFile = "c:\\windows\\system32\\drivers\\etc\\hosts"; if (!isUnix && !is9XorME && !isAtLeast2K) pathToHostsFile = "You must configure the path to the hosts file"; return pathToHostsFile; } public static void cleanHostsFile() { if (pathToHostsFile == null) pathToHostsFile = identifyHostsSystemFilePath(); StringBuffer newHostsContents = new StringBuffer(); try { File hostsFile = new File(pathToHostsFile); BufferedReader reader = new BufferedReader(new FileReader(hostsFile)); while (reader.ready()) { String line = reader.readLine(); if (line.startsWith("# JavaTunnel")) { //skip a line reader.readLine(); } else { newHostsContents.append(line); newHostsContents.append("\n"); } } //dump the filterted contents FileWriter writer = new FileWriter(hostsFile); writer.write(newHostsContents.toString()); writer.close(); } catch (IOException ioe) { if(ioe instanceof FileNotFoundException){ //Or the hosts file configuration is wrong or you dont have permision to touch the hosts file return; } ioe.printStackTrace(); } } /** * @return Returns the pathToHostsFile. */ public static String getPathToHostsFile() { return pathToHostsFile; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -