npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

external-ip

v2.3.1

Published

A node.js library to get your external ip from multiple services

Downloads

12,819

Readme

external-ip

Build Status Dependency Status npm version

XKCD 865

external-ip is a node.js library to get your external ip from multiple services.

Installation

npm install external-ip

Usage

basic

'use strict';

const getIP = require('external-ip')();

getIP((err, ip) => {
    if (err) {
        // every service in the list has failed
        throw err;
    }
    console.log(ip);
});

with configuration

'use strict';

const extIP = require('external-ip');

let getIP = extIP({
    replace: true,
    services: ['https://ipinfo.io/ip', 'http://ifconfig.co/x-real-ip', 'http://ifconfig.io/ip'],
    timeout: 600,
    getIP: 'parallel',
    userAgent: 'Chrome 15.0.874 / Mac OS X 10.8.1'
});

getIP((err, ip) => {
    if (err) {
        throw err;
    }
    console.log(ip);
});

Promises

The API of this library is designed around the classic node.js error-first callback. As of node.js V8, converting this type of callback into a promise is pretty straight forward and requires one more line of code.

basic

'use strict';

const {promisify} = require('util'); //<-- Require promisify
const getIP = promisify(require('external-ip')()); // <-- And then wrap the library

getIP().then((ip)=> {
    console.log(ip);
}).catch((error) => {
    console.error(error);
});

with configuration

'use strict';
const { promisify } = require('util'); //<-- Require promisify
const getIP = promisify(require('external-ip')({
    replace: true,
    services: ['https://ipinfo.io/ip', 'http://icanhazip.com/', 'http://ident.me/'],
    timeout: 600,
    getIP: 'parallel',
    verbose: true
})); // <-- And then wrap the library

getIP().then((ip) => {
    console.log(ip);
}).catch((error) => {
    console.error(error);
});

If you believe this extra step shouldn't be there, feel free to open an issue and/or a pull request.

Configuration

extIP([config])

require('external-ip') returns a constructor function that accepts an optional configuration object. It can be used to create multiple instances with different configuration if necessary

  • services: Array of urls that return the ip in the html body, required if replace is set to true
  • replace: Boolean if true, replaces the internal array of services with the user defined, if false, extends it, default: false
  • timeout: Timeout per request in ms, default 1000
  • getIP: 'sequential' Makes a request to the first url in the list, if that fails sends to the next and so on. 'parallel' Makes requests to all the sites in the list, on the first valid response all the pending requests are canceled, default: 'sequential'
  • userAgent: String Set a custom User-Agent header, default: curl/
  • verbose: Boolean Log additional information to the console, default: false

Returns the configured getIP instance.

getIP(callback)

The callback gets 2 arguments:

  1. error: if every service in the list fails to return a valid ip
  2. ip: your external ip

CLI

install as a global package with npm install -g external-ip.

$ external-ip -h

Usage: external-ip [options]

  Options:

    -h, --help                    output usage information
    -V, --version                 output the version number
    -R, --replace                 replace internal services instead of extending them.
    -s, --services <url>          service url, see examples, required if using -R
    -t, --timeout <ms>            set timeout per request
    -P, --parallel                set to parallel mode
    -u, --userAgent <User-Agent>  provide a User-Agent header, default: curl/
    -v, --verbose                 provide additional details


        This program prints the external IP of the machine.
        All arguments are optional.

        Examples:
        $ external-ip
        $ external-ip -P -t 1500 -R -s http://icanhazip.com/ -s http://ident.me/

        Default services:
        http://icanhazip.com/
        http://ident.me/
        http://tnx.nl/ip
        http://myip.dnsomatic.com/
        http://ipecho.net/plain
        http://diagnostic.opendns.com/myip

        Documentation can be found at https://github.com/J-Chaniotis/external-ip

Test

Change your working directory to the project's root, npm install to get the development dependencies and then run npm test

Links