简体   繁体   English

检索客户端请求ip地址

[英]Retrieving the client request ip address

This post isn't really a question anymore;这篇文章不再是一个问题了。 I just want to post this to help other people out in the future to avoid lost time.我只是想发布这个以帮助其他人在未来避免浪费时间。

Goal : Retrieve the client IP address and set some specific values based on certain octet in IP.目标:检索客户端 IP 地址并根据 IP 中的某些八位字节设置一些特定值。

I was developing a react web-app for my company and needed to support three facilities.我正在为我的公司开发一个反应网络应用程序,需要支持三个设施。 The three locations of-course existed in different geographical regions and had slightly different IP schema's.这三个位置当然存在于不同的地理区域,并且 IP 架构略有不同。

I needed to set some session identifier based on an octet value from the client IP.我需要根据来自客户端 IP 的八位字节值设置一些 session 标识符。 To do so, I did the following steps.为此,我执行了以下步骤。

  1. Setup express route for user to hit on initial visit of app.设置快速路线供用户首次访问应用程序。
  2. Get client IP and store in const/var.获取客户端 IP 并存储在 const/var 中。
  3. Explode IP string by ".""."分解 IP 字符串。 . .
  4. Perform If/Then or Switch to determine value of desired octet.执行If/ThenSwitch以确定所需八位字节的值。
  5. Set some session/logic within matching condition.在匹配条件内设置一些会话/逻辑。

Thanks to express, the req object contains an ip key with the value of the requests IP address.感谢 express, req object 包含一个 ip 键,其值为请求 IP 地址的值。 We can utilize this or some other third party library to get the needed info.我们可以利用这个或其他一些第三方库来获取所需的信息。 Of course there are better/more secure ways to do this, but this is a simple method I've researched and setup.当然有更好/更安全的方法可以做到这一点,但这是我研究和设置的一种简单方法。 Definitely thanks to community for helping me resolve my issue with this.绝对感谢社区帮助我解决这个问题。

    apiRouter.route('/test')

    .get((req, res) => {
        const request_ip = req.ip;      // Returns string like ::ffff:192.168.0.1
        const ip_array = request_ip.split('.')      // Returns array of the string above separated by ".". ["::ffff:192","168","0","1"]

        // The switch statement checks the value of the array above for the index of 2. This would be "0"
        switch(ip_array[2]) {
            case('0'):
                res.json({'request-ip':ip_array, 'location':'Location A'});
                break;
            case('1'):
                res.json({'request-ip':ip_array, 'location':'Location B'});
                break;
            case('2'):
                res.json({'request-ip':ip_array, 'location':'Location C'});
                break;
            default:
                res.json({'request-ip':ip_array, 'location':'Default Location'});
        }

    })

One of my main issues was that I was developing on my local laptop.我的主要问题之一是我在本地笔记本电脑上进行开发。 My node server was running express here.我的节点服务器在这里运行 express。 I was also trying to get my request ip from my local machine.我还试图从我的本地机器上获取我的请求 ip。 This didn't make sense because I was constantly getting back "::1" as my request IP.这没有任何意义,因为我不断返回"::1"作为我的请求 IP。 Baffled, I did much research and finally found it to be an obvious PEBKAC issue.困惑,我做了很多研究,最后发现这是一个明显的PEBKAC问题。 Thanks to nikoss in this post, it made all the sense in the world.多亏了这篇文章中的 nikoss,它在世界上变得很有意义。

This post isn't really a question anymore;这篇帖子不再是一个真正的问题了。 I just want to post this to help other people out in the future to avoid lost time.我只想发布此内容,以帮助将来的其他人避免浪费时间。

Goal : Retrieve the client IP address and set some specific values based on certain octet in IP.目标:检索客户端IP地址,并根据IP中的某些八位位组设置一些特定的值。

I was developing a react web-app for my company and needed to support three facilities.我正在为我的公司开发一个React Web应用程序,并且需要支持三项设施。 The three locations of-course existed in different geographical regions and had slightly different IP schema's.路线的三个位置存在于不同的地理区域,并且具有略微不同的IP模式。

I needed to set some session identifier based on an octet value from the client IP.我需要基于客户端IP的八位字节值设置一些会话标识符。 To do so, I did the following steps.为此,我执行了以下步骤。

  1. Setup express route for user to hit on initial visit of app.设置快速路线,以供用户在应用程序首次访问时点击。
  2. Get client IP and store in const/var.获取客户端IP并存储在const / var中。
  3. Explode IP string by ".""."分隔IP字符串"." .
  4. Perform If/Then or Switch to determine value of desired octet.执行If / ThenSwitch确定所需的八位位组的值。
  5. Set some session/logic within matching condition.在匹配条件内设置一些会话/逻辑。

Thanks to express, the req object contains an ip key with the value of the requests IP address.多亏了express, req对象包含一个带有请求IP地址值的ip密钥。 We can utilize this or some other third party library to get the needed info.我们可以利用这个或其他第三方库来获取所需的信息。 Of course there are better/more secure ways to do this, but this is a simple method I've researched and setup.当然,有更好/更安全的方法可以执行此操作,但这是我研究和设置的一种简单方法。 Definitely thanks to community for helping me resolve my issue with this.非常感谢社区帮助我解决了这一问题。

    apiRouter.route('/test')

    .get((req, res) => {
        const request_ip = req.ip;      // Returns string like ::ffff:192.168.0.1
        const ip_array = request_ip.split('.')      // Returns array of the string above separated by ".". ["::ffff:192","168","0","1"]

        // The switch statement checks the value of the array above for the index of 2. This would be "0"
        switch(ip_array[2]) {
            case('0'):
                res.json({'request-ip':ip_array, 'location':'Location A'});
                break;
            case('1'):
                res.json({'request-ip':ip_array, 'location':'Location B'});
                break;
            case('2'):
                res.json({'request-ip':ip_array, 'location':'Location C'});
                break;
            default:
                res.json({'request-ip':ip_array, 'location':'Default Location'});
        }

    })

One of my main issues was that I was developing on my local laptop.我的主要问题之一是我在本地笔记本电脑上进行开发。 My node server was running express here.我的节点服务器在这里运行Express。 I was also trying to get my request ip from my local machine.我还试图从本地计算机获取请求ip。 This didn't make sense because I was constantly getting back "::1" as my request IP.这没有任何意义,因为我一直在不断取回"::1"作为我的请求IP。 Baffled, I did much research and finally found it to be an obvious PEBKAC issue.莫名其妙,我做了很多研究,最后发现这显然是PEBKAC的问题。 Thanks to nikoss in this post, it made all the sense in the world.感谢这篇文章中的nikoss,它使世界上的一切都变得有意义。

This post isn't really a question anymore;这篇帖子不再是一个真正的问题了。 I just want to post this to help other people out in the future to avoid lost time.我只想发布此内容,以帮助将来的其他人避免浪费时间。

Goal : Retrieve the client IP address and set some specific values based on certain octet in IP.目标:检索客户端IP地址,并根据IP中的某些八位位组设置一些特定的值。

I was developing a react web-app for my company and needed to support three facilities.我正在为我的公司开发一个React Web应用程序,并且需要支持三项设施。 The three locations of-course existed in different geographical regions and had slightly different IP schema's.路线的三个位置存在于不同的地理区域,并且具有略微不同的IP模式。

I needed to set some session identifier based on an octet value from the client IP.我需要基于客户端IP的八位字节值设置一些会话标识符。 To do so, I did the following steps.为此,我执行了以下步骤。

  1. Setup express route for user to hit on initial visit of app.设置快速路线,以供用户在应用程序首次访问时点击。
  2. Get client IP and store in const/var.获取客户端IP并存储在const / var中。
  3. Explode IP string by ".""."分隔IP字符串"." .
  4. Perform If/Then or Switch to determine value of desired octet.执行If / ThenSwitch确定所需的八位位组的值。
  5. Set some session/logic within matching condition.在匹配条件内设置一些会话/逻辑。

Thanks to express, the req object contains an ip key with the value of the requests IP address.多亏了express, req对象包含一个带有请求IP地址值的ip密钥。 We can utilize this or some other third party library to get the needed info.我们可以利用这个或其他第三方库来获取所需的信息。 Of course there are better/more secure ways to do this, but this is a simple method I've researched and setup.当然,有更好/更安全的方法可以执行此操作,但这是我研究和设置的一种简单方法。 Definitely thanks to community for helping me resolve my issue with this.非常感谢社区帮助我解决了这一问题。

    apiRouter.route('/test')

    .get((req, res) => {
        const request_ip = req.ip;      // Returns string like ::ffff:192.168.0.1
        const ip_array = request_ip.split('.')      // Returns array of the string above separated by ".". ["::ffff:192","168","0","1"]

        // The switch statement checks the value of the array above for the index of 2. This would be "0"
        switch(ip_array[2]) {
            case('0'):
                res.json({'request-ip':ip_array, 'location':'Location A'});
                break;
            case('1'):
                res.json({'request-ip':ip_array, 'location':'Location B'});
                break;
            case('2'):
                res.json({'request-ip':ip_array, 'location':'Location C'});
                break;
            default:
                res.json({'request-ip':ip_array, 'location':'Default Location'});
        }

    })

One of my main issues was that I was developing on my local laptop.我的主要问题之一是我在本地笔记本电脑上进行开发。 My node server was running express here.我的节点服务器在这里运行Express。 I was also trying to get my request ip from my local machine.我还试图从本地计算机获取请求ip。 This didn't make sense because I was constantly getting back "::1" as my request IP.这没有任何意义,因为我一直在不断取回"::1"作为我的请求IP。 Baffled, I did much research and finally found it to be an obvious PEBKAC issue.莫名其妙,我做了很多研究,最后发现这显然是PEBKAC的问题。 Thanks to nikoss in this post, it made all the sense in the world.感谢这篇文章中的nikoss,它使世界上的一切都变得有意义。

This post isn't really a question anymore;这篇帖子不再是一个真正的问题了。 I just want to post this to help other people out in the future to avoid lost time.我只想发布此内容,以帮助将来的其他人避免浪费时间。

Goal : Retrieve the client IP address and set some specific values based on certain octet in IP.目标:检索客户端IP地址,并根据IP中的某些八位位组设置一些特定的值。

I was developing a react web-app for my company and needed to support three facilities.我正在为我的公司开发一个React Web应用程序,并且需要支持三项设施。 The three locations of-course existed in different geographical regions and had slightly different IP schema's.路线的三个位置存在于不同的地理区域,并且具有略微不同的IP模式。

I needed to set some session identifier based on an octet value from the client IP.我需要基于客户端IP的八位字节值设置一些会话标识符。 To do so, I did the following steps.为此,我执行了以下步骤。

  1. Setup express route for user to hit on initial visit of app.设置快速路线,以供用户在应用程序首次访问时点击。
  2. Get client IP and store in const/var.获取客户端IP并存储在const / var中。
  3. Explode IP string by ".""."分隔IP字符串"." .
  4. Perform If/Then or Switch to determine value of desired octet.执行If / ThenSwitch确定所需的八位位组的值。
  5. Set some session/logic within matching condition.在匹配条件内设置一些会话/逻辑。

Thanks to express, the req object contains an ip key with the value of the requests IP address.多亏了express, req对象包含一个带有请求IP地址值的ip密钥。 We can utilize this or some other third party library to get the needed info.我们可以利用这个或其他第三方库来获取所需的信息。 Of course there are better/more secure ways to do this, but this is a simple method I've researched and setup.当然,有更好/更安全的方法可以执行此操作,但这是我研究和设置的一种简单方法。 Definitely thanks to community for helping me resolve my issue with this.非常感谢社区帮助我解决了这一问题。

    apiRouter.route('/test')

    .get((req, res) => {
        const request_ip = req.ip;      // Returns string like ::ffff:192.168.0.1
        const ip_array = request_ip.split('.')      // Returns array of the string above separated by ".". ["::ffff:192","168","0","1"]

        // The switch statement checks the value of the array above for the index of 2. This would be "0"
        switch(ip_array[2]) {
            case('0'):
                res.json({'request-ip':ip_array, 'location':'Location A'});
                break;
            case('1'):
                res.json({'request-ip':ip_array, 'location':'Location B'});
                break;
            case('2'):
                res.json({'request-ip':ip_array, 'location':'Location C'});
                break;
            default:
                res.json({'request-ip':ip_array, 'location':'Default Location'});
        }

    })

One of my main issues was that I was developing on my local laptop.我的主要问题之一是我在本地笔记本电脑上进行开发。 My node server was running express here.我的节点服务器在这里运行Express。 I was also trying to get my request ip from my local machine.我还试图从本地计算机获取请求ip。 This didn't make sense because I was constantly getting back "::1" as my request IP.这没有任何意义,因为我一直在不断取回"::1"作为我的请求IP。 Baffled, I did much research and finally found it to be an obvious PEBKAC issue.莫名其妙,我做了很多研究,最后发现这显然是PEBKAC的问题。 Thanks to nikoss in this post, it made all the sense in the world.感谢这篇文章中的nikoss,它使世界上的一切都变得有意义。

You can use this one as well.你也可以使用这个。

fetch('https://get-ip-only.herokuapp.com/').then(r => r.json()).then(resp => console.log(resp.ip)) fetch('https://get-ip-only.herokuapp.com/').then(r => r.json()).then(resp => console.log(resp.ip))

https://get-ip-only.herokuapp.com/ This API provides you the IP only. https://get-ip-only.herokuapp.com/这个 API 只为您提供 IP。

This post isn't really a question anymore;这篇帖子不再是一个真正的问题了。 I just want to post this to help other people out in the future to avoid lost time.我只想发布此内容,以帮助将来的其他人避免浪费时间。

Goal : Retrieve the client IP address and set some specific values based on certain octet in IP.目标:检索客户端IP地址,并根据IP中的某些八位位组设置一些特定的值。

I was developing a react web-app for my company and needed to support three facilities.我正在为我的公司开发一个React Web应用程序,并且需要支持三项设施。 The three locations of-course existed in different geographical regions and had slightly different IP schema's.路线的三个位置存在于不同的地理区域,并且具有略微不同的IP模式。

I needed to set some session identifier based on an octet value from the client IP.我需要基于客户端IP的八位字节值设置一些会话标识符。 To do so, I did the following steps.为此,我执行了以下步骤。

  1. Setup express route for user to hit on initial visit of app.设置快速路线,以供用户在应用程序首次访问时点击。
  2. Get client IP and store in const/var.获取客户端IP并存储在const / var中。
  3. Explode IP string by ".""."分隔IP字符串"." .
  4. Perform If/Then or Switch to determine value of desired octet.执行If / ThenSwitch确定所需的八位位组的值。
  5. Set some session/logic within matching condition.在匹配条件内设置一些会话/逻辑。

Thanks to express, the req object contains an ip key with the value of the requests IP address.多亏了express, req对象包含一个带有请求IP地址值的ip密钥。 We can utilize this or some other third party library to get the needed info.我们可以利用这个或其他第三方库来获取所需的信息。 Of course there are better/more secure ways to do this, but this is a simple method I've researched and setup.当然,有更好/更安全的方法可以执行此操作,但这是我研究和设置的一种简单方法。 Definitely thanks to community for helping me resolve my issue with this.非常感谢社区帮助我解决了这一问题。

    apiRouter.route('/test')

    .get((req, res) => {
        const request_ip = req.ip;      // Returns string like ::ffff:192.168.0.1
        const ip_array = request_ip.split('.')      // Returns array of the string above separated by ".". ["::ffff:192","168","0","1"]

        // The switch statement checks the value of the array above for the index of 2. This would be "0"
        switch(ip_array[2]) {
            case('0'):
                res.json({'request-ip':ip_array, 'location':'Location A'});
                break;
            case('1'):
                res.json({'request-ip':ip_array, 'location':'Location B'});
                break;
            case('2'):
                res.json({'request-ip':ip_array, 'location':'Location C'});
                break;
            default:
                res.json({'request-ip':ip_array, 'location':'Default Location'});
        }

    })

One of my main issues was that I was developing on my local laptop.我的主要问题之一是我在本地笔记本电脑上进行开发。 My node server was running express here.我的节点服务器在这里运行Express。 I was also trying to get my request ip from my local machine.我还试图从本地计算机获取请求ip。 This didn't make sense because I was constantly getting back "::1" as my request IP.这没有任何意义,因为我一直在不断取回"::1"作为我的请求IP。 Baffled, I did much research and finally found it to be an obvious PEBKAC issue.莫名其妙,我做了很多研究,最后发现这显然是PEBKAC的问题。 Thanks to nikoss in this post, it made all the sense in the world.感谢这篇文章中的nikoss,它使世界上的一切都变得有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM