-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.java
More file actions
82 lines (74 loc) · 2.21 KB
/
Application.java
File metadata and controls
82 lines (74 loc) · 2.21 KB
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
import java.io.*;
import java.util.*;
public class Application implements Printable{
/*private Http http;
private Dhcp dhcp;*/
private boolean isPartial;
private String reassembledPacket;
private AppProtocol appProtocol;
/*Get, Head, post, put, delete, connect, options, trace, patch*/
private static final Set<String> HTTP_METHOD = new HashSet<String>(Arrays.asList("GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH", "HTTP"));
//TODO delete num packet in constructor use for debug
Application(AppProtocol appProto, boolean isPartial, String reassembledPacket) {
this.isPartial = isPartial;
this.reassembledPacket = reassembledPacket;
this.appProtocol = appProto;
}
public static AppProtocol buildProtocol(byte[] datagram){
if (isHttp(datagram))
{
return new Http(datagram);
}else if (isDhcp(datagram)){
return new Dhcp(datagram);
}else{
try{
AppProtocol app = new Dns(datagram);
return app;
}catch(Exception e){
//e.printStackTrace();
return null;
}
}
}
public static boolean isHttp(byte[] datagram){
try{
String str = new String(datagram, "US-ASCII");
return HTTP_METHOD.contains(str.substring(0,3)) ||
HTTP_METHOD.contains(str.substring(0,4)) ||
HTTP_METHOD.contains(str.substring(0,5)) ||
HTTP_METHOD.contains(str.substring(0,6)) ||
HTTP_METHOD.contains(str.substring(0,7));
} catch(Exception e){
return false;
}
}
public static boolean isDhcp(byte[] datagram){
if (datagram.length > 240)
return Utils.byteToHex(Arrays.copyOfRange(datagram, 236, 240)).equals("63825363");
return false;
}
public String getProtocol(){
if (this.appProtocol == null)
return "????";
return this.appProtocol.getProtocol();
}
public String tinyPrint(){
if (isPartial)
return this.reassembledPacket.split("\r\n")[0];//.replace("\n", " ");
return this.appProtocol.tinyPrint();
}
public String detailPrint(){
if (isPartial)
return this.reassembledPacket.split("\r\n")[0] + "\n" + this.reassembledPacket.split("\r\n")[1] + "\n...";
return this.appProtocol.detailPrint();
}
public boolean isPartial(){
return this.isPartial;
}
public String getReassembledPacket(){
return this.reassembledPacket;
}
public AppProtocol getAppProto(){
return this.appProtocol;
}
}