Browse Source

factor out settings.py

pull/6/head
l-n-s 7 years ago
parent
commit
5c8cdc97df
  1. 13
      cli
  2. 29
      pbincli/actions.py
  3. 5
      pbincli/api.py
  4. 11
      pbincli/settings.py

13
cli

@ -1,6 +1,8 @@
#!/usr/bin/env python #!/usr/bin/env python
import os, sys, argparse import os, sys, argparse
import pbincli.actions import pbincli.actions
from pbincli.api import PrivateBin
from pbincli.utils import PBinCLIException from pbincli.utils import PBinCLIException
@ -19,9 +21,6 @@ def main():
choices=["5min", "10min", "1hour", "1day", "1week", "1month", "1year", "never"], help="expiration of paste (default: 1day)") choices=["5min", "10min", "1hour", "1day", "1week", "1month", "1year", "never"], help="expiration of paste (default: 1day)")
send_parser.add_argument("-F", "--format", default="plaintext", action="store", send_parser.add_argument("-F", "--format", default="plaintext", action="store",
choices=["plaintext", "syntaxhighlighting", "markdown"], help="format of text (default: plaintext)") choices=["plaintext", "syntaxhighlighting", "markdown"], help="format of text (default: plaintext)")
send_parser.add_argument("--server", help="Set server to work with")
send_parser.add_argument("--use-proxy", default=False, action="store_true", help="Enable using of proxy")
send_parser.add_argument("--proxy", help="Proxy address (example: socks5://127.0.0.1:9050)")
send_parser.add_argument("-t", "--text", help="comment in quotes. Ignored if used stdin") send_parser.add_argument("-t", "--text", help="comment in quotes. Ignored if used stdin")
send_parser.add_argument("-p", "--password", help="password for encrypting paste") send_parser.add_argument("-p", "--password", help="password for encrypting paste")
send_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug") send_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug")
@ -49,9 +48,15 @@ def main():
# parse arguments # parse arguments
args = parser.parse_args() args = parser.parse_args()
server = os.getenv("PRIVATEBIN_SERVER", "https://paste.i2pd.xyz/")
proxy = os.getenv("PRIVATEBIN_PROXY")
api_client = PrivateBin(server, proxy=proxy)
if hasattr(args, "func"): if hasattr(args, "func"):
try: try:
args.func(args) args.func(args, api_client)
except PBinCLIException as pe: except PBinCLIException as pe:
print("PBinCLI error: {}".format(pe)) print("PBinCLI error: {}".format(pe))
sys.exit(1) sys.exit(1)

29
pbincli/actions.py

@ -1,16 +1,12 @@
import json, hashlib, ntpath, os, sys, zlib import json, hashlib, ntpath, os, sys, zlib
import pbincli.actions, pbincli.settings import pbincli.actions
from sjcl import SJCL from sjcl import SJCL
from base64 import b64encode, b64decode from base64 import b64encode, b64decode
from mimetypes import guess_type from mimetypes import guess_type
from pbincli.api import PrivateBin
from pbincli.utils import PBinCLIException, check_readable, check_writable, json_load_byteified from pbincli.utils import PBinCLIException, check_readable, check_writable, json_load_byteified
# Initialise settings
pbincli.settings.init()
def path_leaf(path): def path_leaf(path):
head, tail = ntpath.split(path) head, tail = ntpath.split(path)
return tail or ntpath.basename(head) return tail or ntpath.basename(head)
@ -25,9 +21,7 @@ def compress(s):
return b64encode(''.join(map(chr, b)).encode('utf-8')) return b64encode(''.join(map(chr, b)).encode('utf-8'))
def send(args): def send(args, api_client):
api = PrivateBin(args.server, args.proxy, args.use-proxy)
if args.stdin: if args.stdin:
text = args.stdin.read() text = args.stdin.read()
elif args.text: elif args.text:
@ -87,8 +81,7 @@ def send(args):
# If we use dry option, exit now # If we use dry option, exit now
if args.dry: sys.exit(0) if args.dry: sys.exit(0)
server = pbincli.settings.server result = api_client.post(request)
result = api.post(request)
if args.debug: print("Response:\t{}\n".format(result)) if args.debug: print("Response:\t{}\n".format(result))
@ -99,7 +92,7 @@ def send(args):
sys.exit(1) sys.exit(1)
if 'status' in result and not result['status']: if 'status' in result and not result['status']:
print("Paste uploaded!\nPasteID:\t{}\nPassword:\t{}\nDelete token:\t{}\n\nLink:\t\t{}?{}#{}".format(result['id'], passphrase.decode(), result['deletetoken'], server, result['id'], passphrase.decode())) print("Paste uploaded!\nPasteID:\t{}\nPassword:\t{}\nDelete token:\t{}\n\nLink:\t\t{}?{}#{}".format(result['id'], passphrase.decode(), result['deletetoken'], api_client.server, result['id'], passphrase.decode()))
elif 'status' in result and result['status']: elif 'status' in result and result['status']:
print("Something went wrong...\nError:\t\t{}".format(result['message'])) print("Something went wrong...\nError:\t\t{}".format(result['message']))
sys.exit(1) sys.exit(1)
@ -108,9 +101,7 @@ def send(args):
sys.exit(1) sys.exit(1)
def get(args): def get(args, api_client):
api = PrivateBin(args.server, args.proxy, args.use-proxy)
pasteid, passphrase = args.pasteinfo.split("#") pasteid, passphrase = args.pasteinfo.split("#")
if pasteid and passphrase: if pasteid and passphrase:
@ -124,7 +115,7 @@ def get(args):
if args.debug: print("Password:\t{}".format(password)) if args.debug: print("Password:\t{}".format(password))
result = api.get(pasteid) result = api_client.get(pasteid)
else: else:
print("PBinCLI error: Incorrect request") print("PBinCLI error: Incorrect request")
sys.exit(1) sys.exit(1)
@ -175,7 +166,7 @@ def get(args):
if 'burnafterreading' in result['meta'] and result['meta']['burnafterreading']: if 'burnafterreading' in result['meta'] and result['meta']['burnafterreading']:
print("Burn afrer reading flag found. Deleting paste...") print("Burn afrer reading flag found. Deleting paste...")
result = api.delete(pasteid, 'burnafterreading') result = api_client.delete(pasteid, 'burnafterreading')
if args.debug: print("Delete response:\t{}\n".format(result)) if args.debug: print("Delete response:\t{}\n".format(result))
@ -202,15 +193,13 @@ def get(args):
sys.exit(1) sys.exit(1)
def delete(args): def delete(args, api_client):
api = PrivateBin(args.server, args.proxy, args.use-proxy)
pasteid = args.paste pasteid = args.paste
token = args.token token = args.token
if args.debug: print("PasteID:\t{}\nToken:\t\t{}".format(pasteid, token)) if args.debug: print("PasteID:\t{}\nToken:\t\t{}".format(pasteid, token))
result = api.delete(pasteid, token) result = api_client.delete(pasteid, token)
if args.debug: print("Response:\t{}\n".format(result)) if args.debug: print("Response:\t{}\n".format(result))

5
pbincli/api.py

@ -1,11 +1,10 @@
import requests import requests
import pbincli.settings
class PrivateBin: class PrivateBin:
def __init__(self, server = pbincli.settings.server, proxy = pbincli.settings.proxy, useproxy = pbincli.settings.useproxy): def __init__(self, server, proxy=None):
self.server = server self.server = server
self.headers = {'X-Requested-With': 'JSONHttpRequest'} self.headers = {'X-Requested-With': 'JSONHttpRequest'}
if useproxy: if proxy:
self.proxy = {proxy.split('://')[0]: proxy} self.proxy = {proxy.split('://')[0]: proxy}
else: else:
self.proxy = {} self.proxy = {}

11
pbincli/settings.py

@ -1,11 +0,0 @@
def init():
global server, proxies, useproxy
# Edit that variables
server = "https://paste.i2pd.xyz/"
proxy = "socks5://127.0.0.1:9050"
# True/False
useproxy = False
# There is nothing more to do :D
Loading…
Cancel
Save