1
0
mirror of https://github.com/bitwarden/mobile synced 2025-12-22 19:23:58 +00:00

Use monotonic clock for vault timeout (#1175)

* Use monotonic clock for vault timeout

* free memory

* removed vault timeout timers and added crash logging to iOS clock hack
This commit is contained in:
Matt Portune
2020-12-14 15:29:30 -05:00
committed by GitHub
parent 3227daddaf
commit acf2e4360f
11 changed files with 98 additions and 96 deletions

View File

@@ -430,6 +430,13 @@ namespace Bit.iOS.Core.Services
return false;
}
public long GetActiveTime()
{
// Fall back to UnixTimeSeconds in case this approach stops working. We'll lose clock-change protection but
// the lock functionality will continue to work.
return iOSHelpers.GetSystemUpTimeSeconds() ?? DateTimeOffset.UtcNow.ToUnixTimeSeconds();
}
private void ImagePicker_FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e)
{
if (sender is UIImagePickerController picker)

View File

@@ -1,4 +1,7 @@
using Bit.App.Utilities;
using System;
using System.Runtime.InteropServices;
using Bit.App.Utilities;
using Microsoft.AppCenter.Crashes;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
@@ -7,7 +10,52 @@ namespace Bit.iOS.Core.Utilities
{
public static class iOSHelpers
{
public static System.nfloat? GetAccessibleFont<T>(double size)
[DllImport(ObjCRuntime.Constants.SystemLibrary)]
internal static extern int sysctlbyname([MarshalAs(UnmanagedType.LPStr)] string property, IntPtr output,
IntPtr oldLen, IntPtr newp, uint newLen);
// Returns the difference between when the system was booted and now in seconds, resulting in a duration that
// includes sleep time.
// ref: https://forums.xamarin.com/discussion/20006/access-to-sysctl-h
// ref: https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/src/Platform/XLabs.Platform.iOS/Device/AppleDevice.cs
public static long? GetSystemUpTimeSeconds()
{
long? uptime = null;
IntPtr pLen = default, pStr = default;
try
{
var property = "kern.boottime";
pLen = Marshal.AllocHGlobal(sizeof(int));
sysctlbyname(property, IntPtr.Zero, pLen, IntPtr.Zero, 0);
var length = Marshal.ReadInt32(pLen);
pStr = Marshal.AllocHGlobal(length);
sysctlbyname(property, pStr, pLen, IntPtr.Zero, 0);
var timeVal = Marshal.PtrToStructure<TimeVal>(pStr);
var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
if (timeVal.sec > 0 && now > 0)
{
uptime = now - timeVal.sec;
}
}
catch (Exception e)
{
Crashes.TrackError(e);
}
finally
{
if (pLen != default)
{
Marshal.FreeHGlobal(pLen);
}
if (pStr != default)
{
Marshal.FreeHGlobal(pStr);
}
}
return uptime;
}
public static nfloat? GetAccessibleFont<T>(double size)
{
var pointSize = UIFontDescriptor.PreferredBody.PointSize;
if (size == Device.GetNamedSize(NamedSize.Large, typeof(T)))
@@ -60,5 +108,11 @@ namespace Bit.iOS.Core.Utilities
control, NSLayoutAttribute.Bottom, 1, 10f),
});
}
private struct TimeVal
{
public long sec;
public long usec;
}
}
}