001/*
002 *  $URL: svn://svn.webarts.bc.ca/open/trunk/projects/WebARTS/ca/bc/webarts/tools/portscanner/PortScannerApp.java $
003 *  $Author: tgutwin $
004 *  $Revision: 1224 $
005 *  $Date: 2018-03-02 22:18:54 -0800 (Fri, 02 Mar 2018) $
006 */
007/*
008 *
009 *  Written by Tom Gutwin - WebARTS Design.
010 *  Copyright (C) 2012 WebARTS Design, North Vancouver Canada
011 *  http://www.webarts.bc.ca
012 *
013 *  This program is free software; you can redistribute it and/or modify
014 *  it under the terms of the GNU General Public License as published by
015 *  the Free Software Foundation; either version 2 of the License, or
016 *  (at your option) any later version.
017 *
018 *  This program is distributed in the hope that it will be useful,
019 *  but WITHOUT ANY WARRANTY; without_ even the implied warranty of
020 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
021 *  GNU General Public License for more details.
022 *
023 *  You should have received a copy of the GNU General Public License
024 *  along with this program; if not, write to the Free Software
025 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
026 */
027package ca.bc.webarts.tools.portscanner;
028
029import ca.bc.webarts.widgets.Util;
030
031/**
032 * A multiThreaded app that scans a specified range of IP ports for access using the {@link PortScanner} class.
033 * The user can specify the number of threads and the port range on the commandline.<pre>
034 *
035 *   ------------------------
036 *   WebARTS Threaded Port Scanner
037 *   ------------------------
038 *Syntax:
039 *       java -jar PortScanner.jar [numThreads] hostname/IP
040 *             scans the default port range: 9-50001
041 *    or
042 *       java -cp PortScanner.jar ca.bc.webarts.tools.portscanner.PortScannerApp [numThreads] [startPort] [lastPort] hostname/IP
043 *
044 *    where numThreads is a single int specifying the number of doughnuts to order :)
045 *          hostname/IP is either a hostname or an IP address *
046 **/
047public class PortScannerApp
048{
049  public static int numThreads_ = 10;
050  public static     int portStart = 9;
051  public static     int portEnd = 50001;
052
053
054  /**
055   * main entry to this app.
056   **/
057    public static void main(String args[])
058    {
059      if (args.length>0)
060      {
061
062        System.out.println("Scanning...");
063
064        String host = args[args.length-1];
065        if (args.length>1) numThreads_ = Integer.parseInt(args[0]);
066        if (args.length>2) portStart = Integer.parseInt(args[1]);
067        if (args.length>3) portEnd = Integer.parseInt(args[2]);
068
069        final ObserverNotifier [] observers = new ObserverNotifier[numThreads_];
070        final PortScanner [] observables = new PortScanner[numThreads_];
071        final Thread [] threadPool = new Thread[numThreads_];
072
073      int portRange = (portEnd-portStart)/numThreads_;
074
075        for (int i=0; i< numThreads_; i++)
076        {
077          if (portStart+(i*portRange)+1< portEnd)
078          {
079            System.out.println ("Starting PortScanner thread["+i+"] threads on port Range "+(portStart+(i*portRange)+1) +" - "+ (portStart+((i+1)*portRange)));
080            observers[i] = new ObserverNotifier();
081            observables[i] = init(host, portStart+(i*portRange)+1 , portStart+((i+1)*portRange) , observers[i]);
082            threadPool[i] = new Thread(observables[i]);
083            threadPool[i].start();
084          }
085        }
086        boolean threadsNotComplete = false;
087        int maxCount = 10000;
088        int loopCount = 0;
089        while (threadsNotComplete && loopCount<maxCount)
090        {
091          Util.sleep(500);
092          threadsNotComplete = true;
093          for (int i=0; i< numThreads_; i++) if (!observables[i].completed) threadsNotComplete= false;
094          loopCount++;
095        }
096
097      }
098      else
099      {
100        // Dump usage
101        System.out.println("\n   ------------------------");
102        System.out.println(" WebARTS Threaded Port Scanner");
103        System.out.println("   ------------------------\n");
104        System.out.println("Syntax:");
105        System.out.println("       java -jar PortScanner.jar [numThreads] hostname/IP");
106        System.out.println("            --> scans the default port range: 9-50001");
107        System.out.println("    or");
108        System.out.println("       java -cp PortScanner.jar ca.bc.webarts.tools.portscanner [numThreads] [startPort] [lastPort] hostname/IP");
109        System.out.println("");
110        System.out.println("    where numThreads is a single int specifying the number of doughnuts to order :)");
111        System.out.println("          hostname/IP is either a hostname or an IP address ");
112        System.out.println("");
113      }
114    }
115
116
117    /** Initializes the passed in ObserverNotifier with a new PortScanner.
118    **/
119    private static PortScanner init(String ipAddress, int fromPort, int toPort, ObserverNotifier observer )
120    {
121        PortScanner observable = new PortScanner();
122        observable.setTarget(ipAddress);
123        observable.setFromPort(fromPort);
124        observable.setToPort(toPort);
125        observable.addObserver( observer );
126        return observable;
127    }
128}