-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
214 lines (189 loc) · 8.72 KB
/
script.js
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
document.addEventListener('DOMContentLoaded', () => {
const ipForm = document.getElementById('ipForm');
const resultContainer = document.getElementById('resultContainer');
class IPRiskAnalyzer {
constructor() {
this.riskFactors = {
blacklistedCountries: ['RU', 'CN', 'IR', 'KP', 'SY', 'IQ', 'VN', 'BY'],
riskyCIDRs: [
'5.188.0.0/16', '45.144.0.0/14',
'85.113.0.0/16', '185.153.196.0/22',
'192.168.0.0/16', '10.0.0.0/8'
],
suspiciousISPs: [
'Tor', 'VPN', 'Proxy', 'Hosting',
'Cloud Provider', 'Anonymizer', 'Anonymous'
]
};
}
analyzeIPRisk(data) {
let riskScore = 0;
const analysis = {
totalRiskScore: 0,
riskDetails: {},
recommendations: [],
securityInsights: []
};
// Country Risk Assessment
if (this.riskFactors.blacklistedCountries.includes(data.country_code || data.countryCode)) {
riskScore += 40;
analysis.riskDetails.countryRisk = {
status: 'High Risk',
explanation: 'IP address belongs to a high-risk country'
};
analysis.recommendations.push('Avoid using VPNs or proxies');
}
// Network Range Risk
const checkIPRange = (ip, ranges) => {
return ranges.some(range => {
const [network, cidrMask] = range.split('/');
return ip.startsWith(network.split('.').slice(0, parseInt(cidrMask)).join('.'));
});
};
if (checkIPRange(data.ip, this.riskFactors.riskyCIDRs)) {
riskScore += 30;
analysis.riskDetails.networkRisk = {
status: 'Potentially Dangerous Network',
explanation: 'IP address belongs to a suspicious network range'
};
analysis.recommendations.push('Take additional network security measures');
}
// ISP/Organization Risk
const org = (data.org || data.isp || '').toLowerCase();
if (this.riskFactors.suspiciousISPs.some(isp => org.includes(isp.toLowerCase()))) {
riskScore += 20;
analysis.riskDetails.ispRisk = {
status: 'Anonymity Risk',
explanation: 'IP address is using an anonymization/masking service'
};
analysis.recommendations.push('Use your real IP address');
}
// Geographical Anomaly Risk
if ((data.latitude && data.longitude) &&
(Math.abs(parseFloat(data.latitude)) > 60 || Math.abs(parseFloat(data.longitude)) > 150)) {
riskScore += 10;
analysis.riskDetails.geoRisk = {
status: 'Geographical Anomaly',
explanation: 'IP location is in an unexpected region'
};
analysis.securityInsights.push('Geographical location appears suspicious');
}
// Advanced Threat Detection
if (data.asn && parseInt(data.asn) < 1000) {
riskScore += 15;
analysis.riskDetails.asnRisk = {
status: 'Potential Threat Source',
explanation: 'Low ASN number may indicate suspicious activity'
};
}
analysis.totalRiskScore = Math.min(riskScore, 100);
analysis.riskLevel =
analysis.totalRiskScore < 30 ? 'Low Risk' :
analysis.totalRiskScore < 60 ? 'Medium Risk' : 'High Risk';
return analysis;
}
}
class IPInfoFetcher {
constructor() {
this.apiUrls = [
'https://ipapi.co/${ip}/json/',
'https://ipinfo.io/${ip}/json',
'https://ip-api.com/json/${ip}'
];
}
async fetchIPInfo(ip) {
for (const apiUrlTemplate of this.apiUrls) {
const apiUrl = apiUrlTemplate.replace('${ip}', ip);
try {
const response = await fetch(apiUrl, {
method: 'GET',
headers: { 'Accept': 'application/json' }
});
if (!response.ok) continue;
const data = await response.json();
if (data.error) continue;
return data;
} catch (error) {
console.error(`Error fetching from ${apiUrl}:`, error);
}
}
throw new Error('All APIs failed');
}
}
function displayIPInfo(data) {
const analyzer = new IPRiskAnalyzer();
const riskAnalysis = analyzer.analyzeIPRisk(data);
const riskLevelClass =
riskAnalysis.totalRiskScore < 30 ? 'low-risk' :
riskAnalysis.totalRiskScore < 60 ? 'medium-risk' : 'high-risk';
resultContainer.innerHTML = `
<div class="info">
<p><i class="fas fa-map-pin"></i><strong>IP Address:</strong> ${escapeHtml(data.ip || data.query)}</p>
<p><i class="fas fa-flag"></i><strong>Country:</strong> ${escapeHtml(data.country_name || data.country)}</p>
<p><i class="fas fa-map-marker-alt"></i><strong>Region:</strong> ${escapeHtml(data.region || data.region_name)}</p>
<p><i class="fas fa-building"></i><strong>City:</strong> ${escapeHtml(data.city)}</p>
<p><i class="fas fa-network-wired"></i><strong>ISP:</strong> ${escapeHtml(data.org || data.isp)}</p>
</div>
<div class="risk-analysis ${riskLevelClass}">
<h3>
<i class="fas ${riskLevelClass === 'low-risk' ? 'fa-shield' :
riskLevelClass === 'medium-risk' ? 'fa-exclamation-triangle' : 'fa-skull-crossbones'}"></i>
Security Risk Analysis
</h3>
<div class="risk-score">Risk Score: ${riskAnalysis.totalRiskScore}/100</div>
<p><strong>Risk Level:</strong> ${riskAnalysis.riskLevel}</p>
${Object.entries(riskAnalysis.riskDetails).map(([key, detail]) => `
<p><strong>${detail.status}:</strong> ${detail.explanation}</p>
`).join('')}
${riskAnalysis.recommendations.length ? `
<div class="recommendations">
<h4>Recommendations:</h4>
${riskAnalysis.recommendations.map(rec => `<p>• ${rec}</p>`).join('')}
</div>
` : ''}
${riskAnalysis.securityInsights.length ? `
<div class="security-insights">
<h4>Security Insights:</h4>
${riskAnalysis.securityInsights.map(insight => `<p>• ${insight}</p>`).join('')}
</div>
` : ''}
</div>
`;
}
function displayError(message) {
resultContainer.innerHTML = `
<p class="error">${escapeHtml(message)}</p>
`;
}
function escapeHtml(unsafe) {
return unsafe
? unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'")
: 'No Information';
}
ipForm.addEventListener('submit', async (e) => {
e.preventDefault();
const ipInput = document.getElementById('ipInput');
const ip = ipInput.value.trim();
const ipRegex = /^(\d{1,3}\.){3}\d{1,3}$/;
if (!ipRegex.test(ip)) {
displayError('Invalid IP address. Please enter a valid IP address.');
return;
}
try {
const ipFetcher = new IPInfoFetcher();
const response = await ipFetcher.fetchIPInfo(ip);
if (response && response.ip) {
displayIPInfo(response);
} else {
displayError('Could not retrieve IP information. Please enter a valid IP address.');
}
} catch (error) {
displayError(`An error occurred: ${error.message}`);
}
});
});