-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraceNet.java
More file actions
153 lines (125 loc) · 5.39 KB
/
TraceNet.java
File metadata and controls
153 lines (125 loc) · 5.39 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
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
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.*;
class Hop {
int hop;
String ip;
double latency;
String location;
Hop(int hop, String ip, double latency, String location) {
this.hop = hop;
this.ip = ip;
this.latency = latency;
this.location = location;
}
@Override
public String toString() {
return String.format("Hop %d: %s - %.2f ms [%s]", hop, ip, latency, location);
}
}
public class TraceNet {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.print("Enter domain or IP: ");
String target = sc.nextLine().trim();
String os = System.getProperty("os.name").toLowerCase();
String command = os.contains("win") ? "tracert" : "traceroute";
ProcessBuilder pb = new ProcessBuilder(command, target);
Process process = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
Pattern latencyPattern = Pattern.compile("(\\d+(\\.\\d+)?)\\s*ms");
Pattern ipPattern = Pattern.compile("\\b(\\d{1,3}(\\.\\d{1,3}){3})\\b"); // IPv4
Pattern ipv6Pattern = Pattern.compile("([a-fA-F0-9:]+:+)+[a-fA-F0-9]+"); // IPv6
ArrayList<Hop> hops = new ArrayList<>();
Hop bottleneck = new Hop(0, "", 0, "N/A");
while ((line = reader.readLine()) != null) {
// Optional Debug Line
System.out.println("[DEBUG] " + line);
if (line.contains("Request timed out.") || line.contains("* * *")) {
continue; // or mark as unreachable hop
}
String[] tokens = line.trim().split("\\s+");
if (tokens.length > 1) {
try {
int hopNumber = Integer.parseInt(tokens[0]);
// Extract IP
String ip = "No response";
int start = line.indexOf('[');
int end = line.indexOf(']');
if (start != -1 && end != -1 && end > start) {
ip = line.substring(start + 1, end);
} else {
Matcher ipMatch = ipPattern.matcher(line);
Matcher ipv6Match = ipv6Pattern.matcher(line);
if (ipMatch.find()) {
ip = ipMatch.group();
} else if (ipv6Match.find()) {
ip = ipv6Match.group();
}
}
// Extract latency
Matcher timeMatch = latencyPattern.matcher(line);
double latency = timeMatch.find() ? Double.parseDouble(timeMatch.group(1)) : 0;
// Get location only if valid IP (skip "No response")
String location = (!ip.equals("No response")) ? getIPLocation(ip) : "N/A";
Hop h = new Hop(hopNumber, ip, latency, location);
hops.add(h);
if (latency > bottleneck.latency) {
bottleneck = h;
}
} catch (NumberFormatException ignored) {
}
}
}
System.out.println("\n🛰️ Trace Completed:\n");
for (Hop h : hops) {
System.out.println(h);
}
System.out.printf("\n⚠️ Bottleneck: Hop %d (%s) - %.2f ms [%s]\n",
bottleneck.hop, bottleneck.ip, bottleneck.latency, bottleneck.location);
// Save report
try (PrintWriter out = new PrintWriter("trace_report.txt")) {
out.println("Trace Report for: " + target + "\n");
for (Hop h : hops) {
out.println(h);
}
out.printf("\nBottleneck: Hop %d (%s) - %.2f ms [%s]\n",
bottleneck.hop, bottleneck.ip, bottleneck.latency, bottleneck.location);
}
System.out.println("\n📄 Report saved as 'trace_report.txt'");
}
// Get IP Location without external JSON library
public static String getIPLocation(String ip) {
try {
URL url = new URL("http://ip-api.com/json/" + ip);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(4000);
conn.setReadTimeout(4000);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder json = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
json.append(inputLine);
}
in.close();
String data = json.toString();
String city = extract(data, "\"city\":\"", "\"");
String country = extract(data, "\"country\":\"", "\"");
String org = extract(data, "\"org\":\"", "\"");
return city + ", " + country + " (" + org + ")";
} catch (Exception e) {
return "Geo API failed";
}
}
public static String extract(String json, String start, String end) {
int s = json.indexOf(start);
if (s == -1)
return "N/A";
s += start.length();
int e = json.indexOf(end, s);
return (e == -1) ? "N/A" : json.substring(s, e);
}
}