mirror of
https://github.com/openziti/desktop-edge-win.git
synced 2026-10-15 17:11:19 +00:00
code review changes
This commit is contained in:
+26
-1
@@ -47,6 +47,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/openziti/desktop-edge-win/service/ziti-tunnel/api"
|
||||
"github.com/openziti/desktop-edge-win/service/ziti-tunnel/constants"
|
||||
"github.com/openziti/desktop-edge-win/service/ziti-tunnel/dto"
|
||||
"github.com/openziti/desktop-edge-win/service/ziti-tunnel/util/logging"
|
||||
"net"
|
||||
@@ -245,10 +246,34 @@ func (zid *ZIdentity) Shutdown() {
|
||||
C.uv_async_send((*C.uv_async_t)(unsafe.Pointer(async)))
|
||||
}
|
||||
|
||||
func (zid *ZIdentity) RefreshNeeded() bool {
|
||||
func (zid *ZIdentity) MfaRefreshNeeded() bool {
|
||||
return !(zid.MfaMinTimeout == -1 && zid.MfaMaxTimeout == -1)
|
||||
}
|
||||
|
||||
func (zid *ZIdentity) GetRemainingTime(timeout int32) int32 {
|
||||
var remainingTimeout int32
|
||||
if (timeout - int32(time.Since(zid.MfaLastUpdatedTime).Seconds())) <= 0 {
|
||||
remainingTimeout = 0
|
||||
} else {
|
||||
remainingTimeout = timeout - int32(time.Since(zid.MfaLastUpdatedTime).Seconds())
|
||||
}
|
||||
return remainingTimeout
|
||||
}
|
||||
|
||||
func (zid *ZIdentity) GetMFAState(interval int32) int {
|
||||
var mfaState int
|
||||
if (zid.MfaMaxTimeout > -1) && (zid.MfaMaxTimeout-int32(time.Since(zid.MfaLastUpdatedTime).Seconds())) < 0 {
|
||||
mfaState = constants.MfaAllSvcTimeout
|
||||
} else if (zid.MfaMinTimeout - int32(time.Since(zid.MfaLastUpdatedTime).Seconds())) < 0 {
|
||||
mfaState = constants.MfaFewSvcTimeout
|
||||
} else if (zid.MfaMinTimeout - int32(time.Since(zid.MfaLastUpdatedTime).Seconds())) < interval {
|
||||
mfaState = constants.MfaNearingTimeout
|
||||
} else {
|
||||
mfaState = -1
|
||||
}
|
||||
return mfaState
|
||||
}
|
||||
|
||||
//export doZitiShutdown
|
||||
func doZitiShutdown(async *C.uv_async_t) {
|
||||
ctx := C.ziti_context(async.data)
|
||||
|
||||
@@ -24,4 +24,8 @@ const (
|
||||
Ipv4DefaultMask = 10
|
||||
MinimumFrequency = 5 // minimum notification window
|
||||
MaximumFrequency = 20 // maximum notification window
|
||||
|
||||
MfaAllSvcTimeout = 0
|
||||
MfaFewSvcTimeout = 1
|
||||
MfaNearingTimeout = 2
|
||||
)
|
||||
|
||||
@@ -38,6 +38,7 @@ import (
|
||||
"golang.zx2c4.com/wireguard/tun"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"math/rand"
|
||||
"net"
|
||||
"os"
|
||||
@@ -1135,6 +1136,33 @@ func handleBulkServiceChange(sc cziti.BulkServiceChange) {
|
||||
rts.BroadcastEvent(m)
|
||||
}
|
||||
|
||||
func addUnit(count int, unit string) (result string) {
|
||||
if (count == 1) || (count == 0) {
|
||||
result = strconv.Itoa(count) + " " + unit + " "
|
||||
} else {
|
||||
result = strconv.Itoa(count) + " " + unit + "s "
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func secondsToReadableFmt(input int32) (result string) {
|
||||
seconds := input % (60 * 60 * 24)
|
||||
hours := math.Floor(float64(seconds) / 60 / 60)
|
||||
seconds = input % (60 * 60)
|
||||
minutes := math.Floor(float64(seconds) / 60)
|
||||
seconds = input % 60
|
||||
|
||||
if hours > 0 {
|
||||
result = addUnit(int(hours), "hour") + addUnit(int(minutes), "minute") + addUnit(int(seconds), "second")
|
||||
} else if minutes > 0 {
|
||||
result = addUnit(int(minutes), "minute") + addUnit(int(seconds), "second")
|
||||
} else {
|
||||
result = addUnit(int(seconds), "second")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func handleEvents(isInitialized chan struct{}) {
|
||||
events.run()
|
||||
d := 5 * time.Second
|
||||
@@ -1161,34 +1189,31 @@ func handleEvents(isInitialized chan struct{}) {
|
||||
case <-notificationFrequency.C:
|
||||
cleanNotifications := make([]cziti.NotificationMessage, 0)
|
||||
for _, id := range rts.ids {
|
||||
if id.CId == nil || !id.CId.RefreshNeeded() {
|
||||
if id.CId == nil || !id.CId.MfaRefreshNeeded() {
|
||||
continue
|
||||
}
|
||||
notificationMessage := ""
|
||||
|
||||
var notificationMinTimeout int32 = 0
|
||||
var notificationMaxTimeout int32 = -1
|
||||
if (id.CId.MfaMaxTimeout > -1) && (id.CId.MfaMaxTimeout-int32(time.Since(id.CId.MfaLastUpdatedTime).Seconds())) < 0 {
|
||||
switch mfaState := id.CId.GetMFAState(int32((constants.MaximumFrequency+rts.state.NotificationFrequency)*60)); mfaState {
|
||||
case constants.MfaAllSvcTimeout:
|
||||
notificationMessage = fmt.Sprintf("All of the services of identity %s are timed out", id.Name)
|
||||
} else if (id.CId.MfaMinTimeout - int32(time.Since(id.CId.MfaLastUpdatedTime).Seconds())) < 0 {
|
||||
case constants.MfaFewSvcTimeout:
|
||||
notificationMessage = fmt.Sprintf("Some of the services of identity %s are timed out", id.Name)
|
||||
} else if (id.CId.MfaMinTimeout - int32(time.Since(id.CId.MfaLastUpdatedTime).Seconds())) < int32((constants.MaximumFrequency+rts.state.NotificationFrequency)*60) {
|
||||
notificationMessage = fmt.Sprintf("Some of the services of identity %s are timing out in sometime", id.Name)
|
||||
case constants.MfaNearingTimeout:
|
||||
notificationMinTimeout = id.CId.GetRemainingTime(id.CId.MfaMinTimeout)
|
||||
notificationMessage = fmt.Sprintf("Some of the services of identity %s are timing out in %s", id.Name, secondsToReadableFmt(notificationMinTimeout))
|
||||
default:
|
||||
// do nothing
|
||||
}
|
||||
if id.CId.MfaMaxTimeout > -1 {
|
||||
if (id.CId.MfaMaxTimeout - int32(time.Since(id.CId.MfaLastUpdatedTime).Seconds())) < 0 {
|
||||
notificationMaxTimeout = 0
|
||||
} else {
|
||||
notificationMaxTimeout = id.CId.MfaMaxTimeout - int32(time.Since(id.CId.MfaLastUpdatedTime).Seconds())
|
||||
}
|
||||
}
|
||||
if (id.CId.MfaMinTimeout - int32(time.Since(id.CId.MfaLastUpdatedTime).Seconds())) < 0 {
|
||||
notificationMinTimeout = 0
|
||||
} else {
|
||||
notificationMinTimeout = id.CId.MfaMinTimeout - int32(time.Since(id.CId.MfaLastUpdatedTime).Seconds())
|
||||
}
|
||||
|
||||
if len(notificationMessage) > 0 {
|
||||
|
||||
if id.CId.MfaMaxTimeout > -1 {
|
||||
notificationMaxTimeout = id.CId.GetRemainingTime(id.CId.MfaMaxTimeout)
|
||||
}
|
||||
notificationMinTimeout = id.CId.GetRemainingTime(id.CId.MfaMinTimeout)
|
||||
|
||||
cleanNotifications = append(cleanNotifications, cziti.NotificationMessage{
|
||||
Fingerprint: id.FingerPrint,
|
||||
IdentityName: id.Name,
|
||||
@@ -1247,7 +1272,7 @@ func Clean(src *Id) dto.Identity {
|
||||
//string, ZService
|
||||
val := value.(*cziti.ZService)
|
||||
|
||||
if src.CId.RefreshNeeded() && val.Service.Timeout > 0 {
|
||||
if src.CId.MfaRefreshNeeded() && val.Service.Timeout > 0 {
|
||||
var svcTimeout int32 = -1
|
||||
for _, pc := range val.Service.PostureChecks {
|
||||
if svcTimeout == -1 || svcTimeout > int32(pc.Timeout) {
|
||||
@@ -1267,7 +1292,7 @@ func Clean(src *Id) dto.Identity {
|
||||
})
|
||||
nid.MfaMinTimeout = src.CId.MfaMinTimeout
|
||||
nid.MfaMaxTimeout = src.CId.MfaMaxTimeout
|
||||
if !src.CId.RefreshNeeded() {
|
||||
if !src.CId.MfaRefreshNeeded() {
|
||||
nid.MfaLastUpdatedTime = time.Now()
|
||||
} else {
|
||||
nid.MfaLastUpdatedTime = src.CId.MfaLastUpdatedTime
|
||||
|
||||
Reference in New Issue
Block a user