From 7b90b6a7729e9536655f66cd28b3086bcb7577e3 Mon Sep 17 00:00:00 2001 From: R4SAS Date: Fri, 9 Dec 2022 23:09:45 +0000 Subject: [PATCH] Add URL support for paste deletion, rewrite info parsing code Signed-off-by: R4SAS --- pbincli/__init__.py | 2 +- pbincli/actions.py | 49 ++++++++++++++++++++++++---------------- pbincli/cli.py | 55 ++++++++++++++++++++++----------------------- pbincli/utils.py | 7 +++--- 4 files changed, 62 insertions(+), 51 deletions(-) diff --git a/pbincli/__init__.py b/pbincli/__init__.py index c119273..dcde659 100644 --- a/pbincli/__init__.py +++ b/pbincli/__init__.py @@ -2,6 +2,6 @@ # -*- coding: utf-8 -*- __author__ = "R4SAS " -__version__ = "0.3.2" +__version__ = "0.3.3" __copyright__ = "Copyright (c) R4SAS" __license__ = "MIT" diff --git a/pbincli/actions.py b/pbincli/actions.py index bd4801d..2ca0b9a 100644 --- a/pbincli/actions.py +++ b/pbincli/actions.py @@ -1,16 +1,20 @@ -from pbincli.format import Paste -from pbincli.utils import PBinCLIError, validate_url_ending import signal +from urllib.parse import urlparse, parse_qsl + +from pbincli.api import Shortener +from pbincli.format import Paste +from pbincli.utils import PBinCLIError, check_writable, json_encode, uri_validator, validate_url_ending + def signal_handler(sig, frame): print('Keyboard interrupt received, terminating…') exit(0) + signal.signal(signal.SIGINT, signal_handler) def send(args, api_client, settings=None): - from pbincli.api import Shortener if args.short: shortener = Shortener(settings) @@ -117,19 +121,17 @@ def send(args, api_client, settings=None): def get(args, api_client, settings=None): - from pbincli.utils import check_writable, json_encode, uri_validator - - try: - if uri_validator(args.pasteinfo): - api_client.server, pasteinfo = args.pasteinfo.split("?") - pasteid, passphrase = pasteinfo.split("#") - else: - pasteid, passphrase = args.pasteinfo.split("#") - except ValueError: - PBinCLIError("Provided info hasn't contain valid PasteID#Passphrase string") - - if not (pasteid and passphrase): - PBinCLIError("Incorrect request") + parseduri, isuri = uri_validator(args.pasteinfo) + + if isuri and parseduri.query and parseduri.fragment: + api_client.server = args.pasteinfo.split("?")[0] + pasteid = parseduri.query + passphrase = parseduri.fragment + elif parseduri.path and parseduri.path != "/" and parseduri.fragment: + pasteid = parseduri.path + passphrase = parseduri.fragment + else: + PBinCLIError("Provided info hasn't contain valid URL or PasteID#Passphrase string") if args.verbose: print("Used server: {}".format(api_client.getServer())) if args.debug: print("PasteID:\t{}\nPassphrase:\t{}".format(pasteid, passphrase)) @@ -194,10 +196,19 @@ def get(args, api_client, settings=None): def delete(args, api_client, settings=None): - from pbincli.utils import json_encode + parseduri, isuri = uri_validator(args.pasteinfo) - pasteid = args.paste - token = args.token + if isuri: + api_client.server = args.pasteinfo.split("?")[0] + query = dict(parse_qsl(parseduri.query)) + else: + query = dict(parse_qsl(args.pasteinfo)) + + if 'pasteid' in query and 'deletetoken' in query: + pasteid = query['pasteid'] + token = query['deletetoken'] + else: + PBinCLIError("Provided info hasn't contain required information") if args.verbose: print("Used server: {}".format(api_client.getServer())) if args.debug: print("PasteID:\t{}\nToken:\t\t{}".format(pasteid, token)) diff --git a/pbincli/cli.py b/pbincli/cli.py index 1f46f6f..65aaf6e 100755 --- a/pbincli/cli.py +++ b/pbincli/cli.py @@ -36,20 +36,20 @@ def main(): # a send command send_parser = subparsers.add_parser("send", description="Send data to PrivateBin instance") - send_parser.add_argument("-t", "--text", help="text in quotes. Ignored if used stdin. If not used, forcefully used stdin") - send_parser.add_argument("-f", "--file", help="example: image.jpg or full path to file") - send_parser.add_argument("-p", "--password", help="password for encrypting paste") + send_parser.add_argument("-t", "--text", help="Text in quotes. Ignored if used stdin. If not used, forcefully used stdin") + send_parser.add_argument("-f", "--file", help="Example: image.jpg or full path to file") + send_parser.add_argument("-p", "--password", help="Password for encrypting paste") send_parser.add_argument("-E", "--expire", default="1day", action="store", - choices=["5min", "10min", "1hour", "1day", "1week", "1month", "1year", "never"], help="paste lifetime (default: 1day)") - send_parser.add_argument("-B", "--burn", default=False, action="store_true", help="burn sent paste after reading") - send_parser.add_argument("-D", "--discus", default=False, action="store_true", help="open discussion for sent paste") + choices=["5min", "10min", "1hour", "1day", "1week", "1month", "1year", "never"], help="Paste lifetime (default: 1day)") + send_parser.add_argument("-B", "--burn", default=False, action="store_true", help="Set \"Burn after reading\" flag") + send_parser.add_argument("-D", "--discus", default=False, action="store_true", help="Open discussion for sent paste") send_parser.add_argument("-F", "--format", default="plaintext", action="store", - choices=["plaintext", "syntaxhighlighting", "markdown"], help="format of text (default: plaintext)") - send_parser.add_argument("-q", "--notext", default=False, action="store_true", help="don't send text in paste") + choices=["plaintext", "syntaxhighlighting", "markdown"], help="Format of text (default: plaintext)") + send_parser.add_argument("-q", "--notext", default=False, action="store_true", help="Don't send text in paste") send_parser.add_argument("-c", "--compression", default="zlib", action="store", - choices=["zlib", "none"], help="set compression for paste (default: zlib). Note: works only on v2 paste format") + choices=["zlib", "none"], help="Set compression for paste (default: zlib). Note: works only on v2 paste format") ## URL shortener - send_parser.add_argument("-S", "--short", default=False, action="store_true", help="use URL shortener") + send_parser.add_argument("-S", "--short", default=False, action="store_true", help="Use URL shortener") send_parser.add_argument("--short-api", default=argparse.SUPPRESS, action="store", choices=["tinyurl", "clckru", "isgd", "vgd", "cuttly", "yourls", "custom"], help="API used by shortener service") send_parser.add_argument("--short-url", default=argparse.SUPPRESS, help="URL of shortener service API") @@ -59,45 +59,44 @@ def main(): ## Connection options send_parser.add_argument("-s", "--server", default=argparse.SUPPRESS, help="Instance URL (default: https://paste.i2pd.xyz/)") send_parser.add_argument("-x", "--proxy", default=argparse.SUPPRESS, help="Proxy server address (default: None)") - send_parser.add_argument("--no-check-certificate", default=False, action="store_true", help="disable certificate validation") + send_parser.add_argument("--no-check-certificate", default=False, action="store_true", help="Disable certificate validation") send_parser.add_argument("--no-insecure-warning", default=False, action="store_true", - help="suppress InsecureRequestWarning (only with --no-check-certificate)") + help="Suppress InsecureRequestWarning (only with --no-check-certificate)") ## send_parser.add_argument("-L", "--mirrors", default=argparse.SUPPRESS, help="Comma-separated list of mirrors of service with scheme (default: None)") - send_parser.add_argument("-v", "--verbose", default=False, action="store_true", help="enable verbose output") - send_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug output") - send_parser.add_argument("--dry", default=False, action="store_true", help="invoke dry run") - send_parser.add_argument("stdin", help="input paste text from stdin", nargs="?", type=argparse.FileType("r"), default=sys.stdin) + send_parser.add_argument("-v", "--verbose", default=False, action="store_true", help="Enable verbose output") + send_parser.add_argument("-d", "--debug", default=False, action="store_true", help="Enable debug output") + send_parser.add_argument("--dry", default=False, action="store_true", help="Invoke dry run") + send_parser.add_argument("stdin", help="Input paste text from stdin", nargs="?", type=argparse.FileType("r"), default=sys.stdin) send_parser.set_defaults(func=pbincli.actions.send) # a get command get_parser = subparsers.add_parser("get", description="Get data from PrivateBin instance") get_parser.add_argument("pasteinfo", help="\"PasteID#Passphrase\" or full URL") - get_parser.add_argument("-p", "--password", help="password for decrypting paste") + get_parser.add_argument("-p", "--password", help="Password for decrypting paste") ## Connection options get_parser.add_argument("-s", "--server", default=argparse.SUPPRESS, help="Instance URL (default: https://paste.i2pd.xyz/, ignored if URL used in pasteinfo)") get_parser.add_argument("-x", "--proxy", default=argparse.SUPPRESS, help="Proxy server address (default: None)") - get_parser.add_argument("--no-check-certificate", default=False, action="store_true", help="disable certificate validation") + get_parser.add_argument("--no-check-certificate", default=False, action="store_true", help="Disable certificate validation") get_parser.add_argument("--no-insecure-warning", default=False, action="store_true", - help="suppress InsecureRequestWarning (only with --no-check-certificate)") + help="Suppress InsecureRequestWarning (only with --no-check-certificate)") ## - get_parser.add_argument("-v", "--verbose", default=False, action="store_true", help="enable verbose output") - get_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug output") + get_parser.add_argument("-v", "--verbose", default=False, action="store_true", help="Enable verbose output") + get_parser.add_argument("-d", "--debug", default=False, action="store_true", help="Enable debug output") get_parser.set_defaults(func=pbincli.actions.get) # a delete command - delete_parser = subparsers.add_parser("delete", description="Delete paste from PrivateBin instance using token") - delete_parser.add_argument("-p", "--paste", required=True, help="paste id") - delete_parser.add_argument("-t", "--token", required=True, help="paste deletion token") + delete_parser = subparsers.add_parser("delete", description="Delete paste from PrivateBin instance") + delete_parser.add_argument("pasteinfo", help="Paste deletion URL or string in \"pasteid=PasteID&deletetoken=Token\" format") ## Connection options delete_parser.add_argument("-s", "--server", default=argparse.SUPPRESS, help="Instance URL (default: https://paste.i2pd.xyz/)") delete_parser.add_argument("-x", "--proxy", default=argparse.SUPPRESS, help="Proxy server address (default: None)") - delete_parser.add_argument("--no-check-certificate", default=False, action="store_true", help="disable certificate validation") + delete_parser.add_argument("--no-check-certificate", default=False, action="store_true", help="Disable certificate validation") delete_parser.add_argument("--no-insecure-warning", default=False, action="store_true", - help="suppress InsecureRequestWarning (only with --no-check-certificate)") + help="Suppress InsecureRequestWarning (only with --no-check-certificate)") ## - delete_parser.add_argument("-v", "--verbose", default=False, action="store_true", help="enable verbose output") - delete_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug output") + delete_parser.add_argument("-v", "--verbose", default=False, action="store_true", help="Enable verbose output") + delete_parser.add_argument("-d", "--debug", default=False, action="store_true", help="Enable debug output") delete_parser.set_defaults(func=pbincli.actions.delete) # parse arguments diff --git a/pbincli/utils.py b/pbincli/utils.py index 3d8dd7b..a371c84 100644 --- a/pbincli/utils.py +++ b/pbincli/utils.py @@ -36,9 +36,10 @@ def validate_url_ending(s): return s def uri_validator(x): - from urllib.parse import urlparse + from urllib.parse import urlsplit try: - result = urlparse(x) - return all([result.scheme, result.netloc]) + result = urlsplit(x) + isuri = all([result.scheme, result.netloc]) + return result, isuri except ValueError: return False \ No newline at end of file