11const express = require ( 'express' ) ;
22var cors = require ( 'cors' ) ;
33var app = express ( ) ;
4- // const KEY = require('./key');
54const Base64 = require ( 'base-64' ) ;
65const KEY = process . env . GITHUB_TOKEN
76app . use ( cors ( ) )
@@ -11,37 +10,37 @@ const {
1110} = require ( 'axios' ) ;
1211
1312app . get ( '/' , async ( req , res ) => {
14-
15- const ip = req . headers [ 'x-forwarded-for' ] . split ( "," ) [ 0 ] . trim ( )
16- const agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36' ;
17-
18- const url = "https://api.ip8.com/ip/lookup/" + ip ;
19- // const url = "https://api.ip8.com/ip/lookup/176.10.112.40";
20- await Axios . get ( url , {
21- headers : {
22- 'user-agent' : agent ,
13+ try {
14+ const url = "https://api2.ip8.com/ip/info" ;
15+ const response = await Axios . post ( url ) ;
16+ if ( response . status === 200 && response . data && response . data . ip && response . data . ip . length > 0 ) {
17+ const ipAddress = response . data . ip [ 0 ] ;
18+ if ( response . data . data && response . data . data [ ipAddress ] ) {
19+ const ipData = response . data . data [ ipAddress ] ;
20+ const data = {
21+ headers : req . headers [ 'user-agent' ] ,
22+ ip : ipAddress ,
23+ isp : ipData . isp . isp ,
24+ ispFull : ipData . isp . organization ,
25+ city : ipData . geoip . city ,
26+ pin : ipData . geoip . postalcode ,
27+ timezone : ipData . geoip . timezone ,
28+ country : ipData . geoip . country ,
29+ continent : ipData . geoip . continent ,
30+ latitude : ipData . geoip . latitude ,
31+ longitude : ipData . geoip . longitude ,
32+ } ;
33+ return res . status ( 200 ) . json ( data ) ;
34+ } else {
35+ throw new Error ( "Invalid response data format" ) ;
36+ }
37+ } else {
38+ throw new Error ( "Invalid response from IP info API" ) ;
2339 }
24- } ) . then ( async result => {
25- console . log ( result . data )
26- const data = {
27- headers : req . headers [ 'user-agent' ] ,
28- ip,
29- isp : result . data . details . geoip [ 0 ] . isp . org ,
30- ispFull : result . data . details . geoip [ 0 ] . isp . ASNname ,
31- city : result . data . details . geoip [ 0 ] . city . name ,
32- pin : result . data . details . geoip [ 0 ] . city . zip ,
33- timezone : result . data . details . geoip [ 0 ] . city . timezone ,
34- continent : result . data . details . geoip [ 0 ] . continent . name ,
35- country : result . data . details . geoip [ 0 ] . country . name ,
36- latitude : result . data . details . geoip [ 0 ] . city . details . latitude ,
37- longitude : result . data . details . geoip [ 0 ] . city . details . longitude ,
38- }
39-
40- return res . status ( 200 ) . json ( data )
41- } ) . catch ( e => {
42- console . log ( "Something Went Wrong, wonderfully handled exception" , e )
43- } )
44-
40+ } catch ( error ) {
41+ console . error ( "Error fetching IP information:" , error ) ;
42+ return res . status ( 500 ) . json ( { error : "Internal Server Error" } ) ;
43+ }
4544} ) ;
4645
4746app . get ( '/weather/:city' , async ( req , res ) => { // Because URL PARAMS ARE COOLER
@@ -88,6 +87,7 @@ app.get('/add-sha/:sha', (req, res) => {
8887 } )
8988} )
9089
91- const port = process . env . PORT || 4000 ;
90+ const port = 4000 ;
9291 app . listen ( port , async ( ) => {
92+ console . log ( "Listening on http://localhost:4000" )
9393} ) ;
0 commit comments