-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxyStatus.js
More file actions
138 lines (124 loc) · 3.48 KB
/
Copy pathproxyStatus.js
File metadata and controls
138 lines (124 loc) · 3.48 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
'use strict';
const { proxySettings, proxyLogLabel } = require('./browserLaunch');
const { networkErrorHint } = require('./auth');
const LOGGED_IN_HINT = 'a.layoutButton.adventure, input[name="name"], input[name="password"]';
/**
* Proxy configuration for the GUI (no connectivity test).
* @param {object} [cfg]
*/
function getProxyInfo(cfg) {
const p = proxySettings(cfg);
const missingServer = p.enabled && !p.server;
const configured = p.enabled && !!p.server;
const pool = p.serverCount > 1 ? ` [${(p.serverIndex ?? 0) + 1}/${p.serverCount}]` : '';
let display = 'Off';
if (configured) {
const base = p.username ? `${p.server} (${p.username})` : p.server;
display = `${base}${pool}`;
} else if (missingServer) {
display = 'Enabled — server not set';
}
return {
enabled: p.enabled,
configured,
missingServer,
server: p.server || '',
servers: p.servers || [],
serverCount: p.serverCount || 0,
serverIndex: p.serverIndex ?? 0,
rotation: p.rotation || 'round-robin',
username: p.username || '',
hasAuth: !!(p.username || p.password),
bypass: p.bypass || '',
display,
label: proxyLogLabel(cfg),
};
}
/**
* Navigate to the game URL through the active browser context (uses proxy if configured).
* @param {import('playwright').Page} page
* @param {object} cfg
*/
async function testProxyWithPage(page, cfg) {
const info = getProxyInfo(cfg);
if (!info.configured) {
return {
...info,
state: 'off',
working: null,
message: info.missingServer ? 'Enable proxy.server in config.json' : 'Proxy disabled',
latencyMs: null,
checkedAt: new Date().toISOString(),
};
}
const t0 = Date.now();
const base = String(cfg.url || '').replace(/\/+$/, '');
if (!base) {
return {
...info,
state: 'fail',
working: false,
message: 'config.url is empty',
latencyMs: 0,
checkedAt: new Date().toISOString(),
};
}
try {
// Use the Travian home URL so the test goes through the same proxy as gameplay.
await page.goto(`${base}/`, { waitUntil: 'domcontentloaded', timeout: 25_000 });
const url = page.url();
const hasShell =
/dorf\d|village|travian/i.test(url) ||
!!(await page.$(LOGGED_IN_HINT).catch(() => null));
const latencyMs = Date.now() - t0;
if (hasShell) {
return {
...info,
state: 'ok',
working: true,
message: `Reached Travian (${Math.round(latencyMs / 1000)}s)`,
latencyMs,
checkedAt: new Date().toISOString(),
finalUrl: url,
};
}
return {
...info,
state: 'fail',
working: false,
message: `Unexpected page: ${url.slice(0, 80)}`,
latencyMs,
checkedAt: new Date().toISOString(),
finalUrl: url,
};
} catch (err) {
return {
...info,
state: 'fail',
working: false,
message: networkErrorHint(err),
latencyMs: Date.now() - t0,
checkedAt: new Date().toISOString(),
};
}
}
/** Status object when proxy is on but there is no browser page yet. */
function proxyStatusWithoutSession(cfg) {
const info = getProxyInfo(cfg);
if (!info.configured) {
return { ...info, state: 'off', working: null, message: 'Proxy disabled' };
}
return {
...info,
state: 'unknown',
working: null,
message: 'Log in to test proxy',
latencyMs: null,
checkedAt: null,
};
}
module.exports = {
getProxyInfo,
testProxyWithPage,
proxyStatusWithoutSession,
};