-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpn_ip_swapper.js
71 lines (58 loc) · 2.02 KB
/
vpn_ip_swapper.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
const {
AttachStaticIpCommand,
GetStaticIpCommand,
LightsailClient,
AllocateStaticIpCommand,
DetachStaticIpCommand,
ReleaseStaticIpCommand,
} = require("@aws-sdk/client-lightsail");
const execSync = require("child_process").execSync;
const fs = require("fs");
let rawdata = fs.readFileSync("apikey.json");
let apikey = JSON.parse(rawdata);
async function get_static_ip() {
execSync("nmcli --show-secrets --ask connection down wg0", {
encoding: "utf-8",
}); // the default is 'buffer'
execSync("nmcli connection delete wg0", { encoding: "utf-8" }); // the default is 'buffer'
var client = new LightsailClient({
region: "ap-southeast-2",
credentials: {
accessKeyId: apikey.accessKeyId,
secretAccessKey: apikey.secretAccessKey,
},
});
var input = {
// GetStaticIpRequest
staticIpName: "StaticIp-1", // required
};
var command = new DetachStaticIpCommand(input);
var response = await client.send(command);
command = new ReleaseStaticIpCommand(input);
response = await client.send(command);
command = new AllocateStaticIpCommand(input);
response = await client.send(command);
command = new GetStaticIpCommand(input);
response = await client.send(command);
var ip = response["staticIp"]["ipAddress"];
input = {
// AttachStaticIpRequest
staticIpName: "StaticIp-1", // required
instanceName: "vpn", // required
};
command = new AttachStaticIpCommand(input);
response = await client.send(command);
var command_string_left = "sudo sed -i '/Endpoint/cEndpoint = ";
var command_string_right = ":51820' /etc/wireguard/wg0.conf";
var joined_command_string = command_string_left
.concat(ip)
.concat(command_string_right);
console.log(joined_command_string);
execSync(joined_command_string, { encoding: "utf-8" }); // the default is 'buffer'
execSync(
'sudo nmcli connection import type wireguard file "/etc/wireguard/wg0.conf"',
{ encoding: "utf-8" },
); // the default is 'buffer'
return "finished";
}
module.exports = { get_static_ip };