//CYCLONE MATRIX HOST NODE APPLICATION //Hermann L. Johnson, 2019 //Free for unmodified distribution and non-commercial use. /* Parameters ---------- Manages the operating parameters. */ import java.io.*; import java.util.*; public class Parameters { private String default_root_directory; private String default_config_file; private boolean HELP; private String MODE; private String SHASUM; private String MACFINDER_EXECUTABLE; private String DEFAULTGW_EXECUTABLE; private String CLIENT_DEFAULT_IP; private int CLIENT_REFRESH_TIME; private int CLIENT_MAX_PULL; private int[] CLIENT_UPLOAD_PORTS; private int CLIENT_DOWNLOAD_PORT; private int[] SERVER_UPLOAD_PORTS; private int[] SERVER_DOWNLOAD_PORTS; private int SERVER_MAX_CONNECTIONS; private String[] LOCATION_WHITELIST; private String[] LOCATION_BLACKLIST; private String ROOT_DIRECTORY; private String CONFIG_FILE; private String LOG_PARAMS; private String[] MAC_IN_BLACKLIST; private String[] MAC_OUT_BLACKLIST; private String[] MAC_IN_WHITELIST; private String[] MAC_OUT_WHITELIST; private int DAYS_OLD_LIMIT; private String IMPORT_DATAFILE; private String[] command_line_parameters; public Parameters() { default_root_directory = "CYCLONE"; default_config_file = ".config"; HELP = false; MODE = "gui"; SHASUM = "shasum"; MACFINDER_EXECUTABLE = "macfinder"; DEFAULTGW_EXECUTABLE = "defaultgwfinder"; CLIENT_DEFAULT_IP = "192.168.0.1"; CLIENT_REFRESH_TIME = 300; CLIENT_MAX_PULL = Integer.MAX_VALUE; CLIENT_UPLOAD_PORTS = new int[2]; CLIENT_UPLOAD_PORTS[0] = 443; CLIENT_UPLOAD_PORTS[1] = 444; CLIENT_DOWNLOAD_PORT = 80; SERVER_UPLOAD_PORTS = new int[2]; SERVER_UPLOAD_PORTS[0] = 443; SERVER_UPLOAD_PORTS[1] = 444; SERVER_DOWNLOAD_PORTS = new int[1]; SERVER_DOWNLOAD_PORTS[0] = 80; SERVER_MAX_CONNECTIONS = 10; LOCATION_WHITELIST = new String[1]; LOCATION_WHITELIST[0] = ""; LOCATION_BLACKLIST = new String[1]; LOCATION_BLACKLIST[0] = ""; ROOT_DIRECTORY = default_root_directory; CONFIG_FILE = default_config_file; LOG_PARAMS = ""; MAC_IN_BLACKLIST = new String[1]; MAC_IN_BLACKLIST[0] = ""; MAC_OUT_BLACKLIST = new String[1]; MAC_OUT_BLACKLIST[0] = ""; MAC_IN_WHITELIST = new String[1]; MAC_IN_WHITELIST[0] = ""; MAC_OUT_WHITELIST = new String[1]; MAC_OUT_WHITELIST[0] = ""; DAYS_OLD_LIMIT = 0; IMPORT_DATAFILE = ""; } public synchronized void take(String[] incomingParameters) { command_line_parameters = new String[incomingParameters.length]; for(int count=0;count= 0) HELP = true; if(combined.indexOf("-mode=gui") >= 0) MODE = "gui"; else if(combined.indexOf("-mode=stdio1") >= 0) MODE = "stdio1"; else if(combined.indexOf("-mode=stdio2") >= 0) MODE = "stdio2"; else if(combined.indexOf("-mode=stdio3") >= 0) MODE = "stdio3"; else if(combined.indexOf("-mode=stdio4") >= 0) MODE = "stdio4"; else if(combined.indexOf("-mode=stdiosingle1") >= 0) MODE = "stdiosingle1"; else if(combined.indexOf("-mode=stdiosingle2") >= 0) MODE = "stdiosingle2"; else if(combined.indexOf("-mode=stdiosingle3") >= 0) MODE = "stdiosingle3"; else if(combined.indexOf("-mode=stdiosingle4") >= 0) MODE = "stdiosingle4"; else if(combined.indexOf("-mode=secondary") >= 0) MODE = "secondary"; //if, because it starts with "secondary" and would skip else. if(combined.indexOf("-mode=secondarysinglec") >= 0) MODE = "secondarysinglec"; else if(combined.indexOf("-mode=secondarysingles") >= 0) MODE = "secondarysingles"; parm_value = takeString(combined,"-shasum_executable="); if(!parm_value.equals("")) SHASUM = parm_value; parm_value = takeString(combined,"-macfinder_executable="); if(!parm_value.equals("")) MACFINDER_EXECUTABLE = parm_value; parm_value = takeString(combined,"-defaultgw_executable="); if(!parm_value.equals("")) DEFAULTGW_EXECUTABLE = parm_value; parm_value = takeString(combined,"-client_default_ip="); if(!parm_value.equals("")) CLIENT_DEFAULT_IP = parm_value; int_parm_value = takeNumeric(combined,"-client_refresh_time="); if(int_parm_value>0) CLIENT_REFRESH_TIME = int_parm_value; int_parm_value = takeNumeric(combined,"-client_max_pull="); if(int_parm_value>0) CLIENT_MAX_PULL = int_parm_value; parm_value = takeString(combined,"-client_upload_ports="); if(!parm_value.equals("")) fillClientUploadPorts(parm_value); int_parm_value = takeNumeric(combined,"-client_download_port="); if(int_parm_value>0) CLIENT_DOWNLOAD_PORT = int_parm_value; parm_value = takeString(combined,"-server_upload_ports="); if(!parm_value.equals("")) fillServerUploadPorts(parm_value); parm_value = takeString(combined,"-server_download_ports="); if(!parm_value.equals("")) fillServerDownloadPorts(parm_value); int_parm_value = takeNumeric(combined,"-server_max_connections="); if(int_parm_value>0) SERVER_MAX_CONNECTIONS = int_parm_value; parm_value = takeString(combined,"-location_whitelist="); if(!parm_value.equals("")) fillLocationWhitelist(parm_value); parm_value = takeString(combined,"-location_blacklist="); if(!parm_value.equals("")) fillLocationBlacklist(parm_value); parm_value = takeString(combined,"-root_directory="); if(!parm_value.equals("")) ROOT_DIRECTORY = parm_value; parm_value = takeString(combined,"-config="); if(!parm_value.equals("")) CONFIG_FILE = parm_value; parm_value = takeString(combined,"-log="); if(!parm_value.equals("")) LOG_PARAMS = parm_value; parm_value = takeString(combined,"-mac_in_blacklist="); if(!parm_value.equals("")) fillMacInBlacklist(parm_value); parm_value = takeString(combined,"-mac_out_blacklist="); if(!parm_value.equals("")) fillMacOutBlacklist(parm_value); parm_value = takeString(combined,"-mac_in_whitelist="); if(!parm_value.equals("")) fillMacInWhitelist(parm_value); parm_value = takeString(combined,"-mac_out_whitelist="); if(!parm_value.equals("")) fillMacOutWhitelist(parm_value); int_parm_value = takeNumeric(combined,"-days_old_limit="); if(int_parm_value>0) DAYS_OLD_LIMIT = int_parm_value; parm_value = takeString(combined,"-import_datafile="); if(!parm_value.equals("")) IMPORT_DATAFILE = parm_value; } catch(Exception e) { System.err.println("Error Parameters:1A- Should not be possible."); } } private synchronized void fillServerUploadPorts(String forbreaking) { try { StringTokenizer st = new StringTokenizer(forbreaking,","); if(st.countTokens()==1) SERVER_UPLOAD_PORTS[0] = Integer.parseInt(forbreaking); else { int currentIndex = 0; SERVER_UPLOAD_PORTS = new int[st.countTokens()]; while (st.hasMoreTokens()) { SERVER_UPLOAD_PORTS[currentIndex] = Integer.parseInt( st.nextToken()); currentIndex++; } } } catch(Exception e) { System.err.println("Problem parsing server ports."); SERVER_UPLOAD_PORTS = new int[2]; SERVER_UPLOAD_PORTS[0] = 443; SERVER_UPLOAD_PORTS[1] = 444; } } private synchronized void fillClientUploadPorts(String forbreaking) { try { StringTokenizer st = new StringTokenizer(forbreaking,","); if(st.countTokens()==1) CLIENT_UPLOAD_PORTS[0] = Integer.parseInt(forbreaking); else { int currentIndex = 0; CLIENT_UPLOAD_PORTS = new int[st.countTokens()]; while (st.hasMoreTokens()) { CLIENT_UPLOAD_PORTS[currentIndex] = Integer.parseInt( st.nextToken()); currentIndex++; } } } catch(Exception e) { System.err.println("Problem parsing server ports."); CLIENT_UPLOAD_PORTS = new int[1]; CLIENT_UPLOAD_PORTS[0] = 80; } } private synchronized void fillServerDownloadPorts(String forbreaking) { try { StringTokenizer st = new StringTokenizer(forbreaking,","); if(st.countTokens()==1) SERVER_DOWNLOAD_PORTS[0] = Integer.parseInt(forbreaking); else { int currentIndex = 0; SERVER_DOWNLOAD_PORTS = new int[st.countTokens()]; while (st.hasMoreTokens()) { SERVER_DOWNLOAD_PORTS[currentIndex] = Integer.parseInt( st.nextToken()); currentIndex++; } } } catch(Exception e) { System.err.println("Problem parsing server ports."); SERVER_DOWNLOAD_PORTS = new int[1]; SERVER_DOWNLOAD_PORTS[0] = 80; } } private synchronized void fillLocationWhitelist(String forbreaking) { try { StringTokenizer st = new StringTokenizer(forbreaking,","); if(st.countTokens()==1) LOCATION_WHITELIST[0] = forbreaking; else { int currentIndex = 0; LOCATION_WHITELIST = new String[st.countTokens()]; while (st.hasMoreTokens()) { LOCATION_WHITELIST[currentIndex] = st.nextToken(); currentIndex++; } } } catch(Exception e) { System.err.println("Problem parsing server ports."); LOCATION_WHITELIST = new String[1]; LOCATION_WHITELIST[0] = ""; } } private synchronized void fillLocationBlacklist(String forbreaking) { try { StringTokenizer st = new StringTokenizer(forbreaking,","); if(st.countTokens()==1) LOCATION_BLACKLIST[0] = forbreaking; else { int currentIndex = 0; LOCATION_BLACKLIST = new String[st.countTokens()]; while (st.hasMoreTokens()) { LOCATION_BLACKLIST[currentIndex] = st.nextToken(); currentIndex++; } } } catch(Exception e) { System.err.println("Problem parsing server ports."); LOCATION_BLACKLIST = new String[1]; LOCATION_BLACKLIST[0] = ""; } } private synchronized void fillMacInBlacklist(String forbreaking) { try { StringTokenizer st = new StringTokenizer(forbreaking,","); if(st.countTokens()==1) MAC_IN_BLACKLIST[0] = forbreaking; else { int currentIndex = 0; MAC_IN_BLACKLIST = new String[st.countTokens()]; while (st.hasMoreTokens()) { MAC_IN_BLACKLIST[currentIndex] = st.nextToken(); currentIndex++; } } } catch(Exception e) { System.err.println("Problem parsing server ports."); MAC_IN_BLACKLIST = new String[1]; MAC_IN_BLACKLIST[0] = ""; } } private synchronized void fillMacOutBlacklist(String forbreaking) { try { StringTokenizer st = new StringTokenizer(forbreaking,","); if(st.countTokens()==1) MAC_OUT_BLACKLIST[0] = forbreaking; else { int currentIndex = 0; MAC_OUT_BLACKLIST = new String[st.countTokens()]; while (st.hasMoreTokens()) { MAC_OUT_BLACKLIST[currentIndex] = st.nextToken(); currentIndex++; } } } catch(Exception e) { System.err.println("Problem parsing server ports."); MAC_OUT_BLACKLIST = new String[1]; MAC_OUT_BLACKLIST[0] = ""; } } private synchronized void fillMacInWhitelist(String forbreaking) { try { StringTokenizer st = new StringTokenizer(forbreaking,","); if(st.countTokens()==1) MAC_IN_WHITELIST[0] = forbreaking; else { int currentIndex = 0; MAC_IN_WHITELIST = new String[st.countTokens()]; while (st.hasMoreTokens()) { MAC_IN_WHITELIST[currentIndex] = st.nextToken(); currentIndex++; } } } catch(Exception e) { System.err.println("Problem parsing server ports."); MAC_IN_WHITELIST = new String[1]; MAC_IN_WHITELIST[0] = ""; } } private synchronized void fillMacOutWhitelist(String forbreaking) { try { StringTokenizer st = new StringTokenizer(forbreaking,","); if(st.countTokens()==1) MAC_OUT_WHITELIST[0] = forbreaking; else { int currentIndex = 0; MAC_OUT_WHITELIST = new String[st.countTokens()]; while (st.hasMoreTokens()) { MAC_OUT_WHITELIST[currentIndex] = st.nextToken(); currentIndex++; } } } catch(Exception e) { System.err.println("Problem parsing server ports."); MAC_OUT_WHITELIST = new String[1]; MAC_OUT_WHITELIST[0] = ""; } } private static int takeNumeric(String combined, String searchFor) { int returnInt = 0; String identified = ""; try { identified = takeString(combined,searchFor); if(!identified.equals("")) returnInt = Integer.parseInt(identified); else returnInt = 0; } catch(Exception e) { System.err.println("Bad numeric parameter found!"); returnInt = 0; } return returnInt; } private static String takeString(String combined, String searchFor) { int indexA = 0; int indexB = 0; if((indexA=combined.indexOf(searchFor)) >= 0) { if((indexB=combined.indexOf(" ",indexA)) >= 0) { return combined.substring(combined.indexOf( "=",indexA)+1,indexB); } } return ""; } public synchronized void writeConfiguration() { File configFile; try { //If the default configuration file location is used if(CONFIG_FILE.equals(default_config_file)) configFile = new File(ROOT_DIRECTORY,CONFIG_FILE); else configFile = new File(CONFIG_FILE); //In case the directory is not present. configFile.getParentFile().mkdir(); PrintStream outfilestream = new PrintStream( new BufferedOutputStream( new FileOutputStream(configFile))); outputParametersToStream(outfilestream); outfilestream.flush(); outfilestream.close(); } catch(Exception e) { System.err.println("Problem writing configuration file."); } } private synchronized void processConfigFileLine(String currentLine) { try { StringTokenizer st = new StringTokenizer(currentLine," "); st.nextToken(); if(currentLine.startsWith("MODE")) MODE = st.nextToken(); else if(currentLine.startsWith("SHASUM")) SHASUM = st.nextToken(); else if(currentLine.startsWith("MACFINDER_EXECUTABLE")) MACFINDER_EXECUTABLE = st.nextToken(); else if(currentLine.startsWith("DEFAULTGW_EXECUTABLE")) DEFAULTGW_EXECUTABLE = st.nextToken(); else if(currentLine.startsWith("CLIENT_DEFAULT_IP")) CLIENT_DEFAULT_IP = st.nextToken(); else if(currentLine.startsWith("CLIENT_REFRESH_TIME")) CLIENT_REFRESH_TIME = Integer.parseInt(st.nextToken()); else if(currentLine.startsWith("CLIENT_MAX_PULL")) CLIENT_MAX_PULL = Integer.parseInt(st.nextToken()); else if(currentLine.startsWith("CLIENT_UPLOAD_PORTS")) fillClientUploadPorts(st.nextToken()); else if(currentLine.startsWith("CLIENT_DOWNLOAD_PORT")) CLIENT_DOWNLOAD_PORT = Integer.parseInt(st.nextToken()); else if(currentLine.startsWith("SERVER_UPLOAD_PORTS")) fillServerUploadPorts(st.nextToken()); else if(currentLine.startsWith("SERVER_DOWNLOAD_PORTS")) fillServerDownloadPorts(st.nextToken()); else if(currentLine.startsWith("SERVER_MAX_CONNECTIONS")) SERVER_MAX_CONNECTIONS = Integer.parseInt(st.nextToken()); else if(currentLine.startsWith("LOCATION_WHITELIST")) fillLocationWhitelist(st.nextToken()); else if(currentLine.startsWith("LOCATION_BLACKLIST")) fillLocationBlacklist(st.nextToken()); else if(currentLine.startsWith("ROOT_DIRECTORY")) ROOT_DIRECTORY = st.nextToken(); else if(currentLine.startsWith("CONFIG_FILE")) CONFIG_FILE = st.nextToken(); else if(currentLine.startsWith("LOG_PARAMS")) LOG_PARAMS = st.nextToken(); else if(currentLine.startsWith("MAC_IN_BLACKLIST")) fillMacInBlacklist(st.nextToken()); else if(currentLine.startsWith("MAC_OUT_BLACKLIST")) fillMacOutBlacklist(st.nextToken()); else if(currentLine.startsWith("MAC_IN_WHITELIST")) fillMacInWhitelist(st.nextToken()); else if(currentLine.startsWith("MAC_OUT_WHITELIST")) fillMacOutWhitelist(st.nextToken()); else if(currentLine.startsWith("DAYS_OLD_LIMIT")) DAYS_OLD_LIMIT = Integer.parseInt(st.nextToken()); else if(currentLine.startsWith("IMPORT_DATAFILE")) IMPORT_DATAFILE = st.nextToken(); } catch(Exception e) { System.err.println("Error processing Config line: "+currentLine); } } public synchronized void readConfigFile() { //This method does not override command line parameters, therefore //the take(String[]) method is called at the end. File configFile; try { //If the default configuration file location is used if(CONFIG_FILE.equals(default_config_file)) configFile = new File(ROOT_DIRECTORY,CONFIG_FILE); else configFile = new File(CONFIG_FILE); String currentLine; BufferedReader reader = new BufferedReader( new FileReader(configFile)); while(reader.ready()) { currentLine = reader.readLine(); processConfigFileLine(currentLine); } } catch(Exception e) { System.err.println("Problem reading configuration file."); } take(command_line_parameters); } public synchronized void outputParametersToStream(PrintStream stream) { stream.println("HELP: "+get_HELP()); stream.println("MODE: "+get_MODE()); stream.println("SHASUM: "+get_SHASUM()); stream.println("MACFINDER_EXECUTABLE: "+get_MACFINDER_EXECUTABLE()); stream.println("DEFAULTGW_EXECUTABLE: "+get_DEFAULTGW_EXECUTABLE()); stream.println("CLIENT_DEFAULT_IP: "+get_CLIENT_DEFAULT_IP()); stream.println("CLIENT_REFRESH_TIME: "+get_CLIENT_REFRESH_TIME()); stream.println("CLIENT_MAX_PULL: "+get_CLIENT_MAX_PULL()); stream.print ("CLIENT_UPLOAD_PORTS: "); for(int count=0;count