This repository was archived by the owner on Jan 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
230 lines (189 loc) · 7.23 KB
/
index.ts
File metadata and controls
230 lines (189 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { Elysia } from "elysia";
import { cors } from "@elysiajs/cors";
import { CookieJar } from "tough-cookie";
import fetchCookie from "fetch-cookie";
import { Ratelimit } from "@upstash/ratelimit";
import Redis from "ioredis";
import { createIoRedisAdapter } from "./redis";
const CREDENTIALS = {
identifier: process.env.HYTALE_EMAIL!,
password: process.env.HYTALE_PASSWORD!,
};
if (!CREDENTIALS.identifier || !CREDENTIALS.password) {
console.error("Missing HYTALE_EMAIL or HYTALE_PASSWORD in .env");
process.exit(1);
}
// Redis cache
const redis = new Redis(process.env.REDIS_URL || "redis://localhost:6379");
const CACHE_PREFIX = "hytale:username:";
const AVAILABLE_TTL = 60; // 1 minute for available names
const ratelimit = new Ratelimit({
redis: createIoRedisAdapter(redis),
limiter: Ratelimit.slidingWindow(25, "60 s"), // 25 requests per minute
prefix: "hytale:ratelimit",
});
async function getCachedAvailability(username: string): Promise<boolean | null> {
const cached = await redis.get(`${CACHE_PREFIX}${username.toLowerCase()}`);
if (cached === null) return null;
return cached === "1";
}
async function setCachedAvailability(username: string, available: boolean): Promise<void> {
const key = `${CACHE_PREFIX}${username.toLowerCase()}`;
if (available) {
// Available names cached for 1 minute
await redis.set(key, "1", "EX", AVAILABLE_TTL);
} else {
// Taken names cached forever
await redis.set(key, "0");
}
}
const cookieJar = new CookieJar();
const fetchWithCookies = fetchCookie(fetch, cookieJar);
function isSessionValid(): boolean {
const cookies = cookieJar.getCookiesSync("https://hytale.com");
const sessionCookie = cookies.find(c => c.key === "ory_kratos_session");
if (!sessionCookie?.expires) return false;
// Consider session invalid 5 minutes before actual expiry for safety margin
const expiresAt = new Date(sessionCookie.expires).getTime();
return Date.now() < expiresAt - 5 * 60 * 1000;
}
function getSessionExpiry(): Date | null {
const cookies = cookieJar.getCookiesSync("https://hytale.com");
const sessionCookie = cookies.find(c => c.key === "ory_kratos_session");
if (sessionCookie?.expires) {
return new Date(sessionCookie.expires);
}
return null;
}
async function login(): Promise<void> {
console.log("Initializing login flow...");
const initResponse = await fetchWithCookies(
"https://backend.accounts.hytale.com/self-service/login/browser",
{ redirect: "manual" }
);
const location = initResponse.headers.get("location");
const flowMatch = location?.match(/flow=([a-f0-9-]+)/);
if (!flowMatch) {
throw new Error("Could not extract flow ID");
}
const flowId = flowMatch[1];
console.log("Flow ID:", flowId);
const cookies = await cookieJar.getCookies("https://backend.accounts.hytale.com");
const csrfCookie = cookies.find(c => c.key.startsWith("csrf_token"));
if (!csrfCookie) {
throw new Error("Could not find CSRF token cookie");
}
console.log("Submitting login...");
const formData = new URLSearchParams({
csrf_token: csrfCookie.value,
identifier: CREDENTIALS.identifier,
password: CREDENTIALS.password,
method: "password",
});
const loginResponse = await fetchWithCookies(
`https://backend.accounts.hytale.com/self-service/login?flow=${flowId}`,
{
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: formData,
redirect: "manual",
}
);
const redirectLocation = loginResponse.headers.get("location");
if (loginResponse.status !== 303 || !redirectLocation?.includes("/settings")) {
const body = await loginResponse.text();
throw new Error(`Login failed: ${loginResponse.status} - ${body.slice(0, 200)}`);
}
await fetchWithCookies(redirectLocation, { redirect: "follow" });
console.log("Login successful!");
}
async function ensureLoggedIn(): Promise<void> {
if (isSessionValid()) return;
console.log("Session expired, logging in...");
try {
await login();
} catch (error) {
console.error("Failed to login, terminating...");
process.kill(process.pid, "SIGTERM");
}
}
type CheckResult =
| { ok: true; available: boolean; cached: boolean }
| { ok: false; error: "hytale_api_error"; status: number }
| { ok: false; error: "rate_limited"; retryAfter: number };
async function checkUsername(username: string, ip: string): Promise<CheckResult> {
// Check cache first - no rate limit needed for cached
const cached = await getCachedAvailability(username);
if (cached !== null) {
return { ok: true, available: cached, cached: true };
}
// Not cached - check rate limit before hitting API
const rateLimit = await ratelimit.limit(ip);
if (!rateLimit.success) {
return { ok: false, error: "rate_limited", retryAfter: Math.ceil((rateLimit.reset - Date.now()) / 1000) };
}
// Check API
const response = await fetchWithCookies(
`https://accounts.hytale.com/api/account/username-reservations/availability?username=${encodeURIComponent(username)}`
);
if (response.status !== 200 && response.status !== 400) {
console.error(`Hytale API error: ${response.status}`);
if (response.status === 429) {
process.kill(process.pid, "SIGTERM");
}
return { ok: false, error: "hytale_api_error", status: response.status };
}
const available = response.status === 200;
// Cache the result
await setCachedAvailability(username, available);
return { ok: true, available, cached: false };
}
await ensureLoggedIn();
console.log("Connected to Redis");
const allowedOrigins = (process.env.ALLOWED_ORIGINS || "").split(",").map(origin => origin.trim());
const app = new Elysia()
.use(cors({
origin: process.env.ALLOWED_ORIGINS || "*"
}))
.get("/", () => ({
message: "Hytale Username Checker API",
endpoints: {
"GET /check/:username": "Check if a username is available",
"GET /status": "Get session status",
},
}))
.get("/check/:username", async ({ params, set, request, server }) => {
await ensureLoggedIn();
const ip = request.headers.get('cf-connecting-ip') ?? request.headers.get('x-real-ip') ?? server?.requestIP(request)?.address ?? 'unknown';
const { username } = params;
const result = await checkUsername(username, ip);
if (!result.ok) {
if (result.error === "rate_limited") {
set.status = 429;
set.headers["retry-after"] = String(result.retryAfter);
return { error: "rate_limited", retryAfter: result.retryAfter };
}
set.status = 502;
return { error: "hytale_api_error", status: result.status };
}
console.log(`Username "${username}" is ${result.available ? "available" : "taken"}${result.cached ? " (cached)" : ""}`);
return {
username,
available: result.available,
cached: result.cached,
};
})
.get("/status", () => {
const loggedIn = isSessionValid();
const expiresAt = getSessionExpiry();
const hoursLeft = expiresAt
? (expiresAt.getTime() - Date.now()) / 1000 / 60 / 60
: null;
return {
loggedIn,
expiresAt: expiresAt?.toISOString() ?? null,
hoursLeft: hoursLeft ? Number(hoursLeft.toFixed(2)) : null,
};
})
.listen(8080);
console.log(`🚀 Server running at http://localhost:${app.server?.port}`);