ramm 0.0.10 → 0.0.12

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
@@ -155,28 +155,6 @@ var createSystemdService = async (context, serviceName, pathToServiceFile) => {
155
155
  await execCommand(formatUserspace(context, `sysyemctl start ${serviceName}`));
156
156
  };
157
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
158
  // src/podman.ts
181
159
  var installPodman = async () => {
182
160
  if ((await $2`command -v podman`.nothrow().quiet()).exitCode !== 0) {
@@ -185,6 +163,10 @@ var installPodman = async () => {
185
163
  await createNetwork();
186
164
  };
187
165
  var createNetwork = async () => {
166
+ const netwroks = await execCommand("podman network inspect $(podman network ls -q) -f '{{.NetworkInterface}}'");
167
+ if (netwroks.output.includes("podman_ramm")) {
168
+ return;
169
+ }
188
170
  await execCommand("podman network create --interface-name=podman_ramm ramm");
189
171
  };
190
172
  var getCreateCommand = async (name) => {
@@ -213,8 +195,47 @@ var addNftPodmanRule = async () => {
213
195
  const podmanNetworks = podmanNetworksResult.output.trim().split(`
214
196
  `);
215
197
  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}`);
198
+ await execCommand("nft insert rule inet ramm prerouting iifname @podman_interfaces accept");
199
+ };
200
+ // src/nft.ts
201
+ var createNftTable = (allowed_ssh_ipv4) => {
202
+ const nftTable = `table inet ramm {
203
+ set allowed_ssh_ipv4 {
204
+ type ipv4_addr
205
+ flags dynamic
206
+ elements = { ${allowed_ssh_ipv4} }
207
+ }
208
+
209
+ chain local_chain {
210
+ iif "lo" accept
211
+ ct state established,related accept
212
+ ip saddr @allowed_ssh_ipv4 tcp dport 22 accept
213
+ tcp dport 22 ct state new limit rate over 3/minute burst 5 packets drop
214
+ tcp dport 22 accept
215
+ tcp dport 80 accept
216
+ tcp dport 443 accept
217
+ drop
218
+ }
219
+
220
+ chain prerouting {
221
+ type filter hook prerouting priority mangle; policy accept;
222
+ fib daddr type local jump local_chain
223
+ }
224
+ }
225
+ `;
226
+ return nftTable;
227
+ };
228
+ var setupNftable = async (allowedIpSsh) => {
229
+ const listTable = await execCommandSilent("nft list table inet ramm");
230
+ if (listTable.spawnResult.exitCode === 0) {
231
+ await execCommand("nft delete table inet ramm");
232
+ }
233
+ const nftTable = createNftTable(allowedIpSsh);
234
+ await execCommand(`nft -f - <<EOF
235
+ ${nftTable}
236
+ EOF`);
237
+ await execCommand("nft list ruleset > /etc/nftables.conf");
238
+ await execCommand("systemctl enable nftables");
218
239
  };
219
240
  // src/files.ts
220
241
  import { appendFile, exists } from "fs/promises";
@@ -258,15 +279,19 @@ var checkStrInFile = async (filePath, str) => {
258
279
  }
259
280
  return false;
260
281
  };
261
- var writeIfNew = async (rawFilePath, str) => {
282
+ var createFileIfNeed = async (rawFilePath) => {
262
283
  const filePath = normalizePath(rawFilePath);
263
284
  await createDir(filePath);
264
- if (await checkStrInFile(filePath, str)) {
265
- return;
266
- }
267
285
  if (!await Bun.file(filePath).exists()) {
268
286
  await execCommand(`touch ${filePath}`);
269
287
  }
288
+ };
289
+ var writeIfNew = async (rawFilePath, str) => {
290
+ const filePath = normalizePath(rawFilePath);
291
+ await createFileIfNeed(filePath);
292
+ if (await checkStrInFile(filePath, str)) {
293
+ return;
294
+ }
270
295
  await appendFile(filePath, finalizeWithNewline(str));
271
296
  };
272
297
  // src/ssh.ts
@@ -1 +1,2 @@
1
1
  export declare const writeIfNew: (rawFilePath: string, str: string) => Promise<void>;
2
+ export declare const writeFile: (pathToFile: string, str: 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.10",
4
+ "version": "0.0.12",
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"