@@ -5,21 +5,54 @@ const now = require('performance-now');
55const { isURL } = require ( 'validator' ) ;
66const makeHttpRequest = require ( './httpRequest' ) ;
77const performDnsLookup = require ( './dnsLookup' ) ;
8+ const checkSSLCertificate = require ( './sslCheck' ) ;
89
910function analyzeURL ( req , res ) {
1011 try {
1112 if ( ! req . body . url ) {
1213 throw new Error ( 'URL is missing' ) ;
1314 }
1415
15- const urlToAnalyze = req . body . url ;
16+ let urlToAnalyze = req . body . url . trim ( ) ;
17+ const originalUrl = urlToAnalyze ;
18+ const parsedUrl = url . parse ( urlToAnalyze ) ;
19+ const hadProtocol = ! ! parsedUrl . protocol ;
20+
21+ // If no protocol, try HTTPS first
22+ if ( ! hadProtocol ) {
23+ urlToAnalyze = `https://${ urlToAnalyze } ` ;
24+ }
1625
26+ // Validate the URL
1727 if ( ! isURL ( urlToAnalyze ) ) {
1828 throw new Error ( 'Invalid URL' ) ;
1929 }
2030
2131 const hostname = url . parse ( urlToAnalyze ) . hostname ;
32+ const isHttps = urlToAnalyze . startsWith ( 'https://' ) ;
33+
34+ // Try the request
2235 makeHttpRequest ( urlToAnalyze , { } , ( responseDetails , latencyMs , httpRequestError ) => {
36+ // If HTTPS failed and we added the protocol (no protocol originally), try HTTP as fallback
37+ if ( httpRequestError && isHttps && ! hadProtocol ) {
38+ const httpUrl = urlToAnalyze . replace ( 'https://' , 'http://' ) ;
39+ return makeHttpRequest ( httpUrl , { } , ( httpResponseDetails , httpLatencyMs , httpError ) => {
40+ if ( httpError || ! httpResponseDetails ) {
41+ return res . json ( {
42+ isUp : false ,
43+ ipAddress : null ,
44+ uptime : 0 ,
45+ latencyMs : 0 ,
46+ dnsLookupMs : 0 ,
47+ statusCode : null ,
48+ protocol : 'http' ,
49+ sslInfo : null
50+ } ) ;
51+ }
52+ processResponse ( httpResponseDetails , httpLatencyMs , hostname , 'http' , res ) ;
53+ } ) ;
54+ }
55+
2356 if ( httpRequestError || ! responseDetails ) {
2457 console . error ( `Failed to request ${ urlToAnalyze } : ${ httpRequestError || 'Unknown error' } ` ) ;
2558 return res . json ( {
@@ -28,12 +61,17 @@ function analyzeURL(req, res) {
2861 uptime : 0 ,
2962 latencyMs : 0 ,
3063 dnsLookupMs : 0 ,
31- statusCode : null
64+ statusCode : null ,
65+ protocol : isHttps ? 'https' : 'http' ,
66+ sslInfo : null
3267 } ) ;
3368 }
69+
70+ processResponse ( responseDetails , latencyMs , hostname , isHttps ? 'https' : 'http' , res ) ;
71+ } ) ;
3472
35- const isUp =
36- responseDetails . statusCode >= 200 && responseDetails . statusCode < 400 ;
73+ function processResponse ( responseDetails , latencyMs , hostname , protocol , res ) {
74+ const isUp = responseDetails . statusCode >= 200 && responseDetails . statusCode < 400 ;
3775 if ( ! isUp ) {
3876 return res . json ( {
3977 isUp : false ,
@@ -42,31 +80,58 @@ function analyzeURL(req, res) {
4280 latencyMs,
4381 dnsLookupMs : 0 ,
4482 statusCode : responseDetails . statusCode ,
83+ protocol : protocol ,
84+ sslInfo : null
4585 } ) ;
4686 }
4787
4888 performDnsLookup ( hostname , ( { ipAddress, lookupMs, error : dnsLookupError } ) => {
4989 if ( dnsLookupError ) {
50- console . error ( `Failed to lookup IP address for ${ urlToAnalyze } : ${ dnsLookupError } ` ) ;
90+ console . error ( `Failed to lookup IP address: ${ dnsLookupError } ` ) ;
5191 return res . status ( 500 ) . json ( { error : 'Internal server error' } ) ;
5292 }
5393
54- // Calculate uptime baseline on successful check
55- const uptime = 100 ;
56-
57- console . log (
58- `isUp: ${ isUp } , ipAddress: ${ ipAddress } , uptime: ${ uptime } %, latencyMs: ${ latencyMs } , dnsLookupMs: ${ lookupMs } `
59- ) ;
60- return res . json ( {
61- isUp : true ,
62- ipAddress,
63- uptime,
64- latencyMs,
65- dnsLookupMs : lookupMs ,
66- statusCode : responseDetails . statusCode ,
67- } ) ;
94+ // Check SSL certificate if HTTPS
95+ if ( protocol === 'https' ) {
96+ checkSSLCertificate ( hostname , 443 , ( sslError , sslInfo ) => {
97+ const uptime = 100 ;
98+ const response = {
99+ isUp : true ,
100+ ipAddress,
101+ uptime,
102+ latencyMs,
103+ dnsLookupMs : lookupMs ,
104+ statusCode : responseDetails . statusCode ,
105+ protocol : protocol ,
106+ sslInfo : sslInfo
107+ } ;
108+
109+ console . log (
110+ `isUp: ${ isUp } , ipAddress: ${ ipAddress } , uptime: ${ uptime } %, latencyMs: ${ latencyMs } , dnsLookupMs: ${ lookupMs } , protocol: ${ protocol } `
111+ ) ;
112+ return res . json ( response ) ;
113+ } ) ;
114+ } else {
115+ // HTTP - no SSL info
116+ const uptime = 100 ;
117+ const response = {
118+ isUp : true ,
119+ ipAddress,
120+ uptime,
121+ latencyMs,
122+ dnsLookupMs : lookupMs ,
123+ statusCode : responseDetails . statusCode ,
124+ protocol : protocol ,
125+ sslInfo : null
126+ } ;
127+
128+ console . log (
129+ `isUp: ${ isUp } , ipAddress: ${ ipAddress } , uptime: ${ uptime } %, latencyMs: ${ latencyMs } , dnsLookupMs: ${ lookupMs } , protocol: ${ protocol } `
130+ ) ;
131+ return res . json ( response ) ;
132+ }
68133 } ) ;
69- } ) ;
134+ }
70135 } catch ( error ) {
71136 console . error ( `Error analyzing URL: ${ error . message } ` ) ;
72137 return res . status ( 400 ) . json ( { error : error . message } ) ;
0 commit comments