2023-09-08 13:39:51 +08:00
|
|
|
import { differenceWith, isNumber, unionWith } from 'lodash'
|
2023-09-08 11:35:53 +08:00
|
|
|
import { Accessor, createEffect, createSignal, untrack } from 'solid-js'
|
2023-09-08 13:39:51 +08:00
|
|
|
import { Connection, ConnectionRawMessage } from '~/types'
|
2023-09-08 11:22:16 +08:00
|
|
|
import { selectedEndpoint, useWsRequest } from './request'
|
|
|
|
|
|
|
|
type WsMsg = {
|
2023-09-08 13:39:51 +08:00
|
|
|
connections: ConnectionRawMessage[]
|
2023-09-08 11:22:16 +08:00
|
|
|
uploadTotal: number
|
|
|
|
downloadTotal: number
|
|
|
|
} | null
|
|
|
|
|
2023-09-08 11:55:22 +08:00
|
|
|
// we make connections global, so we can keep track of connections when user in proxy page
|
2023-09-08 11:22:16 +08:00
|
|
|
// when user selects proxy and close some connections they can back and check connections
|
|
|
|
// they closed
|
2023-09-08 15:35:06 +08:00
|
|
|
let allConnections: Connection[] = []
|
|
|
|
const setAllConnections = (allConns: Connection[]) => {
|
|
|
|
allConnections = allConns
|
|
|
|
}
|
2023-09-08 11:22:16 +08:00
|
|
|
|
2023-09-08 13:39:51 +08:00
|
|
|
export let latestConnectionMsg: Accessor<WsMsg> = () => ({
|
2023-09-08 11:35:53 +08:00
|
|
|
uploadTotal: 0,
|
|
|
|
downloadTotal: 0,
|
|
|
|
connections: [],
|
2023-09-08 11:22:16 +08:00
|
|
|
})
|
|
|
|
|
2023-09-08 11:35:53 +08:00
|
|
|
createEffect(() => {
|
|
|
|
if (selectedEndpoint()) {
|
2023-09-08 13:39:51 +08:00
|
|
|
setAllConnections([])
|
|
|
|
latestConnectionMsg = useWsRequest<WsMsg>('connections')
|
2023-09-08 11:35:53 +08:00
|
|
|
}
|
|
|
|
})
|
2023-09-08 11:22:16 +08:00
|
|
|
|
|
|
|
export const useConnections = () => {
|
2023-09-08 13:39:51 +08:00
|
|
|
const [closedConnections, setClosedConnections] = createSignal<Connection[]>(
|
|
|
|
[],
|
|
|
|
)
|
|
|
|
const [activeConnections, setActiveConnections] = createSignal<Connection[]>(
|
|
|
|
[],
|
|
|
|
)
|
2023-09-08 11:22:16 +08:00
|
|
|
const [paused, setPaused] = createSignal(false)
|
|
|
|
|
|
|
|
createEffect(() => {
|
2023-09-08 13:39:51 +08:00
|
|
|
const rawConns = latestConnectionMsg()?.connections
|
2023-09-08 11:22:16 +08:00
|
|
|
|
2023-09-08 13:39:51 +08:00
|
|
|
if (!rawConns) {
|
2023-09-08 11:22:16 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
untrack(() => {
|
2023-09-08 13:39:51 +08:00
|
|
|
const activeConns = restructRawMsgToConnection(
|
|
|
|
rawConns,
|
|
|
|
activeConnections(),
|
|
|
|
)
|
|
|
|
const allConns = mergeAllConnections(activeConns)
|
2023-09-08 11:22:16 +08:00
|
|
|
|
|
|
|
if (!paused()) {
|
2023-09-08 13:39:51 +08:00
|
|
|
const closedConns = diffClosedConnections(activeConns, allConns)
|
|
|
|
|
2023-09-08 13:43:47 +08:00
|
|
|
setActiveConnections(activeConns.slice(-200))
|
|
|
|
setClosedConnections(closedConns.slice(-200))
|
2023-09-08 11:22:16 +08:00
|
|
|
}
|
|
|
|
|
2023-09-08 13:43:47 +08:00
|
|
|
setAllConnections(allConns.slice(-400))
|
2023-09-08 11:22:16 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
2023-09-08 13:39:51 +08:00
|
|
|
closedConnections,
|
|
|
|
activeConnections,
|
2023-09-08 11:22:16 +08:00
|
|
|
paused,
|
|
|
|
setPaused,
|
|
|
|
}
|
|
|
|
}
|
2023-09-08 13:39:51 +08:00
|
|
|
|
2023-09-08 15:22:07 +08:00
|
|
|
export function restructRawMsgToConnection(
|
2023-09-08 13:39:51 +08:00
|
|
|
connections: ConnectionRawMessage[],
|
|
|
|
prevActiveConnections: Connection[],
|
|
|
|
): Connection[] {
|
|
|
|
const prevMap = new Map<string, Connection>()
|
|
|
|
prevActiveConnections.forEach((prev) => prevMap.set(prev.id, prev))
|
|
|
|
|
|
|
|
return connections.map((connection) => {
|
|
|
|
const prevConn = prevMap.get(connection.id)
|
|
|
|
|
|
|
|
if (
|
|
|
|
!prevConn ||
|
|
|
|
!isNumber(prevConn.download) ||
|
|
|
|
!isNumber(prevConn.upload)
|
|
|
|
) {
|
|
|
|
return { ...connection, downloadSpeed: 0, uploadSpeed: 0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...connection,
|
|
|
|
downloadSpeed: connection.download - prevConn.download,
|
|
|
|
uploadSpeed: connection.upload - prevConn.upload,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-08 15:22:07 +08:00
|
|
|
export function mergeAllConnections(activeConns: Connection[]) {
|
2023-09-08 15:35:06 +08:00
|
|
|
return unionWith(allConnections, activeConns, (a, b) => a.id === b.id)
|
2023-09-08 13:39:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function diffClosedConnections(
|
|
|
|
activeConns: Connection[],
|
|
|
|
allConns: Connection[],
|
|
|
|
) {
|
|
|
|
return differenceWith(allConns, activeConns, (a, b) => a.id === b.id)
|
|
|
|
}
|