# HG changeset patch # User virii # Date 2018-05-03 18:29:12 # Node ID 53346fd172cff3c7446667e682d9bbde9cf2b868 Initial diff --git a/dash.pl b/dash.pl new file mode 100644 --- /dev/null +++ b/dash.pl @@ -0,0 +1,111 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use diagnostics; + +use feature qw(:all); +use WWW::Curl::Easy; +use JSON::Parse qw(:all); +use Amazon::Dash::Button; + +my ($response_body, $info, $postfields, $space_status, $new_space_status); +my ($status, $site, $user, $pass, $mac, $curl, $json, $dev, $timeout); + + +$status = "https://spaceapi.c3l.lu/status.json"; # SpaceAPI direct link to status json file +$site = "https://spaceapi.c3l.lu/open"; # SpaceAPI direct link to change the state +$user = ""; # SpaceAPI user +$pass = ""; # SpaceAPI pass +$mac = 'de:ad:be:ef:0a:0a'; # MAC of Dash button. +$timeout = 60; # Timeout in seconds. + + +say "[+] Scanning network traffic"; + +Amazon::Dash::Button->new( from => { # The author of the module says that you + mac => $mac, # can omit a "dev" option to use another + onClick => \&spaceapi, # network interface but actually it never + name => q{SpaceAPI}, # gets used in the code. See the provided + timeout => $timeout # patch to define a different interface. +} )->listen; # Patch changes default to eth0. + + + +sub spaceapi { + +say "[+] Button has been pushed"; + +$curl = WWW::Curl::Easy->new; # $curl = new WWW::Curl::Easy; + +$curl->setopt(CURLOPT_URL, $status); +$curl->setopt(CURLOPT_VERBOSE,0); +$curl->setopt(CURLOPT_WRITEDATA,\$response_body); +$curl->perform; + +$info = $curl->getinfo(CURLINFO_RESPONSE_CODE); +say "[-] Server returned $info" and exit unless $info == 200; + +$json = valid_json($response_body); +say "[-] Malformed json!" and exit unless $json; +$json = parse_json($response_body); + +if ($json->{state}{open} and $json->{state}{message} =~ /members only/i) { + $space_status = 2; # Propably needed in the future + $new_space_status = "CLOSED"; + $postfields = "close=close&message="; +} elsif ($json->{state}{open}) { + $space_status = 1; # Propably needed in the future + $new_space_status = "CLOSED"; + $postfields = "close=close&message="; +} else { + $space_status = 0; # Propably needed in the future + $new_space_status = "OPEN"; + $postfields = "open=open&message="; +} + +$curl->setopt(CURLOPT_URL, $site); +$curl->setopt(CURLOPT_VERBOSE,0); +$curl->setopt(CURLOPT_USERPWD,"$user:$pass"); +$curl->setopt(CURLOPT_COOKIEFILE, 'cookiescurl'); # Apparently you need this +$curl->setopt(CURLOPT_COOKIEJAR, '/tmp/cookiescurl'); # to make Digest Auth work +$curl->setopt(CURLOPT_HTTPAUTH,CURLAUTH_DIGEST); # ...sad! +$curl->setopt(CURLOPT_POSTFIELDS(), $postfields); + +$curl->perform; + +$info = $curl->getinfo(CURLINFO_RESPONSE_CODE); +say "[-] Server returned $info" and exit unless $info == 302||200; +say "[+] Changed Space Status to $new_space_status"; + +} + +=README +SpaceAPI Amazon-Dash-Button-Trigger + ++-------------------------INSTALL-------------------------+ +| | +| 1) Patch Amazon::Dash::Button with the diffs provided | +| 2) ??? | +| 3) Profit! | +| | ++---------------------------------------------------------+ + + +=MAC ADDRESS + +Find out the mac address of your dash button by running: + +perl -MAmazon::Dash::Button -e 'Amazon::Dash::Button->search(dev => q{eth0}, cache => 1)' +If this does not show an entry with "Amazon" behind it then you have a Dash button of the +newer generation. This is not a problem as it now acts as 0.0.0.0:68. Take the shown MAC +of this entry as it's actually your dash button. + +=RUN + +Needs to be run as root as it's using PCAP to check whenever the button has been pushed. + +=cut + + + diff --git a/patches/buttonpatch.diff b/patches/buttonpatch.diff new file mode 100644 --- /dev/null +++ b/patches/buttonpatch.diff @@ -0,0 +1,11 @@ +--- /usr/local/share/perl/5.22.1/Amazon/Dash/Button.pm 2018-04-30 20:29:18.952791765 +0200 ++++ /usr/local/share/perl/5.22.1/Amazon/Dash/Button_patch.pm 2018-04-30 20:30:03.976834765 +0200 +@@ -185,7 +185,7 @@ + # default values + sub _build_timeout { 5 } + sub _build_filter { q{arp or ( udp and ( port 67 or port 68 ) ) } } +-sub _build_dev { 'en0' } ++sub _build_dev { 'eth0' } + + # the list of our button + sub _build_devices { [] } diff --git a/patches/devicepatch.diff b/patches/devicepatch.diff new file mode 100644 --- /dev/null +++ b/patches/devicepatch.diff @@ -0,0 +1,12 @@ +--- /usr/local/share/perl/5.22.1/Amazon/Dash/Button/Device.pm 2018-04-30 20:22:18.956933626 +0200 ++++ /usr/local/share/perl/5.22.1/Amazon/Dash/Button/Device_patch.pm 2018-04-30 20:24:31.638601089 +0200 +@@ -58,7 +58,8 @@ + } + + sub debug { +- warn join( ' ', map { $_ // 'undef' } @_ ); ++ my $debug = 0; ++ warn join( ' ', map { $_ // 'undef' } @_ ) if $debug; + } + + sub check {