Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Tkinter import *
- import os, time, sys, subprocess, webbrowser, random, struct, hashlib, pysftp, md5, stat, binascii, MySQLdb, datetime, threading
- from PIL import Image, ImageTk, ImageDraw, ImageFont
- from Crypto.Cipher import AES
- ##FIXES--------------------------------------------------------
- if getattr(sys, 'frozen', False):
- os.chdir(sys._MEIPASS)
- def retag(tag, *args):
- for widget in args:
- widget.bindtags((tag,) + widget.bindtags())
- #--------------------------------------------------------------
- class GUI:
- def __init__(self):
- self.root = Tk()
- self.DB = MySQLdb.connect(host="my host", user="user", passwd="password", db="pyencrypt")
- self.sftp = SFTP()
- self.sql = SQL(self.DB)
- self.Thd = Threads(self.sftp, self.DB)
- self.active_window = None
- self.mainCanvas = None
- self.quit = False
- self.icons = []
- self.gen_icons()
- self.cwd = str(os.environ['USERPROFILE'] + "/").replace("\\", "/")
- self.rcwd = '/DataVolume/shares/Public/encrypt/main/'
- self.LExplorer = None
- self.RExplorer = None
- self.Lcwd = None
- self.Rcwd = None
- self.LC = None
- self.Rscroll = None
- self.LScroll = None
- def quit(self):
- print("quitting")
- self.quit = True
- self.root.destroy()
- self.root.quit()
- def create_window(self):
- if self.active_window != None:
- clear_window()
- window = Canvas(self.mainCanvas, bg="white", relief="groove", highlightbackground="black", highlightcolor="black", highlightthickness=1, borderwidth=4)
- window.place(x=root.winfo_pointerx() - root.winfo_rootx()-100, y=root.winfo_pointery() - root.winfo_rooty())
- windowFrame = Frame(mc)
- windowFrame.pack(fill=BOTH, expand=1, padx= 5, pady=5)
- self.active_window = mc
- def clear_window(self):
- print("clearing Window")
- self.active_window.destroy()
- def gen_icons(self): #icons[notify0, notify1, folder, default]
- NotifyImage0 = Image.open("assets/notifacation0.gif")
- NotifyImage0 = NotifyImage0.resize((32, 32), Image.ANTIALIAS)
- NotifyIcon0 = ImageTk.PhotoImage(NotifyImage0)
- NotifyImage1 = Image.open("assets/notifacation1.gif")
- NotifyImage1 = NotifyImage1.resize((32, 32), Image.ANTIALIAS)
- NotifyIcon1 = ImageTk.PhotoImage(NotifyImage1)
- folderImage = Image.open("assets/folder.gif")
- folderImage = folderImage.resize((32, 32), Image.ANTIALIAS)
- folderIcon = ImageTk.PhotoImage(folderImage)
- defaultImage = Image.open("assets/default.gif")
- defaultImage = defaultImage.resize((32, 32), Image.ANTIALIAS)
- defaultIcon = ImageTk.PhotoImage(defaultImage)
- icons = [NotifyIcon0, NotifyIcon1, folderIcon, defaultIcon]
- self.icons = icons
- def cButton(self, element, borderwidth=0, bg="white", text="", relief="groove", command=None, side=TOP, expand=0, fill=NONE):
- b = Button(element, borderwidth=borderwidth, bg=bg, text=text, relief=relief, command=command)
- b.pack(side=side, expand=expand, fill=fill)
- return b
- def cFrame(self, element, bg="white", borderwidth=0, relief="groove", side=TOP, padx=0, pady=0, expand=0, fill=NONE):
- f = Frame(element, bg=bg, borderwidth=borderwidth, relief=relief)
- f.pack(side=side, padx=padx, pady=pady, expand=expand, fill=fill)
- return f
- def create_main_window(self):
- self.root.config(bg="white")
- self.root.iconbitmap("assets/encryptor.ico")
- self.root.title("pyEncryptor")
- self.root.geometry("850x600")
- mainCanvas = Canvas(self.root)
- #mainCanvas.bind('<Button-1>', test)
- #mainCanvas.bind('<Button-2>', test)
- mainCanvas.pack(fill=BOTH, expand=1)
- menuFrame=self.cFrame(mainCanvas, bg="white", borderwidth=2, relief="groove", padx=2, pady=2, fill=X, side=TOP)
- folderbutton = self.cButton(menuFrame, borderwidth=0, bg="white", side=LEFT, expand=1)
- folderbutton.config(image=self.icons[2], height=32, width=32)
- notifacationbutton = self.cButton(menuFrame, borderwidth=0, bg="white", side=LEFT, expand=1)
- notifacationbutton.config(image=self.icons[0], height=32, width=32)
- LF = self.cFrame(mainCanvas, bg="white", side=TOP, fill=X)
- LLF = self.cFrame(LF, side=LEFT, padx=2, pady=2, expand=1, fill=X)
- RLF = self.cFrame(LF, side=LEFT, padx=2, pady=2, expand=1, fill=X)
- LLabel = Label(LLF, text="Local Files", borderwidth=2, relief="raised", bg="white")
- LLabel.pack(expand=1, fill=X)
- RLabel = Label(RLF, text="Remote Files", borderwidth=2, relief="raised", bg="white")
- RLabel.pack(expand=1, fill=X)
- FrameHolder = self.cFrame(mainCanvas, expand=1, fill=BOTH)
- localFrame = self.cFrame(FrameHolder, bg="white", borderwidth=2, relief="groove", expand=1, padx=2, pady=2, fill=BOTH, side=LEFT)
- remoteFrame = self.cFrame(FrameHolder, bg="white", borderwidth=2, relief="groove", expand=1, padx=2, pady=2, fill=BOTH, side=RIGHT)
- localNav = self.cFrame(localFrame, bg="white", side=TOP, fill=X)
- remoteNav = self.cFrame(remoteFrame, bg="white", side=TOP, fill=X)
- self.Lcwd = Entry(localNav, borderwidth=2, relief="groove", bg="white")
- self.Lcwd.pack(side=LEFT, fill=X, expand=1)
- self.Lcwd.insert(0, self.cwd)
- self.Rcwd = Entry(remoteNav, borderwidth=2, relief="groove", bg="white")
- self.Rcwd.pack(side=LEFT, fill=X, expand=1)
- self.Rcwd.insert(0, self.rcwd)
- lenter = self.cButton(localNav, text="Enter", borderwidth=2, relief="groove", side=RIGHT)
- retag("localenter", lenter)
- lenter.bind_class("localenter", "<Button-1>",lambda e=Event(), side="Local", textvar="" :self.change_dir(e, side, textvar))
- renter = self.cButton(remoteNav, text="Enter", borderwidth=2, relief="groove", side=RIGHT)
- retag("remoteenter", renter)
- renter.bind_class("remoteenter", "<Button-1>", lambda e=Event(), side="Remote", textvar="" :self.change_dir(e, side, textvar))
- LocalFrameExplorer = self.cFrame(localFrame, bg="white", fill=BOTH, expand=1)
- def update_scrollregion(event):
- self.LC.configure(scrollregion=self.LC.bbox("all"))
- LFrame = self.cFrame(LocalFrameExplorer, fill=BOTH, expand=1)
- LFrame.rowconfigure(0, weight=0)
- LFrame.columnconfigure(0, weight=1)
- self.LC = Canvas(LFrame, bg="white")
- self.LC.pack(side=LEFT, expand=1, fill=BOTH)
- self.LExplorer = self.cFrame(self.LC, bg="white", side=LEFT, expand=1, fill=BOTH)
- self.LC.create_window(0, 0, window=self.LExplorer, anchor='nw')
- self.LScroll = Scrollbar(LFrame, orient=VERTICAL)
- self.LScroll.config(command=self.LC.yview)
- self.LC.config(yscrollcommand=self.LScroll.set)
- self.LScroll.pack(side=RIGHT, fill=Y)
- self.LExplorer.bind("<Configure>", update_scrollregion)
- RemoteFrameExplorer = self.cFrame(remoteFrame, bg="white", fill=BOTH, expand=1)
- def update_scrollregion(event):
- RC.configure(scrollregion=RC.bbox("all"))
- RFrame = self.cFrame(RemoteFrameExplorer, fill=BOTH, expand=1)
- RFrame.rowconfigure(0, weight=0)
- RFrame.columnconfigure(0, weight=1)
- RC = Canvas(RFrame, bg="white")
- RC.pack(side=LEFT, expand=1, fill=BOTH)
- self.RExplorer = self.cFrame(RC, bg="white", expand=1, fill=BOTH)
- RC.create_window(0, 0, window=self.RExplorer, anchor='nw')
- self.Rscroll = Scrollbar(RFrame, orient=VERTICAL)
- self.Rscroll.config(command=RC.yview)
- RC.config(yscrollcommand=self.Rscroll.set)
- self.Rscroll.pack(side=RIGHT, fill=Y)
- self.RExplorer.bind("<Configure>", update_scrollregion)
- statusFrame = self.cFrame(mainCanvas, bg="#BBBBBB", borderwidth=2, relief="groove", fill=X, padx=2, pady=2, side=BOTTOM)
- statusLabel = Label(statusFrame, text="Working", bg="#BBBBBB")
- statusLabel.pack(side=LEFT)
- lfolders, lfiles = self.sftp.get_files_folders("Local", self.cwd)
- rfolders, rfiles = self.sftp.get_files_folders("Remote", self.rcwd)
- self.gen_buttons(self.LExplorer, "Local", lfolders, lfiles)
- self.gen_buttons(self.RExplorer, "Remote", rfolders, rfiles)
- def gen_buttons(self, element, side, folders, files):
- colset = 0
- size = (self.root.winfo_width())*2+25
- Packer = self.cFrame(element, bg="white", borderwidth=2, relief=RAISED)
- L= Label(Packer, image=self.icons[2], width=size, anchor=W, justify=LEFT, text="..", bg="white", borderwidth=0, compound=LEFT)
- L.pack(side=TOP, fill=Y)
- Packer.grid(padx=0, pady=5, sticky="w", row=0, column=0)
- retag(side+".back", Packer, L)
- Packer.bind_class(side+".back", "<Button-1>", lambda e=Event(), side=side, textvar=".." :self.change_dir(e, side, textvar))
- rowset = 1
- for i in folders:
- if i[0:1] ==".":
- j = i[1::]
- else:
- j = i
- Packer = self.cFrame(element, bg="white", borderwidth=2, relief=RAISED)
- L= Label(Packer, image=self.icons[2], width=size, anchor=W, justify=LEFT, text=i, bg="white", borderwidth=0, compound=LEFT)
- L.pack(side=TOP, fill=Y)
- Packer.grid(padx=0, pady=5, sticky="w", row=rowset, column=colset)
- retag(j, Packer, L)
- Packer.bind_class(j, "<Button-1>", lambda e=Event(), side=side, textvar=str(i) :self.change_dir(e, side, textvar))
- rowset+=1
- for i in files:
- if i[0:1] ==".":
- j = i[1::]
- else:
- j = i
- Packer = self.cFrame(element, bg="white", borderwidth=2, relief=RAISED)
- L= Label(Packer, image=self.icons[3], width=size, anchor=W, justify=LEFT, text=i, bg="white", borderwidth=0, compound=LEFT)
- L.pack(side=TOP, fill=Y)
- Packer.grid(padx=0, pady=5, sticky="w", row=rowset, column=colset)
- retag(j, Packer, L)
- Packer.bind_class(j, "<Button-1>", lambda e=Event(), side=side, textvar=str(i):createpopup(e, side, textvar))
- rowset+=1
- self.reset_scroll(side)
- def login(self):
- for widget in self.root.winfo_children():
- widget.destroy()
- self.create_main_window()
- def login_window(self):
- self.root.protocol('WM_DELETE_WINDOW', self.quit)
- self.root.iconbitmap("assets/encryptor.ico")
- windowTFrame1= self.cFrame(self.root, side=TOP)
- windowTFrame2= self.cFrame(self.root, side=TOP)
- windowBFrame= self.cFrame(self.root, side=BOTTOM)
- userL = Label(windowTFrame1, text="username:")
- userE = Entry(windowTFrame1)
- passE = Entry(windowTFrame2, show="*")
- passL = Label(windowTFrame2, text="password:")
- userL.pack(side=LEFT)
- userE.pack(side=RIGHT)
- passL.pack(side=LEFT)
- passE.pack(side=RIGHT)
- userE.insert(0, "AceScott")
- passE.insert(0, "AceScott")
- B = self.cButton(windowBFrame, text="Login", command=self.login)
- self.root.mainloop()
- def change_dir(self, event, side, folder):
- if folder == "..":
- if side == "Local":
- cwd = str(self.Lcwd.get())
- tmp= cwd.split("/")
- cwd = ""
- for i in range(len(tmp)-2):
- cwd+=tmp[i]+"/"
- if cwd[-1:] != "/":
- eof = "/"
- elif cwd[-1:] == "/":
- eof = ""
- self.Lcwd.delete(0, END)
- self.Lcwd.insert(0, cwd)
- lfolders, lfiles= self.sftp.get_files_folders("Local", cwd)
- self.gen_buttons(self.LExplorer, "Local", lfolders, lfiles)
- elif side == "Remote":
- cwd = str(self.Rcwd.get())
- tmp= cwd.split("/")
- cwd = ""
- for i in range(len(tmp)-2):
- cwd+=tmp[i]+"/"
- if cwd[-1:] != "/":
- eof = "/"
- elif cwd[-1:] == "/":
- eof = ""
- cwd = cwd+eof
- self.Rcwd.delete(0, END)
- self.Rcwd.insert(0, cwd)
- lfolders, lfiles= self.sftp.get_files_folders("Remote", cwd)
- self.gen_buttons(self.RExplorer, "Remote", lfolders, lfiles)
- else:
- try:
- self.clear_buttons(side)
- if side == "Local":
- if folder == "":
- eof = ""
- elif folder[-1:] != "/":
- eof = "/"
- elif folder[-1:] == "/":
- eof = ""
- cwd = str(self.Lcwd.get())+folder+eof
- self.Lcwd.delete(0, END)
- self.Lcwd.insert(0, cwd)
- lfolders, lfiles= self.sftp.get_files_folders("Local", cwd)
- self.gen_buttons(self.LExplorer, "Local", lfolders, lfiles)
- elif side == "Remote":
- if folder == "":
- eof = ""
- elif folder[-1:] != "/":
- eof = "/"
- elif folder[-1:] == "/":
- eof = ""
- cwd = str(self.Rcwd.get())+folder+eof
- self.Rcwd.delete(0, END)
- self.Rcwd.insert(0, cwd)
- rfolders, rfiles = self.sftp.get_files_folders("Remote", cwd)
- self.gen_buttons(self.RExplorer, "Remote", rfolders, rfiles)
- except OSError as err:
- raise
- self.error("Sorry. There was an issue getting the cwd.\n Please contact your network administrator.\n %s" %err)
- def clear_buttons(self, side):
- if side == "Local":
- element = self.LExplorer
- elif side == "Remote":
- element = self.RExplorer
- list = element.grid_slaves()
- for l in list:
- l.destroy()
- def reset_scroll(self, side):
- if side == "Local":
- self.LScroll.set(0, 0)
- elif side == "Remote":
- self.RScroll.set(0, 0)
- def error(self, err=""):
- window = Toplevel(self.root)
- window.iconbitmap("assets/encryptor.ico")
- windowTFrame= self.cFrame(window, side=TOP)
- L = Label(windowTFrame, text=err, justify=CENTER, bg="white", borderwidth=0)
- L.pack()
- windowBFrame= GUI.cFrame(window, side=BOTTOM)
- OK = self.cButton(windowBFrame, command=lambda w=window:close_window(w), text="Confirm", fg="#07a501" )
- class SFTP:
- def __init__(self):
- cnopts = pysftp.CnOpts()
- cnopts.hostkeys = None
- self.sftp = pysftp.Connection('my host', username='user', password='password', cnopts=cnopts)
- self.sftp_busy = False
- def get_files_folders(self, side, location):
- print("running get_files_folders with location as %s and side of %s" %(location, side))
- files=[]
- folders=[]
- if side == "Local":
- print(location)
- objects = os.listdir(location)
- for i in range(len(objects)):
- objects[i] = str(objects[i])
- for i in objects:
- fileattr = os.lstat(location+i)
- if stat.S_ISDIR(fileattr.st_mode):
- folders.append(i)
- if stat.S_ISREG(fileattr.st_mode):
- files.append(i)
- elif side == "Remote":
- while self.sftp_busy:
- time.sleep(0.1)
- self.sftp_busy = True
- objects = self.sftp.listdir(location)
- self.sftp_busy = False
- for i in range(len(objects)):
- objects[i] = str(objects[i])
- for i in objects:
- fileattr = self.sftp.lstat(location+i)
- if stat.S_ISDIR(fileattr.st_mode):
- folders.append(i)
- if stat.S_ISREG(fileattr.st_mode):
- files.append(i)
- return folders, files
- class SQL:
- def __init__(self, DB):
- self.notifacation = []
- def sql_checklogin(self, user, passwd):
- key2 = ""
- level = 0
- id = 0
- try:
- cur = self.DB.cursor()
- cur.execute("SELECT * FROM users where username = '%s' and password = '%s' LIMIT 1;" %(user, passwd))
- data = cur.fetchall()
- data = data[0]
- id = data[0]
- key2 = data[3]
- level = data[4]
- return True, key2, level, id
- except OSError as err:
- GUI.error("Sorry. There was an issue checking your login.\n Please contact your network administrator.\n %s" %err)
- return False, key2, level, id
- def sql_set(self, location, user, filepass, ownerid):
- try:
- cur = self.DB.cursor()
- cur.execute("INSERT INTO files (id, location, filepass, owner, ownerid) VALUES (NULL, '%s', '%s', '%s', '%s')" %(location, filepass, user, ownerid))
- self.DB.commit()
- return True
- except OSError as err:
- GUI.error("Sorry. There was an issue processing your file.\n Please contact your network administrator.\n %s" %err)
- return False
- def sql_del(self, location, user, ownerid):
- global DB
- try:
- cur = self.DB.cursor()
- cur.execute("DELETE FROM files WHERE location = '%s' AND owner = '%s' AND ownerid = '%s'" %(location, user, ownerid))
- self.DB.commit()
- return True
- except OSError as err:
- GUI.error("Sorry. There was an issue deleting your file.\n Please contact your network administrator.\n %s" %err)
- return False
- def sql_key1(self, location, user, id):
- try:
- cur = self.DB.cursor()
- cur.execute("SELECT * FROM files where location = '%s' and owner = '%s' and ownerid = '%s' LIMIT 1;" %(location, user, id))
- data = cur.fetchall()
- data = data[0]
- key1 = data[2]
- return True, key1
- except OSError as err:
- GUI.error("Sorry. There was an issue getting the key your file.\n Please contact your network administrator.\n %s" %err)
- return False, ""
- except:
- return False, ""
- def sql_share(self, suser, user, ofile, file, password, recieved, sharedate, expiredate):
- try:
- cur = self.DB.cursor()
- cur.execute("INSERT INTO share (id, recuser, sentfrom, location, ofile, password, recieved, sharedate, expiredate, notifie, ack) VALUES (NULL, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', 0, 0)" %(suser, user, file, ofile, password, recieved, sharedate, expiredate))
- self.DB.commit()
- return True
- except OSError as err:
- GUI.error("Sorry. There was an issue sharing your file.\n Please contact your network administrator.\n %s" %err)
- return False
- def sql_notifacation(self):
- tmp = []
- data = None
- print data
- try:
- cur = self.DB.cursor()
- cur.execute("SELECT * FROM share WHERE sentfrom = '%s' AND recieved = 1 and ack = 0;" %(user))
- data = cur.fetchall()
- if len(data) >= 1:
- for i in range(len(data)):
- #0=id 1=recuser 2=sentfrom 3=location 4=ofile 5=password 6=recieved 7=sharedate 8=expiredate 9=notifie 10=ack
- #recuser ofile notify ack
- tmp = (data[i][1], data[i][4], data[i][9], data[i][10])
- self.notifacation.append(tmp)
- return self.notifacation
- except OSError as err:
- GUI.error("Sorry. There was an issue Checking Notifacations.\n Please contact your network administrator.\n %s" %err)
- return self.notifacation
- def sql_whoami(self, location, file):
- try:
- cur = self.DB.cursor()
- cur.execute("SELECT * FROM files where location = '%s' LIMIT 1;" %(location+file))
- data = cur.fetchall()
- data = data[0]
- whouser = data[3]
- GUI.error('The owner of the file "%s" is %s' %(file, whouser))
- except OSError as err:
- GUI.error("Sorry. There was an issue getting the key your file.\n Please contact your network administrator.\n %s" %err)
- class Threads:
- def __init__(self, sftp, DB):
- self.sftp = sftp
- self.DB = DB
- if __name__ == "__main__":
- try:
- app = GUI()
- app.login_window()
- except KeyboardInterrupt:
- quit = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement