diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..163eb75 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*.cr] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 2 +trim_trailing_whitespace = true diff --git a/.gitignore b/.gitignore index 722935b..b668e40 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,9 @@ +# vim files *.swp -eznet + +# stuff from crystal init +/docs/ +/lib/ +/bin/ +/.shards/ +*.dwarf diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..227f158 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: crystal + +script: + - crystal spec + - crystal tool format --check diff --git a/README.md b/README.md index 5ebcc24..559f281 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ Get the name and process ID of the process running on a given port ## TODO * Add the ability to kill process on port +* Maybe look into other cli operations? (getting IP?) * Come up with other useful stuff * Write up contributors guide * Decide how to do documentation (probably Github wiki?) diff --git a/ltnp/ltnp_operator.cr b/ltnp/ltnp_operator.cr index 228708c..7dd0b92 100644 --- a/ltnp/ltnp_operator.cr +++ b/ltnp/ltnp_operator.cr @@ -2,17 +2,17 @@ require "./ltnp_record" class LtnpOperator def initialize(cmd_output : String) - records = cmd_output.each_line - records = records.each.select(/^tcp/) - @ltnp_records = Array(LtnpRecord).new - records.each { |s| @ltnp_records << LtnpRecord.new s } + records = cmd_output.each_line + records = records.each.select(/^tcp/) + @ltnp_records = Array(LtnpRecord).new + records.each { |s| @ltnp_records << LtnpRecord.new s } end def say_hi - @ltnp_records.each { |r| puts r.to_s } + @ltnp_records.each { |r| puts r.to_s } end def get_port_record(port : String) : LtnpRecord | Nil - @ltnp_records.find { |r| r.port == port } + @ltnp_records.find { |r| r.port == port } end end diff --git a/ltnp/ltnp_record.cr b/ltnp/ltnp_record.cr index 1cf4f45..dfb5736 100644 --- a/ltnp/ltnp_record.cr +++ b/ltnp/ltnp_record.cr @@ -1,7 +1,7 @@ require "../modules/record_helpers" class LtnpRecord - getter proto : String + getter proto : String getter state : String getter address : String getter port : String @@ -12,42 +12,41 @@ class LtnpRecord getter p_name : String def initialize(record : String) - ser_record = RecordHelpers.clean_record(record) - @proto = ser_record[0] - @address, @port = ser_address_port ser_record[3].split(':') - @f_address, @f_port = ser_address_port ser_record[4].split(':') - @state = ser_record[5] - @pid, @p_name = ser_pid_p_name ser_record[6] + ser_record = RecordHelpers.clean_record(record) + @proto = ser_record[0] + @address, @port = ser_address_port ser_record[3].split(':') + @f_address, @f_port = ser_address_port ser_record[4].split(':') + @state = ser_record[5] + @pid, @p_name = ser_pid_p_name ser_record[6] end def to_s - puts "%s, " * 8 % [ - @proto, @state, @address, @port, - @f_address, @f_port, @pid, @p_name - ] + puts "%s, " * 8 % [ + @proto, @state, @address, @port, + @f_address, @f_port, @pid, @p_name, + ] end def ser_address_port(address_port : Array) : {String, String} - address = String.new - port = String.new - unless address_port.size > 2 - address = address_port[0] - port = address_port[1] - else - port = address_port[-1] - end - {address, port} + address = String.new + port = String.new + unless address_port.size > 2 + address = address_port[0] + port = address_port[1] + else + port = address_port[-1] + end + {address, port} end def ser_pid_p_name(pid_pro_str : String) : {String, String} - pid = String.new - p_name = String.new - unless pid_pro_str == "-" - pid_pro = pid_pro_str.split('/') - pid = pid_pro[0] - p_name = pid_pro[1] - end - {pid, p_name} + pid = String.new + p_name = String.new + unless pid_pro_str == "-" + pid_pro = pid_pro_str.split('/') + pid = pid_pro[0] + p_name = pid_pro[1] + end + {pid, p_name} end - end diff --git a/main.cr b/main.cr index 936ac42..d0db420 100644 --- a/main.cr +++ b/main.cr @@ -6,29 +6,29 @@ OptionParser.parse do |parser| parser.banner = "Welcome to eznetstat!" parser.on "-v", "--version", "Show version" do - puts "0.1" - exit + puts "0.1" + exit end - + parser.on "-h", "--help", "Show help" do - puts parser - exit + puts parser + exit end parser.on "-c PORT", "--check-port=PORT", "Get the process name and ID of process on port" do |port| - ltnp_operator = NetstatRunner.run_ltnp - record = ltnp_operator.get_port_record port - unless record.nil? - puts "Port: %s, Process: %s" % [record.port, record.p_name] - else - puts "No processes found on port %s" % port - end - exit + ltnp_operator = NetstatRunner.run_ltnp + record = ltnp_operator.get_port_record port + unless record.nil? + puts "Port: %s, Process: %s" % [record.port, record.p_name] + else + puts "No processes found on port %s" % port + end + exit end parser.on "-k", "--kill-port", "Kill process on port" do - ltnp_operator = NetstatRunner.run_ltnp - ltnp_operator.say_hi - exit + ltnp_operator = NetstatRunner.run_ltnp + ltnp_operator.say_hi + exit end end diff --git a/modules/netstat_runner.cr b/modules/netstat_runner.cr index ead6a9c..c480665 100644 --- a/modules/netstat_runner.cr +++ b/modules/netstat_runner.cr @@ -1,7 +1,7 @@ module NetstatRunner - def self.run_ltnp() : LtnpOperator - io = IO::Memory.new - Process.run("sudo netstat -ltnp", shell: true, output: io) - LtnpOperator.new io.to_s + def self.run_ltnp : LtnpOperator + io = IO::Memory.new + Process.run("sudo netstat -ltnp", shell: true, output: io) + LtnpOperator.new io.to_s end end diff --git a/modules/record_helpers.cr b/modules/record_helpers.cr index aa187b2..9ae8566 100644 --- a/modules/record_helpers.cr +++ b/modules/record_helpers.cr @@ -1,7 +1,7 @@ module RecordHelpers def self.clean_record(record : String) : Array(String) - tokenized_record = record.split(" ") - tokenized_record = tokenized_record.reject { |s| s == "" } - tokenized_record + tokenized_record = record.split(" ") + tokenized_record = tokenized_record.reject { |s| s == "" } + tokenized_record end end diff --git a/shard.yml b/shard.yml new file mode 100644 index 0000000..13fee3a --- /dev/null +++ b/shard.yml @@ -0,0 +1,13 @@ +name: eznet-cli +version: 0.0.1 + +authors: + - BraydonKains + +targets: + eznet-cli: + eznet: ./main.cr + +crystal: 0.33.0 + +license: MIT diff --git a/spec/ltnp/ltnp_operator_spec.cr b/spec/ltnp/ltnp_operator_spec.cr index 10ceffe..a8a4c48 100644 --- a/spec/ltnp/ltnp_operator_spec.cr +++ b/spec/ltnp/ltnp_operator_spec.cr @@ -5,15 +5,15 @@ describe LtnpOperator do sut = LtnpOperator.new File.read("./spec/input/ltnp_example.txt") describe "#get_port_record" do - existing_port = "8081" - nonsense_port = "1234" + existing_port = "8081" + nonsense_port = "1234" - it "should find record when port exists" do - sut.get_port_record(existing_port).nil?.should be_false - end + it "should find record when port exists" do + sut.get_port_record(existing_port).nil?.should be_false + end - it "should not find record when port doesn't exist" do - sut.get_port_record(nonsense_port).nil?.should be_true - end + it "should not find record when port doesn't exist" do + sut.get_port_record(nonsense_port).nil?.should be_true + end end end diff --git a/spec/ltnp/ltnp_record_spec.cr b/spec/ltnp/ltnp_record_spec.cr index 79f29f3..28d6c73 100644 --- a/spec/ltnp/ltnp_record_spec.cr +++ b/spec/ltnp/ltnp_record_spec.cr @@ -2,28 +2,28 @@ require "spec" require "../../ltnp/ltnp_record.cr" describe LtnpRecord do - sut_normal = LtnpRecord.new "tcp 0 0 127.0.0.1:8081 0.0.0.0:* LISTEN 69/myproc" - sut_address = LtnpRecord.new "tcp 0 0 :::8081 0.0.0.0:* LISTEN 69/myproc" - sut_process = LtnpRecord.new "tcp 0 0 :::8081 0.0.0.0:* LISTEN -" + sut_normal = LtnpRecord.new "tcp 0 0 127.0.0.1:8081 0.0.0.0:* LISTEN 69/myproc" + sut_address = LtnpRecord.new "tcp 0 0 :::8081 0.0.0.0:* LISTEN 69/myproc" + sut_process = LtnpRecord.new "tcp 0 0 :::8081 0.0.0.0:* LISTEN -" it "should have expected properties under normal circumstance" do - sut_normal.proto.should eq("tcp") - sut_normal.address.should eq("127.0.0.1") - sut_normal.port.should eq("8081") - sut_normal.f_address.should eq("0.0.0.0") - sut_normal.f_port.should eq("*") - sut_normal.state.should eq("LISTEN") - sut_normal.pid.should eq("69") - sut_normal.p_name.should eq("myproc") + sut_normal.proto.should eq("tcp") + sut_normal.address.should eq("127.0.0.1") + sut_normal.port.should eq("8081") + sut_normal.f_address.should eq("0.0.0.0") + sut_normal.f_port.should eq("*") + sut_normal.state.should eq("LISTEN") + sut_normal.pid.should eq("69") + sut_normal.p_name.should eq("myproc") end it "should have expected properties when Local Address column is edge case" do - sut_address.address.empty?.should be_true - sut_address.port.should eq("8081") + sut_address.address.empty?.should be_true + sut_address.port.should eq("8081") end it "should have expected properties when PID/ProcessName column is edge case" do - sut_process.pid.empty?.should be_true - sut_process.p_name.empty?.should be_true + sut_process.pid.empty?.should be_true + sut_process.p_name.empty?.should be_true end end