Skip to content

Instantly share code, notes, and snippets.

@gugadev
Last active October 26, 2018 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gugadev/f3de0feb1422b622d4ed3e1c0c119c21 to your computer and use it in GitHub Desktop.
Save gugadev/f3de0feb1422b622d4ed3e1c0c119c21 to your computer and use it in GitHub Desktop.
IP Locator
yarn install
yarn run shit --ip=<ip here>
import * as os from 'os'
import * as got from 'got'
import * as yargs from 'yargs'
import * as ora from 'ora'
import chalk from 'chalk'
const log = console.log
const clear = console.clear
interface Response {
country_name: string
city: string
district: string
latitude: number
longitude: number
isp: string
}
class Geolocation {
country: string
city: string
district: string
latitude: number
longitude: number
provider: string
constructor(
country: string,
city: string,
district: string,
latitude: number,
longitude: number,
provider: string
) {
this.country = country
this.city = city
this.district = district
this.latitude = latitude
this.longitude = longitude
this.provider = provider
}
}
class Locator {
private spinner = ora('')
private getPlatform(): string {
const plat = os.platform()
if (plat === 'darwin') {
return 'MacOS'
}
// capitalize
return plat.substr(0, 1).toUpperCase() + plat.substr(1)
}
private fail(message: string): void {
this.spinner.fail(chalk.redBright(message))
process.exit(0)
}
private showWelcome(): void {
clear()
const platform = this.getPlatform()
this.spinner.start(chalk.whiteBright(`Running script on ${platform}`))
}
private showOnMaps(geo: Geolocation): void {
const url = `https://maps.google.com?q=${geo.latitude},${geo.longitude}`
log(
chalk.whiteBright('See on Google Maps', '➡️ ', chalk.cyanBright(url))
)
}
private async locate(ip: string): Promise<Geolocation> {
const endpoint = 'https://api.ipgeolocation.io/ipgeo'
const key = '4743a03c120a48a3a2a8aad83482c8fd'
const ops: any = { json: true }
const res = await got.get(`${endpoint}?apiKey=${key}&ip=${ip}`, ops)
const body = res.body as Response
return new Geolocation(
body.country_name,
body.city,
body.district,
body.latitude,
body.longitude,
body.isp
)
}
private getAddress(): string {
const argv = yargs.argv
if (!argv.ip) {
throw new Error('Enter an IP through --ip flag')
}
return argv.ip
}
private volcate(geo: Geolocation): void {
log('')
log(chalk.greenBright('Country:\t'), chalk.whiteBright(geo.country))
log(chalk.greenBright('City:\t\t'), chalk.whiteBright(geo.city))
log(chalk.greenBright('District:\t'), chalk.whiteBright(geo.district))
log(chalk.greenBright('Latitude:\t'), chalk.whiteBright(geo.latitude.toString()))
log(chalk.greenBright('Longitude:\t'), chalk.whiteBright(geo.longitude.toString()))
log(chalk.greenBright('Provider:\t'), chalk.whiteBright(geo.provider))
log('')
}
public async run(): Promise<void> {
try {
this.showWelcome()
const ip = this.getAddress()
const geo: Geolocation = await this.locate(ip)
this.spinner.succeed()
this.volcate(geo)
this.showOnMaps(geo)
} catch (e) {
this.fail(e.message)
}
}
}
new Locator().run()
{
"scripts": {
"shit": "ts-node app.ts"
},
"dependencies": {
"@types/chalk": "^2.2.0",
"@types/got": "^8.3.4",
"@types/node": "^10.12.0",
"@types/ora": "^1.3.4",
"@types/yargs": "^12.0.1",
"chalk": "^2.4.1",
"got": "^9.2.2",
"ora": "^3.0.0",
"yargs": "^12.0.2"
},
"devDependencies": {
"ts-node": "^7.0.1",
"tslint": "^5.11.0",
"typescript": "^3.1.3"
},
"name": "locateit",
"version": "1.0.0",
"description": "IP hunter script",
"main": "app.ts",
"author": "Gustavo Garzaki",
"license": "MIT"
}
{
"compilerOptions": {
"lib": [
"es2015"
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment