Program: 170784
Project: 170784
Buggy source:
protected void setupServer(){
  try {
    if (!plugin_enabled) {
      if (tracker_context != null) {
        tracker_context.destroy();
        tracker_context=null;
      }
      return;
    }
    final int port=param_port.getValue();
    String protocol_str=param_protocol.getValue().trim();
    String bind_str=param_bind.getValue().trim();
    InetAddress bind_ip=null;
    if (bind_str.length() > 0) {
      try {
        bind_ip=InetAddress.getByName(bind_str);
      }
 catch (      Throwable e) {
      }
      if (bind_ip == null) {
        final NetworkAdmin na=NetworkAdmin.getSingleton();
        InetAddress[] addresses=na.resolveBindAddresses(bind_str);
        if (addresses.length > 0) {
          bind_ip=addresses[0];
          if (!na_intf_listener_added) {
            na_intf_listener_added=true;
            na.addPropertyChangeListener(new NetworkAdminPropertyChangeListener(){
              @Override public void propertyChanged(              String property){
                if (unloaded) {
                  na.removePropertyChangeListener(this);
                }
 else {
                  if (property == NetworkAdmin.PR_NETWORK_INTERFACES) {
                    new AEThread2("String_Node_Str"){
                      @Override public void run(){
                        setupServer();
                      }
                    }
.start();
                  }
                }
              }
            }
);
          }
        }
      }
      if (bind_ip == null) {
        log.log(LoggerChannel.LT_ERROR,"String_Node_Str" + bind_str + "String_Node_Str");
      }
    }
    if (tracker_context != null) {
      URL url=tracker_context.getURLs()[0];
      String existing_protocol=url.getProtocol();
      int existing_port=url.getPort() == -1 ? url.getDefaultPort() : url.getPort();
      InetAddress existing_bind_ip=tracker_context.getBindIP();
      if (existing_port == port && existing_protocol.equalsIgnoreCase(protocol_str) && sameAddress(bind_ip,existing_bind_ip)) {
        return;
      }
      tracker_context.destroy();
      tracker_context=null;
    }
    int protocol=protocol_str.equalsIgnoreCase("String_Node_Str") ? Tracker.PR_HTTP : Tracker.PR_HTTPS;
    Map<String,Object> tc_properties=new HashMap<>();
    Boolean prop_non_blocking=(Boolean)properties.get(PR_NON_BLOCKING);
    if (prop_non_blocking != null && prop_non_blocking) {
      tc_properties.put(Tracker.PR_NON_BLOCKING,true);
    }
    log.log(LoggerChannel.LT_INFORMATION,"String_Node_Str" + port + (bind_ip == null ? "String_Node_Str" : ("String_Node_Str" + bind_str + "String_Node_Str"+ bind_ip+ "String_Node_Str"))+ "String_Node_Str"+ protocol_str+ (root_dir.length() == 0 ? "String_Node_Str" : ("String_Node_Str" + root_dir))+ (properties.size() == 0 ? "String_Node_Str" : ("String_Node_Str" + properties)));
    tracker_context=plugin_interface.getTracker().createWebContext(Constants.APP_NAME + "String_Node_Str" + plugin_interface.getPluginName(),port,protocol,bind_ip,tc_properties);
    Boolean prop_enable_i2p=(Boolean)properties.get(PR_ENABLE_I2P);
    if (prop_enable_i2p == null || prop_enable_i2p) {
      network_dispatcher.dispatch(new AERunnable(){
        @Override public void runSupport(){
          Map<String,Object> options=new HashMap<>();
          options.put(AEProxyFactory.SP_PORT,port);
          Map<String,Object> reply=AEProxyFactory.getPluginServerProxy(plugin_interface.getPluginName(),AENetworkClassifier.AT_I2P,plugin_interface.getPluginID(),options);
          if (reply != null) {
            param_i2p_dest.setVisible(true);
            String host=(String)reply.get("String_Node_Str");
            if (!param_i2p_dest.getValue().equals(host)) {
              param_i2p_dest.setValue(host);
              if (p_sid != null) {
                updatePairing(p_sid);
              }
            }
          }
        }
      }
);
    }
    Boolean prop_enable_tor=(Boolean)properties.get(PR_ENABLE_TOR);
    if (prop_enable_tor == null || prop_enable_tor) {
      network_dispatcher.dispatch(new AERunnable(){
        @Override public void runSupport(){
          Map<String,Object> options=new HashMap<>();
          options.put(AEProxyFactory.SP_PORT,port);
          Map<String,Object> reply=AEProxyFactory.getPluginServerProxy(plugin_interface.getPluginName(),AENetworkClassifier.AT_TOR,plugin_interface.getPluginID(),options);
          if (reply != null) {
            param_tor_dest.setVisible(true);
            String host=(String)reply.get("String_Node_Str");
            if (!param_tor_dest.getValue().equals(host)) {
              param_tor_dest.setValue(host);
              if (p_sid != null) {
                updatePairing(p_sid);
              }
            }
          }
        }
      }
);
    }
    Boolean pr_enable_keep_alive=(Boolean)properties.get(PR_ENABLE_KEEP_ALIVE);
    if (pr_enable_keep_alive != null && pr_enable_keep_alive) {
      tracker_context.setEnableKeepAlive(true);
    }
    tracker_context.addPageGenerator(this);
    tracker_context.addAuthenticationListener(new TrackerAuthenticationAdapter(){
      private String last_pw="String_Node_Str";
      private byte[] last_hash={};
      private final int DELAY=10 * 1000;
      private Map<String,Object[]> fail_map=new HashMap<>();
      @Override public boolean authenticate(      String headers,      URL resource,      String user,      String pw){
        long now=SystemTime.getMonotonousTime();
        String client_address=getHeaderField(headers,"String_Node_Str");
        if (client_address == null) {
          client_address="String_Node_Str";
        }
synchronized (logout_timer) {
          Long logout_time=logout_timer.get(client_address);
          if (logout_time != null && now - logout_time <= LOGOUT_GRACE_MILLIS) {
            tls.set(GRACE_PERIOD_MARKER);
            return (true);
          }
        }
        boolean result=authenticateSupport(headers,resource,user,pw);
        if (!result) {
          if (!pw.equals("String_Node_Str")) {
            AESemaphore waiter=null;
synchronized (fail_map) {
              Object[] x=fail_map.get(client_address);
              if (x == null) {
                x=new Object[]{new AESemaphore("String_Node_Str"),new Long(-1),new Long(-1),now};
                fail_map.put(client_address,x);
              }
 else {
                x[1]=x[2];
                x[2]=x[3];
                x[3]=now;
                long t=(Long)x[1];
                if (now - t < 10 * 1000) {
                  log("String_Node_Str" + client_address + "String_Node_Str");
                  x[2]=now + DELAY;
                  last_pw="String_Node_Str";
                  waiter=(AESemaphore)x[0];
                }
              }
            }
            if (waiter != null) {
              waiter.reserve(DELAY);
            }
          }
        }
 else {
synchronized (fail_map) {
            fail_map.remove(client_address);
          }
          String cookies=getHeaderField(headers,"String_Node_Str");
          if (pairing_session_code != null) {
            if (cookies == null || !cookies.contains(pairing_session_code)) {
              tls.set(pairing_session_code);
            }
          }
        }
        recordAuthRequest(client_address,result);
        if (!result) {
        }
        return (result);
      }
      private boolean authenticateSupport(      String headers,      URL resource,      String user,      String pw){
        boolean result;
        boolean auto_auth=param_auto_auth != null && param_auto_auth.getValue();
        if (!pw_enable.getValue()) {
          String whitelist=p_no_pw_whitelist.getValue().trim();
          if (whitelist.equals("String_Node_Str")) {
            result=true;
          }
 else {
            String actual_host=getHeaderField(headers,"String_Node_Str");
            int actual_port=protocol == Tracker.PR_HTTP ? 80 : 443;
            String referrer=getHeaderField(headers,"String_Node_Str");
            String original_host=actual_host;
            if (actual_host.startsWith("String_Node_Str")) {
              int pos=actual_host.lastIndexOf(']');
              if (pos != -1) {
                String rem=actual_host.substring(pos + 1);
                actual_host=actual_host.substring(0,pos + 1);
                pos=rem.indexOf(':');
                if (pos != -1) {
                  actual_port=Integer.parseInt(rem.substring(pos + 1).trim());
                }
              }
            }
 else {
              int pos=actual_host.indexOf(':');
              if (pos != -1) {
                actual_port=Integer.parseInt(actual_host.substring(pos + 1).trim());
                actual_host=actual_host.substring(0,pos);
              }
            }
            String[] allowed=whitelist.split("String_Node_Str");
            result=false;
            String msg="String_Node_Str";
            if (actual_port != port) {
              msg="String_Node_Str" + port + "String_Node_Str"+ actual_port;
            }
 else {
              for (              String a : allowed) {
                a=a.trim();
                if (a.equals("String_Node_Str")) {
                  InetAddress bind=getServerBindIP();
                  if (bind != null) {
                    if (bind instanceof Inet6Address) {
                      a="String_Node_Str" + bind.getHostAddress() + "String_Node_Str";
                    }
 else {
                      a=bind.getHostAddress();
                    }
                  }
                }
                if (actual_host.equals(a.trim())) {
                  result=true;
                  break;
                }
              }
              if (!result) {
                msg="String_Node_Str" + original_host + "String_Node_Str";
              }
 else {
                if (referrer != null) {
                  result=false;
                  try {
                    URL url=new URL(referrer);
                    int ref_port=url.getPort();
                    if (ref_port == -1) {
                      ref_port=url.getDefaultPort();
                    }
                    if (ref_port == port) {
                      result=true;
                    }
                  }
 catch (                  Throwable e) {
                  }
                  if (!result) {
                    msg="String_Node_Str" + referrer;
                  }
                }
              }
            }
            if (!result) {
              log.log("String_Node_Str" + msg);
            }
          }
        }
 else {
          if (auto_auth) {
            user=user.trim().toLowerCase();
            pw=pw.toUpperCase();
          }
          if (!user.equals(p_user_name.getValue())) {
            result=false;
          }
 else {
            byte[] hash=last_hash;
            if (!last_pw.equals(pw)) {
              hash=plugin_interface.getUtilities().getSecurityManager().calculateSHA1(auto_auth ? pw.toUpperCase().getBytes() : pw.getBytes());
              last_pw=pw;
              last_hash=hash;
            }
            result=Arrays.equals(hash,p_password.getValue());
          }
        }
        if (result) {
          checkCookieSet(headers,resource);
        }
 else         if (auto_auth) {
          int x=checkCookieSet(headers,resource);
          if (x == 1) {
            result=true;
          }
 else           if (x == 0) {
            result=hasOurCookie(getHeaderField(headers,"String_Node_Str"));
          }
        }
 else {
          result=hasOurCookie(getHeaderField(headers,"String_Node_Str"));
        }
        return (result);
      }
      /** 
 * @param headers
 * @param resource
 * @return 0 = unknown, 1 = ok, 2 = bad
 */
      private int checkCookieSet(      String headers,      URL resource){
        if (pairing_access_code == null) {
          return (2);
        }
        String[] locations={resource.getQuery(),getHeaderField(headers,"String_Node_Str")};
        for (        String location : locations) {
          if (location != null) {
            boolean skip_fail=false;
            int param_len=0;
            int p1=location.indexOf("String_Node_Str");
            if (p1 == -1) {
              p1=location.indexOf("String_Node_Str");
              if (p1 != -1) {
                param_len=3;
                skip_fail=true;
              }
            }
 else {
              param_len=16;
            }
            if (p1 != -1) {
              int p2=location.indexOf('&',p1);
              String ac=location.substring(p1 + param_len,p2 == -1 ? location.length() : p2).trim();
              p2=ac.indexOf('#');
              if (p2 != -1) {
                ac=ac.substring(0,p2);
              }
              if (ac.equalsIgnoreCase(pairing_access_code)) {
                tls.set(pairing_session_code);
                return (1);
              }
 else {
                if (!skip_fail) {
                  return (2);
                }
              }
            }
          }
        }
        return (0);
      }
      private String getHeaderField(      String headers,      String field){
        String[] lines=headers.split("String_Node_Str");
        for (        String line : lines) {
          int pos=line.indexOf(':');
          if (pos != -1) {
            if (line.substring(0,pos).equalsIgnoreCase(field)) {
              return (line.substring(pos + 1).trim());
            }
          }
        }
        return (null);
      }
    }
);
  }
 catch (  TrackerException e) {
    log.log("String_Node_Str",e);
  }
}

Fixed version (ground truth):
protected void setupServer(){
  try {
    if (!plugin_enabled) {
      if (tracker_context != null) {
        tracker_context.destroy();
        tracker_context=null;
      }
      return;
    }
    final int port=param_port.getValue();
    String protocol_str=param_protocol.getValue().trim();
    String bind_str=param_bind.getValue().trim();
    InetAddress bind_ip=null;
    if (bind_str.length() > 0) {
      try {
        bind_ip=InetAddress.getByName(bind_str);
      }
 catch (      Throwable e) {
      }
      if (bind_ip == null) {
        final NetworkAdmin na=NetworkAdmin.getSingleton();
        InetAddress[] addresses=na.resolveBindAddresses(bind_str);
        if (addresses.length > 0) {
          bind_ip=addresses[0];
          if (!na_intf_listener_added) {
            na_intf_listener_added=true;
            na.addPropertyChangeListener(new NetworkAdminPropertyChangeListener(){
              @Override public void propertyChanged(              String property){
                if (unloaded) {
                  na.removePropertyChangeListener(this);
                }
 else {
                  if (property == NetworkAdmin.PR_NETWORK_INTERFACES) {
                    new AEThread2("String_Node_Str"){
                      @Override public void run(){
                        setupServer();
                      }
                    }
.start();
                  }
                }
              }
            }
);
          }
        }
      }
      if (bind_ip == null) {
        log.log(LoggerChannel.LT_ERROR,"String_Node_Str" + bind_str + "String_Node_Str");
      }
    }
    if (tracker_context != null) {
      URL url=tracker_context.getURLs()[0];
      String existing_protocol=url.getProtocol();
      int existing_port=url.getPort() == -1 ? url.getDefaultPort() : url.getPort();
      InetAddress existing_bind_ip=tracker_context.getBindIP();
      if (existing_port == port && existing_protocol.equalsIgnoreCase(protocol_str) && sameAddress(bind_ip,existing_bind_ip)) {
        return;
      }
      tracker_context.destroy();
      tracker_context=null;
    }
    int protocol=protocol_str.equalsIgnoreCase("String_Node_Str") ? Tracker.PR_HTTP : Tracker.PR_HTTPS;
    Map<String,Object> tc_properties=new HashMap<>();
    Boolean prop_non_blocking=(Boolean)properties.get(PR_NON_BLOCKING);
    if (prop_non_blocking != null && prop_non_blocking) {
      tc_properties.put(Tracker.PR_NON_BLOCKING,true);
    }
    log.log(LoggerChannel.LT_INFORMATION,"String_Node_Str" + port + (bind_ip == null ? "String_Node_Str" : ("String_Node_Str" + bind_str + "String_Node_Str"+ bind_ip+ "String_Node_Str"))+ "String_Node_Str"+ protocol_str+ (root_dir.length() == 0 ? "String_Node_Str" : ("String_Node_Str" + root_dir))+ (properties.size() == 0 ? "String_Node_Str" : ("String_Node_Str" + properties)));
    tracker_context=plugin_interface.getTracker().createWebContext(Constants.APP_NAME + "String_Node_Str" + plugin_interface.getPluginName(),port,protocol,bind_ip,tc_properties);
    Boolean prop_enable_i2p=(Boolean)properties.get(PR_ENABLE_I2P);
    if (prop_enable_i2p == null || prop_enable_i2p) {
      network_dispatcher.dispatch(new AERunnable(){
        @Override public void runSupport(){
          Map<String,Object> options=new HashMap<>();
          options.put(AEProxyFactory.SP_PORT,port);
          Map<String,Object> reply=AEProxyFactory.getPluginServerProxy(plugin_interface.getPluginName(),AENetworkClassifier.AT_I2P,plugin_interface.getPluginID(),options);
          if (reply != null) {
            param_i2p_dest.setVisible(true);
            String host=(String)reply.get("String_Node_Str");
            if (!param_i2p_dest.getValue().equals(host)) {
              param_i2p_dest.setValue(host);
              if (p_sid != null) {
                updatePairing(p_sid);
              }
            }
          }
        }
      }
);
    }
    Boolean prop_enable_tor=(Boolean)properties.get(PR_ENABLE_TOR);
    if (prop_enable_tor == null || prop_enable_tor) {
      network_dispatcher.dispatch(new AERunnable(){
        @Override public void runSupport(){
          Map<String,Object> options=new HashMap<>();
          options.put(AEProxyFactory.SP_PORT,port);
          Map<String,Object> reply=AEProxyFactory.getPluginServerProxy(plugin_interface.getPluginName(),AENetworkClassifier.AT_TOR,plugin_interface.getPluginID(),options);
          if (reply != null) {
            param_tor_dest.setVisible(true);
            String host=(String)reply.get("String_Node_Str");
            if (!param_tor_dest.getValue().equals(host)) {
              param_tor_dest.setValue(host);
              if (p_sid != null) {
                updatePairing(p_sid);
              }
            }
          }
        }
      }
);
    }
    Boolean pr_enable_keep_alive=(Boolean)properties.get(PR_ENABLE_KEEP_ALIVE);
    if (pr_enable_keep_alive != null && pr_enable_keep_alive) {
      tracker_context.setEnableKeepAlive(true);
    }
    tracker_context.addPageGenerator(this);
    tracker_context.addAuthenticationListener(new TrackerAuthenticationAdapter(){
      private String last_pw="String_Node_Str";
      private byte[] last_hash={};
      private final int DELAY=10 * 1000;
      private Map<String,Object[]> fail_map=new HashMap<>();
      @Override public boolean authenticate(      String headers,      URL resource,      String user,      String pw){
        long now=SystemTime.getMonotonousTime();
        String client_address=getHeaderField(headers,"String_Node_Str");
        if (client_address == null) {
          client_address="String_Node_Str";
        }
synchronized (logout_timer) {
          Long logout_time=logout_timer.get(client_address);
          if (logout_time != null && now - logout_time <= LOGOUT_GRACE_MILLIS) {
            tls.set(GRACE_PERIOD_MARKER);
            return (true);
          }
        }
        boolean result=authenticateSupport(headers,resource,user,pw);
        if (!result) {
          if (!pw.equals("String_Node_Str")) {
            AESemaphore waiter=null;
synchronized (fail_map) {
              Object[] x=fail_map.get(client_address);
              if (x == null) {
                x=new Object[]{new AESemaphore("String_Node_Str"),new Long(-1),new Long(-1),now};
                fail_map.put(client_address,x);
              }
 else {
                x[1]=x[2];
                x[2]=x[3];
                x[3]=now;
                long t=(Long)x[1];
                if (now - t < 10 * 1000) {
                  log("String_Node_Str" + client_address + "String_Node_Str");
                  x[2]=now + DELAY;
                  last_pw="String_Node_Str";
                  waiter=(AESemaphore)x[0];
                }
              }
            }
            if (waiter != null) {
              waiter.reserve(DELAY);
            }
          }
        }
 else {
synchronized (fail_map) {
            fail_map.remove(client_address);
          }
          String cookies=getHeaderField(headers,"String_Node_Str");
          if (pairing_session_code != null) {
            if (cookies == null || !cookies.contains(pairing_session_code)) {
              tls.set(pairing_session_code);
            }
          }
        }
        recordAuthRequest(client_address,result);
        if (!result) {
        }
        return (result);
      }
      private boolean authenticateSupport(      String headers,      URL resource,      String user,      String pw){
        boolean result;
        boolean auto_auth=param_auto_auth != null && param_auto_auth.getValue();
        if (!pw_enable.getValue()) {
          String whitelist=p_no_pw_whitelist.getValue().trim();
          if (whitelist.equals("String_Node_Str")) {
            result=true;
          }
 else {
            String actual_host=getHeaderField(headers,"String_Node_Str");
            int actual_port=protocol == Tracker.PR_HTTP ? 80 : 443;
            String referrer=getHeaderField(headers,"String_Node_Str");
            if (actual_host.startsWith("String_Node_Str")) {
              int pos=actual_host.lastIndexOf(']');
              if (pos != -1) {
                String rem=actual_host.substring(pos + 1);
                actual_host=actual_host.substring(0,pos + 1);
                pos=rem.indexOf(':');
                if (pos != -1) {
                  actual_port=Integer.parseInt(rem.substring(pos + 1).trim());
                }
              }
            }
 else {
              int pos=actual_host.indexOf(':');
              if (pos != -1) {
                actual_port=Integer.parseInt(actual_host.substring(pos + 1).trim());
                actual_host=actual_host.substring(0,pos);
              }
            }
            String[] allowed=whitelist.split("String_Node_Str");
            result=false;
            String msg="String_Node_Str";
            if (actual_port != port) {
              msg="String_Node_Str" + port + "String_Node_Str"+ actual_port;
            }
 else {
              for (              String a : allowed) {
                a=a.trim();
                if (a.equals("String_Node_Str")) {
                  InetAddress bind=getServerBindIP();
                  if (bind != null) {
                    if (bind instanceof Inet6Address) {
                      a="String_Node_Str" + bind.getHostAddress() + "String_Node_Str";
                    }
 else {
                      a=bind.getHostAddress();
                    }
                  }
                }
                if (actual_host.equals(a.trim())) {
                  result=true;
                  break;
                }
              }
              if (!result) {
                msg="String_Node_Str" + actual_host + "String_Node_Str";
              }
 else {
                if (referrer != null) {
                  result=false;
                  try {
                    URL url=new URL(referrer);
                    int ref_port=url.getPort();
                    if (ref_port == -1) {
                      ref_port=url.getDefaultPort();
                    }
                    if (ref_port == port) {
                      result=true;
                    }
                  }
 catch (                  Throwable e) {
                  }
                  if (!result) {
                    msg="String_Node_Str" + referrer;
                  }
                }
              }
            }
            if (!result) {
              log.log("String_Node_Str" + msg);
            }
          }
        }
 else {
          if (auto_auth) {
            user=user.trim().toLowerCase();
            pw=pw.toUpperCase();
          }
          if (!user.equals(p_user_name.getValue())) {
            result=false;
          }
 else {
            byte[] hash=last_hash;
            if (!last_pw.equals(pw)) {
              hash=plugin_interface.getUtilities().getSecurityManager().calculateSHA1(auto_auth ? pw.toUpperCase().getBytes() : pw.getBytes());
              last_pw=pw;
              last_hash=hash;
            }
            result=Arrays.equals(hash,p_password.getValue());
          }
        }
        if (result) {
          checkCookieSet(headers,resource);
        }
 else         if (auto_auth) {
          int x=checkCookieSet(headers,resource);
          if (x == 1) {
            result=true;
          }
 else           if (x == 0) {
            result=hasOurCookie(getHeaderField(headers,"String_Node_Str"));
          }
        }
 else {
          result=hasOurCookie(getHeaderField(headers,"String_Node_Str"));
        }
        return (result);
      }
      /** 
 * @param headers
 * @param resource
 * @return 0 = unknown, 1 = ok, 2 = bad
 */
      private int checkCookieSet(      String headers,      URL resource){
        if (pairing_access_code == null) {
          return (2);
        }
        String[] locations={resource.getQuery(),getHeaderField(headers,"String_Node_Str")};
        for (        String location : locations) {
          if (location != null) {
            boolean skip_fail=false;
            int param_len=0;
            int p1=location.indexOf("String_Node_Str");
            if (p1 == -1) {
              p1=location.indexOf("String_Node_Str");
              if (p1 != -1) {
                param_len=3;
                skip_fail=true;
              }
            }
 else {
              param_len=16;
            }
            if (p1 != -1) {
              int p2=location.indexOf('&',p1);
              String ac=location.substring(p1 + param_len,p2 == -1 ? location.length() : p2).trim();
              p2=ac.indexOf('#');
              if (p2 != -1) {
                ac=ac.substring(0,p2);
              }
              if (ac.equalsIgnoreCase(pairing_access_code)) {
                tls.set(pairing_session_code);
                return (1);
              }
 else {
                if (!skip_fail) {
                  return (2);
                }
              }
            }
          }
        }
        return (0);
      }
      private String getHeaderField(      String headers,      String field){
        String[] lines=headers.split("String_Node_Str");
        for (        String line : lines) {
          int pos=line.indexOf(':');
          if (pos != -1) {
            if (line.substring(0,pos).equalsIgnoreCase(field)) {
              return (line.substring(pos + 1).trim());
            }
          }
        }
        return (null);
      }
    }
);
  }
 catch (  TrackerException e) {
    log.log("String_Node_Str",e);
  }
}