-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp-pf.js
103 lines (90 loc) · 2.56 KB
/
app-pf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import os from 'os';
import fs from 'fs';
// custom
import hlp from './modules/helper.js';
import ask from './modules/ask.js';
import genwgkeys from './modules/genkeys.js';
import req from './modules/req.js';
import { ufiles, data } from './modules/ufiles.js';
let test10net = false;
testIps();
if(!test10net){
console.error(':error: Not private network!');
throw new Error();
}
try{
const getIp = await req('https://freeipapi.com/api/json');
const extIp = JSON.parse(getIp.res.body).ipAddress;
console.info(':info: Your ExtIP:', extIp);
const findSrv = data.srv.data.find(s => {
for(const wi of s.ip_wg){
if(wi == extIp){
return true;
}
}
return false;
});
// req url
const reqUrl = `https://${extIp}:19999/getSignature`;
// req options
const reqOpts = {
searchParams: {
token: data.usr.token,
},
https: {
certificateAuthority: fs.readFileSync(ufiles.crt),
},
headers: {
Host: findSrv.cn,
},
};
// do request
console.log(':info: Getting signature...');
const reqSign = await req(reqUrl, reqOpts);
const signData = JSON.parse(reqSign.res.body);
const portData = JSON.parse(atob(signData.payload));
console.log(`:info: Your srv url: http://${extIp}:${portData.port}/`);
await bindPort(extIp, findSrv.cn, signData);
}
catch(e){
console.error(':error:');
console.log(e);
}
async function bindPort(extIp, cn, signData){
// req url
const reqUrl = `https://${extIp}:19999/bindPort`;
// req options
const reqOpts = {
searchParams: {
payload: signData.payload,
signature: signData.signature,
},
https: {
certificateAuthority: fs.readFileSync(ufiles.crt),
},
headers: {
Host: cn,
},
};
// do request
const doBind = await req(reqUrl, reqOpts);
console.log(JSON.parse(doBind.res.body));
await hlp.sleep(14*60*1000);
await bindPort(extIp, cn, signData);
}
function testIps(){
const ifaces = os.networkInterfaces();
Object.keys(ifaces).forEach(ifname => {
ifnames(ifaces, ifname);
});
}
function ifnames(ifaces, ifname){
ifaces[ifname].forEach(iface => {
if('IPv4' !== iface.family){ return; }
// console.log(iface);
if(iface.address.match(/^10\.\d+\.\d+\.\d+$/)){
test10net = iface.address;
console.info(':info: Your IntIP:', test10net);
}
});
}