def services(): uploadForm = forms.ServicesUploadForm(prefix="upload-services") addServiceForm = forms.AddServiceForm(prefix="add-service") addServiceForm.serviceProtocol.choices = [("tcp", "TCP"), ("udp", "UDP")] if uploadForm.uploadFile.data and uploadForm.validate_on_submit(): newServicesContent = uploadForm.serviceFile.data.read().decode("utf-8").rstrip('\r\n') newServicesSha = hashlib.sha256(newServicesContent.encode()).hexdigest() if newServicesSha != current_app.current_services["sha256"]: ns = NatlasServices(sha256=newServicesSha, services=newServicesContent) db.session.add(ns) db.session.commit() current_app.current_services = NatlasServices.query.order_by(NatlasServices.id.desc()).first().as_dict() flash("New services file with hash %s has been uploaded." % current_app.current_services["sha256"], "success") else: flash("That file is an exact match for our current services file!", "warning") return redirect(url_for('admin.services')) if addServiceForm.serviceName.data and addServiceForm.validate_on_submit(): newServiceName = addServiceForm.serviceName.data newServicePort = str(addServiceForm.servicePort.data) + '/' + addServiceForm.serviceProtocol.data if '\t' + newServicePort in str(current_app.current_services['services']): flash("A service with port %s already exists!" % newServicePort, "danger") else: newServices = current_app.current_services["services"] + "\n" + newServiceName + "\t" + newServicePort newSha = hashlib.sha256(newServices.encode()).hexdigest() ns = NatlasServices(sha256=newSha, services=newServices) db.session.add(ns) db.session.commit() current_app.current_services = NatlasServices.query.order_by(NatlasServices.id.desc()).first().as_dict() flash("New service %s on port %s has been added." % (newServiceName, newServicePort), "success") return redirect(url_for('admin.services')) return render_template( 'admin/services.html', uploadForm=uploadForm, addServiceForm=addServiceForm, current_services=current_app.current_services, servlist=current_app.current_services['as_list'])
def services(): uploadForm = forms.ServicesUploadForm(prefix="upload-services") addServiceForm = forms.AddServiceForm(prefix="add-service") addServiceForm.serviceProtocol.choices = [("tcp", "TCP"), ("udp", "UDP")] if uploadForm.uploadFile.data and uploadForm.validate_on_submit(): newServicesContent = ( uploadForm.serviceFile.data.read().decode("utf-8").rstrip("\r\n") ) new_services = NatlasServices(services=newServicesContent) if not new_services.hash_equals(current_app.current_services["sha256"]): db.session.add(new_services) db.session.commit() current_app.current_services = new_services.as_dict() flash( f'New services file with hash {current_app.current_services["sha256"]} has been uploaded.', "success", ) else: flash( "That file is an exact match for our current services file!", "warning" ) return redirect(url_for("admin.services")) if addServiceForm.serviceName.data and addServiceForm.validate_on_submit(): newServiceName = addServiceForm.serviceName.data newServicePort = ( str(addServiceForm.servicePort.data) + "/" + addServiceForm.serviceProtocol.data ) if "\t" + newServicePort in str(current_app.current_services["services"]): flash(f"A service with port {newServicePort} already exists!", "danger") else: newServices = ( current_app.current_services["services"] + "\n" + newServiceName + "\t" + newServicePort ) ns = NatlasServices(services=newServices) db.session.add(ns) db.session.commit() current_app.current_services = NatlasServices.get_latest_services() flash( f"New service {newServiceName} on port {newServicePort} has been added.", "success", ) return redirect(url_for("admin.services")) current_services = NatlasServices.get_latest_services() return render_template( "admin/services.html", uploadForm=uploadForm, addServiceForm=addServiceForm, current_services=current_services, servlist=current_services["as_list"], )