@@ -42,48 +42,84 @@ <h1>IP Reputation Checker - Both Services</h1>
4242 < div class ="result " id ="result "> </ div >
4343
4444 < script >
45+ const ipv4Regex = / ^ ( 2 5 [ 0 - 5 ] | 2 [ 0 - 4 ] [ 0 - 9 ] | [ 0 1 ] ? [ 0 - 9 ] [ 0 - 9 ] ? ) \. ( 2 5 [ 0 - 5 ] | 2 [ 0 - 4 ] [ 0 - 9 ] | [ 0 1 ] ? [ 0 - 9 ] [ 0 - 9 ] ? ) \. ( 2 5 [ 0 - 5 ] | 2 [ 0 - 4 ] [ 0 - 9 ] | [ 0 1 ] ? [ 0 - 9 ] [ 0 - 9 ] ? ) \. ( 2 5 [ 0 - 5 ] | 2 [ 0 - 4 ] [ 0 - 9 ] | [ 0 1 ] ? [ 0 - 9 ] [ 0 - 9 ] ? ) $ / ;
46+ const ipv6Regex = / ^ ( ( [ 0 - 9 a - f A - F ] { 1 , 4 } : ) { 7 , 7 } [ 0 - 9 a - f A - F ] { 1 , 4 } | ( [ 0 - 9 a - f A - F ] { 1 , 4 } : ) { 1 , 7 } : | ( [ 0 - 9 a - f A - F ] { 1 , 4 } : ) { 1 , 6 } : [ 0 - 9 a - f A - F ] { 1 , 4 } ) $ / ;
47+
48+ function isValidIP ( ip ) {
49+ return ipv4Regex . test ( ip ) || ipv6Regex . test ( ip ) ;
50+ }
51+
52+ function addLabeledValue ( container , label , value ) {
53+ const p = document . createElement ( "p" ) ;
54+ const strong = document . createElement ( "b" ) ;
55+ strong . textContent = `${ label } : ` ;
56+ p . appendChild ( strong ) ;
57+ p . appendChild ( document . createTextNode ( value ?? "N/A" ) ) ;
58+ container . appendChild ( p ) ;
59+ }
60+
4561 async function checkBoth ( ) {
4662 const ip = document . getElementById ( "ipInput" ) . value . trim ( ) ;
4763 const resultDiv = document . getElementById ( "result" ) ;
4864
4965 if ( ! ip ) {
50- resultDiv . innerHTML = "<p>Please enter a valid IP address.</p>" ;
66+ resultDiv . textContent = "Please enter a valid IP address." ;
67+ return ;
68+ }
69+
70+ if ( ! isValidIP ( ip ) ) {
71+ resultDiv . textContent = "Invalid IP address format. Please enter a valid IPv4 or IPv6 address." ;
5172 return ;
5273 }
5374
54- resultDiv . innerHTML = "<p>Checking AbuseIPDB and VirusTotal...</p>" ;
75+ if ( ip . length > 45 ) {
76+ resultDiv . textContent = "IP address is too long." ;
77+ return ;
78+ }
79+
80+ resultDiv . textContent = "Checking AbuseIPDB and VirusTotal..." ;
5581
5682 try {
57- const abuseResponse = await fetch ( `/api/abuseipdb?ip=${ ip } ` ) ;
83+ const abuseResponse = await fetch ( `/api/abuseipdb?ip=${ encodeURIComponent ( ip ) } ` ) ;
5884 const abuseData = await abuseResponse . json ( ) ;
5985
60- const virusResponse = await fetch ( `/api/virustotal?ip=${ ip } ` ) ;
86+ const virusResponse = await fetch ( `/api/virustotal?ip=${ encodeURIComponent ( ip ) } ` ) ;
6187 const virusData = await virusResponse . json ( ) ;
6288
63- resultDiv . innerHTML = `
64- <h3>Results for IP: ${ ip } </h3>
65-
66- <h4>AbuseIPDB</h4>
67- <p><b>Abuse Confidence Score:</b> ${ abuseData . data ?. abuseConfidenceScore || "N/A" } </p>
68- <p><b>Categories:</b> ${ abuseData . data ?. category ?. join ( ", " ) || "N/A" } </p>
69- <p><b>Country:</b> ${ abuseData . data ?. countryCode || "N/A" } </p>
70- <p><b>ISP:</b> ${ abuseData . data ?. isp || "N/A" } </p>
71- <p><b>Domain:</b> ${ abuseData . data ?. domain || "N/A" } </p>
72-
73- <h4>VirusTotal</h4>
74- <p><b>Reputation:</b> ${ virusData . data ?. attributes ?. reputation || "N/A" } </p>
75- <p><b>Network:</b> ${ virusData . data ?. attributes ?. network || "N/A" } </p>
76- <p><b>Last Analysis Stats:</b> ${
77- virusData . data ?. attributes ?. last_analysis_stats
78- ? JSON . stringify ( virusData . data . attributes . last_analysis_stats )
79- : "N/A"
80- } </p>
81- <p><b>Tags:</b> ${ virusData . data ?. attributes ?. tags ?. join ( ", " ) || "N/A" } </p>
82- <p><b>Whois:</b> ${ virusData . data ?. attributes ?. whois || "N/A" } </p>
83- ` ;
89+ resultDiv . textContent = "" ;
90+
91+ const title = document . createElement ( "h3" ) ;
92+ title . textContent = `Results for IP: ${ ip } ` ;
93+ resultDiv . appendChild ( title ) ;
94+
95+ const abuseHeader = document . createElement ( "h4" ) ;
96+ abuseHeader . textContent = "AbuseIPDB" ;
97+ resultDiv . appendChild ( abuseHeader ) ;
98+
99+ addLabeledValue ( resultDiv , "Abuse Confidence Score" , String ( abuseData . data ?. abuseConfidenceScore ?? "N/A" ) ) ;
100+ addLabeledValue ( resultDiv , "Categories" , abuseData . data ?. category ?. join ( ", " ) ?? "N/A" ) ;
101+ addLabeledValue ( resultDiv , "Country" , abuseData . data ?. countryCode ?? "N/A" ) ;
102+ addLabeledValue ( resultDiv , "ISP" , abuseData . data ?. isp ?? "N/A" ) ;
103+ addLabeledValue ( resultDiv , "Domain" , abuseData . data ?. domain ?? "N/A" ) ;
104+
105+ const virusHeader = document . createElement ( "h4" ) ;
106+ virusHeader . textContent = "VirusTotal" ;
107+ resultDiv . appendChild ( virusHeader ) ;
108+
109+ addLabeledValue ( resultDiv , "Reputation" , String ( virusData . data ?. attributes ?. reputation ?? "N/A" ) ) ;
110+ addLabeledValue ( resultDiv , "Network" , virusData . data ?. attributes ?. network ?? "N/A" ) ;
111+ addLabeledValue (
112+ resultDiv ,
113+ "Last Analysis Stats" ,
114+ virusData . data ?. attributes ?. last_analysis_stats
115+ ? JSON . stringify ( virusData . data . attributes . last_analysis_stats )
116+ : "N/A"
117+ ) ;
118+ addLabeledValue ( resultDiv , "Tags" , virusData . data ?. attributes ?. tags ?. join ( ", " ) ?? "N/A" ) ;
119+ addLabeledValue ( resultDiv , "Whois" , virusData . data ?. attributes ?. whois ?? "N/A" ) ;
84120 } catch ( error ) {
85121 console . error ( error ) ;
86- resultDiv . innerHTML = "<p> Error fetching data from both services.</p> " ;
122+ resultDiv . textContent = "Error fetching data from both services." ;
87123 }
88124 }
89125 </ script >
0 commit comments