2020-04-17 19:41:47 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2019-06-29 23:57:11 +00:00
|
|
|
import { Websocket } from '@/plugins/Websocket';
|
2019-07-10 04:25:57 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
2022-02-27 16:47:22 +00:00
|
|
|
import { getWebsocketToken } from '@/api/server';
|
2020-04-17 19:41:47 +00:00
|
|
|
import ContentContainer from '@/components/elements/ContentContainer';
|
|
|
|
import { CSSTransition } from 'react-transition-group';
|
|
|
|
import Spinner from '@/components/elements/Spinner';
|
2020-07-04 22:58:14 +00:00
|
|
|
import tw from 'twin.macro';
|
2019-06-29 23:14:32 +00:00
|
|
|
|
2020-11-04 04:33:05 +00:00
|
|
|
const reconnectErrors = [
|
|
|
|
'jwt: exp claim is invalid',
|
|
|
|
'jwt: created too far in past (denylist)',
|
|
|
|
];
|
|
|
|
|
2019-06-29 23:14:32 +00:00
|
|
|
export default () => {
|
2020-09-27 16:22:09 +00:00
|
|
|
let updatingToken = false;
|
|
|
|
const [ error, setError ] = useState<'connecting' | string>('');
|
2020-04-17 19:41:47 +00:00
|
|
|
const { connected, instance } = ServerContext.useStoreState(state => state.socket);
|
2020-09-27 16:22:09 +00:00
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data?.uuid);
|
2019-07-10 04:25:57 +00:00
|
|
|
const setServerStatus = ServerContext.useStoreActions(actions => actions.status.setServerStatus);
|
|
|
|
const { setInstance, setConnectionState } = ServerContext.useStoreActions(actions => actions.socket);
|
2019-06-29 23:14:32 +00:00
|
|
|
|
2021-02-05 16:42:57 +00:00
|
|
|
const updateToken = (uuid: string, socket: Websocket) => {
|
|
|
|
if (updatingToken) return;
|
|
|
|
|
|
|
|
updatingToken = true;
|
|
|
|
getWebsocketToken(uuid)
|
|
|
|
.then(data => socket.setToken(data.token, true))
|
|
|
|
.catch(error => console.error(error))
|
|
|
|
.then(() => {
|
|
|
|
updatingToken = false;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-12-17 01:54:01 +00:00
|
|
|
const connect = (uuid: string) => {
|
2019-09-25 03:20:29 +00:00
|
|
|
const socket = new Websocket();
|
2019-06-29 23:57:11 +00:00
|
|
|
|
2019-12-22 01:31:04 +00:00
|
|
|
socket.on('auth success', () => setConnectionState(true));
|
2019-06-29 23:57:11 +00:00
|
|
|
socket.on('SOCKET_CLOSE', () => setConnectionState(false));
|
2020-04-17 19:41:47 +00:00
|
|
|
socket.on('SOCKET_ERROR', () => {
|
2020-09-27 16:22:09 +00:00
|
|
|
setError('connecting');
|
2020-04-17 19:41:47 +00:00
|
|
|
setConnectionState(false);
|
|
|
|
});
|
2019-06-29 23:57:11 +00:00
|
|
|
socket.on('status', (status) => setServerStatus(status));
|
2019-06-29 23:14:32 +00:00
|
|
|
|
2019-09-28 20:09:47 +00:00
|
|
|
socket.on('daemon error', message => {
|
|
|
|
console.warn('Got error message from daemon socket:', message);
|
|
|
|
});
|
|
|
|
|
2020-09-27 16:22:09 +00:00
|
|
|
socket.on('token expiring', () => updateToken(uuid, socket));
|
|
|
|
socket.on('token expired', () => updateToken(uuid, socket));
|
|
|
|
socket.on('jwt error', (error: string) => {
|
|
|
|
setConnectionState(false);
|
|
|
|
console.warn('JWT validation error from wings:', error);
|
|
|
|
|
2020-11-04 04:33:05 +00:00
|
|
|
if (reconnectErrors.find(v => error.toLowerCase().indexOf(v) >= 0)) {
|
2020-09-27 16:22:09 +00:00
|
|
|
updateToken(uuid, socket);
|
|
|
|
} else {
|
|
|
|
setError('There was an error validating the credentials provided for the websocket. Please refresh the page.');
|
|
|
|
}
|
|
|
|
});
|
2019-09-28 20:09:47 +00:00
|
|
|
|
2020-12-16 23:55:44 +00:00
|
|
|
socket.on('transfer status', (status: string) => {
|
2020-12-17 01:54:01 +00:00
|
|
|
if (status === 'starting' || status === 'success') {
|
2020-12-16 23:55:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-17 02:30:12 +00:00
|
|
|
// This code forces a reconnection to the websocket which will connect us to the target node instead of the source node
|
|
|
|
// in order to be able to receive transfer logs from the target node.
|
2020-12-16 23:55:44 +00:00
|
|
|
socket.close();
|
|
|
|
setError('connecting');
|
|
|
|
setConnectionState(false);
|
|
|
|
setInstance(null);
|
2020-12-17 01:54:01 +00:00
|
|
|
connect(uuid);
|
2020-12-16 23:55:44 +00:00
|
|
|
});
|
|
|
|
|
2020-12-17 01:54:01 +00:00
|
|
|
getWebsocketToken(uuid)
|
2019-09-25 03:20:29 +00:00
|
|
|
.then(data => {
|
2019-12-22 01:31:04 +00:00
|
|
|
// Connect and then set the authentication token.
|
2019-09-25 03:20:29 +00:00
|
|
|
socket.setToken(data.token).connect(data.socket);
|
2019-12-22 01:31:04 +00:00
|
|
|
|
|
|
|
// Once that is done, set the instance.
|
2019-09-25 03:20:29 +00:00
|
|
|
setInstance(socket);
|
|
|
|
})
|
2019-09-09 00:48:37 +00:00
|
|
|
.catch(error => console.error(error));
|
2020-12-16 23:55:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
connected && setError('');
|
|
|
|
}, [ connected ]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
return () => {
|
|
|
|
instance && instance.close();
|
|
|
|
};
|
|
|
|
}, [ instance ]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
// If there is already an instance or there is no server, just exit out of this process
|
|
|
|
// since we don't need to make a new connection.
|
|
|
|
if (instance || !uuid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(uuid);
|
2020-09-27 16:22:09 +00:00
|
|
|
}, [ uuid ]);
|
2019-06-29 23:14:32 +00:00
|
|
|
|
2020-04-17 19:41:47 +00:00
|
|
|
return (
|
|
|
|
error ?
|
2020-07-05 20:56:04 +00:00
|
|
|
<CSSTransition timeout={150} in appear classNames={'fade'}>
|
2020-07-04 22:58:14 +00:00
|
|
|
<div css={tw`bg-red-500 py-2`}>
|
|
|
|
<ContentContainer css={tw`flex items-center justify-center`}>
|
2020-09-27 16:22:09 +00:00
|
|
|
{error === 'connecting' ?
|
|
|
|
<>
|
|
|
|
<Spinner size={'small'}/>
|
|
|
|
<p css={tw`ml-2 text-sm text-red-100`}>
|
|
|
|
We're having some trouble connecting to your server, please wait...
|
|
|
|
</p>
|
|
|
|
</>
|
|
|
|
:
|
2020-11-04 04:33:05 +00:00
|
|
|
<p css={tw`ml-2 text-sm text-white`}>
|
2020-09-27 16:22:09 +00:00
|
|
|
{error}
|
|
|
|
</p>
|
|
|
|
}
|
2020-04-17 19:41:47 +00:00
|
|
|
</ContentContainer>
|
|
|
|
</div>
|
|
|
|
</CSSTransition>
|
|
|
|
:
|
|
|
|
null
|
|
|
|
);
|
2019-06-29 23:14:32 +00:00
|
|
|
};
|