def blacklist(): scope = ScopeItem.getBlacklist() blacklistSize = current_app.ScopeManager.get_blacklist_size() newForm = forms.NewScopeForm() delForm = forms.ScopeDeleteForm() editForm = forms.ScopeToggleForm() importForm = forms.ImportBlacklistForm() addTagForm = forms.TagScopeForm() addTagForm.tagname.choices = [(row.name, row.name) for row in Tag.query.all()] if newForm.validate_on_submit(): target = ipaddress.ip_network(newForm.target.data, False) newTarget = ScopeItem(target=target.with_prefixlen, blacklist=True) db.session.add(newTarget) db.session.commit() current_app.ScopeManager.update() flash(f"{newTarget.target} blacklisted!", "success") return redirect(url_for("admin.blacklist")) return render_template( "admin/blacklist.html", scope=scope, blacklistSize=blacklistSize, delForm=delForm, editForm=editForm, newForm=newForm, importForm=importForm, addTagForm=addTagForm, )
def import_scope(scopetype=""): if scopetype == "blacklist": importBlacklist = True importForm = forms.ImportBlacklistForm() elif scopetype == "scope": importBlacklist = False importForm = forms.ImportScopeForm() else: return abort(404) if importForm.validate_on_submit(): newScopeItems = importForm.scope.data.split("\n") fail, exist, success = ScopeItem.import_scope_list( newScopeItems, importBlacklist ) db.session.commit() current_app.ScopeManager.update() if success: flash(f"{len(success)} targets added to {scopetype}!", "success") if exist: flash(f"{len(exist)} targets already existed!", "info") if fail: flash(f"{len(fail)} targets failed to import!", "danger") for item in fail: flash(f"{item}", "danger") else: for field, errors in importForm.errors.items(): for error in errors: flash(error, "danger") return redirect(url_for(f"admin.{scopetype}"))
def importScope(scopetype=''): if scopetype == 'blacklist': importBlacklist = True importForm = forms.ImportBlacklistForm() elif scopetype == 'scope': importBlacklist = False importForm = forms.ImportScopeForm() else: abort(404) if importForm.validate_on_submit(): successImports = [] alreadyExists = [] failedImports = [] newScopeItems = importForm.scope.data.split('\n') for item in newScopeItems: item = item.strip() fail, exist, success = ScopeItem.importScope(item, importBlacklist) failedImports = failedImports + fail alreadyExists = alreadyExists + exist successImports = successImports + success db.session.commit() current_app.ScopeManager.update() if len(successImports) > 0: flash('%s targets added to %s!' % (len(successImports), scopetype), 'success') if len(alreadyExists) > 0: flash('%s targets already existed!' % len(alreadyExists), 'info') if len(failedImports) > 0: flash('%s targets failed to import!' % len(failedImports), 'danger') for item in failedImports: flash('%s' % item, 'danger') else: for field, errors in importForm.errors.items(): for error in errors: flash(error, 'danger') return redirect(url_for('admin.%s' % scopetype))