Unable to obtain local IP address of client in Angular application using Chrome

Asked 2 years ago, Updated 2 years ago, 85 views

We are currently developing web applications at Angular 7 (Nodejs).

When accessing the web application (hereinafter referred to as the application), it was necessary to obtain the local IP of the client terminal (the terminal accessing this application) on the first screen, and the IP acquisition process was implemented by referring to the site below.


Get Local IP with "http://api.ipify.org/?format=json"
Reference site: https://www.c-sharpcorner.com/article/how-to-get-the-client-ip-address-in-angular-application/

When I was running the application locally on "http://localhost:4200/#/", the application was working properly using the above method. When building and deploying Angular resources (TypeScript/HTML/SCSS) to a Windows IIS web server, a Mixed Content error was encountered as a Chrome browser-specific (probably) error and local IP address acquisition process failed.
Enter a description of the image here

I understand that this is caused by the processing of IP acquisition via http communication, but I can't get a good solution even if I try to use other sites as a reference.Could you please let me know if there is an appropriate <https communication to obtain a local IP (nonpublic IP like 10.xx.xx.xx) site or nnpm or node module to obtain a local IP address?

By the way, I searched the appropriate sites for に, but all of them are public IP acquisition sites, so I haven't found any sites related to local IP.
②I have been reviewing the following two points for about a week, but I have not been able to successfully construct the process of obtaining local IP in the typescript file.
1.Using the node_modules "local-ipv4-address" module
The command node local-ipv4-address succeeded in displaying the local IP of 10.xx.xx.xx on the command screen.
An error occurs when I write code for local-ipv4-address in hello.js created in the Angular project and run the app locally on "http://localhost:4200/#/".It has not been resolved.
Hello.js
in the Angular project hello.js

in the Angular project

Error on Chrome when running locally
Error on Chrome when running locally

2. I'm verifying the use of RTCPeerConnection, but it doesn't work.
Reference site: https://stackblitz.com/edit/angular-get-local-ip

import {Component, ChangeDetectorRef, NgZone} from "@angular/core";

US>declare global{
  interface window {
    RTCPeerConnection:RTCPeerConnection;
    mozRTCPeerConnection:RTCPeerConnection;
    webkitRTCPeerConnection:RTCPeerConnection;
  }
}

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.scss"]
})
export class AppComponent {
  loading —boolean=false;
  localIp=sessionStorage.getItem('LOCAL_IP');
  private ipRegex=newRegExp(/([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4})/);

  constructor(
    private zone —NgZone
  ) {  }

  ngOnInit(){
    This.determineLocalIp();
  }

  private determineLocalIp(){
    **window.RTCPeerConnection**=this.getRTCPeerConnection();
    varpc = new RTCPeerConnection ({iceServers:[]});
    pc.createDataChannel(');
    pc.createOffer().then(pc.setLocalDescription.bind(pc));

    pc.oniccandidate=(ice)=>{
      This.zone.run(()=>{
        if(!ice||!ice.candidate||!ice.candidate){
          return;
        }

        This.localIp=this.ipRegex.exec(ice.candidate.candidate)[1];
        sessionStorage.setItem('LOCAL_IP', this.localIp);

        pc.oniccandidate=()=>{};
        pc.close();
      });
    };
  }
  private getRTCPeerConnection(){
    return window.RTCPeerConnection||
      window.mozRTCPeerConnection||
      window.webkitRTCPeerConnection;
  }
}

*Error below in bold text "window.RTCPeerConnection" with above code

Type 'RTCPeerConnection' is not assignable to type 'RTCPeerConnection & {new(configuration?:RTCConfiguration):RTCPeerConnection;prototype:RTCPeerConnection; generateCertificate (keygenAlgorithm:Alarmament;PromptientIdentification) ...
  Type 'RTCPeerConnection' is missing the following properties from type '{ new (configuration?: RTCConfiguration): RTCPeerConnection;prototype: RTCPeerConnection; generateCertificate (keygenAlgorism: AlgorithmIdentifier): Promise <getSource...;;;;

Development Server: Windows Server 2016 (AWS EC2)
Web Server:Windows IIS Development Program (see Capture below)
Enter a description of the image here

node.js google-chrome typescript https angular

2022-09-30 19:54

1 Answers

Local IP addresses are not visible from the outside, so there is no site where you can get a local IP address through REST API etc. There is a site where you can use WebRTC to display the local IP address on the screen.
Also, the error is only mixed content because I tried to hit the API with http even though IIS is https. Based on the linked code,

-return this.http.get("http://api.ipify.org/?format=json");  
+    return this.http.get("https://api.ipify.org/?format=json");  

Just make it https as shown in .

I understand that you are using the Angular-CLI. If so, you should be able to use require in webpack if you write it in the same place as other codes. However, the package local-ipv4-address just hits the API of each OS on the node and doesn't seem to be able to get an IP address on the browser.

Apparently, the error is a TypeScript compilation error, but it is caused by trying to overwrite window.RTCPeerConnection. (I don't think I need to redefine the type in the first place.)
Wouldn't the line be just const RTCPeerConnection=this.getRTCPeerConnection()?


2022-09-30 19:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.