-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(proxies): auto close connections when proxy selected
- Loading branch information
Zephyruso
authored and
Zephyruso
committed
Sep 8, 2023
1 parent
59a7273
commit 15d20cd
Showing
9 changed files
with
144 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { differenceWith, unionWith } from 'lodash' | ||
import { | ||
createEffect, | ||
createMemo, | ||
createResource, | ||
createSignal, | ||
untrack, | ||
} from 'solid-js' | ||
import { Connection, ConnectionWithSpeed } from '~/types' | ||
import { selectedEndpoint, useWsRequest } from './request' | ||
|
||
type WsMsg = { | ||
connections: Connection[] | ||
uploadTotal: number | ||
downloadTotal: number | ||
} | null | ||
|
||
// we make allconnections global so we can keep track of connections when user in proxy page | ||
// when user selects proxy and close some connections they can back and check connections | ||
// they closed | ||
const [allConnectionsWithSpeed, setAllConnectionsWithSpeed] = createSignal< | ||
ConnectionWithSpeed[] | ||
>([]) | ||
|
||
const [latestConnectionWsMessage] = createResource(async () => { | ||
await new Promise<void>((resolve) => { | ||
createEffect(() => { | ||
if (selectedEndpoint()) { | ||
resolve() | ||
} | ||
}) | ||
}) | ||
|
||
return useWsRequest<WsMsg>('connections') | ||
}) | ||
|
||
export const connections = createMemo(() => latestConnectionWsMessage()?.()) | ||
|
||
export const useConnections = () => { | ||
const [closedConnectionsWithSpeed, setClosedConnectionsWithSpeed] = | ||
createSignal<ConnectionWithSpeed[]>([]) | ||
const [activeConnectionsWithSpeed, setActiveConnectionsWithSpeed] = | ||
createSignal<ConnectionWithSpeed[]>([]) | ||
const [paused, setPaused] = createSignal(false) | ||
|
||
const updateConnectionsWithSpeed = (connections: Connection[]) => { | ||
const prevActiveConnections = activeConnectionsWithSpeed() | ||
const prevMap = new Map<string, Connection>() | ||
prevActiveConnections.forEach((prev) => prevMap.set(prev.id, prev)) | ||
|
||
const activeConnnections: ConnectionWithSpeed[] = connections.map( | ||
(connection) => { | ||
const prevConn = prevMap.get(connection.id) | ||
|
||
if (!prevConn) { | ||
return { ...connection, downloadSpeed: 0, uploadSpeed: 0 } | ||
} | ||
|
||
return { | ||
...connection, | ||
downloadSpeed: | ||
connection.download - (prevConn.download ?? connection.download), | ||
uploadSpeed: | ||
connection.upload - (prevConn.upload ?? connection.upload), | ||
} | ||
}, | ||
) | ||
|
||
const allConnections = unionWith( | ||
allConnectionsWithSpeed(), | ||
activeConnnections, | ||
(a, b) => a.id === b.id, | ||
) | ||
const closedConnections = differenceWith( | ||
allConnections, | ||
activeConnnections, | ||
(a, b) => a.id === b.id, | ||
) | ||
|
||
return { | ||
activeConns: activeConnnections.slice(-200), | ||
closedConns: closedConnections.slice(-200), | ||
allConns: allConnections.slice(-400), | ||
} | ||
} | ||
|
||
createEffect(() => { | ||
const connection = connections()?.connections | ||
|
||
if (!connection) { | ||
return | ||
} | ||
|
||
untrack(() => { | ||
const { activeConns, closedConns, allConns } = | ||
updateConnectionsWithSpeed(connection) | ||
|
||
if (!paused()) { | ||
setActiveConnectionsWithSpeed(activeConns) | ||
setClosedConnectionsWithSpeed(closedConns) | ||
} | ||
|
||
setAllConnectionsWithSpeed(allConns) | ||
}) | ||
}) | ||
|
||
return { | ||
closedConnectionsWithSpeed, | ||
activeConnectionsWithSpeed, | ||
paused, | ||
setPaused, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters