Commit 72a1483

mo khan <mo@mokhan.ca>
2015-02-10 01:54:32
use agent registration to endpoint for registering fake agent.
1 parent f004d81
Changed files (2)
lib/tasks/agent.rake
@@ -1,24 +1,24 @@
 namespace :agent do
   require 'fake_agent'
-  ENDPOINT='http://localhost:3000'
 
   desc "watch all files"
   task watch: :environment do
-    agent = FakeAgent.new(Agent.first.id, ENDPOINT)
+    agent = FakeAgent.new
+    agent.register
     agent.watch(Dir.pwd)
   end
 
   desc "scan directory"
   task scan: :environment do
-    agent = FakeAgent.new(Agent.first.id, ENDPOINT)
+    agent = FakeAgent.new
+    agent.register
     agent.scan(Dir.pwd)
   end
 
   desc "scan network traffic"
   task :nfm do
-    id = Agent.first.id
-    agent = FakeAgent.new(id, ENDPOINT)
-
+    agent = FakeAgent.new
+    agent.register
     agent.packet_capture('eth0')
   end
 end
lib/fake_agent.rb
@@ -2,13 +2,20 @@ require 'socket'
 
 class FakeAgent
   include PacketFu
+  DEFAULT_ENDPOINT='http://localhost:3000'
   attr_reader :id, :endpoint
 
-  def initialize(id, endpoint)
-    @id = id
+  def initialize(endpoint = DEFAULT_ENDPOINT)
     @endpoint = endpoint
   end
 
+  def register
+    url = "#{endpoint}/agents.json"
+    response = Typhoeus.post(url, body: { agent: { hostname: Socket.gethostname } })
+    json = JSON.parse(response.body)
+    @id = json["id"]
+  end
+
   def watch(directory)
     listener = Listen.to(directory, debug: true) do |modified, added, removed|
       publish_event(:modified, modified)