0

I'm trying to make an app where a client will search some string in a textarea and that string will be looped through to find an IP address. If it finds an IP then it will take its value and use it in an API to request for more data. The string, which the client searches will always have an IP. But I'm not getting an ip. Here's the code:

HTML

<div class="row">
        <div class="col-sm-12 mb-5">
                <form action="/finder" method="GET" id="ipForm">
                        <textarea type="text" name="header" id="form-input" class="form-control form-input form-inline justify-content-center" required></textarea>
                        <input type="submit" class="btn btn-primary" id="form-submit" value="CHECK">
                </form>
        </div>
</div>

Router

router.get("/finder", (req, res) => {
    if(req.query.header) {
        var query = req.query.header;
        var rawQuery = query.split("\n");

        for(var i = 0; i < rawQuery.length; i++) {
            var ip = /Received:\s+from.*?\[((?:[0-9]{1,3}\.){3}[0-9]{1,3})\]/i.exec(rawQuery[i]);

            if(ip !== null) {
                var url = "https://api.ipregistry.co/" + ip;
                request(url, function(error, response, body) {

                    if(!error && response.statusCode === 200) {
                        const data = JSON.parse(body);
                        res.render("../views/finder", {data: data});
                    }       
                });
            }else{
                // var data = "Unable to retrieve data.";
                // res.render("../views/finder", {data: data});
                console.log("no ip found");
            }
        }
    }else{
        res.render("../views/finder", {data: null});
    }
});

Edited with more info - input value will be something like this

Return-path: <[email protected]>
Received: from mac.com ([10.13.11.252])
  by ms031.mac.com (Sun Java System Messaging Server 6.2-8.04 (built Feb 28
  2007)) with ESMTP id <[email protected]> for [email protected]; Thu,
  09 Aug 2007 04:24:50 -0700 (PDT)
Received: from mail.dsis.net (mail.dsis.net [70.183.59.5])
  by mac.com (Xserve/smtpin22/MantshX 4.0) with ESMTP id l79BOnNS000101
  for <[email protected]>; Thu, 09 Aug 2007 04:24:49 -0700 (PDT)
Received: from [192.168.2.77] (70.183.59.6) by mail.dsis.net with ESMTP
  (EIMS X 3.3.2) for <[email protected]>; Thu, 09 Aug 2007 04:24:49 -0700
Date: Thu, 09 Aug 2007 04:24:57 -0700
From: Frank Sender <[email protected]>
Subject: Test
To: Joe User <[email protected]>
Message-id: <[email protected]>
MIME-version: 1.0 (Apple Message framework v752.2)
X-Mailer: Apple Mail (2.752.2)
Content-type: text/plain; charset=US-ASCII; format=flowed
Content-transfer-encoding: 7bit
5
  • 0.0.0.0 1.1.1.1 is it a valid input? What type of input do you expect from user? Commented Nov 1, 2019 at 11:33
  • 1
    provide an example of the query value Commented Nov 1, 2019 at 11:37
  • @Dijkstra - No I am searching the string for "Received from" and taking the IP-address from it. @ Artem - updated the op. Commented Nov 1, 2019 at 12:21
  • @Zak So, req.query.header contains that input value? Commented Nov 1, 2019 at 12:33
  • Yes, I'm taking that input value with this. Tried req,body.header but didnt work. Commented Nov 1, 2019 at 12:40

2 Answers 2

2

This pattern (?:[0-9]{1,3}\.){3}[0-9]{1,3} returns an IP for sure. If you expecting multiple Ips you may just want to use /(?:[0-9]{1,3}\.){3}[0-9]{1,3}/g instead of /(?:[0-9]{1,3}\.){3}[0-9]{1,3}/i to search globally instead of looping through sentences.

In your question you are adding extra regular expression to search for "Received from IP_ADDRESS" and those extra text has to match as well. Try to match the IP without it.

Sign up to request clarification or add additional context in comments.

1 Comment

There will be more IPs that I don't want to get info about. Just need the one in Received from section. Please check the op for the type of input I am expecting.
1

I tried to play with your data and the way you are trying to extract ip from your data. Here is an screenshot of it. So, it seems like your regex returns an array. Try to console.log the ip variable and check what it contains. In my experiment, to access the ip address I had to do this ip[1].

enter image description here

1 Comment

I tried this. And it works. But there's another issue. If I use the input value from the op then I get the ip. But if I put a different input value then I get data but my page breaks with no css and an error in the terminal which says "TypeError: req.next is not a function". Any idea what could be causing this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.