0

I know I am not the first to ask this question ,I am working in an Angular 4 application ,In this I need to take the public IP of my system(user system).

For that I have searched for the reference in Stackoverflow but most of the post gives the solution to consume third part API url for getting IP.

But in my case I want to programmatically trace the public IP of the user system.

I am looking for the suggestions or solution in this.

Nikson
  • 900
  • 2
  • 21
  • 51
  • 2
    Is something in Typescript / angular refraining from applying the same as in https://stackoverflow.com/a/32841164/3702797? – Kaiido May 17 '18 at 07:29
  • 1
    @Kaiido I already tried this solution ,which returns my network IP instead of public IP – Nikson May 17 '18 at 07:34
  • 1
    Try the [`public-ip`](https://github.com/sindresorhus/public-ip) package. – tony19 May 17 '18 at 07:51
  • When the user loads the application, do they do so by hitting a webserver? Is the webserver under your control? If so, you might be able to get the users IP from the request sent to the webserver. Then you can write it in to the page/scripts/anything thats returned to your application. As an example, with ASP.Net you can use the HttpRequest object to [get the IP](https://msdn.microsoft.com/en-us/library/system.web.httprequest.userhostaddress(v=vs.110).aspx). –  May 17 '18 at 08:22
  • @tony19 it worked thanks – Nikson May 17 '18 at 10:31

1 Answers1

0

You could use the public-ip package. Note this internally uses a third-party server to determine your public IP address, but at least that's abstracted away.

import { Component, OnInit } from '@angular/core';
import publicIp from 'public-ip';

@Component({
  selector: 'public-ip',
  template: `<h1>Your public IP is {{publicIpAddr}}</h1>`
})
export class PublicIpComponent implements OnInit {
  publicIpAddr: string;

  ngOnInit() {
    publicIp.v4().then((ip) => this.publicIpAddr = ip);
  }
}

demo

tony19
  • 125,647
  • 18
  • 229
  • 307
  • 1
    If 3rd party is down.. your code breaks and we cannot depend on 3rd party without testing them thoroughly... – Ziggler Jul 16 '18 at 23:19