-
Notifications
You must be signed in to change notification settings - Fork 4
/
PortScanner.java
191 lines (135 loc) · 5.17 KB
/
PortScanner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package portscanner;
/**
*
* @author pradeep
*/
import java.io.*;
import java.util.*;
import java.net.*;
import java.util.concurrent.*;
import java.util.concurrent.Executor.*;
import java.util.concurrent.ExecutorService.*;
import java.util.concurrent.Future.*;
/*
paralelizing code to reduce time .
*/
public class PortScanner {
public static void main(String[] args) throws Exception
{
//openPorts = new ArrayList<>() ;
Scanner sc = new Scanner(System.in);
System.out.println("Port Scanner\n");
System.out.print("Enter url : ");
String url = sc.next() ;
if( ! url.startsWith("http://") || ! url.startsWith("https://") || ! url.startsWith("www.") )
{
url = "http://".concat(url);
}
// getting port range.
System.out.print("Enter Port Range : ");
int start = sc.nextInt() ;
System.out.print(" to ") ;
int end = sc.nextInt() ;
long st = System.currentTimeMillis() ;
// connecting to host .
System.out.println("\nGetting host ip .........");
InetAddress addr = InetAddress.getByName(new URL(url).getHost());
String ip = addr.getHostAddress() ; //ip from url
long tm = System.currentTimeMillis() - st ;
System.out.println("Time to get host : "+ (tm/1000.0) +" Seconds" );
long startT = System.currentTimeMillis() ; //starting time
// starting scanning port .
System.out.println("Scanning ports for ip : "+ip+" ............. ");
System.out.println("\nOpen - Ports are : ");
final ExecutorService es = Executors.newFixedThreadPool(20); //run 20 threads.
final List<Future<result>> futures = new ArrayList<>() ;
for(int i = start ; i <= end ; i++ )
{
futures.add( portIsOpen(es , ip , i ) ) ;
}
es.shutdown();
int openPorts = 0 ;
// printing open ports .
for(final Future<result> f : futures )
{
if( f.get().giveStatus() )
{
System.out.println("port : "+ f.get().givePort()+" . Time : "+ f.get().timeTaken +" Seconds" );
openPorts++;
}
}
long time = System.currentTimeMillis() - startT ;
System.out.println("\nTotal "+ openPorts +" ports Open.");
System.out.println("** Done. Scanned "+(end-start+1)+" ports In " + (time/(1000.0*60)) + " Minutes.");
}
public static class result
{
private final int port ;
private final boolean isOpen ;
private static double timeTaken = 0.00 ;
result(int p , boolean b )
{
this.port = p;
this.isOpen = b;
}
public int givePort()
{
return this.port;
}
public boolean giveStatus()
{
return this.isOpen ;
}
}
//now trying ExecutorService and Future API for multiple threads running.
public static Future<result> portIsOpen(final ExecutorService es , String ip , int port )
{
return es.submit(new Callable<result>()
{
@Override
public result call()
{
result rl = new result(port , false);
long strT = System.currentTimeMillis() ;
try
{
Socket soc = new Socket();
soc.connect(new InetSocketAddress(ip,port) , 200); //timeout 2.0 sec
soc.close();
rl = new result(port , true);
long te = System.currentTimeMillis() - strT ;
rl.timeTaken = te/1000.0 ;
return rl;
}
catch(Exception e)
{
long te = System.currentTimeMillis() - strT ;
rl.timeTaken = te/1000.0 ;
return rl ;
}
}
} ) ; //return statement end.
}
//this process is time consuming.
private static boolean isOpen(String ip , int port )
{
try
{
System.out.println("Socket creating.");
Socket socket = new Socket() ;
socket.connect(new InetSocketAddress(ip,port), 200);
socket.close();
return true;
}
catch(Exception e)
{
//System.out.println("Returning false : "+e);
return false;
}
}
}