use of org.havenapp.main.model.EventTrigger in project haven by guardianproject.
the class MonitorService method alert.
/**
* Sends an alert according to type of connectivity
*/
public synchronized void alert(int alertType, String path) {
Date now = new Date();
boolean doNotification = false;
if (mLastEvent == null) {
mLastEvent = new Event();
mLastEvent.save();
doNotification = true;
} else if (mPrefs.getNotificationTimeMs() == 0) {
doNotification = true;
} else if (mPrefs.getNotificationTimeMs() > 0 && mLastNotification != null) {
// check if time window is within configured notification time window
doNotification = ((now.getTime() - mLastNotification.getTime()) > mPrefs.getNotificationTimeMs());
} else {
doNotification = true;
}
EventTrigger eventTrigger = new EventTrigger();
eventTrigger.setType(alertType);
eventTrigger.setPath(path);
mLastEvent.addEventTrigger(eventTrigger);
// we don't need to resave the event, only the trigger
eventTrigger.save();
if (doNotification) {
mLastNotification = new Date();
/*
* If SMS mode is on we send an SMS or Signal alert to the specified
* number
*/
StringBuilder alertMessage = new StringBuilder();
alertMessage.append(getString(R.string.intrusion_detected, eventTrigger.getStringType(this)));
if (mPrefs.getSignalUsername() != null) {
// since this is a secure channel, we can add the Onion address
if (mPrefs.getRemoteAccessActive() && (!TextUtils.isEmpty(mPrefs.getRemoteAccessOnion()))) {
alertMessage.append(" http://").append(mPrefs.getRemoteAccessOnion()).append(':').append(WebServer.LOCAL_PORT);
}
SignalSender sender = SignalSender.getInstance(this, mPrefs.getSignalUsername());
ArrayList<String> recips = new ArrayList<>();
StringTokenizer st = new StringTokenizer(mPrefs.getSmsNumber(), ",");
while (st.hasMoreTokens()) recips.add(st.nextToken());
String attachment = null;
if (eventTrigger.getType() == EventTrigger.CAMERA) {
attachment = eventTrigger.getPath();
} else if (eventTrigger.getType() == EventTrigger.MICROPHONE) {
attachment = eventTrigger.getPath();
} else if (eventTrigger.getType() == EventTrigger.CAMERA_VIDEO) {
attachment = eventTrigger.getPath();
}
sender.sendMessage(recips, alertMessage.toString(), attachment);
} else if (mPrefs.getSmsActivation()) {
SmsManager manager = SmsManager.getDefault();
StringTokenizer st = new StringTokenizer(mPrefs.getSmsNumber(), ",");
while (st.hasMoreTokens()) manager.sendTextMessage(st.nextToken(), null, alertMessage.toString(), null, null);
}
}
}
use of org.havenapp.main.model.EventTrigger in project haven by guardianproject.
the class WebServer method showEvent.
private void showEvent(Event event, StringBuffer page) {
List<EventTrigger> triggers = event.getEventTriggers();
page.append("<h1>Event: ").append(event.getStartTime().toLocaleString()).append("</h1><hr/>\n");
for (EventTrigger eventTrigger : triggers) {
String title = eventTrigger.getStringType(mContext);
String desc = eventTrigger.getTriggerTime().toString();
page.append("<b>");
page.append(title).append("</b><br/>");
page.append(desc).append("<br/>");
String mediaPath = "/event/" + event.getId() + "/trigger/" + eventTrigger.getId();
if (eventTrigger.getType() == EventTrigger.CAMERA) {
page.append("<img src=\"").append(mediaPath).append("\" width=\"100%\"/>");
page.append("<a href=\"").append(mediaPath).append("\">Download Media").append("</a>");
} else if (eventTrigger.getType() == EventTrigger.MICROPHONE) {
page.append("<audio src=\"").append(mediaPath).append("\"></audio>");
page.append("<a href=\"").append(mediaPath).append("\">Download Media").append("</a>");
}
page.append("<hr/>");
}
}
use of org.havenapp.main.model.EventTrigger in project haven by guardianproject.
the class WebServer method serve.
@Override
public Response serve(IHTTPSession session) {
StringBuffer page = new StringBuffer();
Cookie cookie = null;
if (mPassword != null) {
// We have to use session.parseBody() to obtain POST data.
// See https://github.com/NanoHttpd/nanohttpd/issues/427
Map<String, String> content = new HashMap<>();
Method method = session.getMethod();
if (Method.PUT.equals(method) || Method.POST.equals(method))
try {
session.parseBody(content);
} catch (IOException ioe) {
Log.e(TAG, "unable to parse body of request", ioe);
} catch (ResponseException re) {
Log.e(TAG, "unable to parse body of request", re);
}
String inPassword = session.getParms().get("p");
String inSid = session.getCookies().read("sid");
if (inPassword != null && safeEquals(inPassword, mPassword)) {
mSession = UUID.randomUUID().toString();
cookie = new OnionCookie("sid", mSession, 100000);
session.getCookies().set(cookie);
} else if (inSid == null || mSession == null || (inSid != null && (!safeEquals(inSid, mSession)))) {
showLogin(page);
return newFixedLengthResponse(page.toString());
}
}
Uri uri = Uri.parse(session.getUri());
List<String> pathSegs = uri.getPathSegments();
if (pathSegs.size() == 4 && pathSegs.get(2).equals("trigger")) {
// long eventId = Long.parseLong(pathSegs.get(1));
long eventTriggerId = Long.parseLong(pathSegs.get(3));
EventTrigger eventTrigger = EventTrigger.findById(EventTrigger.class, eventTriggerId);
try {
File fileMedia = new File(eventTrigger.getPath());
FileInputStream fis = new FileInputStream(fileMedia);
return newChunkedResponse(Response.Status.OK, getMimeType(eventTrigger), fis);
} catch (IOException ioe) {
Log.e(TAG, "unable to return media file", ioe);
}
} else if (uri.getPath().startsWith("/feed")) {
// do RSS feed
} else {
page.append("<html><head><title>").append(appTitle).append("</title>");
page.append("<meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=utf-8\" />");
page.append("<meta name = \"viewport\" content = \"user-scalable=no, initial-scale=1.0, maximum-scale=1.0, width=device-width\">");
page.append("</head><body>");
if (TextUtils.isEmpty(uri.getPath()) || uri.getPath().equals("/"))
showEvents(page);
else {
try {
if (pathSegs.size() == 2 && pathSegs.get(0).equals("event")) {
long eventId = Long.parseLong(pathSegs.get(1));
Event event = Event.findById(Event.class, eventId);
showEvent(event, page);
}
} catch (Exception e) {
Log.e(TAG, "Something went wrong with parsing the path", e);
}
}
page.append("</body></html>\n");
Response response = newFixedLengthResponse(page.toString());
session.getCookies().unloadQueue(response);
return response;
}
Response response = newFixedLengthResponse(Response.Status.INTERNAL_ERROR, "text/plain", "Error");
session.getCookies().unloadQueue(response);
return response;
}
use of org.havenapp.main.model.EventTrigger in project haven by guardianproject.
the class EventActivity method generateLog.
private String generateLog() {
StringBuilder mEventLog = new StringBuilder();
setTitle("Event @ " + mEvent.getStartTime().toLocaleString());
for (EventTrigger eventTrigger : mEvent.getEventTriggers()) {
mEventLog.append("Event Triggered @ ").append(eventTrigger.getTriggerTime().toString()).append("\n");
String sType = eventTrigger.getStringType(this);
mEventLog.append("Event Type: ").append(sType);
mEventLog.append("\n==========================\n");
}
return mEventLog.toString();
}
use of org.havenapp.main.model.EventTrigger in project haven by guardianproject.
the class EventActivity method shareEvent.
private void shareEvent() {
String title = "Phoneypot: " + mEvent.getStartTime().toLocaleString();
// need to "send multiple" to get more than one attachment
final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, title);
emailIntent.putExtra(Intent.EXTRA_TEXT, generateLog());
// has to be an ArrayList
ArrayList<Uri> uris = new ArrayList<>();
// convert from paths to Android friendly Parcelable Uri's
for (EventTrigger trigger : mEvent.getEventTriggers()) {
File fileIn = new File(trigger.getPath());
Uri u = Uri.fromFile(fileIn);
uris.add(u);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(emailIntent, getString(R.string.share_event_action)));
}
Aggregations