diff --git a/shared/js/connectionlog/History.ts b/shared/js/connectionlog/History.ts index b2996dc9..43138528 100644 --- a/shared/js/connectionlog/History.ts +++ b/shared/js/connectionlog/History.ts @@ -406,6 +406,41 @@ export class ConnectionHistory { return result; } + async lastConnectInfo(target: string, targetType: "address" | "server-unique-id", onlySucceeded?: boolean) : Promise { + if(!this.database) { + return undefined; + } + + const transaction = this.database.transaction(["attempt-history"], "readonly"); + const store = transaction.objectStore("attempt-history"); + + const cursor = store.index(targetType === "server-unique-id" ? "serverUniqueId" : "targetAddress").openCursor(target, "prev"); + while(true) { + const entry = await new Promise((resolve, reject) => { + cursor.onsuccess = () => resolve(cursor.result); + cursor.onerror = () => reject(cursor.error); + }); + + if(!entry) { + return undefined; + } + + if(entry.value.serverUniqueId === kUnknownHistoryServerUniqueId && onlySucceeded) { + continue; + } + + return { + id: entry.value.id, + timestamp: entry.value.timestamp, + serverUniqueId: entry.value.serverUniqueId, + + nickname: entry.value.nickname, + hashedPassword: entry.value.hashedPassword, + targetAddress: entry.value.targetAddress, + }; + } + } + async countConnectCount(target: string, targetType: "address" | "server-unique-id") : Promise { if(!this.database) { return -1; diff --git a/shared/js/tree/Server.ts b/shared/js/tree/Server.ts index 402a4332..556203c5 100644 --- a/shared/js/tree/Server.ts +++ b/shared/js/tree/Server.ts @@ -10,7 +10,6 @@ import {openServerInfo} from "../ui/modal/ModalServerInfo"; import {createServerModal} from "../ui/modal/ModalServerEdit"; import {spawnIconSelect} from "../ui/modal/ModalIconSelect"; import {spawnAvatarList} from "../ui/modal/ModalAvatarList"; -import {connection_log} from "../ui/modal/ModalConnect"; import {Registry} from "../events"; import {ChannelTreeEntry, ChannelTreeEntryEvents} from "./ChannelTreeEntry"; import { tr } from "tc-shared/i18n/localize"; @@ -334,21 +333,6 @@ export class ServerEntry extends ChannelTreeEntry { this.info_request_promise_reject = undefined; this.info_request_promise_resolve = undefined; } - - connection_log.update_address_info({ - hostname: this.remote_address.host, - port: this.remote_address.port - }, { - clients_online: this.properties.virtualserver_clientsonline, - clients_total: this.properties.virtualserver_maxclients, - country: this.properties.virtualserver_country_code, - flag_password: this.properties.virtualserver_flag_password, - name: this.properties.virtualserver_name, - icon_id: this.properties.virtualserver_icon_id, - server_unique_id: this.properties.virtualserver_unique_identifier, - - password_hash: undefined /* we've here no clue */ - }); } /* this result !must! be cached for at least a second */ diff --git a/shared/js/ui/modal/ModalBookmarks.ts b/shared/js/ui/modal/ModalBookmarks.ts index ac440bfd..fdc26d84 100644 --- a/shared/js/ui/modal/ModalBookmarks.ts +++ b/shared/js/ui/modal/ModalBookmarks.ts @@ -10,7 +10,7 @@ import { DirectoryBookmark, save_bookmark } from "../../bookmarks"; -import {connection_log, Regex} from "../../ui/modal/ModalConnect"; +import {Regex} from "../../ui/modal/ModalConnect"; import {availableConnectProfiles} from "../../profiles/ConnectionProfile"; import {spawnYesNo} from "../../ui/modal/ModalYesNo"; import {Settings, settings} from "../../settings"; @@ -19,7 +19,8 @@ import {LogCategory} from "../../log"; import * as i18nc from "../../i18n/country"; import {formatMessage} from "../../ui/frames/chat"; import {generateIconJQueryTag, getIconManager} from "tc-shared/file/Icons"; -import { tr } from "tc-shared/i18n/localize"; +import {tr} from "tc-shared/i18n/localize"; +import {connectionHistory} from "tc-shared/connectionlog/History"; export function spawnBookmarkModal() { let modal: Modal; @@ -63,25 +64,40 @@ export function spawnBookmarkModal() { if (selected_bookmark && selected_bookmark.type === BookmarkType.ENTRY) { const entry = selected_bookmark as Bookmark; - const history = connection_log.history().find(e => e.address.hostname === entry.server_properties.server_address && e.address.port === entry.server_properties.server_port); - if (history) { - label_server_name.text(history.name); - label_server_region.empty().append( - $.spawn("div").addClass("country flag-" + history.country.toLowerCase()), - $.spawn("div").text(i18nc.country_name(history.country, tr("Global"))) - ); - label_client_count.text(history.clients_online + "/" + history.clients_total); - label_connection_count.empty().append( - ...formatMessage(tr("You've connected {} times"), $.spawn("div").addClass("connect-count").text(history.total_connection)) - ); - } else { - label_server_name.text(tr("Unknown")); - label_server_region.empty().text(tr("Unknown")); - label_client_count.text(tr("Unknown")); - label_connection_count.empty().append( - ...formatMessage(tr("You {} connected to that server address"), $.spawn("div").addClass("connect-never").text("never")) - ); - } + label_server_name.text(tr("Unknown")); + label_server_region.empty().text(tr("Unknown")); + label_client_count.text(tr("Unknown")); + label_connection_count.empty().append( + ...formatMessage(tr("You {} connected to that server address"), $.spawn("div").addClass("connect-never").text("never")) + ); + + const targetAddress = entry.server_properties.server_address + (entry.server_properties.server_port === 9987 ? "" : ":" + entry.server_properties.server_port); + connectionHistory.lastConnectInfo(targetAddress, "address", true).then(async connectionInfo => { + if(!connectionInfo) { + return; + } + + const info = await connectionHistory.queryServerInfo(connectionInfo.serverUniqueId); + if(info) { + label_server_name.text(info.name); + label_server_region.empty().append( + $.spawn("div").addClass("country flag-" + (info.country || "xx").toLowerCase()), + $.spawn("div").text(i18nc.country_name(info.country || "xx", tr("Global"))) + ); + label_client_count.text(info.clientsOnline + "/" + info.clientsMax); + } + + { + const count = await connectionHistory.countConnectCount(targetAddress, "address"); + if(count > 0) { + label_connection_count.empty().append( + ...formatMessage(tr("You've connected {} times"), $.spawn("div").addClass("connect-count").text(count)) + ); + } + } + + }); + label_last_ping.text(tr("Average ping isn't yet supported")); } else { label_server_name.text("--"); diff --git a/shared/js/ui/modal/ModalConnect.ts b/shared/js/ui/modal/ModalConnect.ts index 52068386..695d94ac 100644 --- a/shared/js/ui/modal/ModalConnect.ts +++ b/shared/js/ui/modal/ModalConnect.ts @@ -1,7 +1,4 @@ import {Settings, settings} from "../../settings"; -import * as log from "../../log"; -import {LogCategory} from "../../log"; -import * as loader from "tc-loader"; import {createModal} from "../../ui/elements/Modal"; import {ConnectionProfile, defaultConnectProfile, findConnectProfile, availableConnectProfiles} from "../../profiles/ConnectionProfile"; import * as i18nc from "../../i18n/country"; @@ -10,102 +7,6 @@ import {server_connections} from "tc-shared/ConnectionManager"; import {generateIconJQueryTag, getIconManager} from "tc-shared/file/Icons"; import { tr } from "tc-shared/i18n/localize"; -//FIXME: Move this shit out of this file! -export namespace connection_log { - //TODO: Save password data - export type ConnectionData = { - name: string; - - icon_id: number; - server_unique_id: string; - - country: string; - clients_online: number; - clients_total: number; - - flag_password: boolean; - password_hash: string; - } - - export type ConnectionEntry = ConnectionData & { - address: { hostname: string; port: number }, - total_connection: number; - - first_timestamp: number; - last_timestamp: number; - } - - let _history: ConnectionEntry[] = []; - - export function log_connect(address: { hostname: string; port: number }) { - let entry = _history.find(e => e.address.hostname.toLowerCase() == address.hostname.toLowerCase() && e.address.port == address.port); - if (!entry) { - _history.push(entry = { - last_timestamp: Date.now(), - first_timestamp: Date.now(), - address: address, - clients_online: 0, - clients_total: 0, - country: 'unknown', - name: 'Unknown', - icon_id: 0, - server_unique_id: "unknown", - total_connection: 0, - - flag_password: false, - password_hash: undefined - }); - } - entry.last_timestamp = Date.now(); - entry.total_connection++; - _save(); - } - - export function update_address_info(address: { hostname: string; port: number }, data: ConnectionData) { - _history.filter(e => e.address.hostname.toLowerCase() == address.hostname.toLowerCase() && e.address.port == address.port).forEach(e => { - for (const key of Object.keys(data)) { - if (typeof (data[key]) !== "undefined") { - e[key] = data[key]; - } - } - }); - _save(); - } - - export function update_address_password(address: { hostname: string; port: number }, password_hash: string) { - _history.filter(e => e.address.hostname.toLowerCase() == address.hostname.toLowerCase() && e.address.port == address.port).forEach(e => { - e.password_hash = password_hash; - }); - _save(); - } - - function _save() { - settings.changeGlobal(Settings.KEY_CONNECT_HISTORY, JSON.stringify(_history)); - } - - export function history(): ConnectionEntry[] { - return _history.sort((a, b) => b.last_timestamp - a.last_timestamp); - } - - export function delete_entry(address: { hostname: string; port: number }) { - _history = _history.filter(e => !(e.address.hostname.toLowerCase() == address.hostname.toLowerCase() && e.address.port == address.port)); - _save(); - } - - loader.register_task(loader.Stage.JAVASCRIPT_INITIALIZING, { - name: 'connection history load', - priority: 1, - function: async () => { - _history = []; - try { - _history = JSON.parse(settings.global(Settings.KEY_CONNECT_HISTORY, "[]")); - } catch (error) { - log.warn(LogCategory.CLIENT, tr("Failed to load connection history: {}"), error); - } - } - }); -} - declare const native_client; export function spawnConnectModal(options: { @@ -155,7 +56,6 @@ export function spawnConnectModal(options: { const apply = (header, body, footer) => { const container_last_server_body = modal.htmlTag.find(".container-last-servers .table .body"); const container_empty = container_last_server_body.find(".body-empty"); - let current_connect_data: connection_log.ConnectionEntry; const button_connect = footer.find(".button-connect"); const button_connect_tab = footer.find(".button-connect-new-tab"); @@ -168,7 +68,6 @@ export function spawnConnectModal(options: { let updateFields = (reset_current_data: boolean) => { if (reset_current_data) { - current_connect_data = undefined; container_last_server_body.find(".selected").removeClass("selected"); } @@ -291,8 +190,8 @@ export function spawnConnectModal(options: { /* connect history show */ { - - for (const entry of connection_log.history().slice(0, 10)) { + /* connection_log.history().slice(0, 10) */ + for (const entry of []) { $.spawn("div").addClass("row").append( $.spawn("div").addClass("column delete").append($.spawn("div").addClass("icon_em client-delete")).on('click', event => { event.preventDefault(); @@ -301,7 +200,7 @@ export function spawnConnectModal(options: { row.hide(250, () => { row.detach(); }); - connection_log.delete_entry(entry.address); + //connection_log.delete_entry(entry.address); container_empty.toggle(container_last_server_body.children().length > 1); }) ).append( diff --git a/web/app/dns/resolver.ts b/web/app/dns/resolver.ts index 17b0a2de..18bcc40e 100644 --- a/web/app/dns/resolver.ts +++ b/web/app/dns/resolver.ts @@ -349,7 +349,7 @@ export async function resolveTeaSpeakServerAddress(address: ServerAddress, _opti }; } -export async function resolveAddressIpv4(address: string) : Promise { +export async function resolve_address_ipv4(address: string) : Promise { const result = await executeDnsRequest(address, RRType.A); if(!result.length) return undefined;