Skip to content

Commit

Permalink
use nft -i to pass nftables cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
carlhoerberg committed Sep 14, 2022
1 parent 0d40d50 commit e87b23d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/nftables.cr
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# Class to interact with nftables
# Not linked to libnftables, but calls out to the `nft` binary
class Nftables
def self.run_cmd(cmd : String) : Nil
status = Process.run("nft", {cmd}, output: Process::Redirect::Inherit, error: Process::Redirect::Inherit)
status.success? || raise Error.new("nftables command '#{cmd}' failed")
def initialize
input, @io = IO.pipe
nft = Process.new("nft", {"-i"}, input: input, output: Process::Redirect::Inherit, error: Process::Redirect::Inherit)
spawn do
nft.wait
abort "nft exited"
end
end

def self.run_file(file : String) : Nil
status = Process.run("nft", {"-f", file}, output: Process::Redirect::Inherit, error: Process::Redirect::Inherit)
status.success? || raise Error.new("nftables file '#{file}' failed")
def run_cmd(cmd : String) : Nil
@io.puts cmd
end

class Error < Exception; end
Expand Down
3 changes: 2 additions & 1 deletion src/server-cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ begin
puts "HMAC keys: #{c.hmac_keys.size}"
if c.nftables_cmd.bytesize > 0
puts "nftables command: #{c.nftables_cmd}"
nft = Nftables.new
on_accept = ->(ip_str : String) {
Nftables.run_cmd sprintf(c.nftables_cmd, ip_str)
nft.run_cmd sprintf(c.nftables_cmd, ip_str)
}
else
puts "Open command: #{c.open_cmd}"
Expand Down

0 comments on commit e87b23d

Please sign in to comment.