Signed-off-by: Jeroen Oudshoorn <oudshoorn.jeroen@gmail.com>
This commit is contained in:
Jeroen Oudshoorn
2023-10-12 17:59:38 +02:00
parent a151465c90
commit 88b688209e
6 changed files with 75 additions and 38 deletions

View File

@ -10,6 +10,7 @@ import os
import pwnagotchi
from pwnagotchi import utils
from pwnagotchi.google import cmd as google_cmd
from pwnagotchi.plugins import cmd as plugins_cmd
from pwnagotchi import log
from pwnagotchi import restart
@ -98,7 +99,7 @@ def do_auto_mode(agent):
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser = plugins_cmd.add_parsers(parser)
parser = google_cmd.add_parsers(parser)
parser2 = google_cmd.add_parsers(parser)
parser.add_argument('-C', '--config', action='store', dest='config', default='/etc/pwnagotchi/default.toml',
help='Main configuration file.')
@ -134,6 +135,9 @@ if __name__ == '__main__':
log.setup_logging(args, config)
rc = plugins_cmd.handle_cmd(args, config)
sys.exit(rc)
elif google_cmd.used_google_cmd(args):
rc = google_cmd.handle_cmd(args)
sys.exit(rc)
if args.version:
print(pwnagotchi.__version__)

View File

View File

@ -1,13 +1,8 @@
# Handles the commandline stuff
import os
import pydrive2
from pydrive2.auth import GoogleAuth
import logging
import glob
import re
import shutil
from fnmatch import fnmatch
from pwnagotchi.utils import download_file, unzip, save_config, parse_version, md5
from pwnagotchi.plugins import default_path
def add_parsers(parser):
@ -16,45 +11,83 @@ def add_parsers(parser):
"""
subparsers = parser.add_subparsers()
# pwnagotchi google
parser_plugins = subparsers.add_parser('google')
plugin_subparsers = parser_plugins.add_subparsers(dest='googlecmd')
parser_google = subparsers.add_parser('google')
google_subparsers = parser_google.add_subparsers(dest='googlecmd')
# pwnagotchi plugins search
parser_plugins_search = plugin_subparsers.add_parser('search', help='Search for pwnagotchi plugins')
parser_plugins_search.add_argument('pattern', type=str, help="Search expression (wildcards allowed)")
parser_google_auth = google_subparsers.add_parser('auth', help='Google Authentication')
parser_google_auth.add_argument('true', type=str, help="This will start the authentication process")
return parser
def used_plugin_cmd(args):
def used_google_cmd(args):
"""
Checks if the plugins subcommand was used
"""
return hasattr(args, 'plugincmd')
return hasattr(args, 'googlecmd')
def handle_cmd(args, config):
def handle_cmd(args):
"""
Parses the arguments and does the thing the user wants
"""
if args.plugincmd == 'update':
return update(config)
elif args.plugincmd == 'search':
args.installed = True # also search in installed plugins
return list_plugins(args, config, args.pattern)
elif args.plugincmd == 'install':
return install(args, config)
elif args.plugincmd == 'uninstall':
return uninstall(args, config)
elif args.plugincmd == 'list':
return list_plugins(args, config)
elif args.plugincmd == 'enable':
return enable(args, config)
elif args.plugincmd == 'disable':
return disable(args, config)
elif args.plugincmd == 'upgrade':
return upgrade(args, config, args.pattern)
elif args.plugincmd == 'edit':
return edit(args, config)
if args.plugincmd == 'auth':
return auth(args)
elif args.plugincmd == 'refresh':
return refresh(args)
raise NotImplementedError()
raise NotImplementedError()
def auth(args):
if args == "true":
# start authentication process
user_input = input("By completing these steps you give pwnagotchi access to your personal Google Drive!\n"
"Personal credentials will be stored only locally for automated verification in the future.\n"
"No one else but you have access to these.\n"
"Do you agree? \n\n[y(es)/n(o)]")
if user_input.lower() in ('y', 'yes'):
try:
gauth = GoogleAuth(settings_file="settings.yaml")
print(gauth.GetAuthUrl())
user_input = input("Please copy this URL into a browser, "
"complete the verification and then copy/paste the code from addressbar.")
gauth.Auth(user_input)
gauth.SaveCredentialsFile("credentials.json")
except Exception as e:
logging.error(f"Error: {e}")
return
def refresh(args):
if int(args):
# refresh token for x amount of time (seconds)
gauth = GoogleAuth(settings_file="settings.yaml")
try:
# Try to load saved client credentials
gauth.LoadCredentialsFile("credentials.json")
except pydrive2.auth.InvalidCredentialsError:
print(gauth.GetAuthUrl())
user_input = input("Please copy this URL into a browser, "
"complete the verification and then copy/paste the code from addressbar.")
gauth.Auth(user_input)
if gauth.access_token_expired:
if gauth.credentials is not None:
try:
# Refresh the token
gauth.Refresh()
except pydrive2.auth.RefreshError:
print(gauth.GetAuthUrl())
user_input = input("Please copy this URL into a browser, "
"complete the verification and then copy/paste the code from addressbar.")
gauth.Auth(user_input)
else:
print(gauth.GetAuthUrl())
user_input = input("Please copy this URL into a browser, "
"complete the verification and then copy/paste the code from addressbar.")
gauth.Auth(user_input)
gauth.Authorize()
gauth.SaveCredentialsFile("credentials.json")
print("No refresh is required.")
return

View File

@ -75,7 +75,7 @@ def handle_cmd(args, config):
if args.plugincmd == 'update':
return update(config)
elif args.plugincmd == 'search':
args.installed = True # also search in installed plugins
args.installed = True # also search in installed plugins
return list_plugins(args, config, args.pattern)
elif args.plugincmd == 'install':
return install(args, config)

View File

@ -51,8 +51,7 @@ class GdriveSync(plugins.Plugin):
self.backup = False
try:
gauth = GoogleAuth()
gauth = GoogleAuth(settings_file="settings.yaml")
gauth.Authorize()
# Create GoogleDrive instance

View File

@ -22,3 +22,4 @@ torch==2.0.1
torchvision==0.15.2
stable_baselines3
RPi.GPIO
pydrive2