|
|
|
@ -28,6 +28,7 @@ var (
|
|
|
|
manager *PasswordManager = &PasswordManager{
|
|
|
|
manager *PasswordManager = &PasswordManager{
|
|
|
|
verifiedSessions: make(map[string]time.Time),
|
|
|
|
verifiedSessions: make(map[string]time.Time),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
passwordEnabled bool
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var noInterceptor bool
|
|
|
|
var noInterceptor bool
|
|
|
|
@ -36,6 +37,9 @@ var (
|
|
|
|
whiteListMethods = map[string]bool{
|
|
|
|
whiteListMethods = map[string]bool{
|
|
|
|
"/m9z/password/verify": true,
|
|
|
|
"/m9z/password/verify": true,
|
|
|
|
"/m9z/password/change": true,
|
|
|
|
"/m9z/password/change": true,
|
|
|
|
|
|
|
|
//"/m9z/password/enable": true,
|
|
|
|
|
|
|
|
//"/m9z/password/disable": true,
|
|
|
|
|
|
|
|
"/m9z/password/status": true,
|
|
|
|
// 实时监控
|
|
|
|
// 实时监控
|
|
|
|
"/m9z/touchdisplay/getDeviceID": true,
|
|
|
|
"/m9z/touchdisplay/getDeviceID": true,
|
|
|
|
"/m9z/getDeviceStatus2": true,
|
|
|
|
"/m9z/getDeviceStatus2": true,
|
|
|
|
@ -55,12 +59,14 @@ type PasswordManager struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
mu sync.RWMutex
|
|
|
|
hashedPwd string
|
|
|
|
hashedPwd string
|
|
|
|
salt string
|
|
|
|
salt string
|
|
|
|
|
|
|
|
enabledState int
|
|
|
|
verifiedSessions map[string]time.Time
|
|
|
|
verifiedSessions map[string]time.Time
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type PasswordData struct {
|
|
|
|
type PasswordData struct {
|
|
|
|
HashedPassword string `json:"hashed_password"`
|
|
|
|
HashedPassword string `json:"hashed_password"`
|
|
|
|
Salt string `json:"salt"`
|
|
|
|
Salt string `json:"salt"`
|
|
|
|
|
|
|
|
Enabled int `json:"enabled"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func getPwdFilePath() string {
|
|
|
|
func getPwdFilePath() string {
|
|
|
|
@ -97,6 +103,16 @@ func (m *PasswordManager) loadFromFile() error {
|
|
|
|
|
|
|
|
|
|
|
|
m.hashedPwd = pwdData.HashedPassword
|
|
|
|
m.hashedPwd = pwdData.HashedPassword
|
|
|
|
m.salt = pwdData.Salt
|
|
|
|
m.salt = pwdData.Salt
|
|
|
|
|
|
|
|
m.enabledState = pwdData.Enabled
|
|
|
|
|
|
|
|
switch pwdData.Enabled {
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
|
|
passwordEnabled = true
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
|
|
passwordEnabled = false
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
passwordEnabled = g.Config().Password.Enabled
|
|
|
|
|
|
|
|
m.enabledState = 0
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -107,9 +123,20 @@ func (m *PasswordManager) saveToFile() error {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var enabledState int
|
|
|
|
|
|
|
|
switch {
|
|
|
|
|
|
|
|
case passwordEnabled:
|
|
|
|
|
|
|
|
enabledState = 1
|
|
|
|
|
|
|
|
case !passwordEnabled && m.enabledState != 0:
|
|
|
|
|
|
|
|
enabledState = 2
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
enabledState = m.enabledState
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pwdData := PasswordData{
|
|
|
|
pwdData := PasswordData{
|
|
|
|
HashedPassword: m.hashedPwd,
|
|
|
|
HashedPassword: m.hashedPwd,
|
|
|
|
Salt: m.salt,
|
|
|
|
Salt: m.salt,
|
|
|
|
|
|
|
|
Enabled: enabledState,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
data, err := json.Marshal(pwdData)
|
|
|
|
data, err := json.Marshal(pwdData)
|
|
|
|
@ -126,6 +153,24 @@ func (m *PasswordManager) verify(pwd string) bool {
|
|
|
|
return m.hashedPwd == hashPassword(pwd, m.salt)
|
|
|
|
return m.hashedPwd == hashPassword(pwd, m.salt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (m *PasswordManager) enabled() bool {
|
|
|
|
|
|
|
|
m.mu.RLock()
|
|
|
|
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
|
|
|
|
return passwordEnabled
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (m *PasswordManager) setEnabled(enabled bool) error {
|
|
|
|
|
|
|
|
m.mu.Lock()
|
|
|
|
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
passwordEnabled = enabled
|
|
|
|
|
|
|
|
if enabled {
|
|
|
|
|
|
|
|
m.enabledState = 1
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
m.enabledState = 2
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.saveToFile()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *PasswordManager) change(newPwd string) error {
|
|
|
|
func (m *PasswordManager) change(newPwd string) error {
|
|
|
|
m.mu.Lock()
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
@ -203,11 +248,15 @@ func Init() {
|
|
|
|
g.GVA_LOG.Sugar().Warnf("load password data failed: %v, using default", err)
|
|
|
|
g.GVA_LOG.Sugar().Warnf("load password data failed: %v, using default", err)
|
|
|
|
manager.hashedPwd = hashPassword(DefaultPassword, DefaultSalt)
|
|
|
|
manager.hashedPwd = hashPassword(DefaultPassword, DefaultSalt)
|
|
|
|
manager.salt = DefaultSalt
|
|
|
|
manager.salt = DefaultSalt
|
|
|
|
|
|
|
|
manager.enabledState = 0
|
|
|
|
manager.saveToFile()
|
|
|
|
manager.saveToFile()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
towgo.SetFunc("/m9z/password/verify", verifyPwd)
|
|
|
|
towgo.SetFunc("/m9z/password/verify", verifyPwd)
|
|
|
|
towgo.SetFunc("/m9z/password/change", changePwd)
|
|
|
|
towgo.SetFunc("/m9z/password/change", changePwd)
|
|
|
|
|
|
|
|
towgo.SetFunc("/m9z/password/enable", enablePassword)
|
|
|
|
|
|
|
|
towgo.SetFunc("/m9z/password/disable", disablePassword)
|
|
|
|
|
|
|
|
towgo.SetFunc("/m9z/password/status", getPasswordStatus)
|
|
|
|
towgo.SetFunc("/m9z/system/getTime", getSystemTime)
|
|
|
|
towgo.SetFunc("/m9z/system/getTime", getSystemTime)
|
|
|
|
towgo.SetFunc("/m9z/system/setTime", setSystemTime)
|
|
|
|
towgo.SetFunc("/m9z/system/setTime", setSystemTime)
|
|
|
|
|
|
|
|
|
|
|
|
@ -277,7 +326,7 @@ func changePwd(rpcConn towgo.JsonRpcConnection) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func pwdInterceptor(conn towgo.JsonRpcConnection) error {
|
|
|
|
func pwdInterceptor(conn towgo.JsonRpcConnection) error {
|
|
|
|
if noInterceptor {
|
|
|
|
if noInterceptor || !passwordEnabled {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -365,3 +414,31 @@ func setSystemTime(rpcConn towgo.JsonRpcConnection) {
|
|
|
|
"unix_time": unixTime,
|
|
|
|
"unix_time": unixTime,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func enablePassword(rpcConn towgo.JsonRpcConnection) {
|
|
|
|
|
|
|
|
if err := manager.setEnabled(true); err != nil {
|
|
|
|
|
|
|
|
rpcConn.WriteError(500, "save failed: "+err.Error())
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcConn.WriteResult(map[string]interface{}{
|
|
|
|
|
|
|
|
"success": true,
|
|
|
|
|
|
|
|
"message": "password enabled",
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func disablePassword(rpcConn towgo.JsonRpcConnection) {
|
|
|
|
|
|
|
|
if err := manager.setEnabled(false); err != nil {
|
|
|
|
|
|
|
|
rpcConn.WriteError(500, "save failed: "+err.Error())
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcConn.WriteResult(map[string]interface{}{
|
|
|
|
|
|
|
|
"success": true,
|
|
|
|
|
|
|
|
"message": "password disabled",
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func getPasswordStatus(rpcConn towgo.JsonRpcConnection) {
|
|
|
|
|
|
|
|
rpcConn.WriteResult(map[string]interface{}{
|
|
|
|
|
|
|
|
"enabled": passwordEnabled,
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|