Beispiel #1
0
// check users
func checkUsers(users []user.User) {
	for _, user := range users {
		Log.Debug("check user for ", user.GetPort())

		isExists, err := storage.Exists(user)
		if err != nil {
			Log.Error("check exists error: ", err)
			continue
		}
		if !isExists && user.IsEnable() {
			Log.Info("new user to run", user)
			err := storage.StoreUser(user.GetUserInfo())
			if err != nil {
				Log.Error("store  error: ", err)
			}
			go runWithCustomMethod(user)
			continue
		}
		if !user.IsEnable() && isExists {
			Log.Info("user would be disable,port:  ", user.GetPort())
			passwdManager.del(strconv.Itoa(user.GetPort()))
			err := storage.Del(user)
			if err != nil {
				Log.Error(err)
			}
			continue
		}

		if !user.IsEnable() {
			continue
		}

		sUser, err := storage.GetUserInfo(user)
		if err != nil {
			Log.Error("get user error: ", err)
			continue
		}
		if sUser.Passwd != user.GetPasswd() || sUser.Method != user.GetMethod() {
			Log.Info(fmt.Sprintf("user port [%v] passwd or method change ,restart user...", user.GetPort()))
			passwdManager.del(strconv.Itoa(user.GetPort()))
			err := storage.StoreUser(user.GetUserInfo())
			if err != nil {
				Log.Error("store  error: ", err)
			}
			go runWithCustomMethod(user)
		}
	}
}
Beispiel #2
0
// sync users traffic
func syncUsers(users []user.User) {
	for _, user := range users {
		size, err := storage.GetSize(user)
		if err != nil {
			Log.Error(fmt.Sprintf("get size fail for port:%d", user.GetPort()), err)
			continue
		}
		if size < 1024 {
			continue
		}
		err = user.UpdateTraffic(int(size))
		if err != nil {
			Log.Error(fmt.Sprintf("update size fail for port:%d", user.GetPort()), err)
			continue
		}
		Log.Info(fmt.Sprintf("success update traffic usage for port %d,total update size %d", user.GetPort(), size))
		err = storage.SetSize(user, 0)
		if err != nil {
			Log.Error(fmt.Sprintf("set storage size to 0 fail for port:%d", user.GetPort()), err)
			continue
		}
	}
}