From b350918367a2135b3c6f646f2c074eec5a3651c9 Mon Sep 17 00:00:00 2001 From: Jimmy Whitwell Date: Wed, 23 Oct 2024 15:34:00 +0100 Subject: [PATCH] hasing version number and using it as part of invite code --- package.json | 2 ++ pnpm-lock.yaml | 35 ++++++++++++++++++++++++++++++ src/gui/components/ChannelBoot.tsx | 16 +++++++++++++- src/gui/components/ChannelView.tsx | 22 ++++++++++++++----- src/gui/components/Titlebar.tsx | 6 ++++- 5 files changed, 74 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index ed1f1de4..06e34c9b 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ }, "dependencies": { "@react-three/postprocessing": "^2.16.3", + "md5": "^2.3.0", "postprocessing": "^6.36.3", "react": "^18.3.1", "react-dom": "^18.3.1", @@ -56,6 +57,7 @@ "@types/eslint-config-prettier": "^6.11.3", "@types/eslint__js": "^8.42.3", "@types/libsodium-wrappers": "^0.7.14", + "@types/md5": "^2.3.5", "@types/mocha": "^10.0.7", "@types/react": "^18.3.3", "@types/react-dom": "^18.3.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 42d96649..c8e62398 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,6 +11,9 @@ importers: '@react-three/postprocessing': specifier: ^2.16.3 version: 2.16.3(@react-three/fiber@8.16.8(react-dom@18.3.1(react@18.3.1))(react@18.3.1)(three@0.167.1))(@types/three@0.167.1)(react@18.3.1)(three@0.167.1) + md5: + specifier: ^2.3.0 + version: 2.3.0 postprocessing: specifier: ^6.36.3 version: 6.36.3(three@0.167.1) @@ -60,6 +63,9 @@ importers: '@types/libsodium-wrappers': specifier: ^0.7.14 version: 0.7.14 + '@types/md5': + specifier: ^2.3.5 + version: 2.3.5 '@types/mocha': specifier: ^10.0.7 version: 10.0.7 @@ -775,6 +781,9 @@ packages: '@types/libsodium-wrappers@0.7.14': resolution: {integrity: sha512-5Kv68fXuXK0iDuUir1WPGw2R9fOZUlYlSAa0ztMcL0s0BfIDTqg9GXz8K30VJpPP3sxWhbolnQma2x+/TfkzDQ==} + '@types/md5@2.3.5': + resolution: {integrity: sha512-/i42wjYNgE6wf0j2bcTX6kuowmdL/6PE4IVitMpm2eYKBUuYCprdcWVK+xEF0gcV6ufMCRhtxmReGfc6hIK7Jw==} + '@types/mocha@10.0.7': resolution: {integrity: sha512-GN8yJ1mNTcFcah/wKEFIJckJx9iJLoMSzWcfRRuxz/Jk+U6KQNnml+etbtxFK8lPjzOw3zp4Ha/kjSst9fsHYw==} @@ -1140,6 +1149,9 @@ packages: resolution: {integrity: sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w==} engines: {node: ^12.17.0 || ^14.13 || >=16.0.0} + charenc@0.0.2: + resolution: {integrity: sha512-yrLQ/yVUFXkzg7EDQsPieE/53+0RlaWTs+wBrvW36cyilJ2SaDWfl4Yj7MtLTXleV9uEKefbAGUPv2/iWSooRA==} + check-error@2.1.1: resolution: {integrity: sha512-OAlb+T7V4Op9OwdkjmguYRqncdlx5JiofwOAUkmTF+jNdHwzTaTs4sRAGpzLF3oOz5xAyDGrPgeIDFQmDOTiJw==} engines: {node: '>= 16'} @@ -1190,6 +1202,9 @@ packages: resolution: {integrity: sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==} engines: {node: '>= 8'} + crypt@0.0.2: + resolution: {integrity: sha512-mCxBlsHFYh9C+HVpiEacem8FEBnMXgU9gy4zmNC+SXAZNB/1idgp/aulFJ4FgCi7GPEVbfyng092GqL2k2rmow==} + cssesc@3.0.0: resolution: {integrity: sha512-/Tb/JcjK111nNScGob5MNtsntNM1aCNUDipB/TkwZFhyDrrE47SOx/18wF2bbjgc3ZzCSKW1T5nt5EbFoAz/Vg==} engines: {node: '>=4'} @@ -1681,6 +1696,9 @@ packages: resolution: {integrity: sha512-gDYaKHJmnj4aWxyj6YHyXVpdQawtVLHU5cb+eztPGczf6cjuTdwve5ZIEfgXqH4e57An1D1AKf8CZ3kYrQRqYA==} engines: {node: '>= 0.4'} + is-buffer@1.1.6: + resolution: {integrity: sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==} + is-callable@1.2.7: resolution: {integrity: sha512-1BC0BVFhS/p0qtw6enp8e+8OD0UrK0oFLztSjNzhcKA3WDuJxxAPXzPuPtKkjEY9UUoEWlX/8fgKeu2S8i9JTA==} engines: {node: '>= 0.4'} @@ -1993,6 +2011,9 @@ packages: resolution: {integrity: sha512-LS9X+dc8KLxXCb8dni79fLIIUA5VyZoyjSMCwTluaXA0o27cCK0bhXkpgw+sTXVpPy/lSO57ilRixqk0vDmtRA==} engines: {node: '>=6'} + md5@2.3.0: + resolution: {integrity: sha512-T1GITYmFaKuO91vxyoQMFETst+O71VUPEU3ze5GNzDm0OWdP8v1ziTaAEPUr/3kLsY3Sftgz242A1SetQiDL7g==} + memorystream@0.3.1: resolution: {integrity: sha512-S3UwM3yj5mtUSEfP41UZmt/0SCoVYUcU1rkXv+BQ5Ig8ndL4sPoJNBUJERafdPb5jjHJGuMgytgKvKIf58XNBw==} engines: {node: '>= 0.10.0'} @@ -3436,6 +3457,8 @@ snapshots: '@types/libsodium-wrappers@0.7.14': {} + '@types/md5@2.3.5': {} + '@types/mocha@10.0.7': {} '@types/node@10.17.60': {} @@ -3902,6 +3925,8 @@ snapshots: chalk@5.3.0: {} + charenc@0.0.2: {} + check-error@2.1.1: {} chokidar@3.6.0: @@ -3964,6 +3989,8 @@ snapshots: shebang-command: 2.0.0 which: 2.0.2 + crypt@0.0.2: {} + cssesc@3.0.0: {} csstype@3.1.3: {} @@ -4564,6 +4591,8 @@ snapshots: call-bind: 1.0.7 has-tostringtag: 1.0.2 + is-buffer@1.1.6: {} + is-callable@1.2.7: {} is-core-module@2.15.0: @@ -4843,6 +4872,12 @@ snapshots: semver: 5.7.2 optional: true + md5@2.3.0: + dependencies: + charenc: 0.0.2 + crypt: 0.0.2 + is-buffer: 1.1.6 + memorystream@0.3.1: {} merge-stream@2.0.0: {} diff --git a/src/gui/components/ChannelBoot.tsx b/src/gui/components/ChannelBoot.tsx index 3ecdcffb..0fdeb9ba 100644 --- a/src/gui/components/ChannelBoot.tsx +++ b/src/gui/components/ChannelBoot.tsx @@ -1,3 +1,4 @@ +import md5 from 'md5'; import { memo, useMemo } from 'react'; import { config as socketConfig } from 'socket:application'; import { BOOTSTRAP_PEERS } from '../../runtime/bootstrap'; @@ -288,8 +289,21 @@ const terminalFlow = ({ reject('invalid key'); return; } + const [channelId, hostVersionHash] = input.split(':'); + if (!hostVersionHash) { + reject('invalid key'); + return; + } + const clientVersionHash = md5( + getVersionStringFromConfig(socketConfig), + ).slice(0, 4); + if (hostVersionHash !== clientVersionHash) { + reject('client app version incompatible with host'); + return; + } + client - .joinChannel(input) + .joinChannel(channelId) .then(() => { resolve('OK'); }) diff --git a/src/gui/components/ChannelView.tsx b/src/gui/components/ChannelView.tsx index 0652ecc6..63371052 100644 --- a/src/gui/components/ChannelView.tsx +++ b/src/gui/components/ChannelView.tsx @@ -1,11 +1,13 @@ import { useLiveQuery } from 'dexie-react-hooks'; +import md5 from 'md5'; import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'; +import { config as socketConfig } from 'socket:application'; import { SESSION_TIME_SECONDS } from '../../examples/spaceshooter'; import { ChannelInfo } from '../../runtime/channels'; import { PeerInfo } from '../../runtime/db'; import { DefaultMetrics } from '../../runtime/metrics'; import { sleep } from '../../runtime/timers'; -import { hardReset } from '../../runtime/utils'; +import { getVersionStringFromConfig, hardReset } from '../../runtime/utils'; import { getPlayerColorUi } from '../fixtures/player-colors'; import { useClient } from '../hooks/use-client'; import { useCredentials } from '../hooks/use-credentials'; @@ -32,6 +34,14 @@ export const SIM_END = SESSION_TIME_SECONDS / (FIXED_UPDATE_RATE / 1000); const src = '/examples/spaceshooter.js'; // not a real src yet see runtime/game.ts +const getChannelCode = (channelId: string) => { + return ( + channelId + + ':' + + md5(getVersionStringFromConfig(socketConfig)).slice(0, 4) + ); +}; + export default memo(function ChannelView({ channel, details, @@ -48,9 +58,11 @@ export default memo(function ChannelView({ const [showConnectedPeers, setShowConnectedPeers] = useState(false); const copyKeyToClipboard = () => { - navigator.clipboard.writeText(channel.id).catch((err) => { - console.error('clipboard write failed:', err); - }); + navigator.clipboard + .writeText(getChannelCode(channel.id)) + .catch((err) => { + console.error('clipboard write failed:', err); + }); }; const socket = useSocket(); @@ -207,7 +219,7 @@ export default memo(function ChannelView({ alignItems: 'center', }} > - {channel.id}{' '} + {getChannelCode(channel.id)}{' '} - Playerchain Demo + + Playerchain Demo v{getVersionStringFromConfig(socketConfig)} +