-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
109 lines (97 loc) · 3.18 KB
/
index.ts
File metadata and controls
109 lines (97 loc) · 3.18 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { Platform, AppState, AppStateStatus } from 'react-native';
import React, { useEffect } from 'react';
import NetInfo from '@react-native-community/netinfo';
import RNPrivacyShieldNative from './nativeModule';
// ---- Native Module Definition ----
export const enableScreenSecurity = () => {
if (Platform.OS === 'android') {
RNPrivacyShieldNative.enableAndroidSecureFlag();
} else if (Platform.OS === 'ios') {
RNPrivacyShieldNative.enableIOSScreenCapturePrevention();
}
};
export const disableScreenSecurity = () => {
if (Platform.OS === 'android') {
RNPrivacyShieldNative.disableAndroidSecureFlag();
} else if (Platform.OS === 'ios') {
RNPrivacyShieldNative.disableIOSScreenCapturePrevention();
}
};
// ---- Redact Multitasking Preview ----
export const useRedactMultitaskPreview = () => {
useEffect(() => {
if (Platform.OS === 'ios') {
RNPrivacyShieldNative.setMultitaskingPreviewRedacted(true);
return () => RNPrivacyShieldNative.setMultitaskingPreviewRedacted(false);
}
}, []);
};
// ---- Insecure Network Detection ----
export const useInsecureNetworkAlert = (onInsecureDetected: () => void) => {
useEffect(() => {
const unsubscribe = NetInfo.addEventListener((state) => {
if (state.isConnected) {
const isUnsafe = state.details?.isConnectionExpensive || !state.isInternetReachable;
if (isUnsafe) onInsecureDetected();
}
});
return () => unsubscribe();
}, []);
};
// ---- Cookie and Tracking Data Cleaner ----
export const clearTrackingData = async (): Promise<void> => {
await RNPrivacyShieldNative.clearCookies();
await RNPrivacyShieldNative.clearLocalStorage();
await RNPrivacyShieldNative.clearTrackingCache();
};
// ---- Secure Logger ----
type LogLevel = 'info' | 'warn' | 'error';
export const secureLog = (message: string, level: LogLevel = 'info', obfuscateKeys: string[] = []) => {
const obfuscated = obfuscateKeys.reduce((msg, key) => {
const regex = new RegExp(`${key}=([^&\s]+)`, 'g');
return msg.replace(regex, `${key}=****`);
}, message);
switch (level) {
case 'info':
console.info(`[PrivacyShield] ${obfuscated}`);
break;
case 'warn':
console.warn(`[PrivacyShield] ${obfuscated}`);
break;
case 'error':
console.error(`[PrivacyShield] ${obfuscated}`);
break;
}
};
// ---- Screen Shield Hook for Sensitive Screens ----
export const useScreenPrivacyShield = () => {
useEffect(() => {
enableScreenSecurity();
return () => disableScreenSecurity();
}, []);
};
// ---- App State Tracking for Auto Privacy ----
export const useAutoShieldOnBackground = () => {
useEffect(() => {
const handleAppStateChange = (nextState: AppStateStatus) => {
if (nextState !== 'active') {
enableScreenSecurity();
} else {
disableScreenSecurity();
}
};
const subscription = AppState.addEventListener('change', handleAppStateChange);
return () => subscription.remove();
}, []);
};
// ---- Exports ----
export default {
enableScreenSecurity,
disableScreenSecurity,
useScreenPrivacyShield,
useRedactMultitaskPreview,
useInsecureNetworkAlert,
clearTrackingData,
secureLog,
useAutoShieldOnBackground,
};