2024-07-24 20:40:21 +08:00
|
|
|
import { makePersisted } from '@solid-primitives/storage'
|
2023-09-08 13:39:51 +08:00
|
|
|
import { differenceWith, isNumber, unionWith } from 'lodash'
|
2023-09-10 18:47:01 +08:00
|
|
|
import { CONNECTIONS_TABLE_MAX_CLOSED_ROWS } from '~/constants'
|
2023-09-08 13:39:51 +08:00
|
|
|
import { Connection, ConnectionRawMessage } from '~/types'
|
2023-09-08 11:22:16 +08:00
|
|
|
|
2023-09-08 18:42:15 +08:00
|
|
|
export type WsMsg = {
|
2023-09-15 13:37:11 +08:00
|
|
|
connections?: ConnectionRawMessage[]
|
2023-09-08 11:22:16 +08:00
|
|
|
uploadTotal: number
|
|
|
|
downloadTotal: number
|
|
|
|
} | null
|
|
|
|
|
2024-07-25 17:09:22 +08:00
|
|
|
// DIRECT is from clash
|
|
|
|
// direct and dns-out is from the example of sing-box official site
|
2024-07-24 20:40:21 +08:00
|
|
|
export const [quickFilterRegex, setQuickFilterRegex] = makePersisted(
|
2024-07-25 17:09:22 +08:00
|
|
|
createSignal<string>('DIRECT|direct|dns-out'),
|
2024-07-24 20:40:21 +08:00
|
|
|
{
|
|
|
|
name: 'quickFilterRegex',
|
|
|
|
storage: localStorage,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
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-16 23:31:40 +08:00
|
|
|
export const [allConnections, setAllConnections] = createSignal<Connection[]>(
|
|
|
|
[],
|
|
|
|
)
|
2023-09-19 20:24:13 +08:00
|
|
|
export const [latestConnectionMsg, setLatestConnectionMsg] =
|
|
|
|
createSignal<WsMsg>(null)
|
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(),
|
|
|
|
)
|
2023-09-17 14:55:03 +08:00
|
|
|
|
|
|
|
mergeAllConnections(activeConnections())
|
2023-09-08 11:22:16 +08:00
|
|
|
|
|
|
|
if (!paused()) {
|
2023-09-17 14:55:03 +08:00
|
|
|
const closedConns = diffClosedConnections(activeConns, allConnections())
|
2023-09-08 13:39:51 +08:00
|
|
|
|
2023-09-10 18:47:01 +08:00
|
|
|
setActiveConnections(activeConns)
|
|
|
|
setClosedConnections(
|
|
|
|
closedConns.slice(-CONNECTIONS_TABLE_MAX_CLOSED_ROWS),
|
|
|
|
)
|
2023-09-08 11:22:16 +08:00
|
|
|
}
|
|
|
|
|
2023-09-17 14:55:03 +08:00
|
|
|
setAllConnections((allConnections) =>
|
|
|
|
allConnections.slice(
|
2023-09-10 18:47:01 +08:00
|
|
|
-(activeConns.length + CONNECTIONS_TABLE_MAX_CLOSED_ROWS),
|
|
|
|
),
|
|
|
|
)
|
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 17:09:32 +08:00
|
|
|
export const restructRawMsgToConnection = (
|
2023-09-08 13:39:51 +08:00
|
|
|
connections: ConnectionRawMessage[],
|
|
|
|
prevActiveConnections: Connection[],
|
2023-09-08 17:09:32 +08:00
|
|
|
): Connection[] => {
|
2023-09-08 13:39:51 +08:00
|
|
|
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 17:09:32 +08:00
|
|
|
export const mergeAllConnections = (activeConns: Connection[]) => {
|
2023-09-17 14:55:03 +08:00
|
|
|
setAllConnections((allConnections) =>
|
|
|
|
unionWith(allConnections, activeConns, (a, b) => a.id === b.id),
|
|
|
|
)
|
2023-09-08 13:39:51 +08:00
|
|
|
}
|
|
|
|
|
2023-09-08 17:09:32 +08:00
|
|
|
const diffClosedConnections = (
|
2023-09-08 13:39:51 +08:00
|
|
|
activeConns: Connection[],
|
|
|
|
allConns: Connection[],
|
2023-09-17 14:55:03 +08:00
|
|
|
) => differenceWith(allConns, activeConns, (a, b) => a.id === b.id)
|