2121import android .app .Activity ;
2222import android .app .ActivityManager ;
2323import android .app .ActivityManager .RunningServiceInfo ;
24- import android .app .AlertDialog ;
2524import android .content .Context ;
26- import android .content .DialogInterface ;
2725import android .content .Intent ;
2826import android .content .SharedPreferences ;
2927import android .content .pm .PackageInfo ;
3230import android .net .wifi .WifiManager ;
3331import android .net .wifi .WifiManager .WifiLock ;
3432import android .os .Build ;
35- import android .os .Bundle ;
3633import android .os .Environment ;
3734import android .os .PowerManager ;
3835import android .os .PowerManager .WakeLock ;
@@ -156,6 +153,8 @@ public static void init(Context context) throws Exception{
156153 mKnownIssues = new KnownIssues ();
157154 mPlugins = new ArrayList <Plugin >();
158155 mOpenPorts = new SparseIntArray (3 );
156+ mServices = new HashMap <String , String >();
157+ mPorts = new HashMap <String , String >();
159158
160159 // if we are here, network initialization didn't throw any error, lock wifi
161160 WifiManager wifiManager = (WifiManager ) mContext .getSystemService (Context .WIFI_SERVICE );
@@ -520,38 +519,46 @@ public static void unregisterSettingListener(SettingReceiver receiver) {
520519 }
521520 }
522521
523- private static void preloadServices (){
524- if (mServices == null || mPorts == null ){
525- try {
526- // preload network service map and mac vendors
527- mServices = new HashMap <String , String >();
528- mPorts = new HashMap <String , String >();
529-
530- @ SuppressWarnings ("ConstantConditions" )
531- FileInputStream fstream = new FileInputStream (mContext .getFilesDir ().getAbsolutePath () + "/tools/nmap/nmap-services" );
522+ public static void preloadServices (){
523+ if (mServices .size () > 0 && mPorts .size () > 0 )
524+ return ;
532525
533- DataInputStream in = new DataInputStream ( fstream ) ;
534- BufferedReader reader = new BufferedReader ( new InputStreamReader ( in )) ;
535- String line ;
536- Matcher matcher ;
526+ FileReader fr = null ;
527+ BufferedReader reader = null ;
528+ try {
529+ // preload network service and ports map
537530
538- while ((line = reader .readLine ()) != null ){
539- line = line .trim ();
531+ //@SuppressWarnings("ConstantConditions")
532+ fr = new FileReader (mContext .getFilesDir ().getAbsolutePath () + "/tools/nmap/nmap-services" );
533+ reader = new BufferedReader (fr );
534+ String line ;
535+ Matcher matcher ;
536+ String port , proto ;
540537
541- if ((matcher = SERVICE_PARSER .matcher (line )) != null && matcher .find ()){
542- String proto = matcher .group (1 ),
543- port = matcher .group (2 );
538+ while ((line = reader .readLine ()) != null ) {
539+ if ((matcher = SERVICE_PARSER .matcher (line )) != null && matcher .find ()) {
540+ proto = matcher .group (1 );
541+ port = matcher .group (2 );
544542
545- mServices .put (proto , port );
546- mPorts .put (port , proto );
547- }
543+ mServices .put (proto , port );
544+ mPorts .put (port , proto );
548545 }
549-
550- in .close ();
551546 }
552- catch (Exception e ){
553- errorLogging (e );
547+
548+ } catch (Exception e ) {
549+ mServices .clear ();
550+ mPorts .clear ();
551+
552+ errorLogging (e );
553+ }
554+ finally {
555+ try {
556+ if (fr != null )
557+ fr .close ();
558+ if (reader != null )
559+ reader .close ();
554560 }
561+ catch (Exception e ){}
555562 }
556563 }
557564
@@ -946,7 +953,6 @@ public static String getMacVendor(byte[] mac){
946953 }
947954
948955 public static String getProtocolByPort (String port ){
949- preloadServices ();
950956
951957 return mPorts .containsKey (port ) ? mPorts .get (port ) : null ;
952958 }
@@ -956,7 +962,6 @@ public static String getProtocolByPort(int port) {
956962 }
957963
958964 public static int getPortByProtocol (String protocol ){
959- preloadServices ();
960965
961966 return mServices .containsKey (protocol ) ? Integer .parseInt (mServices .get (protocol )) : 0 ;
962967 }
0 commit comments