3838
3939import org .acra .ACRA ;
4040import org .acra .ACRAConfiguration ;
41+ import org .apache .commons .compress .utils .IOUtils ;
4142import org .csploit .android .R ;
4243import org .csploit .android .WifiScannerActivity ;
4344import org .csploit .android .gui .dialogs .FatalDialog ;
@@ -149,8 +150,10 @@ public static void init(Context context) throws Exception{
149150 mStoragePath = getSettings ().getString ("PREF_SAVE_PATH" , Environment .getExternalStorageDirectory ().toString ());
150151 mSessionName = "csploit-session-" + java .lang .System .currentTimeMillis ();
151152 mKnownIssues = new KnownIssues ();
152- mPlugins = new ArrayList <Plugin >();
153+ mPlugins = new ArrayList <>();
153154 mOpenPorts = new SparseIntArray (3 );
155+ mServices = new HashMap <>();
156+ mPorts = new HashMap <>();
154157
155158 // if we are here, network initialization didn't throw any error, lock wifi
156159 WifiManager wifiManager = (WifiManager ) mContext .getSystemService (Context .WIFI_SERVICE );
@@ -515,38 +518,39 @@ public static void unregisterSettingListener(SettingReceiver receiver) {
515518 }
516519 }
517520
518- private static void preloadServices (){
519- if (mServices == null || mPorts == null ){
520- try {
521- // preload network service map and mac vendors
522- mServices = new HashMap <String , String >();
523- mPorts = new HashMap <String , String >();
524-
525- @ SuppressWarnings ("ConstantConditions" )
526- FileInputStream fstream = new FileInputStream (mContext .getFilesDir ().getAbsolutePath () + "/tools/nmap/nmap-services" );
521+ public static void preloadServices (){
522+ if (!mServices .isEmpty ())
523+ return ;
527524
528- DataInputStream in = new DataInputStream ( fstream ) ;
529- BufferedReader reader = new BufferedReader ( new InputStreamReader ( in )) ;
530- String line ;
531- Matcher matcher ;
525+ FileReader fr = null ;
526+ BufferedReader reader = null ;
527+ try {
528+ // preload network service and ports map
532529
533- while ((line = reader .readLine ()) != null ){
534- line = line .trim ();
530+ fr = new FileReader (mContext .getFilesDir ().getAbsolutePath () + "/tools/nmap/nmap-services" );
531+ reader = new BufferedReader (fr );
532+ String line ;
533+ Matcher matcher ;
534+ String port , proto ;
535535
536- if ((matcher = SERVICE_PARSER .matcher (line )) != null && matcher .find ()){
537- String proto = matcher .group (1 ),
538- port = matcher .group (2 );
536+ while ((line = reader .readLine ()) != null ) {
537+ if ((matcher = SERVICE_PARSER .matcher (line )) != null && matcher .find ()) {
538+ proto = matcher .group (1 );
539+ port = matcher .group (2 );
539540
540- mServices .put (proto , port );
541- mPorts .put (port , proto );
542- }
541+ mServices .put (proto , port );
542+ mPorts .put (port , proto );
543543 }
544-
545- in .close ();
546- }
547- catch (Exception e ){
548- errorLogging (e );
549544 }
545+
546+ } catch (Exception e ) {
547+ mServices .clear ();
548+ mPorts .clear ();
549+
550+ errorLogging (e );
551+ } finally {
552+ IOUtils .closeQuietly (reader );
553+ IOUtils .closeQuietly (fr );
550554 }
551555 }
552556
@@ -941,7 +945,6 @@ public static String getMacVendor(byte[] mac){
941945 }
942946
943947 public static String getProtocolByPort (String port ){
944- preloadServices ();
945948
946949 return mPorts .containsKey (port ) ? mPorts .get (port ) : null ;
947950 }
@@ -951,7 +954,6 @@ public static String getProtocolByPort(int port) {
951954 }
952955
953956 public static int getPortByProtocol (String protocol ){
954- preloadServices ();
955957
956958 return mServices .containsKey (protocol ) ? Integer .parseInt (mServices .get (protocol )) : 0 ;
957959 }
0 commit comments