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
#!/usr/bin/python
# -*- coding: GBK -*-
#-------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------
#
 
from Common import mylog
import CommonDefine
from datetime import datetime
from uuid import uuid1
import pymongo
import traceback
#from Config import DBConfig
import base64
from DBCommon import error
import ChConfig
 
__state = CommonDefine.mssNone
 
 
def setServerState(newState):
    global __state
    __state = newState
    
def getServerState():
    global __state
    return __state
 
def getAppPath():
    return ChConfig.GetDBPath()
 
 
def GetTransactionIDStr(len):
    nowtime = datetime.today()
    return ("%04d%02d%02d%02d%02d%02d%03d%X-%X-%X-%X%X%X"%(nowtime.timetuple()[:6] + tuple([nowtime.microsecond/1000]) + uuid1().fields))[:len]
 
 
BASE64_ENCODE_CNT = 3
XOR_KEY = 151
def GetEncodePsw(psw):
    ret = ""
    try:
        for i in range(BASE64_ENCODE_CNT):
            psw = base64.decodestring(psw)
    except:
        msg = error.formatMsg('error', error.ERROR_NO_40, '%s'%traceback.format_exc())
        mylog.error(msg)   
        return False,ret
    
    for i in psw:
        ret += chr(ord(i)^XOR_KEY)
    return True, ret
 
 
#µÇ¼Êý¾Ý¿âÑéÖ¤
def LoginMongoDB(user, pwd, ip = '', port = '', connection = None ):
    try:
        if not connection:
            connection = pymongo.Connection(ip, port, auto_start_request=False)
        db = connection.admin
        return db.authenticate(user, pwd) 
    except:
        mylog.info('Login mongodb failed:user = %s,pwd = %s'%(user, pwd))
        msg = error.formatMsg('error', error.ERROR_NO_41, '%s'%traceback.format_exc())
        mylog.error(msg)   
        return False