Skip to content

Commit

Permalink
initial commit with already working tcpflow port
Browse files Browse the repository at this point in the history
  • Loading branch information
hukl committed Dec 30, 2011
0 parents commit e513c66
Show file tree
Hide file tree
Showing 18 changed files with 204 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
*.beam
8 changes: 8 additions & 0 deletions Gemfile
@@ -0,0 +1,8 @@
source 'http://rubygems.org'

group :development, :test do
gem 'guard-shell'
gem 'rb-fsevent'
gem 'growl'
gem 'rake'
end
38 changes: 38 additions & 0 deletions Guardfile
@@ -0,0 +1,38 @@
# # -*- encoding : utf-8 -*-

# automatically run the eunit tests

def run_eunit_all
cmd = "./rebar eunit skip_deps=true"
puts "Executing #{cmd}"
puts `#{cmd}`
if $? == 0
Growl.notify_ok "eunit: all tests passed."
else
Growl.notify_error "eunit: tests failed."
end
end

def run_eunit(app, src, suite)
if File.exist?(File.join(File.dirname(__FILE__), app, 'test', "#{suite}_tests.erl"))
cmd = "./rebar eunit skip_deps=true suite=#{suite}"
puts "Executing #{cmd}"
puts `#{cmd}`
if $? == 0
Growl.notify_ok "#{suite}: eunit passed."
else
Growl.notify_error "#{suite}: eunit failed."
end
else
puts "No tests for #{suite.inspect}"
Growl.notify_warning "No tests for #{suite}!"
end
end

def run_spec
end

guard 'shell' do
watch(%r{(apps/.*?)(src|test)/([^.].*?)(_tests)?.erl}) {|m| run_eunit(m[1], m[2], m[3]) }
watch(%r{apps/(.*?)/include/([^.].*).hrl}) {|m| run_eunit_all }
end
12 changes: 12 additions & 0 deletions Rakefile
@@ -0,0 +1,12 @@
# -*- encoding : utf-8 -*-

require 'rubygems'
require 'bundler'
begin
Bundler.setup(:default, :development)
rescue Bundler::BundlerError => e
$stderr.puts e.message
$stderr.puts "Run `bundle install` to install missing gems"
exit e.status_code
end
require 'rake'
Empty file added apps/fis/deps/.gitkeep
Empty file.
Empty file added apps/fis/ebin/.gitkeep
Empty file.
Empty file added apps/fis/include/.gitkeep
Empty file.
Empty file added apps/fis/priv/.gitkeep
Empty file.
9 changes: 9 additions & 0 deletions apps/fis/rebar.config
@@ -0,0 +1,9 @@
% {erl_opts, [warnings_as_errors, debug_info]}.
{erl_opts, [debug_info]}.
{require_otp_vsn, "R15"}.
{deps, [
{misultin, "0.9-dev", {git, "git://github.com/ostinelli/misultin.git", {tag, "5143979e41"}}}
]}.
{clean_files, ["ebin/*.beam"]}.

{xref_checks, [undefined_function_calls]}.
Empty file added apps/fis/src/.gitkeep
Empty file.
13 changes: 13 additions & 0 deletions apps/fis/src/fis.app.src
@@ -0,0 +1,13 @@
{application, fis,
[
{description, ""},
{vsn, "0.0.1"},
{registered, []},
{applications, [
kernel,
stdlib,
sasl
]},
{mod, {fis, []}},
{env, []}
]}.
16 changes: 16 additions & 0 deletions apps/fis/src/fis_app.erl
@@ -0,0 +1,16 @@
-module(fis_app).

-behaviour(application).

%% Application callbacks
-export([start/2, stop/1]).

%% ===================================================================
%% Application callbacks
%% ===================================================================

start(_StartType, _StartArgs) ->
fis_sup:start_link().

stop(_State) ->
ok.
31 changes: 31 additions & 0 deletions apps/fis/src/fis_sup.erl
@@ -0,0 +1,31 @@

-module(fis_sup).

-behaviour(supervisor).

%% API functions
-export([start_link/0]).

%% Supervisor callbacks
-export([init/1]).

%% Helper macro for declaring children of supervisor
-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).

%% ===================================================================
%% API functions
%% ===================================================================

start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).

%% ===================================================================
%% Supervisor callbacks
%% ===================================================================

init([]) ->
Children = [],

RestartStrategy = {one_for_one, 0, 1},

{ok, {RestartStrategy, Children}}.
70 changes: 70 additions & 0 deletions apps/fis/src/fis_tcpflow.erl
@@ -0,0 +1,70 @@

-module(fis_tcpflow).
-behavior(gen_server).

% Managment API
-export([start/0, start_link/0, stop/0]).

% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
code_change/3]).

% Public API
-export([]).

% Record which holds the Port
-record(state, { port, regex }).

%% ===================================================================
%% Management API
%% ===================================================================

start() ->
gen_server:start( {local, ?MODULE}, ?MODULE, [], []).

start_link() ->
gen_server:start_link( {local, ?MODULE}, ?MODULE, [], []).

stop() -> gen_server:cast(?MODULE, stop).

%% ===================================================================
%% gen_server callbacks
%% ===================================================================

init([]) ->
Cmd = "tcpflow -i en1 -c -s -b 1024 dst port 80",
Port = open_port({spawn, Cmd}, [stream, use_stdio, exit_status, binary]),

{ ok, Regex } = re:compile("GET\\s(.*\\.ico).+\\r\\nHost\\:\\s?(.+)\\b"),
{ ok, #state{ port = Port, regex = Regex } }.

handle_call( _Msg, _From, State ) ->
{ reply, ok, State }.

handle_cast( stop, State ) ->
{ stop, normal, State };

handle_cast( _Msg, State ) ->
{ noreply, normal, State }.

code_change(_OldVsn, State, _Extra) ->
{ ok, State }.

handle_info( Info, State ) ->
{_, {data, Data}} = Info,
case re:run( Data, State#state.regex, [global,{capture,[1,2],list}] ) of
{match, [[Path,Host]]} ->
error_logger:info_msg("Host: ~p~nPath: ~p~n", [Host, Path]);
_ -> ok
end,
{ noreply, State }.

terminate( _Reason, _State ) ->
whatever.

%% ===================================================================
%% Public API
%% ===================================================================



Empty file added apps/fis/test/.gitkeep
Empty file.
Empty file added deps/.gitkeep
Empty file.
6 changes: 6 additions & 0 deletions rebar.config
@@ -0,0 +1,6 @@
{sub_dirs, ["apps/fis", "rel"]}.

% {erl_opts, [warnings_as_errors, debug_info]}.
{erl_opts, [debug_info]}.

{require_otp_vsn, "R15"}.
Empty file added rel/.gitkeep
Empty file.

0 comments on commit e513c66

Please sign in to comment.