Current Path :
/
proc
/
self
/
root
/
proc
/
self
/
root
/
opt
/
api_ip
/
Or
Select Your Path :
Upload File :
New :
File
Dir
//proc/self/root/proc/self/root/opt/api_ip/api_ip.py
#!/usr/bin/python2.7 from flask import Flask, render_template, request, jsonify, redirect, url_for, g, abort from app.models import User from app import db, app, login_manager import json import logging import subprocess from flask_login import login_required, login_user, current_user, logout_user import os login_manager.login_view = 'login' login_manager.setup_app(app) cur_dir = os.path.dirname(os.path.realpath(__file__)) logger = logging.getLogger("indexfile") logger.setLevel(logging.INFO) fh = logging.FileHandler(cur_dir + "/logs/main.log") formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) # add handler to logger object logger.addHandler(fh) trusted_proxies = ('91.223.223.239', '77.222.144.118', '195.191.25.30') #######authorization########### @login_manager.user_loader def load_user(userid): return db.session.query(User).get(userid) @app.route("/login", methods=["GET", "POST"]) def login(): if current_user.is_authenticated: return redirect(url_for('index')) if request.method == "POST": name = request.form["name"] password = request.form["password"] users = User.query.filter_by(username=name) for user in users: if user.check_password(password): login_user(user) return redirect(url_for('index')) return jsonify({'status': 0, 'error': 'authentication failed'}) ##########before########## @app.before_request def limit_remote_addr(): if request.remote_addr not in trusted_proxies: abort(403) # Forbidden ######################## @app.route('/logout/') @login_required def logout(): logout_user() return redirect(url_for('login')) @app.route('/', methods=["GET", "POST"]) @login_required def index(): return 'auth' @app.route('/api/rebuild_nginx_conf', methods=["GET", "POST"]) @login_required def rebuild_nginx_conf(): data = request.json com = '/scripts/nginxctl rebuildvhost {}'.format(data['domain']) p = subprocess.Popen(com, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stat=1 for i in p.stderr.readlines(): if 'error' in i: stat=0 if stat: com2 = 'service nginx restart' p2 = subprocess.Popen(com, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stat=1 for i in p2.stderr.readlines(): if 'error' in i: stat=0 if stat: return jsonify({'status': 1}) return jsonify({'status': 0}) @app.route('/api/change_mailips', methods=["GET", "POST"]) @login_required def change_mailips(): data = request.json new_list = [] new_list.append(data['domain'] + ': ' + data['ip'] + '\n') with open('/etc/mailips') as f: for line in f.readlines(): new_list.append(line) with open('/etc/mailips', 'w') as f: f.writelines(new_list) com2 = '/scripts/restartsrv_exim' p2 = subprocess.Popen(com2, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if bool(p2.stderr.readlines()) is False: return jsonify({'status': 1}) return jsonify({'status': 0}) @app.route('/api/del_change_mailips', methods=["GET", "POST"]) @login_required def del_change_mailips(): data = request.json new_list = [] with open('/etc/mailips') as f: for line in f.readlines(): if data['domain'] not in line: new_list.append(line) with open('/etc/mailips', 'w') as f: f.writelines(new_list) com2 = '/scripts/restartsrv_exim' p2 = subprocess.Popen(com2, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if bool(p2.stderr.readlines()) is False: return jsonify({'status': 1}) return jsonify({'status': 0}) if __name__ == '__main__': app.run(host='0.0.0.0', port=9999)