external-ips 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.gitattributes ADDED
@@ -0,0 +1,2 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Hkey1
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # External IPs
2
+ This is Node.js module to use many IPs in HTTP(s) requests or WS.
3
+ It may be useful to avoid rate limits in some external IPs or when you parsed some site.
4
+
5
+ ## Usage
6
+ You need buy additionals IPs in hosting
7
+ Then add it in Linux: https://askubuntu.com/questions/585468/how-do-i-add-an-additional-ip-address-to-an-interface-in-ubuntu-14
8
+
9
+
10
+ ### Base
11
+ ```js
12
+ const IPs = require('external-ips');
13
+
14
+ console.log('all', IPs.length);
15
+ console.log('IPv4', IPs.v4.length);
16
+ console.log('IPv6', IPs.v6.length);
17
+
18
+ console.log(IPs.v4.next());
19
+ console.log(IPs.v4.next());
20
+
21
+ console.log(IPs.v4.random());
22
+ ```
23
+
24
+ ### HTTP/HTTPs
25
+ ```js
26
+ const http = require('node:http');
27
+ ...
28
+ http.request(url, {
29
+ localAddress: IPs.v4.next(),
30
+ //or IPs.v4.random(),
31
+ })
32
+ });
33
+ ```
34
+
35
+ ### WS
36
+ ```js
37
+ ...
38
+ const WS = require('ws');
39
+ ...
40
+ new WS(address, protocols, {
41
+ localAddress: IPs.v4.next()
42
+ })
43
+ ```
44
+
45
+ ### Agent
46
+ ```js
47
+ ...
48
+ const agent = new https.Agent();
49
+ ...
50
+ IPs.v4.patchAgent(agent);
51
+ ```
52
+
53
+ #### multi family
54
+ ```js
55
+ ...
56
+ IPs.patchAgent(agent, 4);
57
+ ```
58
+
59
+
60
+ #### Fixed IPs
61
+
62
+ ```js
63
+ ...
64
+ const agent1 = new http.Agent();
65
+ const agent2 = new http.Agent();
66
+ ...
67
+ IPs.v4._next().patchAgent(agent1); //or _random()
68
+ IPs.v4._next().patchAgent(agent2); //or _random()
69
+ ```
package/index.js ADDED
@@ -0,0 +1,160 @@
1
+ const os = require('node:os');
2
+ const assert = require('node:assert');
3
+
4
+ function normalizeFamily(family){
5
+ if(family==='IPv6' || family==='v6' || family==='6' || family===6){
6
+ return 6;
7
+ } else if(family==='IPv4' || family==='v4' || family==='4' || family===4){
8
+ return 4;
9
+ } else if(family===false || family===null || family===undefined){
10
+ return false;
11
+ } else throw new Error('bad family='+family);
12
+ }
13
+
14
+ class AbstractObject {
15
+ constructor(family){
16
+ assert(this.constructor!==AbstractObject); //Abstract
17
+ assert.equal(typeof this.__next, 'function');
18
+ assert.equal(typeof this.__random, 'function');
19
+ this.family = normalizeFamily(family);
20
+ }
21
+ patchAgent(agent, defaultFamily=false){
22
+ defaultFamily = normalizeFamily(defaultFamily);
23
+ assert.equal(typeof agent, 'object');
24
+ assert.equal(typeof agent.createConnection, 'function');
25
+ const old = agent.createConnection;
26
+ agent.createConnection = (options, callback)=>{
27
+ const family = normalizeFamily(options.family) || defaultFamily;
28
+ if(!family || !this.family || this.family===family){
29
+ options.localAddress ||= this.next().address;
30
+ }
31
+ return old.call(this.agent, options, callback);
32
+ }
33
+ }
34
+ checkFamily(family){
35
+ family = normalizeFamily(family);
36
+ family===false || this.family===false || assert.equal(family, this.family);
37
+ return family;
38
+ }
39
+ random(family){
40
+ return this._random(family).address;
41
+ }
42
+ next(family){
43
+ return this._next(family).address;
44
+ }
45
+ _random(family){
46
+ return this.__random(this.checkFamily(family))
47
+ }
48
+ _next(family){
49
+ return this.__next(this.checkFamily(family))
50
+ }
51
+ };
52
+
53
+ class IP extends AbstractObject{
54
+ constructor(info){
55
+ assert(info.address); assert(info.family);
56
+ super(info.family);
57
+ assert(this.family)
58
+ this.info = info;
59
+ this.address = info.address;
60
+ }
61
+ toString(){
62
+ return this.address;
63
+ }
64
+ __random(){return this}
65
+ __next(){return this}
66
+ };
67
+
68
+ class FamilyList extends AbstractObject{
69
+ ips = [];
70
+ constructor(family){
71
+ super(family);
72
+ Object.defineProperty(this, 'lastIndex', {
73
+ value: 0,
74
+ enumerable: false,
75
+ writable : true,
76
+ })
77
+ }
78
+ push(...ips){
79
+ ips.forEach(ip=>{
80
+ assert(ip instanceof IP);
81
+ assert(ip.family)
82
+ this.checkFamily(ip.family)
83
+ });
84
+ return this.ips.push(...ips);
85
+ }
86
+ get length(){
87
+ return this.ips.length;
88
+ }
89
+ __random(){
90
+ return this[Math.floor(this.length*Math.random())]
91
+ }
92
+ __next(){
93
+ const res = this[this.lastIndex];
94
+ this.lastIndex = (this.lastIndex+1)%this.length;
95
+ return res;
96
+ }
97
+ print(){
98
+ console.log('v'+(this.family || '*'), this.length, this.ips.map(ip=>ip+''))
99
+ }
100
+ };
101
+
102
+
103
+ class MainList extends AbstractObject{
104
+ constructor(){
105
+ super(false);
106
+ this.all = new FamilyList(false);
107
+ this.v4 = new FamilyList(4);
108
+ this.v6 = new FamilyList(6);
109
+ Object.defineProperty(this, 'byFamily', {
110
+ value : {
111
+ false : this.all,
112
+ 4 : this.v4,
113
+ 6 : this.v6,
114
+ },
115
+ enumerable: false,
116
+ writable : false,
117
+ });
118
+ }
119
+ get length(){
120
+ return this.all.length;
121
+ }
122
+ get ips(){
123
+ return this.all.ips;
124
+ }
125
+ __random(family){
126
+ return this.byFamily[family].random(family);
127
+ }
128
+ __next(){
129
+ return this.byFamily[family].random(family);
130
+ }
131
+ push(...ips){
132
+ ips.forEach(ip=>assert(ip instanceof IP));
133
+ if(ips.length===1){
134
+ const ip = ips[0];
135
+ this.all.push(ip);
136
+ this.byFamily[ip.family].push(ip);
137
+ } else {
138
+ ips.forEach(ip=>this.push(ip))
139
+ }
140
+ }
141
+ print(){
142
+ this.v4.print();
143
+ this.v6.print();
144
+ }
145
+ };
146
+
147
+ const mainList = new MainList();
148
+ Object.values(os.networkInterfaces()).forEach(arr=>{
149
+ mainList.push(...arr.filter(ip=>!ip.internal).map(ip=>new IP(ip)))
150
+ });
151
+
152
+ //mainList.print();
153
+
154
+ mainList.MainList = MainList;
155
+ mainList.IP = IP;
156
+ mainList.AbstractObject = AbstractObject;
157
+ mainList.FamilyList = FamilyList;
158
+
159
+
160
+ module.exports = mainList;
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "external-ips",
3
+ "version": "0.0.1",
4
+ "main": "./index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1"
7
+ },
8
+ "author": "Andrey Belousoff <a.v.belousoff@gmail.com>",
9
+ "license": "MIT",
10
+ "description": "any IPs in HTTP(s) requests or WS",
11
+ "keywords": ["ips", "ip"],
12
+ "dependencies": {
13
+ },
14
+ "engines": {
15
+ "node": ">=0.1.0"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/Hkey1/external-ips"
20
+ },
21
+ "readme":"External IPs",
22
+ "readmeFilename": "README.md",
23
+ "bugs": {
24
+ "url": "https://github.com/Hkey1/external-ips/issues"
25
+ },
26
+ "homepage": "https://github.com/Hkey1/external-ips"
27
+ }