Skip to content

Commit

Permalink
Rate limit userKey input #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Soltares committed Oct 10, 2024
1 parent e40f6a7 commit 6201b12
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
8 changes: 5 additions & 3 deletions ui/src/Settings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
tracerouteRateLimit,
nodeInactiveTimer
} from 'api/src/vars'
import { hasAccess, userKey } from './lib/util'
import { hasAccess, userKey, blockUserKey } from './lib/util'
import { State } from 'api/src/lib/state'
import { tick } from 'svelte'
import axios from 'axios'
Expand Down Expand Up @@ -115,10 +115,10 @@
</label>
<div>This key of your choosing will be required to have access to certain features when connected remotely such as Connect and Disconnect.</div>
<div>
Enter this key into
Remote users will enter this key into
<span class="font-mono bg-black/20 px-2 rounded py-0.5">Client Access Key</span> to gain access.
</div>
{:else}
{:else if !$blockUserKey}
<form on:submit|preventDefault={applyClientKey}>
<label>
<div class="font-bold">Client Access Key</div>
Expand All @@ -127,5 +127,7 @@
{#if $hasAccess}<span class="ml-1"></span>{/if}
</label>
</form>
{:else}
<div>The client access key entered is incorrect. Please verify the key matches exactly including case-sensitive characters.</div>
{/if}
</div>
22 changes: 20 additions & 2 deletions ui/src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,28 @@ import { tick } from 'svelte'
import { derived, get, writable } from 'svelte/store'
import { enableAudioAlerts } from '../Settings.svelte'

export let blockUserKey = writable(false)
export const userKey = writable(localStorage.getItem('userKey') || '')
userKey.subscribe((value) => localStorage.setItem('userKey', value))

export const hasAccess = derived([accessKey, userKey], ([$accessKey, $userKey]) => window.location.hostname == 'localhost' || ($accessKey != '' && $accessKey == $userKey))
window['userKey'] = userKey

let failedUserKeyAttempts = 0
userKey.subscribe(async (value) => {
await tick()

if (get(hasAccess)) {
failedUserKeyAttempts = 0
} else {
failedUserKeyAttempts += 1
blockUserKey.set(true)
setTimeout(() => {
blockUserKey.set(false)
}, Math.min(1000 * failedUserKeyAttempts, 10000))
}

console.log({ failedUserKeyAttempts })
localStorage.setItem('userKey', value)
})

export function unixSecondsTimeAgo(seconds) {
return seconds ? timeAgo(Date.now() / 1000 - seconds) : ''
Expand Down

0 comments on commit 6201b12

Please sign in to comment.