Skip to content

Commit

Permalink
update mock context to match new inproxy module
Browse files Browse the repository at this point in the history
add missing guava dependency to android build.gradle
  • Loading branch information
tmgrask committed Sep 25, 2024
1 parent cdc36c2 commit 729eea6
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 95 deletions.
3 changes: 3 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ dependencies {
// Psiphon lib
implementation (name: 'ca.psiphon', ext: 'aar')

// Guava (need for ListenableFuture in the Worker related code)
implementation 'com.google.guava:guava:33.0.0-android'

// Autovalue
compileOnly "com.google.auto.value:auto-value-annotations:1.10.4"
annotationProcessor "com.google.auto.value:auto-value:1.10.4"
Expand Down
8 changes: 3 additions & 5 deletions src/account/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ import {
} from "@/src/constants";
// TODO: pending new psiphon module
//import { useInProxyContext } from "@/src/psiphon/context";
import {
InProxyParametersSchema,
formatConduitBip32Path,
} from "@/src/psiphon/inproxy";
import { useInProxyContext } from "@/src/psiphon/mockContext";
import { useInProxyContext } from "@/src/inproxy/mockContext";
import { InProxyParametersSchema } from "@/src/inproxy/types";
import { formatConduitBip32Path } from "@/src/inproxy/utils";

export interface AccountContextValue {
rootKeyPair: Ed25519KeyPair;
Expand Down
2 changes: 1 addition & 1 deletion src/app/(app)/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useAuthContext } from "@/src/auth/context";
import {
InProxyActivityProvider,
InProxyProvider,
} from "@/src/psiphon/mockContext";
} from "@/src/inproxy/mockContext";

export default function AppLayout() {
const { mnemonic, deviceNonce } = useAuthContext();
Expand Down
14 changes: 7 additions & 7 deletions src/components/ConduitOrbToggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
import {
useInProxyActivityContext,
useInProxyContext,
} from "@/src/psiphon/mockContext";
} from "@/src/inproxy/mockContext";
import { palette, sharedStyles as ss } from "@/src/styles";

export function ConduitOrbToggle({ size }: { size: number }) {
Expand Down Expand Up @@ -137,12 +137,12 @@ export function ConduitOrbToggle({ size }: { size: number }) {
// play in initial animation and video
const [showVideo, setShowVideo] = React.useState(false);
React.useEffect(() => {
const inProxyStatus = getInProxyStatus().status;
if (inProxyStatus === "running") {
const inProxyStatus = getInProxyStatus();
if (inProxyStatus === "RUNNING") {
// Already Running: play intro animation without delay
setShowVideo(false);
animateIntro(0);
} else if (inProxyStatus === "stopped") {
} else if (inProxyStatus === "STOPPED") {
// Stopped: play intro video and delay animation
setShowVideo(true);
animateIntro(2800);
Expand All @@ -152,8 +152,8 @@ export function ConduitOrbToggle({ size }: { size: number }) {

// set animation state based on InProxy state
React.useEffect(() => {
const inProxyStatus = getInProxyStatus().status;
if (inProxyStatus === "running") {
const inProxyStatus = getInProxyStatus();
if (inProxyStatus === "RUNNING") {
if (inProxyCurrentConnectedClients === 0) {
if (animationState !== "announcing") {
animateAnnouncing();
Expand All @@ -166,7 +166,7 @@ export function ConduitOrbToggle({ size }: { size: number }) {
}
randomizeVelocity.setActive(true);
}
} else if (inProxyStatus === "stopped") {
} else if (inProxyStatus === "STOPPED") {
randomizeVelocity.setActive(false);
if (!["idle", "loading"].includes(animationState)) {
animateTurnOff();
Expand Down
11 changes: 6 additions & 5 deletions src/components/ConduitSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import { handleError, wrapError } from "@/src/common/errors";
import { MBToBytes, bytesToMB } from "@/src/common/utils";
import { EditableNumberSlider } from "@/src/components/EditableNumberSlider";
import { ProxyID } from "@/src/components/ProxyID";
import { InProxyParametersSchema, getProxyId } from "@/src/psiphon/inproxy";
import { useInProxyContext } from "@/src/psiphon/mockContext";
import { useInProxyContext } from "@/src/inproxy/mockContext";
import { InProxyParametersSchema } from "@/src/inproxy/types";
import { getProxyId } from "@/src/inproxy/utils";
import { lineItemStyle, palette, sharedStyles as ss } from "@/src/styles";
import {
useDerivedValue,
Expand Down Expand Up @@ -121,7 +122,7 @@ export function ConduitSettings() {
settingsChanged = true;
}
if (settingsChanged) {
if (getInProxyStatus().status === "running") {
if (getInProxyStatus() === "RUNNING") {
setDisplayRestartConfirmation(true);
} else {
await commitChanges();
Expand Down Expand Up @@ -328,10 +329,10 @@ export function ConduitSettings() {
const fadeIn = useSharedValue(0);
React.useEffect(() => {
const inProxyStatus = getInProxyStatus();
if (inProxyStatus.status === "running") {
if (inProxyStatus === "RUNNING") {
// fade in right away
fadeIn.value = withTiming(1, { duration: 2000 });
} else if (inProxyStatus.status === "stopped") {
} else if (inProxyStatus === "STOPPED") {
// fade in after a delay for particle animation
fadeIn.value = withDelay(2800, withTiming(1, { duration: 2000 }));
}
Expand Down
12 changes: 6 additions & 6 deletions src/components/ConduitStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { niceBytes } from "@/src/common/utils";
import {
useInProxyActivityContext,
useInProxyContext,
} from "@/src/psiphon/mockContext";
} from "@/src/inproxy/mockContext";
import { palette, sharedStyles as ss } from "@/src/styles";

export function ConduitStatus({
Expand Down Expand Up @@ -48,9 +48,9 @@ export function ConduitStatus({
const fadeInGradient = useSharedValue(0);
React.useEffect(() => {
const inProxyStatus = getInProxyStatus();
if (inProxyStatus.status === "running") {
if (inProxyStatus === "RUNNING") {
fadeInGradient.value = withTiming(1, { duration: 2000 });
} else if (inProxyStatus.status === "stopped") {
} else if (inProxyStatus === "STOPPED") {
fadeInGradient.value = withDelay(
2800,
withTiming(1, { duration: 2000 }),
Expand All @@ -64,14 +64,14 @@ export function ConduitStatus({
const [shouldAnimateIn, setShouldAnimateIn] = React.useState(true);
const [shouldAnimateOut, setShouldAnimateOut] = React.useState(true);
React.useEffect(() => {
const inProxyStatus = getInProxyStatus().status;
if (inProxyStatus === "running") {
const inProxyStatus = getInProxyStatus();
if (inProxyStatus === "RUNNING") {
if (shouldAnimateIn) {
fader.value = withTiming(1, { duration: 1000 });
setShouldAnimateIn(false);
setShouldAnimateOut(true);
}
} else if (inProxyStatus === "stopped") {
} else if (inProxyStatus === "STOPPED") {
if (shouldAnimateOut) {
fader.value = withTiming(0, { duration: 1000 });
setShouldAnimateIn(true);
Expand Down
6 changes: 3 additions & 3 deletions src/components/LogoWordmark.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {

// @ts-ignore (this file is gitignored)
import { GIT_HASH } from "@/src/git-hash";
import { useInProxyContext } from "@/src/psiphon/mockContext";
import { useInProxyContext } from "@/src/inproxy/mockContext";
import { palette, sharedStyles as ss } from "@/src/styles";

export function LogoWordmark({
Expand All @@ -43,10 +43,10 @@ export function LogoWordmark({
const fadeIn = useSharedValue(0);
React.useEffect(() => {
const inProxyStatus = getInProxyStatus();
if (inProxyStatus.status === "running") {
if (inProxyStatus === "RUNNING") {
// fade in right away
fadeIn.value = withTiming(1, { duration: 2000 });
} else if (inProxyStatus.status === "stopped") {
} else if (inProxyStatus === "STOPPED") {
// fade in after a delay for particle animation
fadeIn.value = withDelay(2800, withTiming(1, { duration: 2000 }));
}
Expand Down
File renamed without changes.
54 changes: 30 additions & 24 deletions src/psiphon/mockContext.tsx → src/inproxy/mockContext.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import AsyncStorage from "@react-native-async-storage/async-storage";
import React from "react";
import { z } from "zod";

import {
InProxyActivityByPeriod,
InProxyActivityStats,
InProxyParameters,
InProxyStatus,
InProxyStatusSchema,
InProxyStatusEnum,
InProxyStatusEnumSchema,
} from "@/src/inproxy/types";
import {
getDefaultInProxyParameters,
getZeroedInProxyActivityStats,
} from "@/src/psiphon/inproxy";
import { z } from "zod";
} from "@/src/inproxy/utils";

const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
async function* generateMockData(
Expand Down Expand Up @@ -136,7 +138,7 @@ export function InProxyActivityProvider({
}
}

if (getInProxyStatus().status === "running") {
if (getInProxyStatus() === "RUNNING") {
runMock();
} else {
stopMock();
Expand All @@ -160,7 +162,7 @@ export interface InProxyContextValue {
inProxyMustUpgrade: boolean;
toggleInProxy: () => Promise<void>;
selectInProxyParameters: (params: InProxyParameters) => Promise<void>;
getInProxyStatus: () => InProxyStatus;
getInProxyStatus: () => InProxyStatusEnum;
sendFeedback: () => Promise<void>;
}

Expand All @@ -179,17 +181,18 @@ export function useInProxyContext(): InProxyContextValue {
return value;
}

const InProxyStateSchema = z.object({
const InProxyStateSyncSchema = z.object({
running: z.boolean(),
synced: z.boolean(),
});
type InProxyState = z.infer<typeof InProxyStateSchema>;
type InProxyStateSync = z.infer<typeof InProxyStateSyncSchema>;

export function InProxyProvider({ children }: { children: React.ReactNode }) {
const [inProxyState, setInProxyState] = React.useState<InProxyState>({
running: false,
synced: false,
});
const [inProxyStateSync, setInProxyStateSync] =
React.useState<InProxyStateSync>({
running: false,
synced: false,
});
const [inProxyParameters, setInProxyParameters] =
React.useState<InProxyParameters>(getDefaultInProxyParameters());

Expand All @@ -213,12 +216,12 @@ export function InProxyProvider({ children }: { children: React.ReactNode }) {
const running = await AsyncStorage.getItem("MockInProxyRunning");
console.log("check if inproxy running, stored value: ", running);
if (running === "1") {
setInProxyState({
setInProxyStateSync({
running: true,
synced: true,
});
} else {
setInProxyState({
setInProxyStateSync({
running: false,
synced: true,
});
Expand All @@ -239,32 +242,35 @@ export function InProxyProvider({ children }: { children: React.ReactNode }) {
params.limitUpstreamBytesPerSecond.toString(),
);
setInProxyParameters(params);
console.log("MOCK: InProxy parameters selected successfully", params);
console.log("MOCK: InProxy parameters selected successfully");
}

async function toggleInProxy(): Promise<void> {
//await requestNotificationsPermissions();
await AsyncStorage.setItem(
"MockInProxyRunning",
inProxyState.running ? "0" : "1",
inProxyStateSync.running ? "0" : "1",
);
setInProxyState({ running: !inProxyState.running, synced: true });
setInProxyStateSync({
running: !inProxyStateSync.running,
synced: true,
});
console.log("MOCK: InProxyModule.toggleInProxy() invoked");
}

const getInProxyStatus = React.useCallback(() => {
let state;
if (!inProxyState.synced) {
state = { status: "unknown" };
if (!inProxyStateSync.synced) {
state = "UNKNOWN";
} else {
if (inProxyState.running) {
state = { status: "running" };
if (inProxyStateSync.running) {
state = "RUNNING";
} else {
state = { status: "stopped" };
state = "STOPPED";
}
}
return InProxyStatusSchema.parse(state);
}, [inProxyState]);
return InProxyStatusEnumSchema.parse(state);
}, [inProxyStateSync]);

async function sendFeedback(): Promise<void> {
console.log("MOCK: InProxyModule.sendFeedback() invoked");
Expand Down
67 changes: 67 additions & 0 deletions src/inproxy/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { z } from "zod";

import { Base64Unpadded64Bytes } from "@/src/common/validators";

export const InProxyStatusEnumSchema = z.enum([
"RUNNING",
"STOPPED",
"UNKNOWN",
]);

export const InProxyStateSchema = z.object({
status: InProxyStatusEnumSchema,
networkState: z.enum(["HAS_INTERNET", "NO_INTERNET"]),
});

export const InProxyErrorSchema = z.object({
data: z.enum([
"proxyStartFailed",
"proxyRestartFailed",
"inProxyMustUpgrade",
]),
});

export const InProxyActivityDataByPeriodSchema = z.object({
bytesUp: z.array(z.number()).length(288),
bytesDown: z.array(z.number()).length(288),
connectingClients: z.array(z.number()).length(288),
connectedClients: z.array(z.number()).length(288),
});

export const InProxyActivityStatsSchema = z.object({
elapsedTime: z.number(),
totalBytesUp: z.number(),
totalBytesDown: z.number(),
currentConnectingClients: z.number(),
currentConnectedClients: z.number(),
dataByPeriod: z.object({
"1000ms": InProxyActivityDataByPeriodSchema,
}),
});

export const InProxyEventSchema = z.object({
type: z.enum(["proxyState", "proxyError", "inProxyActivityStats"]),
data: z.union([
InProxyStateSchema,
InProxyErrorSchema,
InProxyActivityStatsSchema,
]),
});

// These are the user-configurable parameters for the inproxy.
export const InProxyParametersSchema = z.object({
privateKey: Base64Unpadded64Bytes,
maxClients: z.number().int().positive(),
limitUpstreamBytesPerSecond: z.number().int().positive(),
limitDownstreamBytesPerSecond: z.number().int().positive(),
});

export type InProxyParameters = z.infer<typeof InProxyParametersSchema>;
export type InProxyStatusEnum = z.infer<typeof InProxyStatusEnumSchema>;
export type InProxyState = z.infer<typeof InProxyStateSchema>;
export type InProxyError = z.infer<typeof InProxyErrorSchema>;
export type InProxyActivityStats = z.infer<typeof InProxyActivityStatsSchema>;
export type InProxyActivityByPeriod = z.infer<
typeof InProxyActivityDataByPeriodSchema
>;
export type InProxyEvent = z.infer<typeof InProxyEventSchema>;
Loading

0 comments on commit 729eea6

Please sign in to comment.