Home Web Front-end JS Tutorial How to capture proxy IP in node.js?

How to capture proxy IP in node.js?

May 03, 2017 am 10:03 AM
ip node.js

This article mainly introduces the relevant information about node.js grabbing proxy ip example code. Friends in need can refer to it

node.js implements grabbing proxy ip

Main file: index.js

/*
* 支持:node.js v7.9.0
*/
const cheerio=require('cheerio');
const fetch =require('node-fetch');
const Promise=require('bluebird');
let mongoose=require('mongoose');

Promise.promisifyAll(mongoose);
let Schema=mongoose.Schema;
mongoose.connect('mongodb://localhost:27017/ipproxypool');
let IPpool=new Schema({
  ip:{type:String,unique:true}
})
let Ipproxy=mongoose.model('IP',IPpool);

function fetchUrl(url){
  fetch(url,{
    method:'get',
    headers:{
    }
  })
  .then(res=>res.text())
  .then(body=>{
    let $=cheerio.load(body);
    let length=$('#list table tbody').find('tr').length;
    for (let i=0;i<length;i++){
    let ipaddress= $(&#39;#list table tbody&#39;).find(&#39;tr&#39;).eq(i).find(&#39;td&#39;).eq(0).text() ;
    let port = $(&#39;#list table tbody&#39;).find(&#39;tr&#39;).eq(i).find(&#39;td&#39;).eq(1).text();
    console.log(`IP:${ipaddress}:${port}`);
    let ip=`${ipaddress}:${port}`
    let ippool=new Ipproxy({
      ip:ip
    })
    ippool.save();
    }
  })
}

var sleep = function (time) {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      resolve(&#39;ok&#39;);
    }, time);
  })
};
const pageNumber=10;
var start = async function(){
  for(let j=1;j<pageNumber;j++){
     console.log(`当前是第${j}次等待..`);
    fetchUrl(`http://www.kuaidaili.com/free/inha/${j}/`);
    await sleep(1500);
  }
}
start();
Copy after login

Package support: package.json

{
 "name": "demo-4-ipproxypool",
 "version": "1.0.0",
 "description": "",
 "main": "index.js",
 "scripts": {
  "test": "echo \"Error: no test specified\" && exit 1"
 },
 "author": "false-l",
 "license": "",
 "devDependencies": {
  "babel-preset-es2015": "^6.24.1",
  "babel-preset-react": "^6.24.1",
  "babel-preset-stage-3": "^6.24.1"
 },
 "dependencies": {
  "babel-core": "^6.24.1",
  "bluebird": "^3.5.0",
  "cheerio": "^0.22.0",
  "koa": "^2.2.0",
  "koa-router": "^7.1.1",
  "mongoose": "^4.9.6",
  "node-fetch": "^1.6.3"
 }
}
Copy after login

The mongodb database needs to be installed locally for Store the captured IP. IP verification is not yet implemented. I write this mainly out of curiosity.

The above code can capture the IP of the IP proxy website and store it in the mongodb database.

The following is a simple server implementation based on the koa2 api interface

server

const Promise=require(&#39;bluebird&#39;);
let mongoose=require(&#39;mongoose&#39;);
const koa=require(&#39;koa&#39;);
const app=new koa();
var router = require(&#39;koa-router&#39;)();
Promise.promisifyAll(mongoose);
let Schema=mongoose.Schema;
mongoose.connect(&#39;mongodb://localhost:27017/ipproxypool&#39;);
let IPpool=new Schema({
  ip:{type:String,unique:true}
})
let Ipproxy=mongoose.model(&#39;IP&#39;,IPpool);

app.use(async (ctx, next) => {
 await next();
 var data=await Ipproxy.find({},function(err,ips){
  var ipmap=[];
   ips.forEach(function(ip){
     ipmap[ip._id]=ip;
     //console.log(ip)
   });
 })
 var map=data.map(ip=>ip.ip);
 ctx.response.type = &#39;text/json&#39;;
 ctx.response.body = map;
});
app.listen(3000);
console.log(&#39;server listen:3000&#39;)
Copy after login

As for why there is both promise and async, it is because I’m not very familiar with asynchronous syntax, so how do I know how to write it?

Usage:

According to package.json

npm install //Installation support

node index.js //Get Proxy ip

node server.js //Run simple ip interface

The above is the detailed content of How to capture proxy IP in node.js?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do websites set black/whitelist IP restrictions and country and city IP access restrictions through nginx? How do websites set black/whitelist IP restrictions and country and city IP access restrictions through nginx? Jun 01, 2023 pm 05:27 PM

1. Black/white list IP restricted access configuration nginx There are several ways to configure black and white lists. Here are only two commonly used methods. 1. The first method: allow, denydeny and allow instructions belong to ngx_http_access_module. nginx loads this module by default, so it can be used directly. This method is the simplest and most direct. The setting is similar to the firewall iptable. How to use: Add directly to the configuration file: #Whitelist settings, followed by allow is accessible IPlocation/{allow123.13.123.12;allow23.53.32.1/100;denyall;}#Blacklist settings,

What does binding ip and mac mean? What does binding ip and mac mean? Mar 09, 2023 pm 04:44 PM

IP and mac binding refers to associating a specific IP address with a specific MAC address, so that only the device using the MAC address can use the IP address for network communication. Binding ip and mac can prevent the IP address of the bound host from being spoofed. Prerequisites: 1. The MAC address is unique and cannot be spoofed; it can only be bound to hosts on the network directly connected to the router (that is, The host's gateway is on the router).

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

Detailed graphic explanation of the memory and GC of the Node V8 engine Detailed graphic explanation of the memory and GC of the Node V8 engine Mar 29, 2023 pm 06:02 PM

This article will give you an in-depth understanding of the memory and garbage collector (GC) of the NodeJS V8 engine. I hope it will be helpful to you!

How to check IP address on WeChat How to check IP address on WeChat May 31, 2023 am 09:16 AM

How to check the IP address on WeChat: 1. Log in to the computer version of WeChat, right-click the taskbar at the bottom of the screen, and click "Task Manager"; 2. When the task manager pops up, click "Details" in the lower left corner; 3. Task management Enter the "Performance" option of the browser and click "Open Resource Monitor"; 4. Select "Network" and check the WeChat process "Wechat.exe"; 5. Click "TCP Connection" below to monitor the WeChat network IP related situation. Sending a message and getting a reply will reveal the other person's IP address.

How to set directory whitelist and ip whitelist in nginx How to set directory whitelist and ip whitelist in nginx May 18, 2023 pm 03:52 PM

1. Set the directory whitelist: There is no restriction on the specified request path. If there is no restriction on the request path to the api directory, it can be written as server{location/app{proxy_passhttp://192.168.1.111:8095/app ;limit_connconn20;limit_rate500k;limit_reqzone=fooburst=5nodelay;}location/app/api{proxy_passhttp://192.168.1.111:8095/app/api}}#Because nginx will give priority to accurate matching

Let's talk in depth about the File module in Node Let's talk in depth about the File module in Node Apr 24, 2023 pm 05:49 PM

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

How to obtain the user's real IP address based on nginx reverse proxy How to obtain the user's real IP address based on nginx reverse proxy May 13, 2023 pm 05:07 PM

Introduction When nginx is used as a reverse proxy, the IP address obtained by the default configuration backend comes from nginx. Use request.getRemoteAddr(); to obtain the IP address of nginx, not the user's real IP. 1. Modify Nginx Configuration: server{listen80;server_namejenkins.local.com;location/{proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_passhttp://192.168.10.

See all articles