hch
2021-05-26 1489385312af89acc9bc5f026b823c427ebafeec
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
#!/usr/bin/python
# -*- coding: GBK -*-
#
##@package
#
# @todo: 
#
# @author: Alee
# @date 2019-2-20 ÏÂÎç04:50:04
# @version 1.0
#
# @note: 
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
from bottle import Bottle, request
from lib import mylog
import os
import md5
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import datetime
import subprocess
import urllib
# get: request.query.username request.GET.get('username','')
# post: request.forms.get('username')  request.POST.get('username')
#===============================================================================
# @myapp.route('/cool/kk/:name3/:count#\\d+#')
# def maybe(name3, count):
# client_ip = request.environ.get('REMOTE_ADDR')
# client_ip = request.environ.get('HTTP_X_FORWARDED_FOR') or request.environ.get('REMOTE_ADDR')
#===============================================================================
 
g_AllMapCnt = 0
 
myapp = Bottle()
 
# ¿ÉÖ´Ðй̶¨µÄÔ¶³ÌÃüÁî
 
@myapp.route('/control/openandclose.php', method='POST')
def RemoteCmd():
    dataDict = request.POST
#    for key in dataDict:
#        print key
#        mylog.debug("---%s"%dataDict[key])
    result = dataDict.get("control", "")
    if not result:
        return
    
    sign = dataDict.get("sign", "")
    if not sign:
        return
    
    ofile = open(r".\key.txt")
    text = ofile.read().strip()
    ofile.close()
    if sign != md5.md5("%s1234666%s"%(result, text)).hexdigest():
        mylog.debug("Ç©Ãûʧ°Ü")
        return
    
    if result == "open":
        pResult = os.popen('tasklist /FI "IMAGENAME eq EventServer.exe"')
        pResult = pResult.read()
        if "PID" in pResult or "pid" in pResult:
            return " =======·þÎñÆ÷ÒѾ­¿ªÆô£¬ÇëÔÙ´ÎÈ·ÈÏ·þÎñÆ÷Çé¿ö£¬ EventServer.exeÔËÐÐÖÐ"
        #os.system(r"call D:\ProjectServer\LaunchServerManager\Õý³£¿ª·þ.bat")
        # system call»áµ¼ÖÂ×Ó½ø³ÌÕ¼Óø¸½ø³ÌµÄÎļþÃèÊö·ûµ¼Ö¶˿ÚÎÞ·¨ÊÍ·Å£¬ Ó°ÏìÖØÆôʹÓÃ
        # Èç¹ûÐèÒª±£³Ö×Ó½ø³Ì³ÖÐø³¤ÆÚÔËÐеÄÓ¦¸Ãµ÷Óàsubprocess.Popen(cmd, close_fds=True)
        cmd = r"D:\ProjectServer\LaunchServerManager\Õý³£¿ª·þ.bat"
        subprocess.Popen(cmd, close_fds=True)
        return "open success"
    elif result == "close":
        # ¼ÇµÃ¹Ø·þºó»òÕßÖØÆô»úÆ÷Ðè×Ô¶¯Æô¶¯·þÎñÆ÷¿ØÖÆÌ¨
        os.system(r"call D:\ProjectServer\LaunchServerManager\Õý³£¹Ø·þ.bat")
        return "close success"
    
    return
 
# OnLogError OnPyError
# ·þÎñÆ÷¿ªÆôµÄ״̬»ã±¨
@myapp.route('/control/state.php')
def ServerState():
    global g_AllMapCnt
    try:
        dataDict = request.GET
        stateType = dataDict.get("Type", "")
        if not stateType:
            return
        if stateType == "MapInit":
            # ·þÎñÆ÷¹Ø±ÕµÄʱºò»áÖØÆô£¬´Ë״ֻ̬ÓÃÓÚ¿ª·þµÄʱºòÊÇ·ñ³õʼ»¯OK
            g_AllMapCnt = dataDict.get("MapCount", 0)
            if g_AllMapCnt:
                ifile = open(".\MapCount.txt", "w")
                ifile.write(str(datetime.datetime.today()) + "\t·þÎñÆ÷¿ªÆôÍê±Ï£¬µØÍ¼Êý=%s"%g_AllMapCnt)
                ifile.close()
            #return "·þÎñÆ÷¿ªÆôÍê±Ï£¬µØÍ¼Êý=%s"%dataDict.get("MapCount", 0)
            return
        
        elif stateType in ["MapError", "GameServerError", "PyMongoError"]:
            #groupID=%s&userDBName=%s&Type=PyMongoError
            ServerInfo = "´íÎóÀàÐÍ:%s ·þÎñÆ÷×é:%s, dbname:%s msg:%s"%(stateType,
                                                       dataDict.get("groupID", 0),
                                                       dataDict.get("userDBName", 0),
                                                       urllib.unquote_plus(dataDict.get("MsgInfo", "0")))
            SendEmail(ServerInfo)
            return
        elif stateType == "MapDisconnect":
            # µØÍ¼5·ÖÖÓδÏìÓ¦£¬¿ÉÒÔ¿¨ÁË»òÕßÉÁÍË ¹Ø±Õ
            #groupID=%s&userDBName=%s&Type=PyMongoError
            ServerInfo = "´íÎóÀàÐÍ:µØÍ¼±»¹Ø±Õ%s ·þÎñÆ÷×é:%s, dbname:%s"%(stateType,
                                                       dataDict.get("groupID", 0),
                                                       dataDict.get("userDBName", 0))
            SendEmail(ServerInfo)
            return  
        elif stateType == "GameWarning":
            # ÓÎϷͨÓþ¯¸æÓʼþʹÓÃ
            ServerInfo = "ÓÎÏ·¾¯±¨:·þÎñÆ÷×é:%s, dbname:%s, ¾¯¸æÐÅÏ¢:%s"%(
                                                       dataDict.get("groupID", 0),
                                                       dataDict.get("userDBName", 0),
                                                       urllib.unquote_plus(dataDict.get("MsgInfo", 0)))
            SendEmail(ServerInfo)
        elif stateType in ["MapServerRaiseException", "GameServerRaiseException", "RaiseException"]:
            ServerInfo = "%s Å׳öTryÒì³£±¨´í:·þÎñÆ÷×é:%s, dbname:%s, MapID:%s Òì³£ÐÅÏ¢:%s"%(
                                                       stateType,
                                                       dataDict.get("groupID", 0),
                                                       dataDict.get("userDBName", 0),
                                                       dataDict.get("mapID", 0),
                                                       urllib.unquote_plus(dataDict.get("MsgInfo", 0)))
            SendEmail(ServerInfo)
        elif stateType == "QueryMapOK":
            if g_AllMapCnt == 0:
                if not os.path.exists(".\MapCount.txt"):
                    return "===ÔÝÎÞ·´À¡£¬ÉԵȺó²éѯ==="
                ifile = open(".\MapCount.txt")
                text = ifile.read()
                ifile.close()
                return "=========ÉÏÒ»´Î¼Ç¼µÄ¿ªÆôÇé¿ö, Çë¹ý»áÔÙ²éѯ========", text
            return "·þÎñÆ÷¿ªÆôÍê±Ï£¬µØÍ¼Êý:%s"%g_AllMapCnt
    except Exception, e:
        print e
        ServerInfo = "¿ØÖÆÌ¨±¨´í£¬·þÎñÆ÷Òì³££¡  %s"%e
        try:
            SendEmail(ServerInfo)
        except:
            print "ÎÞ·¨·¢ËÍÓʼþ"
 
# Óʼþ»ã±¨
def SendEmail(ServerInfo):
     
    # µÚÈý·½ SMTP ·þÎñ
    mail_host="smtp.qq.com"  #ÉèÖ÷þÎñÆ÷
    mail_user="2199274165@qq.com"    #Óû§Ãû
    mail_pass="asmizpysxngtdjic"   # ¿ªÍ¨QQÓÊÏäµÄSMTP£¬¶ÌÐÅÑéÖ¤ºó»ñÈ¡µÄ£¬·ÇÃÜÂë
     
     
    sender = '2199274165@qq.com'
    
    # ÕâÀï¿ÉÒÔÌîдÐèÒª½ÓÊջ㱨µÄÓʼþµØÖ·
    receivers = ['305670599@qq.com']#, '1142397645@qq.com', '305670599@qq.com', '995275190@qq.com']  
     
    message = MIMEText('Òì³£»ã±¨:%s'%ServerInfo, 'plain', 'gbk')
    message['From'] = Header("ÓÎÏ··þÎñÆ÷Òì³£»ã±¨", 'gbk')
    message['To'] =  Header("ÔËά", 'gbk')    # ²»·¢Ë͵ϰ»á±»¼Ç¼ΪÀ¬»øÓʼþ
     
    subject = 'ÓÎÏ··þÎñÆ÷Òì³£'
    message['Subject'] = Header(subject, 'gbk')
     
     
 
    smtpObj = smtplib.SMTP() 
    smtpObj.connect(mail_host, 25)    # 25 Îª SMTP ¶Ë¿ÚºÅ£¬ Èç¹ûÓÃSSL ÐèÒª»»¶Ë¿Ú
    smtpObj.login(mail_user,mail_pass)  
    smtpObj.sendmail(sender, receivers, message.as_string())
    print "Óʼþ·¢Ëͳɹ¦"