sites.py 7.1 KB
Newer Older
jose's avatar
jose committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
#coding: utf-8
#-------------------------------------------------------------------
# 宝塔Linux面板
#-------------------------------------------------------------------
# Copyright (c) 2015-2017 宝塔软件(http:#bt.cn) All rights reserved.
#-------------------------------------------------------------------
# Author: 黄文良 <287962566@qq.com>
#-------------------------------------------------------------------

#------------------------------
# 网站管理类
#------------------------------
import io,re,public,os,sys,shutil,files
from BTPanel import session
from flask import request
from json import loads,dumps

class sites:
    __isNginx = None
    __nginxVhost = None
    __httpdVhost = None
    __nginxRewrite = None
    __certPath = None
    def __init__(self):
        self.__isNginx = ('nginx' == public.get_webserver())
        self.__nginxVhost = 'vhost/nginx'
        self.__httpdVhost = 'vhost/apache'
        self.__nginxRewrite = 'vhost/rewrite'
        self.__certPath = 'vhost/cert'

    #添加网站
    def create_site(self,get):
        pdata = loads(get['data'])

        #表单验证
        checkRootPath = self.check_root_path(pdata['rootPath'])
        if checkRootPath: return checkRootPath
        domainFormat = self.domain_format(pdata['domains'])
        if domainFormat: return public.ReturnMsg(False,'SITE_DOMAIN_FORMAT_ERR' , (domainFormat,))
        domainExists = self.domain_exists(pdata['domains'])
        if domainExists: return public.ReturnMsg(False,'SITE_DOMAIN_EXIST' , (domainExists,))
        return self._generate_nginx_conf(pdata['siteName'])
        
        
        '''
server
{
    listen 80;
    server_name huang.bt.cn bt001.bt.cn;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/huang.bt.cn;
        
    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
    #error_page 404/404.html;
    #SSL-END
    
    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改
    error_page 404 /404.html;
    error_page 502 /502.html;
    #ERROR-PAGE-END
    
    #PHP-INFO-START  PHP引用配置,可以注释或修改
    include enable-php-70.conf;
    #PHP-INFO-END
    
    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
    include /www/server/panel/vhost/rewrite/huang.bt.cn.conf;
    #REWRITE-END
    
    #禁止访问的文件或目录
    location ~ ^/(\.user\.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README\.md)
    {
        return 404;
    }
    
    #一键申请SSL证书验证目录相关设置
    location ~ \.well-known
    {
        allow all;
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log off;
        access_log off;
    }
    
    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log off;
        access_log off;
    }
    access_log  /www/wwwlogs/huang.bt.cn.log;
    error_log  /www/wwwlogs/huang.bt.cn.error.log;
};'''


        return pdata
        #if pdata['siteName'].find('*') != -1: return public.returnMsg(False,'SITE_ADD_ERR_DOMAIN_TOW');

        



    #删除网站
    def remove_site(self,get):
        pass

    #添加域名
    def add_domain(self,get):
        pass

    #删除域名
    def remove_domain(self,get):
        pass

    #打开SSL
    def open_ssl(self,get):
        pass

    #关闭SSL
    def close_ssl(self,get):
        pass

    #检查指定域名是否存在
    def domain_exists(self,domains):
        if type(domains) == str: domains = [domains]
        sql = public.M('domain');
        for domain in domains:
            tmp = domain.split(':')
            if len(tmp) == 1: tmp.append('80')
            pid = sql.table('domain').where('name=? and port=?',(tmp[0],tmp[1])).getField('pid')
            if pid: 
                if not sql.table('sites').where('id=?',(pid,)).count():
                    sql.table('domain').where('pid=?',(pid,)).delete()
                else:
                    return domain
        return False

    #检查域名格式是否不正确
    def domain_format(self,domains):
        if type(domains) == str: domains = [domains]
        reg = "^([\w\-\*]{1,100}\.){1,8}([\w\-]{1,24}|[\w\-]{1,24}\.[\w\-]{1,24})$";
        for domain in domains:
            if not re.match(reg,domain): return domain
        return False

    #检查指定目录是否不合法
    def check_root_path(self,rootPath):
        if not files.files().CheckDir(rootPath): return public.returnMsg(False,'PATH_ERROR');
        return False

    #重新生成配置文件
    def generate_conf(self,get):
        return self._generate_nginx_conf(get.siteName)

    #生成配置文件
    def _generate_nginx_conf(self,siteName):
        siteInfo = public.M('sites').where('name=?',(siteName,)).field('id,name,path,status').find()
        if siteInfo:
            siteDomains = public.M('domain').where('pid=?',(siteInfo['id'],)).field('name,port').select()
            siteBinding = public.M('binding').where('pid=?',(siteInfo['id'],)).field('domain,port,path').select()
        siteConfig = {
	                    "siteId": 2,
	                    "defaultDoc": "index.php index.html index.htm default.php default.htm default.html",
	                    "fpmConfig": {
		                    "type": "PHP",
		                    "version": "5.4"
	                    },
	                    "ssl": {
		                    "open": False,
		                    "cert": "",
		                    "privateKey": "",
		                    "pool": ""
	                    },

	                    "binDingSsl": {
		                    "/www": {
			                    "open": False,
			                    "cert": "",
			                    "privateKey": "",
			                    "pool": ""
		                    }
	                    },

	                    "redirect": [{
		                    "code": 301,
		                    "var": "$host",
		                    "rule": "^bt.cn$",
		                    "to": "https://www.bt.cn",
		                    "args": "$request_uri"
	                    }, {
		                    "code": 302,
		                    "var": "$uri",
		                    "rule": "^/test/",
		                    "to": "https://www.bt.cn",
		                    "args": "$request_uri"
	                    }],
	                    "proxy": {
		                    "open": False,
		                    "url": "http://www.bt.cn",
		                    "host": "www.bt.cn",
		                    "subOpen": False,
		                    "src": "",
		                    "dst": "",
		                    "cache": False
	                    },
	                    "antiStealingLink": {
		                    "open": False,
		                    "extName": ["jpg", "png", "gif", "js", "css"],
		                    "domains": ["www.bt.cn", "bt.cn"],
		                    "code": 404
	                    },
	                    "networkLimit": {
		                    "open": False,
		                    "perServer": 500,
		                    "perIp": 25,
		                    "rate": 512
	                    }
                    }


        return siteConfig