external-ips 0.0.2 → 0.0.4
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/index.js +35 -16
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -22,13 +22,18 @@ class AbstractObject {
|
|
|
22
22
|
defaultFamily = normalizeFamily(defaultFamily);
|
|
23
23
|
assert.equal(typeof agent, 'object');
|
|
24
24
|
assert.equal(typeof agent.createConnection, 'function');
|
|
25
|
-
const old
|
|
25
|
+
const old = agent.createConnection;
|
|
26
|
+
const self = this;
|
|
26
27
|
agent.createConnection = (options, callback)=>{
|
|
27
|
-
const family = normalizeFamily(options.family)
|
|
28
|
-
if(!family || !
|
|
29
|
-
|
|
28
|
+
const family = options.family ? normalizeFamily(options.family)||defaultFamily : defaultFamily;
|
|
29
|
+
if((!family || !self.family || self.family===family) && !options.localAddress){
|
|
30
|
+
const ip = self.random();
|
|
31
|
+
if(ip){
|
|
32
|
+
//console.log('++++++', ip, ip.address);
|
|
33
|
+
options.localAddress = ip.address ? ip.address : ip;
|
|
34
|
+
} //else console.log('++++++---', ip);
|
|
30
35
|
}
|
|
31
|
-
return old.call(
|
|
36
|
+
return old.call(agent, options, callback);
|
|
32
37
|
}
|
|
33
38
|
}
|
|
34
39
|
checkFamily(family){
|
|
@@ -37,10 +42,16 @@ class AbstractObject {
|
|
|
37
42
|
return family;
|
|
38
43
|
}
|
|
39
44
|
random(family){
|
|
40
|
-
|
|
45
|
+
const ip = this._random(family);
|
|
46
|
+
if(ip){
|
|
47
|
+
return ip.address;
|
|
48
|
+
} //else console.log('!!!!!!!ip.rnd', this.constructor.name, this.length)
|
|
41
49
|
}
|
|
42
50
|
next(family){
|
|
43
|
-
|
|
51
|
+
const ip = this._next(family);
|
|
52
|
+
if(ip){
|
|
53
|
+
return ip.address;
|
|
54
|
+
} //else console.log('!!!!!!!ip.nxt', this.constructor.name, this.length)
|
|
44
55
|
}
|
|
45
56
|
_random(family){
|
|
46
57
|
return this.__random(this.checkFamily(family))
|
|
@@ -65,15 +76,18 @@ class IP extends AbstractObject{
|
|
|
65
76
|
__next(){return this}
|
|
66
77
|
};
|
|
67
78
|
|
|
79
|
+
fid = 0
|
|
68
80
|
class FamilyList extends AbstractObject{
|
|
69
|
-
ips
|
|
81
|
+
ips = [];
|
|
82
|
+
lastIndex = 0;
|
|
70
83
|
constructor(family){
|
|
71
84
|
super(family);
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
85
|
+
this.fid = fid++;
|
|
86
|
+
//Object.defineProperty(this, 'lastIndex', {
|
|
87
|
+
// value: 0,
|
|
88
|
+
// enumerable: false,
|
|
89
|
+
// writable : true,
|
|
90
|
+
//})
|
|
77
91
|
}
|
|
78
92
|
push(...ips){
|
|
79
93
|
ips.forEach(ip=>{
|
|
@@ -87,7 +101,11 @@ class FamilyList extends AbstractObject{
|
|
|
87
101
|
return this.ips.length;
|
|
88
102
|
}
|
|
89
103
|
__random(){
|
|
90
|
-
|
|
104
|
+
const pos = Math.floor(this.length*Math.random());
|
|
105
|
+
const res = this.ips[pos];
|
|
106
|
+
//console.log(this.ips);
|
|
107
|
+
//console.log('__random', 'length='+this.length, 'pos='+pos, res)
|
|
108
|
+
return res;
|
|
91
109
|
}
|
|
92
110
|
__next(){
|
|
93
111
|
const res = this.ips[this.lastIndex];
|
|
@@ -149,12 +167,13 @@ Object.values(os.networkInterfaces()).forEach(arr=>{
|
|
|
149
167
|
mainList.push(...arr.filter(ip=>!ip.internal).map(ip=>new IP(ip)))
|
|
150
168
|
});
|
|
151
169
|
|
|
170
|
+
//console.log('--------------')
|
|
152
171
|
//mainList.print();
|
|
172
|
+
//console.log('/--------------')
|
|
153
173
|
|
|
154
174
|
mainList.MainList = MainList;
|
|
155
175
|
mainList.IP = IP;
|
|
156
176
|
mainList.AbstractObject = AbstractObject;
|
|
157
177
|
mainList.FamilyList = FamilyList;
|
|
158
178
|
|
|
159
|
-
|
|
160
|
-
module.exports = mainList;
|
|
179
|
+
module.exports = mainList;
|