Permalink
Browse files

Moved on-demand stuff to its own place. Using JSON as transport method

  • Loading branch information...
1 parent e11dfef commit 17b0d6943ec9bfb49cff4cffb38eec7217693777 @tomas tomas committed Dec 1, 2010
Showing with 95 additions and 16 deletions.
  1. +1 −0 core/base
  2. +8 −0 core/framework
  3. +14 −6 core/functions
  4. +59 −0 core/on_demand
  5. +13 −10 core/response
View
@@ -14,3 +14,4 @@
. "$base_path/core/modules"
. "$base_path/core/push"
. "$base_path/core/actions"
+. "$base_path/core/on_demand"
View
@@ -31,6 +31,14 @@ capitalize(){
echo "$1" | awk '{for(i=1;i<=NF;i++)sub(/./,toupper(substr($i,1,1)),$i)}1'
}
+get_in_quotes(){
+ echo "$1" | sed "s/.*$2=\"\(.*\)\".*/\1/"
+}
+
+get_json_val(){
+ echo "$1" | sed "s/.*$2\":\"\([^\"]*\)\".*/\1/"
+}
+
# returns a urlencoded string, expects string as $1
urlencode(){
echo "$1" | tr $line_breaker "^" | sed -e 's/%/%25/g;s/ /%20/g;s/!/%21/g;s/"/%22/g;s/#/%23/g;s/\$/%24/g;s/\&/%26/g;s/=/%3D/g;s/'\''/%27/g;s/(/%28/g;s/)/%29/g' -e "s/\^$//;s/\^/%0A/g"
View
@@ -313,14 +313,17 @@ on_demand_stdin(){
}
on_demand_loop(){
- on_demand_stdin | on_demand_command | while read message; do
- if [[ "$message" =~ "run" ]]; then
- log " -- Command issued: $message"
- if [ "$message" == "run" ]; then
+ on_demand_stdin | on_demand_command | while read data; do
+ local message_type=`get_json_val "$data" "type"`
+ if [ "$message_type" == "text" ]; then
+ local message_body=`get_in_quotes "$data" 'body'`
+ if [ "$message_body" == "run" ]; then
"$base_path/prey.sh" --on-demand & disown -h
+ else
+ log " -- Message received: $message_body"
fi
else
- log " -- Message received: $message"
+ log " -- Unsupported message type. Please update to new version"
fi
done
}
@@ -333,7 +336,7 @@ enable_on_demand_mode(){
sleep 2
if [ -f "$on_demand_pipe" ]; then
log " -- On demand mode active!"
- on_demand_send "register $device_key"
+ on_demand_handshake
else
log " !! Coundn't set up on-demand mode."
fi
@@ -342,6 +345,11 @@ enable_on_demand_mode(){
fi
}
+on_demand_handshake(){
+ on_demand_send "{\"action\": \"connect\", \"data\": { \"client_version\": \"${version}\",\"key\":\"${device_key}\",\"pass\":\"${api_key}\"}}"
+}
+
+
on_demand_send(){
echo "$1" >> "$on_demand_pipe"
}
View
@@ -0,0 +1,59 @@
+#!/bin/bash
+####################################################################
+# Prey Core On-Demand Functions - (c) 2010 Fork Ltd.
+# URL: http://preyproject.com
+# License: GPLv3
+####################################################################
+
+on_demand_command(){
+ openssl s_client -no_ssl2 -quiet -connect $on_demand_host:$on_demand_port 2> /dev/null
+ log " -- OpenSSL returned. Removing pipe/lockfile..."
+ rm -f "$on_demand_pipe"
+ kill_process "$on_demand_pipe"
+}
+
+on_demand_stdin(){
+ tail -f "$on_demand_pipe" 2> /dev/null
+}
+
+on_demand_loop(){
+ on_demand_stdin | on_demand_command | while read data; do
+ local message_type=`get_json_val "$data" "type"`
+ if [ "$message_type" == "text" ]; then
+ local message_body=`get_in_quotes "$data" 'body'`
+ if [ "$message_body" == "run" ]; then
+ "$base_path/prey.sh" --on-demand & disown -h
+ else
+ log " -- Message received: $message_body"
+ fi
+ else
+ log " -- Unsupported message type. Please update to new version"
+ fi
+ done
+}
+
+enable_on_demand_mode(){
+ if [ ! -f "$on_demand_pipe" ]; then
+ echo "" > "$on_demand_pipe"
+ on_demand_loop &
+ log " -- On demand listener running with PID $!"
+ sleep 2
+ if [ -f "$on_demand_pipe" ]; then
+ log " -- On demand mode active!"
+ on_demand_handshake
+ else
+ log " !! Coundn't set up on-demand mode."
+ fi
+ else
+ log " -- Prey on demand is already running!"
+ fi
+}
+
+on_demand_handshake(){
+ on_demand_send "{\"action\": \"connect\", \"data\": { \"client_version\": \"${version}\",\"key\":\"${device_key}\",\"pass\":\"${api_key}\"}}"
+}
+
+
+on_demand_send(){
+ echo "$1" >> "$on_demand_pipe"
+}
View
@@ -5,18 +5,10 @@
# License: GPLv3
####################################################################
-process_config(){
-
- local prey_configuration=`echo -e "$response" | awk -F"[<>]" ' /<configuration>/,/<\/configuration>/' | grep -v "configuration>"`
- if [ -z "$prey_configuration" ]; then
- return 1
- fi
-
- log "\n${bold} >> Reading configuration...${bold_end}\n"
-
+process_xml(){
# inmense thanks to this post, it saved my day
# http://edwardawebb.com/linux/scope-issue-bash-loops
- STDOUT=$(echo -e "$prey_configuration")
+ STDOUT=$(echo -e "$1")
while read line; do
local key=`get_key "$line"`
@@ -29,6 +21,17 @@ process_config(){
set_config "$key" "$value"
done <<< "$STDOUT"
+}
+
+process_config(){
+
+ local prey_configuration=`echo -e "$response" | awk -F"[<>]" ' /<configuration>/,/<\/configuration>/' | grep -v "configuration>"`
+ if [ -z "$prey_configuration" ]; then
+ return 1
+ fi
+
+ log "\n${bold} >> Reading configuration...${bold_end}\n"
+ process_xml "$prey_configuration"
if [ "$offline_actions" == "true" ]; then # offline actions selected
echo -e "$response" > "$last_response"

0 comments on commit 17b0d69

Please sign in to comment.