Skip to content

Commit

Permalink
interface
Browse files Browse the repository at this point in the history
  • Loading branch information
perfecto25 committed Jul 29, 2020
1 parent dafc951 commit 73b7cc2
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 7 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ Fcat can open up ports for testing and close them once testing is complete.
---
## Installation

to do: add Bintray repo with Ubuntu & Centos binaries
### Centos
(required libs)

yum install libevent

install Fcat binaries located on Release page

---
## Usage
Expand Down
Binary file added fcat
Binary file not shown.
4 changes: 3 additions & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: fcat
version: 0.1.1
version: 0.1.2

authors:
- Mike R (https://github.com/perfecto25)
Expand All @@ -12,6 +12,8 @@ dependencies:
clim:
github: at-grandpa/clim
version: 0.13.0
ipaddr:
github: arcage/crystal-ipaddr

crystal: 0.35.1

Expand Down
5 changes: 3 additions & 2 deletions src/fcat.cr
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ module Fcat
run as client: fcat conn -h targetHost/IP -p 2400,2900-3500
"
version "Version 0.1.1"
version "Version 0.1.2"
option "-p PORT", "--port=PORT", type: String, desc: "Ports (example: -p 1200,1300,1400-1800)", default: "11235"
option "-h HOST", "--host=HOSTNAME/IP", type: String, desc: "Hostname or IP", default: "localhost"
option "-i INTERFACE", "--interface=NAME/IP", type: String, desc: "Network interface name", default: "0.0.0.0"
argument "conn", type: String, desc: "connect to ports", default: ""

run do |opts, args|
Expand All @@ -70,7 +71,7 @@ module Fcat

# server mode
if args.conn == ""
serve_ports(port_arr)
serve_ports(port_arr, opts.interface)
end

else
Expand Down
83 changes: 80 additions & 3 deletions src/server.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,83 @@

require "socket"
require "colorize"
#require "net_sample"

def serve_ports(port_list)
lib LibC
AF_PACKET = 17

struct SockaddrLl
sll_family : UShort
sll_protocol : UInt16
sll_ifinex : Int
sll_hatype : UShort
sll_pkttype : UChar
sll_halen : UChar
sll_addr : StaticArray(UChar, 8)
end

union IfaIfu
ifu_broadaddr : Sockaddr*
ifu_dstaddr : Sockaddr*
end

struct Ifaddrs
ifa_next : Ifaddrs*
ifa_name : Char*
ifa_flags : UInt
ifa_addr : Sockaddr*
ifa_netmask : Sockaddr*
ifa_ifu : IfaIfu
ifa_data : Void*
end
IFHWADDRLEN = 6
INET_ADDRSTRLEN = 16
INET6_ADDRSTRLEN = 46

fun getifaddrs(ifaddr : Ifaddrs**) : Int
fun freeifaddrs(ifaddr : Ifaddrs*) : Void
fun inet_ntop(af : Int, src : Void*, dst : Char*, size : SocklenT) : Char*
end

# get all NIC interfaces and their IPV4 IPs
def get_nic_ip()
LibC.getifaddrs(out ifaddrs)
ifap = ifaddrs.as(LibC::Ifaddrs*)
nics = Hash(String, String).new
while ifap
ifa = ifap.value
if ifa_addr = ifa.ifa_addr
if_name = String.new(ifa.ifa_name)
case ifa_addr.value.sa_family
when LibC::AF_INET
ina = ifa_addr.as(LibC::SockaddrIn*).value
dst = StaticArray(UInt8, LibC::INET_ADDRSTRLEN).new(0)
addr = ina.sin_addr.s_addr
LibC.inet_ntop(LibC::AF_INET, pointerof(addr).as(Void*), dst, LibC::INET_ADDRSTRLEN)
nics[if_name] = String.new(dst.to_unsafe)
end
end
ifap = ifa.ifa_next
end
nics
end


def serve_ports(port_list, interface)

# regex check if interface = ip address or hostname
if interface.match(/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/)
ip = interface
else # if iface name, get IP for iface
begin
ip = get_nic_ip[interface]
rescue
puts "invalid interface name"
exit
end
end


channel = Channel(String).new

port_list.each do |port|
Expand All @@ -17,8 +91,11 @@ def serve_ports(port_list)

spawn do
begin
server = TCPServer.new("0.0.0.0", intport)
puts "fcat serving port: #{intport}".colorize.fore(:green)
server = TCPServer.new(ip, intport)
p1 = "fcat serving >".colorize.fore(:green)
p2 = "#{ip}".colorize.fore(:white)
p3 = "#{intport}".colorize.fore(:cyan)
puts "#{p1} #{p2}:#{p3}"
rescue ex
puts "unable to serve port #{port} - #{ex.message}".colorize.fore(:yellow)
next
Expand Down

0 comments on commit 73b7cc2

Please sign in to comment.