Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3362976
D12440.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
7 KB
Referenced Files
None
Subscribers
None
D12440.diff
View Options
diff --git a/lib/hooks/qr-auth.js b/lib/hooks/qr-auth.js
--- a/lib/hooks/qr-auth.js
+++ b/lib/hooks/qr-auth.js
@@ -49,8 +49,7 @@
setUnauthorizedDeviceID,
addListener,
removeListener,
- connected: tunnelbrokerConnected,
- isAuthorized,
+ socketState,
sendMessage,
} = useTunnelbroker();
@@ -61,8 +60,7 @@
if (
!secondaryDeviceID ||
!aesKey ||
- !tunnelbrokerConnected ||
- !isAuthorized ||
+ !socketState.isAuthorized ||
!primaryDeviceID
) {
return;
@@ -79,13 +77,12 @@
});
})();
}, [
- tunnelbrokerConnected,
- isAuthorized,
sendMessage,
primaryDeviceID,
aesKey,
secondaryDeviceID,
composeMessage,
+ socketState,
]);
const tunnelbrokerMessageListener = React.useCallback(
diff --git a/lib/tunnelbroker/tunnelbroker-context.js b/lib/tunnelbroker/tunnelbroker-context.js
--- a/lib/tunnelbroker/tunnelbroker-context.js
+++ b/lib/tunnelbroker/tunnelbroker-context.js
@@ -45,6 +45,15 @@
};
type Promises = { [clientMessageID: string]: PromiseCallbacks };
+type TunnelbrokerSocketState =
+ | {
+ +connected: true,
+ +isAuthorized: boolean,
+ }
+ | {
+ +connected: false,
+ };
+
type TunnelbrokerContextType = {
+sendMessage: (
message: TunnelbrokerClientMessageToDevice,
@@ -52,8 +61,7 @@
) => Promise<void>,
+addListener: (listener: TunnelbrokerSocketListener) => void,
+removeListener: (listener: TunnelbrokerSocketListener) => void,
- +connected: boolean,
- +isAuthorized: boolean,
+ +socketState: TunnelbrokerSocketState,
+setUnauthorizedDeviceID: (unauthorizedDeviceID: ?string) => void,
};
@@ -121,7 +129,9 @@
const previousInitMessage =
React.useRef<?TunnelbrokerInitializationMessage>(null);
- const [connected, setConnected] = React.useState(false);
+ const [socketState, setSocketState] = React.useState<TunnelbrokerSocketState>(
+ { connected: false },
+ );
const listeners = React.useRef<Set<TunnelbrokerSocketListener>>(new Set());
const socket = React.useRef<?WebSocket>(null);
const socketSessionCounter = React.useRef(0);
@@ -143,7 +153,7 @@
stopHeartbeatTimeout();
heartbeatTimeoutID.current = setTimeout(() => {
socket.current?.close();
- setConnected(false);
+ setSocketState({ connected: false });
}, tunnelbrokerHeartbeatTimeout);
}, [stopHeartbeatTimeout]);
@@ -186,7 +196,7 @@
// when we're already connected (or pending disconnection),
// or there's no init message to start with, we don't need
// to do anything
- if (connected || !initMessage || socket.current) {
+ if (socketState.connected || !initMessage || socket.current) {
return;
}
@@ -198,7 +208,7 @@
tunnelbrokerSocket.onclose = () => {
// this triggers the effect hook again and reconnect
- setConnected(false);
+ setSocketState({ connected: false });
onClose?.();
socket.current = null;
console.log('Connection to Tunnelbroker closed');
@@ -236,18 +246,21 @@
message.type ===
tunnelbrokerMessageTypes.CONNECTION_INITIALIZATION_RESPONSE
) {
- if (message.status.type === 'Success' && !connected) {
- setConnected(true);
+ if (message.status.type === 'Success' && !socketState.connected) {
+ setSocketState({ connected: true, isAuthorized });
console.log(
'session with Tunnelbroker created. isAuthorized:',
isAuthorized,
);
- } else if (message.status.type === 'Success' && connected) {
+ } else if (
+ message.status.type === 'Success' &&
+ socketState.connected
+ ) {
console.log(
'received ConnectionInitializationResponse with status: Success for already connected socket',
);
} else {
- setConnected(false);
+ setSocketState({ connected: false });
console.log(
'creating session with Tunnelbroker error:',
message.status.data,
@@ -285,7 +298,6 @@
}
})();
}, [
- connected,
isSocketActive,
isAuthorized,
resetHeartbeatTimeout,
@@ -293,6 +305,7 @@
identityClient,
onClose,
createInitMessage,
+ socketState.connected,
]);
const sendMessageToDeviceRequest: (
@@ -300,7 +313,7 @@
) => Promise<void> = React.useCallback(
request => {
return new Promise((resolve, reject) => {
- const socketActive = connected && socket.current;
+ const socketActive = socketState.connected && socket.current;
if (!shouldBeClosed && !socketActive) {
throw new Error('Tunnelbroker not connected');
}
@@ -315,7 +328,7 @@
}
});
},
- [connected, secondaryTunnelbrokerConnection, shouldBeClosed],
+ [socketState, secondaryTunnelbrokerConnection, shouldBeClosed],
);
const sendMessage: (
@@ -406,13 +419,12 @@
const value: TunnelbrokerContextType = React.useMemo(
() => ({
sendMessage,
- connected,
- isAuthorized,
+ socketState,
addListener,
removeListener,
setUnauthorizedDeviceID,
}),
- [addListener, connected, removeListener, sendMessage, isAuthorized],
+ [sendMessage, socketState, addListener, removeListener],
);
return (
diff --git a/native/profile/tunnelbroker-menu.react.js b/native/profile/tunnelbroker-menu.react.js
--- a/native/profile/tunnelbroker-menu.react.js
+++ b/native/profile/tunnelbroker-menu.react.js
@@ -42,7 +42,7 @@
);
const identityContext = React.useContext(IdentityClientContext);
- const { connected, addListener, sendMessage, removeListener } =
+ const { socketState, addListener, sendMessage, removeListener } =
useTunnelbroker();
const [messages, setMessages] = useState<TunnelbrokerMessage[]>([]);
const [recipient, setRecipient] = useState('');
@@ -126,7 +126,7 @@
<View style={styles.section}>
<View style={styles.submenuButton}>
<Text style={styles.submenuText}>Connected</Text>
- <Text style={styles.text}>{connected.toString()}</Text>
+ <Text style={styles.text}>{socketState.connected.toString()}</Text>
</View>
</View>
diff --git a/web/settings/account-settings.react.js b/web/settings/account-settings.react.js
--- a/web/settings/account-settings.react.js
+++ b/web/settings/account-settings.react.js
@@ -85,7 +85,7 @@
const stringForUser = useStringForUser(currentUserInfo);
const staffCanSee = useStaffCanSee();
- const { sendMessage, connected, addListener, removeListener } =
+ const { sendMessage, socketState, addListener, removeListener } =
useTunnelbroker();
const openTunnelbrokerModal = React.useCallback(
() =>
@@ -191,7 +191,7 @@
<ul>
<li>
<span>Connected</span>
- <span>{connected.toString()}</span>
+ <span>{socketState.connected.toString()}</span>
</li>
<li>
<span>Send message to device</span>
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Nov 25, 11:38 PM (21 h, 48 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2581656
Default Alt Text
D12440.diff (7 KB)
Attached To
Mode
D12440: [lib] improve handling Tunnelbroker socket state
Attached
Detach File
Event Timeline
Log In to Comment