private void exportParameters() { synchronized (exported_parameters) { if (!exported_parameters_dirty) { return; } exported_parameters_dirty = false; try { TreeMap<String, String> tm = new TreeMap<String, String>(); Set<String> exported_keys = new HashSet<String>(); for (String[] entry : exported_parameters.values()) { String key = entry[0]; String value = entry[1]; exported_keys.add(key); if (value != null) { tm.put(key, value); } } for (Map.Entry<String, String> entry : imported_parameters.entrySet()) { String key = entry.getKey(); if (!exported_keys.contains(key)) { tm.put(key, entry.getValue()); } } File parent_dir = new File(SystemProperties.getUserPath()); File props = new File(parent_dir, "exported_params.properties"); PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(props), "UTF-8")); try { for (Map.Entry<String, String> entry : tm.entrySet()) { pw.println(entry.getKey() + "=" + entry.getValue()); } } finally { pw.close(); } } catch (Throwable e) { e.printStackTrace(); } } }
static void readConfig() { tracker_ip = COConfigurationManager.getStringParameter("Tracker IP", ""); tracker_ip = UrlUtils.expandIPV6Host(tracker_ip); String override_ips = COConfigurationManager.getStringParameter("Override Ip", ""); StringTokenizer tok = new StringTokenizer(override_ips, ";"); Map new_override_map = new HashMap(); while (tok.hasMoreTokens()) { String ip = tok.nextToken().trim(); if (ip.length() > 0) { new_override_map.put(AENetworkClassifier.categoriseAddress(ip), ip); } } override_map = new_override_map; InetAddress bad = NetworkAdmin.getSingleton().getSingleHomedServiceBindAddress(); if (bad == null || bad.isAnyLocalAddress()) { bind_ip = ""; } else { bind_ip = bad.getHostAddress(); } }
public static void setAZTracker(URL tracker_url, boolean az_tracker) { String key = tracker_url.getHost() + ":" + tracker_url.getPort(); synchronized (az_trackers) { boolean changed = false; if (az_trackers.get(key) == null) { if (az_tracker) { az_trackers.put(key, new Long(SystemTime.getCurrentTime())); changed = true; } } else { if (!az_tracker) { if (az_trackers.remove(key) != null) { changed = true; } } } if (changed) { COConfigurationManager.setParameter("Tracker Client AZ Instances", az_trackers); } } }
public void addParameterListener(String parameter, ParameterListener new_listener) { if (parameter == null || new_listener == null) { return; } synchronized (parameterListenerz) { ParameterListener[] listeners = parameterListenerz.get(parameter); if (listeners == null) { parameterListenerz.put(parameter, new ParameterListener[] {new_listener}); } else { ParameterListener[] new_listeners = new ParameterListener[listeners.length + 1]; if (Constants.IS_CVS_VERSION && listeners.length > 100) { Debug.out(parameter); } int pos; if (new_listener instanceof PriorityParameterListener) { new_listeners[0] = new_listener; pos = 1; } else { new_listeners[listeners.length] = new_listener; pos = 0; } for (int i = 0; i < listeners.length; i++) { ParameterListener existing_listener = listeners[i]; if (existing_listener == new_listener) { return; } new_listeners[pos++] = existing_listener; } if (DEBUG_PARAMETER_LISTENERS) { System.out.println(parameter + "->" + new_listeners.length); } parameterListenerz.put(parameter, new_listeners); } } }
private ConfigurationManager(Map data) { // default state of play for config initialised from map is debug log files off unless already // specified if (data.get("Logger.DebugFiles.Enabled") == null) { data.put("Logger.DebugFiles.Enabled", new Long(0)); } propertiesMap = new ConcurrentHashMapWrapper<String, Object>(data); }
public void load(String filename) { Map data = FileUtil.readResilientConfigFile(filename, false); // horrendous recursive loading going on here due to logger + config depedencies. If already // loaded // then use the existing data as it might have already been written to... if (propertiesMap == null) { ConcurrentHashMapWrapper<String, Object> c_map = new ConcurrentHashMapWrapper<String, Object>(data.size() + 256, 0.75f, 8); c_map.putAll(data); propertiesMap = c_map; } /* * Can't do this yet. Sometimes, there's a default set to x, but the code * calls get..Parameter(..., y). y != x. When the user sets the the parameter * to x, we remove it from the list. Later, the get..Parameter(.., y) returns * y because there is no entry. * * The solution is to not allow get..Parameter(.., y) when there's a default * value. Another reason to not allow it is that having two defaults confuses * coders. * // Remove entries that are default. Saves memory, reduces // file size when saved again ConfigurationDefaults def = ConfigurationDefaults.getInstance(); Iterator it = new TreeSet(propertiesMap.keySet()).iterator(); while (it.hasNext()) { String key = (String)it.next(); Object defValue = def.getDefaultValueAsObject(key); if (defValue == null) continue; if (defValue instanceof Long) { int iDefValue = ((Long)defValue).intValue(); int iValue = getIntParameter(key, iDefValue); if (iValue == iDefValue) propertiesMap.remove(key); } if (defValue instanceof String) { String sDefValue = defValue.toString(); String sValue = getStringParameter(key, sDefValue); if (sValue.compareTo(sDefValue) == 0) propertiesMap.remove(key); } } */ }
public void removeParameterListener(String parameter, ParameterListener listener) { if (parameter == null || listener == null) { return; } synchronized (parameterListenerz) { ParameterListener[] listeners = parameterListenerz.get(parameter); if (listeners == null) { return; } if (listeners.length == 1) { if (listeners[0] == listener) { parameterListenerz.remove(parameter); } } else { ParameterListener[] new_listeners = new ParameterListener[listeners.length - 1]; int pos = 0; for (int i = 0; i < listeners.length; i++) { ParameterListener existing_listener = listeners[i]; if (existing_listener != listener) { if (pos == new_listeners.length) { return; } new_listeners[pos++] = existing_listener; } } if (DEBUG_PARAMETER_LISTENERS) { System.out.println(parameter + "->" + new_listeners.length); } parameterListenerz.put(parameter, new_listeners); } } }
private void notifyParameterListeners(String parameter) { ParameterListener[] listeners; synchronized (parameterListenerz) { listeners = parameterListenerz.get(parameter); } if (listeners == null) { return; } for (ParameterListener listener : listeners) { if (listener != null) { try { listener.parameterChanged(parameter); } catch (Throwable e) { Debug.printStackTrace(e); } } } }
public static String adjustHostFromHosting(String host_in) { if (tracker_ip.length() > 0) { String address_type = AENetworkClassifier.categoriseAddress(host_in); String target_ip = (String) override_map.get(address_type); if (target_ip == null) { target_ip = tracker_ip; } if (host_in.equals("127.0.0.1")) { // System.out.println( "adjustHostFromHosting: " + host_in + " -> " + tracker_ip ); return (target_ip); } if (host_in.equals(bind_ip)) { // System.out.println( "adjustHostFromHosting: " + host_in + " -> " + tracker_ip ); return (target_ip); } } return (host_in); }
public void registerExportedParameter(String name, String key) { synchronized (exported_parameters) { String[] entry = exported_parameters.get(key); if (entry == null) { entry = new String[] {name, imported_parameters.remove(name)}; exported_parameters.put(key, entry); } } addParameterListener(key, exportable_parameter_listener); updateExportableParameter(key); }
private void updateExportableParameter(String key) { Object o_value = getParameter(key); String value; if (o_value == null) { value = null; } else if (o_value instanceof byte[]) { try { value = new String((byte[]) o_value, "UTF-8"); } catch (UnsupportedEncodingException e) { value = null; } } else { value = String.valueOf(o_value); } synchronized (exported_parameters) { String[] entry = exported_parameters.get(key); if (entry != null) { String existing = entry[1]; if (existing != value) { if (existing == null || value == null || !existing.equals(value)) { entry[1] = value; if (!exported_parameters_dirty) { exported_parameters_dirty = true; new DelayedEvent( "epd", 5000, new AERunnable() { @Override public void runSupport() { exportParameters(); } }); } } } } } }
public static boolean isAZTracker(URL tracker_url) { String host = tracker_url.getHost(); if (Constants.isAzureusDomain(host)) { return (true); } synchronized (az_trackers) { return (az_trackers.containsKey(host + ":" + tracker_url.getPort())); } }
public static boolean isUDPProbeOK(URL tracker_url) { String host = tracker_url.getHost(); if (Constants.isAzureusDomain(host)) { return (false); } synchronized (udp_probe_results) { return (udp_probe_results.containsKey(host)); } }
public AuthenticatorWindow() { SESecurityManager.addPasswordListener(this); // System.out.println( "AuthenticatorWindow"); Map cache = COConfigurationManager.getMapParameter(CONFIG_PARAM, new HashMap()); try { Iterator it = cache.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); String key = (String) entry.getKey(); Map value = (Map) entry.getValue(); String user = new String((byte[]) value.get("user"), "UTF-8"); char[] pw = new String((byte[]) value.get("pw"), "UTF-8").toCharArray(); auth_cache.put(key, new authCache(key, new PasswordAuthentication(user, pw), true)); } } catch (Throwable e) { COConfigurationManager.setParameter(CONFIG_PARAM, new HashMap()); Debug.printStackTrace(e); } }
public static void setUDPProbeResult(URL tracker_url, boolean probe_ok) { String key = tracker_url.getHost(); synchronized (udp_probe_results) { boolean changed = false; if (udp_probe_results.get(key) == null) { if (probe_ok) { // arbitrary max size here just in case something weird happens if (udp_probe_results.size() > 512) { udp_probe_results.clear(); } udp_probe_results.put(key, new Long(SystemTime.getCurrentTime())); changed = true; } } else { if (!probe_ok) { if (udp_probe_results.remove(key) != null) { changed = true; } } } if (changed) { COConfigurationManager.setParameter("Tracker Client UDP Probe Results", udp_probe_results); } } }
private void loadExportedParameters() { synchronized (exported_parameters) { try { File parent_dir = new File(SystemProperties.getUserPath()); File props = new File(parent_dir, "exported_params.properties"); if (props.exists()) { LineNumberReader lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(props), "UTF-8")); try { while (true) { String line = lnr.readLine(); if (line == null) { break; } String[] bits = line.split("="); if (bits.length == 2) { String key = bits[0].trim(); String value = bits[1].trim(); if (key.length() > 0 && value.length() > 0) { imported_parameters.put(key, value); } } } } finally { lnr.close(); } } } catch (Throwable e) { e.printStackTrace(); } } COConfigurationManager.setIntDefault("instance.port", Constants.INSTANCE_PORT); registerExportedParameter("instance.port", "instance.port"); }
public void setAuthenticationOutcome( String realm, String protocol, String host, int port, boolean success) { try { this_mon.enter(); String tracker = protocol + "://" + host + ":" + port + "/"; String auth_key = realm + ":" + tracker; authCache cache = (authCache) auth_cache.get(auth_key); if (cache != null) { cache.setOutcome(success); } } finally { this_mon.exit(); } }
protected void saveAuthCache() { try { this_mon.enter(); HashMap map = new HashMap(); Iterator it = auth_cache.values().iterator(); while (it.hasNext()) { authCache value = (authCache) it.next(); if (value.isPersistent()) { try { HashMap entry_map = new HashMap(); entry_map.put("user", value.getAuth().getUserName().getBytes("UTF-8")); entry_map.put("pw", new String(value.getAuth().getPassword()).getBytes("UTF-8")); map.put(value.getKey(), entry_map); } catch (Throwable e) { Debug.printStackTrace(e); } } } COConfigurationManager.setParameter(CONFIG_PARAM, map); } finally { this_mon.exit(); } }
public void generate(IndentWriter writer) { writer.println("Configuration Details"); try { writer.indent(); writer.println( "version=" + Constants.AZUREUS_VERSION + ", subver=" + Constants.AZUREUS_SUBVER); writer.println("System Properties"); try { writer.indent(); Properties props = System.getProperties(); Iterator it = new TreeSet(props.keySet()).iterator(); while (it.hasNext()) { String key = (String) it.next(); writer.println(key + "=" + props.get(key)); } } finally { writer.exdent(); } writer.println("Environment"); try { writer.indent(); Map<String, String> env = System.getenv(); if (env == null) { writer.println("Not supported"); } else { Iterator it = new TreeSet(env.keySet()).iterator(); while (it.hasNext()) { String key = (String) it.next(); writer.println(key + "=" + env.get(key)); } } } finally { writer.exdent(); } writer.println("Azureus Config"); ConfigurationDefaults defaults = ConfigurationDefaults.getInstance(); try { writer.indent(); Set<String> keys = new TreeSet<String>( new Comparator<String>() { public int compare(String o1, String o2) { return (o1.compareToIgnoreCase(o2)); } }); keys.addAll(propertiesMap.keySet()); Iterator<String> it = keys.iterator(); while (it.hasNext()) { String key = it.next(); // don't dump crypto stuff if (ignoreKeyForDump(key)) { continue; } Object value = propertiesMap.get(key); boolean bParamExists = defaults.doesParameterDefaultExist(key.toString()); if (!bParamExists) { key = "[NoDef] " + key; } else { Object def = defaults.getParameter(key); if (def != null && value != null) { if (!BEncoder.objectsAreIdentical(def, value)) { key = "-> " + key; } } } if (value instanceof Long) { writer.println(key + "=" + value); } else if (value instanceof List) { writer.println( key + "=" + BDecoder.decodeStrings((List) BEncoder.clone(value)) + "[list]"); } else if (value instanceof Map) { writer.println( key + "=" + BDecoder.decodeStrings((Map) BEncoder.clone(value)) + "[map]"); } else if (value instanceof byte[]) { byte[] b = (byte[]) value; boolean hex = false; for (int i = 0; i < b.length; i++) { char c = (char) b[i]; if (!(Character.isLetterOrDigit(c) || "\\ `¬\"£$%^&*()-_=+[{]};:'@#~,<.>/?'".indexOf(c) != -1)) { hex = true; break; } } writer.println( key + "=" + (hex ? ByteFormatter.nicePrint(b) : bytesToString((byte[]) value))); } else { writer.println(key + "=" + value + "[unknown]"); } } } finally { writer.exdent(); } } finally { writer.exdent(); } }
public PasswordAuthentication getAuthentication( String realm, String protocol, String host, int port) { try { this_mon.enter(); String tracker = protocol + "://" + host + ":" + port + "/"; InetAddress bind_ip = NetworkAdmin.getSingleton().getSingleHomedServiceBindAddress(); String self_addr; // System.out.println( "auth req for " + realm + " - " + tracker ); if (bind_ip == null || bind_ip.isAnyLocalAddress()) { self_addr = "127.0.0.1"; } else { self_addr = bind_ip.getHostAddress(); } // when the tracker is connected to internally we don't want to prompt // for the password. Here we return a special user and the password hash // which is picked up in the tracker auth code - search for "<internal>"! // also include the tracker IP as well as for scrapes these can occur on // a raw torrent which hasn't been modified to point to localhost if (host.equals(self_addr) || host.equals(COConfigurationManager.getStringParameter("Tracker IP", ""))) { try { byte[] pw = COConfigurationManager.getByteParameter("Tracker Password", new byte[0]); String str_pw = new String(Base64.encode(pw)); return (new PasswordAuthentication("<internal>", str_pw.toCharArray())); } catch (Throwable e) { Debug.printStackTrace(e); } } String auth_key = realm + ":" + tracker; authCache cache = (authCache) auth_cache.get(auth_key); if (cache != null) { PasswordAuthentication auth = cache.getAuth(); if (auth != null) { return (auth); } } String[] res = getAuthenticationDialog(realm, tracker); if (res == null) { return (null); } else { PasswordAuthentication auth = new PasswordAuthentication(res[0], res[1].toCharArray()); boolean save_pw = res[2].equals("true"); boolean old_entry_existed = auth_cache.put(auth_key, new authCache(auth_key, auth, save_pw)) != null; if (save_pw || old_entry_existed) { saveAuthCache(); } return (auth); } } finally { this_mon.exit(); } }