def nginx(request, domain=None): """ show nginx detail domain :param request: :param domain: :return: """ try: userId = request.session['userID'] account = Account.objects.get(id=userId) try: provision = Provision.objects.get(domain=domain, deactive_flg=0, account_id=account.id) # create file backup and temp NginxSetting = settingManager.SettingManager( provision.provision_name) if NginxSetting.before_edit_nginx(provision.domain): # read file temp fileSsl = open( settings.URL_NGINX_TEMP + provision.provision_name + '_ssl.conf', 'r') fileHttp = open( settings.URL_NGINX_TEMP + provision.provision_name + '_http.conf', 'r') textSsl = fileSsl.read() textHttp = fileHttp.read() fileSsl.close() fileHttp.close() form_http = NginxHttpForm( initial={ 'domain': provision.domain, 'type': 'http', 'config_content': textHttp }) form_ssl = NginxSslForm( initial={ 'domain': provision.domain, 'type': 'ssl', 'config_content': textSsl }) else: form_ssl = NginxSslForm() form_http = NginxHttpForm() except ObjectDoesNotExist: return HttpResponseRedirect('/settings/listDomain') except KeyError: return HttpResponseRedirect('/login') return render( request, 'phpManager/nginx.html', { 'page_title': 'Nginx Manager', 'provision': provision, 'form_http': form_http, 'form_ssl': form_ssl })
def changeIp(request, waf_id=None): """ change rule allow IP :param request: :param waf_id: :return: """ result = {'status': 0, 'msg': ''} try: transaction.set_autocommit(autocommit=False) userID = request.session['userID'] account = Account.objects.get(pk=userID) waf = Waf.objects.select_related('provision').get( pk=waf_id, type=0, provision__account_id=account.id, provision__deactive_flg=0) Retriction = settingManager.SettingManager( waf.provision.provision_name) if request.POST: form = ChangeIpForm(request.POST) if form.is_valid(): waf.ip = request.POST['new_ip'] waf.save() if Retriction.delete_filterip(waf.url, waf.serial): if Retriction.add_filterip(waf.url, waf.ip, waf.serial): result['status'] = 1 result[ 'msg'] = 'New IP has been updated for configuration.' else: raise ValueError( 'An error occured while trying to update new IP.Can not add Rule new' ) else: raise ValueError( 'An error occured while trying to update new IP.Can not add Rule new' ) else: for error in form.errors.values(): raise ValueError(str(error[0])) transaction.commit() except ObjectDoesNotExist: result['msg'] = 'Record is not exist!' transaction.rollback() except KeyError: return HttpResponseRedirect('/login') except BaseException as e: result['msg'] = str(e) transaction.rollback() return JsonResponse(result)
def changePassword(request, waf_id=None): """ change password authen retric :param request: :param waf_id: :return: """ result = {'status': 0, 'msg': ''} try: transaction.set_autocommit(autocommit=False) userID = request.session['userID'] account = Account.objects.get(pk=userID) waf = Waf.objects.select_related('provision').get( pk=waf_id, type=1, provision__account_id=account.id, provision__deactive_flg=0) Retriction = settingManager.SettingManager( waf.provision.provision_name) if request.POST: form = ChangeAuthForm(request.POST) if form.is_valid(): waf.password = request.POST['new_password'] waf.save() if Retriction.delete_authentication(waf.url, waf.serial): if Retriction.add_authentication(waf.url, waf.user, waf.password, waf.serial): result['status'] = 1 result[ 'msg'] = 'New password has been updated for configuration.' else: raise ValueError( 'An error occured while trying to update new password.Can not add Rule new' ) else: raise ValueError( 'An error occured while trying to update new password.Can not add Rule new!' ) else: raise ValueError('Params is validate! Please check value') transaction.commit() except ObjectDoesNotExist: result['msg'] = 'Record is not exist!' transaction.rollback() except KeyError: return HttpResponseRedirect('/login') except BaseException as e: result['msg'] = str(e) transaction.rollback() return JsonResponse(result)
def saveReba(request, pro_id=None): """ Save Restrict access by authentication configuration :param request: :param pro_id: :return: """ result = {'status': 0, 'msg': ''} try: userID = request.session['userID'] account = Account.objects.get(pk=userID) provision = Provision.objects.get(pk=pro_id, account_id=account.id, deactive_flg=0) if request.POST: try: transaction.set_autocommit(False) form = AuthenRebaForm(request.POST) if form.is_valid(): waf = form.save(commit=False) waf.type = 1 waf.serial = int(datetime.datetime.now().timestamp()) waf.provision_id = provision.id Retriction = settingManager.SettingManager( provision.provision_name) if Retriction.add_authentication(waf.url, waf.user, waf.password, waf.serial): waf.save() result[ 'msg'] = 'You create account access by authentication success!' else: raise ValueError( 'Can not create access by authentication. Please try again!' ) else: for error in form.errors.values(): raise ValueError(str(error[0])) result['status'] = 1 transaction.commit() except BaseException as e: result['msg'] = str(e) transaction.rollback() except ObjectDoesNotExist: result['msg'] = 'Provision is not exist!' except KeyError: return HttpResponseRedirect('/login') return JsonResponse(result)
def configNginx(request): """ Config nginx :param request: :return: """ result = {'status': 0, 'msg': ''} try: userId = request.session['userID'] account = Account.objects.get(id=userId) if request.POST: form = NginxSslForm(request.POST) if form.is_valid(): provision = Provision.objects.get( domain=request.POST['domain'], deactive_flg=0, account_id=account.id) fileConfig = open( settings.URL_NGINX_TEMP + provision.provision_name + '_' + request.POST['type'] + '.conf', 'w') fileConfig.write(request.POST['config_content']) fileConfig.close() NginxSetting = settingManager.SettingManager( provision.provision_name) if NginxSetting.edit_nginx(provision.domain): result['status'] = 1 result[ 'msg'] = "Config nginx domain <b>{}</b> success!".format( provision.domain) else: result['msg'] = "Can not change nginx!Please try again!" else: result['msg'] = "Params is validate! Please check value" result['errors'] = form.errors except KeyError: return HttpResponseRedirect('/login') except BaseException as e: result['msg'] = str(e) return JsonResponse(result)
def deleteRebi(request, waf_id=None): """ Delete Restrict access by ip filter configuration :param request: :param waf_id: :return: """ result = {'status': 0, 'msg': ''} try: transaction.set_autocommit(autocommit=False) userID = request.session['userID'] account = Account.objects.get(pk=userID) waf = Waf.objects.select_related('provision').get( pk=waf_id, type=0, provision__account_id=account.id, provision__deactive_flg=0) Retriction = settingManager.SettingManager( waf.provision.provision_name) if Retriction.delete_filterip(waf.url, waf.serial): waf.delete() transaction.commit() result['status'] = 1 result['msg'] = 'Deleting configuration is completed successfully!' else: raise ValueError('An error ocured while deleting configuration.!') except ObjectDoesNotExist: result['msg'] = 'Record is not exist!' transaction.rollback() except KeyError: return HttpResponseRedirect('/login') except BaseException as e: result['msg'] = str(e) transaction.rollback() return JsonResponse(result)