import os import shutil import subprocess class LliurexGuardEasy: def __init__(self): pass #def __init def load_list_file(self, files): path = '/etc/squid3/lliurex/' lista = range(len(files)) i = 0 for file in files: try: file2read = path + file filereaded = open (file2read, 'r') listreaded= filereaded.readlines() filereaded.close() if len(listreaded) == 0: lista[i]="" else: lista[i]=listreaded i = i + 1 print str(lista) except Exception as e: print str(e) print str(lista) return lista #def load_list_file def set_list_file(self, file, content_list): print(file) print(content_list) path = '/etc/squid3/lliurex/' file2write = path + file print (file2write) filereaded = open (file2write, 'w') for x in content_list: if x != "": filereaded.write(x + "\n") filereaded.close() return 0 #def load_ssl_ports_file def allow_only(self,content_list): pass #def allow_only def create_backup(self,name,desc,type): # Copying files in /etc/squid3/lliurex to /etc/lliurex-guard-easy/templates template_path="/etc/lliurex-guard-easy/templates/" if not os.path.exists(template_path+name): os.makedirs(template_path+name) shutil.copy("/etc/squid3/lliurex/allow-dst-domains.conf", template_path+name) shutil.copy("/etc/squid3/lliurex/allow-SSL-ports.conf", template_path+name) shutil.copy("/etc/squid3/lliurex/deny-dst-domains.conf", template_path+name) shutil.copy("/etc/squid3/lliurex/deny-dst-domains-expr.conf", template_path+name) config = open (template_path+name+".config", 'w') config.write('{\n "template": "'+str(name)+'",\n"desc":"'+str(desc)+'",\n "class":"'+str(type)+'"\n}'); config.close() pass #def create_backup def restore_backup(self,files): pass #def restore_backup def get_templates(self): template_path="/etc/lliurex-guard-easy/templates" templates=[] for i in os.listdir(template_path): if '.config' in i: json_data=open(template_path+"/"+i) data = json.load(json_data) json_data.close() templates.append(json.dumps(data)) return templates def set_template(self, template): # Copying files try: template_path="/etc/lliurex-guard-easy/templates/"+template dest_path="/etc/squid3/lliurex/" #print "copying "+template_path+" to "+dest_path for file in os.listdir(template_path): #print "File: "+file full_file_name = os.path.join(template_path, file) if (os.path.isfile(full_file_name)): shutil.copy(full_file_name, dest_path) except Exception as e: print "Exception: "+str(e) def remove_custom(self, template): try: template_path="/etc/lliurex-guard-easy/templates/"+template shutil.rmtree(template_path) os.remove(template_path+".config") except Exception as e: print "Exception: "+str(e) # #def set_template(self, template): # # Copying files # template_path="/etc/lliurex-guard-easy/templates/"+template # dest_path="/etc/squid3/lliurex/" # # for file in os.listdir(template_path): # full_file_name = os.path.join(template_path, file) # if (os.path.isfile(full_file_name)): # shutil.copy(full_file_name, dest_path) # # pass # # def restart_proxy(self): try: # Check syntax first result=subprocess.check_call(["squid3 -k check 2>/tmp/squid_check_result"], shell=True) try: ret=subprocess.check_call(["service squid3 restart"], shell=True) return {'status':True,'msg':'Success'} except Exception: return {'status':False,'msg':'Unexpected Error'} except Exception: with open ("/tmp/squid_check_result", "r") as myfile: data=myfile.read().replace("\n", "
") return {'status':False,'msg':str(data)} pass #def restart_proxy