ramm 0.0.3 → 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/dist/ramm.js +180 -32
- package/dist/types/base.d.ts +12 -0
- package/dist/types/context.d.ts +13 -0
- package/dist/types/debug.d.ts +3 -0
- package/dist/types/init.d.ts +2 -0
- package/dist/types/nft.d.ts +2 -0
- package/dist/types/packages.d.ts +1 -0
- package/dist/types/podman.d.ts +6 -0
- package/dist/types/ramm.d.ts +9 -13
- package/dist/types/systemd.d.ts +6 -0
- package/package.json +1 -1
package/dist/ramm.js
CHANGED
|
@@ -1,80 +1,228 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
// src/
|
|
3
|
-
var {
|
|
2
|
+
// src/base.ts
|
|
3
|
+
var {spawn } = globalThis.Bun;
|
|
4
|
+
|
|
5
|
+
// src/debug.ts
|
|
4
6
|
var debugInternal = (func, command) => {
|
|
5
7
|
console.info(`>>>${func}<<< ${command}`);
|
|
6
8
|
};
|
|
9
|
+
var debugSilentCommand = (command) => {
|
|
10
|
+
debugInternal("\x1B[32mCommand (silent)\x1B[0m", `\x1B[38;5;85m${command}\x1B[0m`);
|
|
11
|
+
};
|
|
7
12
|
var debugCommand = (command) => {
|
|
8
13
|
debugInternal("\x1B[32mCommand\x1B[0m", `\x1B[38;5;85m${command}\x1B[0m`);
|
|
9
14
|
};
|
|
10
15
|
var debug = (text) => {
|
|
11
16
|
debugInternal("\x1B[34mBlock\x1B[0m", `\x1B[38;5;81m${text}\x1B[0m`);
|
|
12
17
|
};
|
|
13
|
-
var execCommand = async (command) => {
|
|
14
|
-
debugCommand(command);
|
|
15
|
-
const result = await spawn(["bash", "-c", command], {
|
|
16
|
-
stdin: "inherit",
|
|
17
|
-
stdout: "inherit",
|
|
18
|
-
stderr: "inherit"
|
|
19
|
-
});
|
|
20
|
-
await result.exited;
|
|
21
|
-
};
|
|
22
18
|
|
|
19
|
+
// src/context.ts
|
|
23
20
|
class Context {
|
|
24
21
|
name;
|
|
25
22
|
domain;
|
|
26
|
-
|
|
23
|
+
userspace;
|
|
24
|
+
sudo;
|
|
25
|
+
constructor({
|
|
26
|
+
name,
|
|
27
|
+
address,
|
|
28
|
+
userspace = false,
|
|
29
|
+
sudo = false
|
|
30
|
+
}) {
|
|
27
31
|
this.name = name;
|
|
28
32
|
this.domain = address;
|
|
33
|
+
this.sudo = sudo;
|
|
34
|
+
this.userspace = userspace;
|
|
29
35
|
}
|
|
30
36
|
getAddress() {
|
|
31
37
|
return `${this.name}@${this.domain}`;
|
|
32
38
|
}
|
|
33
39
|
}
|
|
34
|
-
|
|
40
|
+
|
|
41
|
+
// src/base.ts
|
|
42
|
+
var defaultContext = new Context({
|
|
43
|
+
name: "root",
|
|
44
|
+
address: "0.0.0.0",
|
|
45
|
+
userspace: false,
|
|
46
|
+
sudo: false
|
|
47
|
+
});
|
|
48
|
+
var tee = async (read) => {
|
|
49
|
+
const reader = read.getReader();
|
|
50
|
+
let output = "";
|
|
51
|
+
while (true) {
|
|
52
|
+
const { value, done: doneReading } = await reader.read();
|
|
53
|
+
if (doneReading) {
|
|
54
|
+
return output;
|
|
55
|
+
}
|
|
56
|
+
const decoder = new TextDecoder("utf-8");
|
|
57
|
+
output += decoder.decode(value);
|
|
58
|
+
process.stdout.write(value);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
var readStreamToStr = async (read) => {
|
|
62
|
+
const reader = read.getReader();
|
|
63
|
+
let output = "";
|
|
64
|
+
while (true) {
|
|
65
|
+
const { value, done: doneReading } = await reader.read();
|
|
66
|
+
if (doneReading) {
|
|
67
|
+
return output;
|
|
68
|
+
}
|
|
69
|
+
const decoder = new TextDecoder("utf-8");
|
|
70
|
+
output += decoder.decode(value);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
var execCommand = async (command) => {
|
|
74
|
+
debugCommand(command);
|
|
75
|
+
const result = spawn(["bash", "-c", command], {
|
|
76
|
+
stdin: "inherit",
|
|
77
|
+
stdout: "pipe",
|
|
78
|
+
stderr: "inherit"
|
|
79
|
+
});
|
|
80
|
+
const output = await tee(result.stdout);
|
|
81
|
+
await result.exited;
|
|
82
|
+
return {
|
|
83
|
+
output,
|
|
84
|
+
spawnResult: result
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
var execCommandSilent = async (command) => {
|
|
88
|
+
debugSilentCommand(command);
|
|
89
|
+
const result = spawn(["bash", "-c", command]);
|
|
90
|
+
const output = await readStreamToStr(result.stdout);
|
|
91
|
+
await result.exited;
|
|
92
|
+
return {
|
|
93
|
+
output,
|
|
94
|
+
spawnResult: result
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
var copyFiles = async (from, to, context) => {
|
|
35
98
|
await execCommand(`rsync -avz ${from} ${context.getAddress()}:${to}`);
|
|
36
99
|
};
|
|
37
|
-
var exec = async (
|
|
100
|
+
var exec = async (command, context) => {
|
|
38
101
|
await execCommand(`ssh ${context.getAddress()} "${command}"`);
|
|
39
102
|
};
|
|
103
|
+
// src/init.ts
|
|
104
|
+
var installBun = async (context) => {
|
|
105
|
+
const bunPath = new URL(import.meta.resolve("./bun.sh")).pathname;
|
|
106
|
+
await copyFiles(bunPath, "./bun.sh", context);
|
|
107
|
+
await exec("./bun.sh", context);
|
|
108
|
+
};
|
|
109
|
+
// src/podman.ts
|
|
110
|
+
var {$: $2 } = globalThis.Bun;
|
|
111
|
+
|
|
112
|
+
// src/packages.ts
|
|
113
|
+
var {$ } = globalThis.Bun;
|
|
40
114
|
var installSystemPackage = async (packageName) => {
|
|
41
|
-
const osName = await $`cat /etc/os-release | grep ^ID= | cut -d'=' -f2`.text();
|
|
115
|
+
const osName = (await $`cat /etc/os-release | grep ^ID= | cut -d'=' -f2`.text()).trim();
|
|
42
116
|
if (osName === "ubuntu") {
|
|
43
|
-
execCommand(`apt-get install -y ${packageName}`);
|
|
117
|
+
await execCommand(`apt-get install -y ${packageName}`);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// src/systemd.ts
|
|
122
|
+
var systemctlWordLangth = "systemctl ".length;
|
|
123
|
+
var formatUserspace = (context, command) => {
|
|
124
|
+
const userPart = context.userspace ? "--user" : "";
|
|
125
|
+
return `systemctl ${userPart} ${command.slice(systemctlWordLangth)}`;
|
|
126
|
+
};
|
|
127
|
+
var getSysyemdServiceName = (name) => {
|
|
128
|
+
return `${name}.service`;
|
|
129
|
+
};
|
|
130
|
+
var getSystemdPathToService = (context, serviceName) => {
|
|
131
|
+
if (context.userspace) {
|
|
132
|
+
return `~/.config/systemd/user/${serviceName}`;
|
|
44
133
|
}
|
|
134
|
+
return `/etc/systemd/system/${serviceName}`;
|
|
135
|
+
};
|
|
136
|
+
var stopSystemdService = async (constext, name) => {
|
|
137
|
+
await execCommand(formatUserspace(constext, `systemctl stop ${name}`));
|
|
138
|
+
};
|
|
139
|
+
var restartSystemdService = async (name, constext = defaultContext) => {
|
|
140
|
+
await execCommand(formatUserspace(constext, `systemctl restart ${name}`));
|
|
45
141
|
};
|
|
142
|
+
var checkSystemdService = async (context, serviceName) => {
|
|
143
|
+
const { spawnResult } = await execCommand(formatUserspace(context, `systemctl is-active ${serviceName}`));
|
|
144
|
+
return spawnResult.exitCode === 0;
|
|
145
|
+
};
|
|
146
|
+
var createSystemdService = async (context, serviceName, pathToServiceFile) => {
|
|
147
|
+
const pathToSeviceTarget = getSystemdPathToService(context, serviceName);
|
|
148
|
+
await execCommand(`cp -v ${pathToServiceFile} ${pathToSeviceTarget}`);
|
|
149
|
+
await execCommand(formatUserspace(context, "systemctl daemon-reload"));
|
|
150
|
+
await execCommand(formatUserspace(context, `systemctl enable ${serviceName}`));
|
|
151
|
+
await execCommand(formatUserspace(context, `sysyemctl start ${serviceName}`));
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
// src/nft.ts
|
|
155
|
+
var localNftChainName = "local_chain";
|
|
156
|
+
var setupNftable = async (allowedIpSsh) => {
|
|
157
|
+
const listTable = await execCommandSilent("nft list table inet ramm");
|
|
158
|
+
if (listTable.spawnResult.exitCode === 0) {
|
|
159
|
+
await execCommand("nft delete table inet ramm");
|
|
160
|
+
}
|
|
161
|
+
await execCommand("nft add table inet ramm");
|
|
162
|
+
await execCommand("nft add chain inet ramm input '{ type filter hook input priority 0 ; }'");
|
|
163
|
+
await execCommand(`nft add set inet ramm allowed_ssh_ipv4 '{ type ipv4_addr; flags dynamic; elements = { ${allowedIpSsh} } }'`);
|
|
164
|
+
await execCommand(`nft add chain inet ramm ${localNftChainName}`);
|
|
165
|
+
await execCommand(`nft add rule inet ramm ${localNftChainName} iif lo accept`);
|
|
166
|
+
await execCommand(`nft add rule inet ramm ${localNftChainName} ct state established,related accept`);
|
|
167
|
+
await execCommand(`nft add rule inet ramm ${localNftChainName} ip saddr @allowed_ssh_ipv4 tcp dport 22 accept`);
|
|
168
|
+
await execCommand(`nft add rule inet ramm ${localNftChainName} tcp dport 22 ct state new limit rate over 3/minute drop`);
|
|
169
|
+
await execCommand(`nft add rule inet ramm ${localNftChainName} tcp dport 22 accept`);
|
|
170
|
+
await execCommand(`nft add rule inet ramm ${localNftChainName} tcp dport 80 accept`);
|
|
171
|
+
await execCommand(`nft add rule inet ramm ${localNftChainName} tcp dport 443 accept`);
|
|
172
|
+
await execCommand(`nft add rule inet ramm ${localNftChainName} drop`);
|
|
173
|
+
await execCommand("nft add rule inet ramm input fib daddr type local jump local_chain ");
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
// src/podman.ts
|
|
46
177
|
var installPodman = async () => {
|
|
47
|
-
if ((await
|
|
48
|
-
|
|
178
|
+
if ((await $2`command -v podman`.nothrow().quiet()).exitCode !== 0) {
|
|
179
|
+
await installSystemPackage("podman");
|
|
49
180
|
}
|
|
50
|
-
await
|
|
181
|
+
await createNetwork();
|
|
182
|
+
};
|
|
183
|
+
var createNetwork = async () => {
|
|
184
|
+
await execCommand("podman network create --interface-name=podman_ramm ramm");
|
|
185
|
+
};
|
|
186
|
+
var getCreateCommand = async (name) => {
|
|
187
|
+
const podmanCreateCommand = await $2`podman inspect --format '{{.Config.CreateCommand}}' ${name}`.nothrow().quiet();
|
|
188
|
+
return podmanCreateCommand.text().slice(0, -1).slice(1, -1) || "";
|
|
51
189
|
};
|
|
52
|
-
var runPodmanContainer = async (
|
|
53
|
-
|
|
54
|
-
|
|
190
|
+
var runPodmanContainer = async (name, command) => {
|
|
191
|
+
if (await getCreateCommand(name) !== command) {
|
|
192
|
+
await $2`podman rm -f ${name}`;
|
|
55
193
|
await execCommand(command);
|
|
56
194
|
return;
|
|
57
195
|
}
|
|
58
|
-
const podmanCreateCommandText = podmanCreateCommand.text().slice(0, -1);
|
|
59
|
-
if (podmanCreateCommandText !== `[${command}]`) {
|
|
60
|
-
await $`podman rm -f ${name}`;
|
|
61
|
-
execCommand(command);
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
196
|
console.info("Podman container is already running");
|
|
65
197
|
};
|
|
66
|
-
var
|
|
67
|
-
const
|
|
68
|
-
await
|
|
69
|
-
|
|
198
|
+
var runPodmanContainerService = async (name, command, context = defaultContext) => {
|
|
199
|
+
const serviceName = getSysyemdServiceName(name);
|
|
200
|
+
if (await checkSystemdService(context, serviceName)) {
|
|
201
|
+
await stopSystemdService(context, serviceName);
|
|
202
|
+
}
|
|
203
|
+
await runPodmanContainer(name, command);
|
|
204
|
+
await execCommand(`podman generate systemd --name --new ${name} > ${serviceName}`);
|
|
205
|
+
await createSystemdService(context, serviceName, serviceName);
|
|
206
|
+
};
|
|
207
|
+
var addNftPodmanRule = async () => {
|
|
208
|
+
const podmanNetworksResult = await execCommand("podman network inspect $(podman network ls -q) -f '{{.NetworkInterface}}'");
|
|
209
|
+
const podmanNetworks = podmanNetworksResult.output.trim().split(`
|
|
210
|
+
`);
|
|
211
|
+
await execCommand(`nft add set inet ramm podman_interfaces '{ type ifname; flags dynamic; elements = { ${podmanNetworks.join(", ")} }; }'`);
|
|
212
|
+
await execCommand("nft add chain inet ramm forward '{ type filter hook input priority 0 ; }'");
|
|
213
|
+
await execCommand(`nft add rule inet ramm forward iifname @podman_interfaces jump ${localNftChainName}`);
|
|
70
214
|
};
|
|
71
215
|
export {
|
|
216
|
+
setupNftable,
|
|
217
|
+
runPodmanContainerService,
|
|
72
218
|
runPodmanContainer,
|
|
219
|
+
restartSystemdService,
|
|
73
220
|
installSystemPackage,
|
|
74
221
|
installPodman,
|
|
75
222
|
installBun,
|
|
76
223
|
exec,
|
|
77
224
|
debug,
|
|
78
225
|
copyFiles,
|
|
226
|
+
addNftPodmanRule,
|
|
79
227
|
Context
|
|
80
228
|
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Context } from "./context.ts";
|
|
2
|
+
export declare const defaultContext: Context;
|
|
3
|
+
export declare const execCommand: (command: string) => Promise<{
|
|
4
|
+
output: string;
|
|
5
|
+
spawnResult: import("bun").Subprocess<"inherit", "pipe", "inherit">;
|
|
6
|
+
}>;
|
|
7
|
+
export declare const execCommandSilent: (command: string) => Promise<{
|
|
8
|
+
output: string;
|
|
9
|
+
spawnResult: import("bun").Subprocess<"ignore", "pipe", "inherit">;
|
|
10
|
+
}>;
|
|
11
|
+
export declare const copyFiles: (from: string, to: string, context: Context) => Promise<void>;
|
|
12
|
+
export declare const exec: (command: string, context: Context) => Promise<void>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare class Context {
|
|
2
|
+
name: string;
|
|
3
|
+
domain: string;
|
|
4
|
+
userspace: boolean;
|
|
5
|
+
sudo: boolean;
|
|
6
|
+
constructor({ name, address, userspace, sudo, }: {
|
|
7
|
+
name: string;
|
|
8
|
+
address: string;
|
|
9
|
+
userspace?: boolean;
|
|
10
|
+
sudo?: boolean;
|
|
11
|
+
});
|
|
12
|
+
getAddress(): string;
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const installSystemPackage: (packageName: string) => Promise<void>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Context } from "./context.ts";
|
|
2
|
+
export declare const installPodman: () => Promise<void>;
|
|
3
|
+
export declare const getCreateCommand: (name: string) => Promise<string>;
|
|
4
|
+
export declare const runPodmanContainer: (name: string, command: string) => Promise<void>;
|
|
5
|
+
export declare const runPodmanContainerService: (name: string, command: string, context?: Context) => Promise<void>;
|
|
6
|
+
export declare const addNftPodmanRule: () => Promise<void>;
|
package/dist/types/ramm.d.ts
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
export
|
|
9
|
-
export
|
|
10
|
-
export declare const installSystemPackage: (packageName: string) => Promise<void>;
|
|
11
|
-
export declare const installPodman: () => Promise<void>;
|
|
12
|
-
export declare const runPodmanContainer: (command: string, name: string) => Promise<void>;
|
|
13
|
-
export declare const installBun: (context: Context) => Promise<void>;
|
|
1
|
+
export { exec, copyFiles } from "./base.ts";
|
|
2
|
+
export { Context } from "./context.ts";
|
|
3
|
+
export { installBun } from "./init.ts";
|
|
4
|
+
export { installPodman, runPodmanContainer } from "./podman.ts";
|
|
5
|
+
export { runPodmanContainerService, addNftPodmanRule } from "./podman.ts";
|
|
6
|
+
export { restartSystemdService } from "./systemd.ts";
|
|
7
|
+
export { installSystemPackage } from "./packages.ts";
|
|
8
|
+
export { debug } from "./debug.ts";
|
|
9
|
+
export { setupNftable } from "./nft.ts";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Context } from "./context.ts";
|
|
2
|
+
export declare const getSysyemdServiceName: (name: string) => string;
|
|
3
|
+
export declare const stopSystemdService: (constext: Context, name: string) => Promise<void>;
|
|
4
|
+
export declare const restartSystemdService: (name: string, constext?: Context) => Promise<void>;
|
|
5
|
+
export declare const checkSystemdService: (context: Context, serviceName: string) => Promise<boolean>;
|
|
6
|
+
export declare const createSystemdService: (context: Context, serviceName: string, pathToServiceFile: string) => Promise<void>;
|
package/package.json
CHANGED