def load_dumped_session(): if check_session(): sess = None with open('/tmp/session.pkl', 'rb') as f: sess = dill.load(f) return util.generate_respons(True, "success", sess) elif check_env(): regenerate_session() return load_dumped_session() else: util.log_err("Loading Session Failed") msg = "Please login first" return util.generate_respons(False, msg)
def check_zone_authorization(dnslist): listed = list_dns() user_dns = listed['data'] returnlist = list() for i in dnslist: if i not in user_dns: print("You are not authorized to access {}".format(i)) else: returnlist.append(i) if not returnlist: return util.generate_respons(True, "Success") else: return util.generate_respons(True, "success", returnlist)
def remove_zone(zone): pbar = tqdm(total=100) pbar.set_description("Obtaining DNS Data") json_send = jsonmodel['rm']['zone']['data'] pbar.update(10) id_zone = ls.get_data('zone', key='id_zone', tags='nm_zone', value=zone) pbar.update(20) id_zone = id_zone['data'][0] pbar.update(10) json_send['remove']['tags']['id_zone'] = id_zone pbar.update(10) try: pbar.set_description("Removing DNS") unsync_send = {"unset": "zone", "data": {"id_zone": id_zone}} res_sync = unsync(unsync_send) if not res_sync["status"]: util.log_err(res_sync['message']) return util.generate_respons(False, "Failure on removing DNS") res = app.send_request('zone', json_send) pbar.update(50) pbar.close() except Exception as e: util.log_err(e) finally: return res
def searchId(endpoint, name): data = dict() data = jsonmodel['search'][endpoint]['data'] url = get_url(endpoint) keys = list(data['where']['tags'].keys()) headers = get_headers() headers = headers['data'] for i in keys: if 'id' not in i: key = i data['where']['tags'][key] = str(name) try: res = requests.post(url=url, data=json.dumps(data), headers=headers) res = res.json() res = res['data'] respons = res[0][get_idkey(endpoint, headers=headers)] except Exception as e: return generate_respons(False, str(e)) return generate_respons(True, 'success', respons)
def send_request(endpoint, data): headers = get_headers() headers = headers['data'] url = get_url(endpoint) try: result = requests.post(url=url, data=json.dumps(data), headers=headers) respons = result.json() except Exception as e: respons = generate_respons(False, str(e)) return respons
def list_dns(): id_zone = get_data('userzone', key='id_zone') if not 'data' in id_zone: return util.generate_respons(False, "You don't own any dns yet") else: id_zone = id_zone['data'] data = jsonmodel['search']['zone']['data'] dnslist = list() try: for i in id_zone: data['where']['tags']['id_zone'] = i temp = config.send_request('zone', data=data) temp = temp['data'][0]['nm_zone'] dnslist.append(temp) except Exception as e: print(str(e)) return util.generate_respons(False, str(e)) return util.generate_respons(True, "success", dnslist)
def load_yaml(filename): if check_yaml(filename): data = None try: with open(("{}/restknot/"+filename).format(DUMP_FOLDER),'r') as f : data = yaml.load(f) return generate_respons(True,'success',data) except Exception as e: return generate_repons(False,str(e)) else: return util.generate_respons(False,"File doesn't exist")
def setDefaultDns(name): header = (get_headers())['data'] header['user_id'] = (get_user_id())['data'] res = requests.post("http://103.89.5.121:6968/api/user/dnscreate", data={'domain': str(name)}, headers=header) res = res.json() if 'code' not in res: sys.stderr.write(res['message']) return generate_respons(False, res['message']) tying_zone(header['user_id'], res['data']['data']['id_zone'])
def syncdat(obj): if obj['sync'] == 'dns': d_json = {"conf-insert": {"tags": {"id_zone": obj['data']['id_zone']}}} elif obj['sync'].upper() == 'SOA': d_json = { "zone-soa-insert": { "tags": { "id_zone": obj['data']['id_zone'] } } } elif obj['sync'].upper() == 'NS': d_json = { "zone-ns-insert": { "tags": { "id_zone": obj['data']['id_zone'] } } } elif obj['sync'] == 'record': r_type = obj['data']['type'] if r_type.upper() == 'SRV': cmd = 'zone-srv-insert' elif r_type.upper() == 'MX': cmd = 'zone-mx-insert' else: cmd = 'zone-insert' d_json = {cmd: {"tags": {"id_record": obj['data']['id_record']}}} try: res = send_request('command', d_json) if res["code"] == 200: return generate_respons(True, "Success") else: return generate_respons(False, "Fail") except Exception as e: print(res) return generate_respons(False, str(e))
def parse_yaml(data): data_list = list() try: for i in data: for j in data[i]: for k in data[i][j]: data_dict = dict() data_dict['--nm-zn'] = i data_dict['--nm'] = j key = list(k.keys()) key = key[0] data_dict['--type'] = key.upper() data_dict['--ttl'] = k[key]['ttl'] data_dict['--nm-con'] = k[key]['content'] data_dict['--date'] = get_time() if 'content-serial' in k[key]: data_dict['--nm-con-ser'] = k[key]['content-serial'] data_list.append(data_dict) idx = len(data_list) - 1 while idx >= 0: if data_list[idx]['--type'] == 'SRV' or data_list[idx][ '--type'] == 'MX': if not '--nm-con-ser' in data_list[idx]: del data_list[idx] else: if '--nm-con-ser' in data_list[idx]: del data_list[idx] idx = idx - 1 respon = generate_respons(True, 'success', data_list) except Exception as e: respon = generate_respons(False, str(e)) finally: return respon
def get_data(endpoint, key=None, tags=None, value=None): headers = auth.get_headers() url = util.get_url(endpoint) try: res = requests.get(url=url, headers=headers['data']) res = res.json() res = res['data'] check = bool(tags) & bool(value) data = list() if key != None and not check: data = list() for i in res: data.append(i[key]) elif key == None and check: data = list() for i in res: if i[tags] == value: data.append(i) elif bool(key) & check: for i in res: if i[tags] == value: data.append(i[key]) else: data = res return util.generate_respons(True, "success", data) except TypeError: util.log_err("DNS is empty") return util.generate_respons(False, "DNS is empty") except Exception as e: util.log_err(str(e)) return util.generate_respons(False, str(e))
def remove_record(records): json_send = jsonmodel['rm']['record']['data'] result = list() pbar = tqdm(total=100) step = (100 / (len(records))) for i in records: pbar.set_description("Removing Record Index - {}".format( records.index(i))) json_send = jsonmodel['rm']['record']['data'] json_send['remove']['tags']['id_record'] = i try: sync_dat = {"unset": "record", "data": {"id_record": i}} ress = unsync(sync_dat) if not ress["status"]: util.log_err(ress["message"]) return util.generate_respons(False, "Failure on zone removal") res = app.send_request('record', json_send) result.append(res) pbar.update(step) except Exception as e: util.log_err(e) pbar.close() return result
def setRecord(obj): from libs.list import check_zone_authorization with open('libs/templates/endpoints.json', 'r') as f : jsonmodel = json.load(f) temp = copy.deepcopy(obj) check = check_zone_authorization([obj['--nm-zn']]) if not check['status']: return generate_respons(True,'Authorization failure') try : data = searchId('zone',obj['--nm-zn']) temp['--id-zone'] = data['data'] data = searchId('type',obj['--type'].upper()) temp['--id-type'] = data['data'] data = searchId('ttl',obj['--ttl']) temp['--id-ttl'] = data['data'] except Exception as e: return generate_respons(False,"Zone/Type/TTL doesn't exist\n" + str(e)) #insert Record json_data = copy.deepcopy(jsonmodel['create']['record']['data']) for i in json_data['insert']['fields']: json_data['insert']['fields'][i] = temp[json_data['insert']['fields'][i]] res = send_request('record',json_data) temp['--id-record'] = res['message']['id'] #insert ttldata json_data = jsonmodel['create']['ttldata']['data'] for i in json_data['insert']['fields']: json_data['insert']['fields'][i] = temp[json_data['insert']['fields'][i]] res = send_request('ttldata',json_data) temp['--id-ttldata'] = res['message']['id'] #insert content json_data = jsonmodel['create']['content']['data'] for i in json_data['insert']['fields']: json_data['insert']['fields'][i] = temp[json_data['insert']['fields'][i]] res = send_request('content',json_data) temp['--id-content'] = res['message']['id'] #insert content serial record_type = obj['--type'].upper() if record_type == 'SRV' or record_type == 'MX': json_data = jsonmodel['create']['content_serial']['data'] for i in json_data['insert']['fields']: json_data['insert']['fields'][i] = temp[json_data['insert']['fields'][i]] res = send_request('content_serial',json_data) temp['--id-content-serial'] = res['message']['id'] f.close() if record_type == 'MX': cmd = 'zone-mx-insert' datasync = {"command" : cmd, "tags" : temp['--id-zone']} elif record_type == 'SRV': cmd = 'zone-srv-insert' datasync = {"command" : cmd, "tags" : temp['--id-zone']} else : cmd = 'zone-insert' datasync = {"command" : cmd, "tags" : temp['--id-record']} try: sync(datasync) except Exception as e: print("Error \n",str(e)) return generate_respons(False,'Sync failure') return generate_respons(True,'success',data)
def setRecord(obj): from libs.listing import check_zone_authorization with open('libs/templates/endpoints.json', 'r') as f: jsonmodel = json.load(f) pbar = tqdm(total=100) pbar.set_description("Preparing Data") temp = copy.deepcopy(obj) check = check_zone_authorization([obj['--nm-zn']]) if not check['status']: return generate_respons(True, 'Authorization failure') try: data = searchId('zone', obj['--nm-zn']) temp['--id-zone'] = data['data'] data = searchId('type', obj['--type'].upper()) temp['--id-type'] = data['data'] data = searchId('ttl', obj['--ttl']) temp['--id-ttl'] = data['data'] except Exception as e: return generate_respons(False, "Zone/Type/TTL doesn't exist\n" + str(e)) pbar.update(20) pbar.set_description("Sending Record") #insert Record json_data = copy.deepcopy(jsonmodel['create']['record']['data']) for i in json_data['insert']['fields']: json_data['insert']['fields'][i] = temp[json_data['insert'] ['fields'][i]] res = send_request('record', json_data) temp['--id-record'] = res['message']['id'] pbar.update(20) pbar.set_description("Sending TTL") #insert ttldata json_data = jsonmodel['create']['ttldata']['data'] for i in json_data['insert']['fields']: json_data['insert']['fields'][i] = temp[json_data['insert'] ['fields'][i]] res = send_request('ttldata', json_data) temp['--id-ttldata'] = res['message']['id'] pbar.update(20) #insert content pbar.set_description("Sending Content Data") json_data = jsonmodel['create']['content']['data'] for i in json_data['insert']['fields']: json_data['insert']['fields'][i] = temp[json_data['insert'] ['fields'][i]] res = send_request('content', json_data) temp['--id-content'] = res['message']['id'] pbar.set_description("Sending Content Data") pbar.update(20) #insert content serial record_type = obj['--type'].upper() if record_type == 'SRV' or record_type == 'MX': json_data = jsonmodel['create']['content_serial']['data'] for i in json_data['insert']['fields']: json_data['insert']['fields'][i] = temp[json_data['insert'] ['fields'][i]] res = send_request('content_serial', json_data) temp['--id-content-serial'] = res['message']['id'] f.close() if record_type == 'SOA' or record_type == 'NS': d_sync = {"sync": record_type, "data": {"id_zone": temp['--id-zone']}} else: d_sync = { "sync": "record", "data": { "type": record_type, "id_record": temp['--id-record'] } } try: pbar.set_description("Sync Data") res = syncdat(d_sync) pbar.update(20) pbar.close() except Exception as e: sys.stderr.write(str(e)) return generate_respons(False, 'Sync failure') return generate_respons(True, 'success', data)
def list_record(dnslist, tag=None): pbar = tqdm(total=100) dnslist = check_zone_authorization(dnslist) if not 'data' in dnslist.keys(): return util.generate_respons(False, "Zone doesn't exist") dnslist = dnslist['data'] dnsdata = list() recorddata = list() #get dns data step = (100 / (3 * len(dnslist))) pbar.set_description("Obtaining DNS") for dns in dnslist: res = get_data("zone", tags="nm_zone", value=dns) res = res['data'][0] dnsdata.append(res) pbar.update(step) #get record data pbar.set_description("Obtaining Record Data") temp = list() for dns in dnsdata: json_send = jsonmodel['search']['record']['data'] json_send['where']['tags']['id_zone'] = dns['id_zone'] res = config.send_request('record', json_send) if 'data' not in res: print('{} dont have any record'.format(str(dns))) else: res = res['data'] for i in res: json_send = jsonmodel['view']['record'] json_send['view']['tags']['id_record'] = i['id_record'] result = config.send_request('record', json_send) result = result['data'][0] recorddata.append({**i, **result}) pbar.update(step) ### GET TTLDATA, CONTENT step = (100 / (6 * len(recorddata))) for record in recorddata: json_send = jsonmodel['view']['ttldata'] json_send['view']['tags']['id_record'] = record['id_record'] res = config.send_request('ttldata', json_send) res = res['data'][0] record.update(res) json_send = jsonmodel['view']['content_data'] json_send['view']['tags']['id_record'] = record['id_record'] res = config.send_request('content', json_send) res = res['data'] st = '' for i in res: st += i['nm_content'] + ' ' record.update({"nm_content": st}) pbar.update(step) for record in recorddata: if record['nm_type'] == 'SRV' or record['nm_type'] == 'MX': json_send = jsonmodel['view']['content_serial'] json_send['view']['tags']['id_record'] = record['id_record'] res = config.send_request('content_serial', json_send) res = res['data'] st = '' for row in res: st += row['nm_content_serial'] record.update({"nm_content_serial": st}) pbar.update(step) idx = len(recorddata) - 1 while idx >= 0: if recorddata[idx]['nm_type'] == 'SOA' or recorddata[idx][ 'nm_type'] == 'NS': del recorddata[idx] idx = idx - 1 pbar.close() if tag is not None: result = filter_record(recorddata, tag) return util.generate_respons(True, 'success', result) else: return util.generate_respons(True, 'success', recorddata)