-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from OthmanEmpire/develop
Synchronising development branch with master branch
- Loading branch information
Showing
16 changed files
with
327 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,62 @@ | ||
""" | ||
Responsible for controlling the Raspberry Pi pins. | ||
Responsible for controlling the Raspberry Pi pins connected to an LED. | ||
""" | ||
|
||
|
||
import RPi.GPIO as GPIO | ||
import time | ||
|
||
|
||
LED_PIN = 18 | ||
import RPi.GPIO as GPIO | ||
|
||
|
||
def initialisePi(): | ||
def initialisePi(pin: int): | ||
""" | ||
Initialises the PIN to be connected to the LED. | ||
:return: | ||
:param pin: The pin connected to the LED. | ||
:return: RPi.GPIO.PWM, representing the pin connected to the LED. | ||
""" | ||
FREQUENCY = 1000 | ||
|
||
GPIO.setmode(GPIO.BCM) | ||
GPIO.setup(LED_PIN, GPIO.OUT) | ||
GPIO.setup(LED_PIN, GPIO.LOW) | ||
GPIO.setup(pin, GPIO.OUT) | ||
GPIO.setup(pin, GPIO.LOW) | ||
|
||
led = GPIO.PWM(LED_PIN, 1000) | ||
led = GPIO.PWM(pin, FREQUENCY) | ||
led.start(0) | ||
return led | ||
|
||
|
||
def blinkLED(pin, pause): | ||
def blinkLED(pin, speed: int): | ||
""" | ||
Causes the LED to gradually change brightness (0% -> 100% -> 0%). | ||
:param pin: | ||
:param pause: Integer, the number of seconds to | ||
:return: | ||
:param pin: RPi.GPIO.PWM, representing the pin connected to the LED. | ||
:param speed: The number of seconds the LED takes to blink fully. | ||
""" | ||
DUTY_DELAY = 0.05 | ||
INTERVAL = 1 | ||
MIN_PERCENT = 0 | ||
MAX_PERCENT = 100 | ||
STEPS = (MAX_PERCENT - MIN_PERCENT) / INTERVAL | ||
DUTY_DELAY = speed / (2 * STEPS) | ||
|
||
print("Fire me up Scotty! (Pin 18)") | ||
|
||
for percentage in range(0, 100, 5): | ||
for percentage in range(MIN_PERCENT, MAX_PERCENT, INTERVAL): | ||
pin.ChangeDutyCycle(percentage) | ||
time.sleep(DUTY_DELAY) | ||
|
||
for percentage in range(100, 0, -5): | ||
for percentage in range(MAX_PERCENT, MIN_PERCENT, -INTERVAL): | ||
pin.ChangeDutyCycle(percentage) | ||
time.sleep(DUTY_DELAY) | ||
|
||
print("Power is dry!") | ||
|
||
|
||
def main(): | ||
""" | ||
Entry point of the module. | ||
""" | ||
led = initialisePi() | ||
blinkLED(led, 1) | ||
LED_PIN = 18 | ||
led = initialisePi(LED_PIN) | ||
blinkLED(led, 3) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
""" | ||
Block internet connectivity via arp poisoning. | ||
""" | ||
|
||
import functools | ||
import logging | ||
import logging.config | ||
import shlex | ||
import subprocess | ||
from subprocess import PIPE, Popen | ||
from threading import Timer | ||
|
||
|
||
import schedule | ||
|
||
|
||
def oneTimeJob(func): | ||
""" | ||
Decorator that causes the given scheduled function to run only once. | ||
As a bonus, it also logs the subsequent job to be ran. | ||
NOTE: This decorator suppresses any results returned by the given function. | ||
:param func: function, the function to be wrapped. | ||
:return: function, the wrapped function. | ||
""" | ||
@functools.wraps(func) | ||
def wrapper(*args, **kwargs): | ||
try: | ||
logging.info("Next job at {}.".format(schedule.jobs[1].next_run)) | ||
except IndexError: | ||
# TODO: Test this execution flow! | ||
logging.info("No next job scheduled!") | ||
finally: | ||
func(*args, **kwargs) | ||
return schedule.CancelJob | ||
return wrapper | ||
|
||
|
||
@oneTimeJob | ||
def blockInternet(duration): | ||
""" | ||
For the physical device running this script that is connected to a | ||
network, arp poisons its default gateway on the network for all hosts, | ||
thereby 'suspending' connectivity to WAN (internet) for the given duration. | ||
Pre-requisites for this function to run properly: | ||
1. Install arpspoof on OS: sudo apt-get install dsniff. | ||
2. Run this function using an OS account with root privileges. | ||
:param duration: Integer, the block duration in minutes. | ||
:return: Scheduler.CancelJob, making this function a one-off job (no repeat). | ||
""" | ||
import time | ||
# Fetch network parameters from OS for arp spoofing | ||
print("GOING TO SLEEP!") | ||
|
||
def noob(): | ||
print("STILL SLEEPING") | ||
time.sleep(1) | ||
p1 = subprocess.Popen(shlex.split("ping -t google.com"), shell=True) | ||
# p1.communicate() | ||
|
||
schedule.every().day.at("20:28").do(noob) | ||
print("DONE!") | ||
|
||
while True: | ||
schedule.run_all() | ||
print("WAITING") | ||
time.sleep(1) | ||
|
||
# Arp spoof entire network for a limited duration | ||
# proc = Popen(shlex.split(cmd), stdout=PIPE, stderr=PIPE) | ||
# timer = Timer(seconds, proc.kill) | ||
# logging.info("Blocking internet for {} seconds!".format(seconds)) | ||
# logging.info("Ran the following command to block: '{}'".format(cmd)) | ||
# | ||
# try: | ||
# timer.start() | ||
# finally: | ||
# logging.info("Block time over, unblocking internet now!") | ||
# timer.cancel() | ||
|
||
|
||
def main(): | ||
blockInternet(10) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/bin/bash | ||
|
||
PROGRAM=$0 | ||
|
||
function usage() { | ||
echo -e "\nCOMMAND: '$PROGRAM'" | ||
|
||
echo -e "\nDESCRIPTION: A utility used by project_aroundtheclock to" | ||
echo " temporarily block the internet. This command is invoked by" | ||
echo " the project itself and shouldn't normally be used as a standalone." | ||
|
||
echo -e "\nDEPENDENCIES: dnsniff" | ||
|
||
echo -e "\nUSAGE: $PROGRAM <INTERFACE> <GATEWAY> <DURATION>" | ||
echo " INTERFACE the network interface connected to the blocking network" | ||
echo " GATEWAY the gateway that needs to be blocked temporarily" | ||
echo " DURATION the duration is seconds to block internet" | ||
exit 1 | ||
} | ||
|
||
function blockInternet() { | ||
INTERFACE="$1" | ||
GATEWAY="$2" | ||
DURATION="$3" | ||
|
||
timeout "$DURATION" arpspoof -i "$INTERFACE" "$GATEWAY" | ||
exit 0 | ||
} | ||
|
||
if [ $# -eq 3 ]; then | ||
INTERFACE="$1" | ||
GATEWAY="$2" | ||
DURATION="$3" | ||
|
||
blockInternet "$INTERFACE" "$GATEWAY" "$DURATION" | ||
else | ||
usage | ||
fi |
Oops, something went wrong.