#coding: utf-8
import os,sys,json
import io

#设定输出位utf8
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

panelPath = os.getenv('BT_PANEL')
if not panelPath:
    panelPath = "/www/server/panel"

panelPath = "E:/Projects/panel-go"
os.chdir(panelPath)

class_dir = panelPath + "/class"
if not class_dir in sys.path:
    sys.path.insert(0, class_dir)

public_dir = panelPath + "/class/public"
if not public_dir in sys.path:
    sys.path.insert(0, public_dir)

public = __import__('public')
import db

# nlist = ["public.aes_decrypt","public.readFile","public.GetRandomString","public.writeFile","public.returnMsg","public.aes_encrypt","public.url_encode","public.get_path_size","public.to_size","public.ExecShell","public.WriteLog","public.get_database_character","public.format_date","public.M","public.get_plugin_path","public.encrypt","public.ReadFile","public.get_panel_path","public.check_firewall_rule","public.get_url","public.get_error_info","public.to_dict_obj","public.process_exists","public.serviceReload","public.print_log","public.get_page","public.is_free","public.httpPost","public.get_push_info","public.init_msg","public.WriteFile","public.Md5","public.GetNumLines","public.set_module_logs","public.xsssec","public.print__log","public.FileMd5","public.getPanelAddr","public.base64.b64encode","public.dict_obj","public.get_site_php_version","public.get_python_bin","public.set_ownership","public.GetLocalIp","public.httpGet","public.cache_get","public.run_thread","public.get_webserver","public.to_string","public.get_user_info","public.cache_set","public.ReturnMsg","public.mod_reload","public.GetConfigValue","public.ServiceReload","public.get_local_ip","public.get_server_ip","public.get_setup_path","public.gen_password","public.rsa_decrypt","public.path_safe_check","public.is_nginx_process_exists","public.is_httpd_process_exists","public.GetHost","public.version","public.get_php_versions","public.CheckMyCnf","public.getMsg","public.md5","public.public.print_log_log","public.getJson","public.returnResult","public.get_ip","public.CheckPort","public.check_tcp","public.query_dns","public.check_ip","public.get_root_domain","public.get_run_pip","public.bt_print","public.downloadFile","public.xss_version","public.get_os_version","public.getCpuType","public.set_mode","public.get_mode_and_user","public.err_collect","public.is_mysql_process_exists","public.is_redis_process_exists","public.is_pure_ftpd_process_exists","public.is_php_fpm_process_exists","public.is_memcached_process_exists","public.is_mongodb_process_exists","public.IsRestart","public.is_aarch","public.send_dingding","public.send_mail","public.is_ipv6","public.ip2long","public.is_domain","public.phpReload","public.is_process_exists_by_exe","public.HttpGet","public.checkWebConfig","public.CheckCert","public.inArray","public.get_error_onfo","public.get_linux_distribution","public.GetLanguage","public.GetJson","public.get_plugin_info","public.lang","public.return_message","public.GetMsg","public.getSpeed","public.writeSpeed","public.long2ip","public.is_ipv4","public.GetWebServer","public.getDate","public.print_error","public.GetClientIp","public.set_own","public.returnJson","public.get_network_ip","public.PanelError","public.get_requests_headers","public.is_process_exists_by_cmdline","public.get_sys_path","public.trim","public.xssencode2","public.strip","public.get_cron_path","public.get_modules","public.read_config","public.html_decode","public.request_php","public.get_cert_data","public.get_soft_path","public.writeFile2","public.get_firewall_status","public.get_sshd_port","public.get_mysql_info","public.set_tasks_run","public.password_salt","public.SetConfigValue","public.get_panel_log_file","public.get_run_python","public.get_csrf_sess_html_token_value","public.ReturnJson","public.xssdecode","public.error_not_login","public.PanelError: ', '').strip","public.return_status_code","public.exists_args","public.get_error_object","public.get_script_object","public.return_error","public.exec_hook","public.return_data","public.hasPwd","public.get_run_pip","public.readFile","public.writeFile","public.WriteLog","public.ExecShell","public.get_path_size","public.to_size","public.M","public.GetConfigValue","public.format_date","public.GetLocalIp","public.init_msg","public.returnMsg","public.get_url","public.get_pip_list","public.bt_print","public.downloadFile","public.getPanelAddr","public.GetRandomString","public.GetClientIp","public.md5","public.ReadFile","public.serviceReload","public.httpPost","public.format_path","public.get_server_status","public.query_server_config","public.change_server_start_type","public.set_server_status","public.to_path","public.get_dos_disk","public.GetNumLines","public.get_error_info","public.delete_server","public.rmdir","public.copytree","public.move","public.get_page","public.is_free","public.get_run_python","public.httpGet","public.load_module","public.get_soft_path","public.get_mysql_info","public.ServiceReload","public.aes_decrypt","public.aes_encrypt","public.CheckMyCnf","public.getMsg","public.get_panel_path","public.dict_obj","public.query_dns","public.check_ip","public.url_encode","public.get_root_domain","public.create_server","public.kill","public.ReadReg","public.get_database_character","public.get_server_path","public.WriteReg","public.downloadFileByWget","public.set_file_access","public.process_exists","public.ReturnMsg","public.get_webserver","public.Md5","public.get_python_bin","public.get_mode_and_user","public.get_requests_headers","public.set_mode","public.WriteFile","public.is_64bitos","public.set_system_path","public.checkIp","public.get_file_list","public.returnJson","public.getStrBetween","public.GetRandomString1","public.GetRandomString2","public.get_paths","public.getDate","public.mod_reload","public.get_cert_data","public.get_user_info","public.writeFile2","public.version","public.rmtree","public.get_plugin_path","public.get_php_version","public.set_module_logs","public.run_thread","public.html_decode","public.loads_json","public.format_xml","public.dumps_json","public.submit_panel_bug","public.get_sys_version","public.is_ipv6","public.is_ipv4"]

# not_exists = [
#     "strip",
#     "get_sys_version",
#     "exec_hook",
#     "get_script_object",
#     "get_error_object",
#     "error_not_login",
#     "get_csrf_sess_html_token_value",
#     "get_error_onfo",
#     "get_modules",
#     "get_csrf_sess_html_token_value",
#     "error_not_login",
#     "get_php_version",
#     "err_collect",
#     "public"
#     "get_site_php_version",
#     "base64.b64encode",
#     "check_firewall_rule",
#     "encrypt",
#     "public.print_log_log",
#     "print__log"
# ]
# #测试所有函数
# for func in nlist:
#     try:
#         name = func
#         if name.startswith("public."):
#             name = func[7:]

#         name = name.strip()
#         if name in not_exists:
#             continue

#         result = eval(func + "()")
#         print(f"{func}: {result}")
#     except:
#         err = public.get_error_info()
#         # has no attribute
#         if err.find("has no attribute") != -1:
            
#             print(f"{func}: {err}")


def en_punycode(domain):
    """
    中文域名转punycode
    @domain 要转换的域名
    """
    import re
    tmp = domain.lower().split('.')
    newdomain = ''
    for dkey in tmp:
        # 匹配非ascii字符
        match = re.search(u"[\x80-\xff]+", dkey)
        if not match: match = re.search(u"[\u4e00-\u9fa5]+", dkey)
        if not match:
            newdomain += dkey + '.'
        else:
            newdomain += 'xn--' + dkey.encode('punycode').decode('utf-8') + '.'
    return newdomain[0:-1]

#print(en_punycode("测试中文.com"))

sql = db.Sql();
pid = sql.table('databases').where('name=?', ("1111",)).getField('id');

print(pid)
find = sql.table('backup').where('pid=?', (pid,)).find()
print(find)