ramm 0.0.3 → 0.0.5

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