Skip to content
This repository was archived by the owner on Mar 31, 2025. It is now read-only.

Commit 58036dc

Browse files
cbundymattwebbio
authored andcommitted
remove static /admin path from all requests and set default
1 parent cc1237e commit 58036dc

File tree

6 files changed

+74
-67
lines changed

6 files changed

+74
-67
lines changed

src/client.test.ts

+20-20
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ describe('Client', () => {
1010
const host = new Host('http://10.0.0.2', 'mypassword');
1111

1212
const createClient = async () => {
13-
nock(host.fullUrl).get('/admin/index.php?login').reply(200);
13+
nock(host.fullUrl).get('/index.php?login').reply(200);
1414
nock(host.fullUrl)
15-
.post('/admin/index.php?login')
15+
.post('/index.php?login')
1616
.reply(
1717
200,
1818
'<html><body><div id="token">abcdefgijklmnopqrstuvwxyzabcdefgijklmnopqrst</div></body></html>'
@@ -27,8 +27,8 @@ describe('Client', () => {
2727

2828
describe('create', () => {
2929
test('should throw error if status code is not ok', async () => {
30-
const initialRequest = nock(host.fullUrl).get('/admin/index.php?login').reply(200);
31-
const loginRequest = nock(host.fullUrl).post('/admin/index.php?login').reply(500);
30+
const initialRequest = nock(host.fullUrl).get('/index.php?login').reply(200);
31+
const loginRequest = nock(host.fullUrl).post('/index.php?login').reply(500);
3232

3333
const expectError = expect(Client.create(host)).rejects;
3434

@@ -47,8 +47,8 @@ describe('Client', () => {
4747
});
4848

4949
test('should throw error if no token is present', async () => {
50-
const initialRequest = nock(host.fullUrl).get('/admin/index.php?login').reply(200);
51-
const loginRequest = nock(host.fullUrl).post('/admin/index.php?login').reply(200);
50+
const initialRequest = nock(host.fullUrl).get('/index.php?login').reply(200);
51+
const loginRequest = nock(host.fullUrl).post('/index.php?login').reply(200);
5252

5353
const expectError = expect(Client.create(host)).rejects;
5454

@@ -66,9 +66,9 @@ describe('Client', () => {
6666
});
6767

6868
test('should throw error if token is in incorrect format', async () => {
69-
const initialRequest = nock(host.fullUrl).get('/admin/index.php?login').reply(200);
69+
const initialRequest = nock(host.fullUrl).get('/index.php?login').reply(200);
7070
const loginRequest = nock(host.fullUrl)
71-
.post('/admin/index.php?login')
71+
.post('/index.php?login')
7272
.reply(200, '<html><body><div id="token">abcdef</div></body></html>');
7373

7474
const expectError = expect(Client.create(host)).rejects;
@@ -87,9 +87,9 @@ describe('Client', () => {
8787
});
8888

8989
test('should return client', async () => {
90-
const initialRequest = nock(host.fullUrl).get('/admin/index.php?login').reply(200);
90+
const initialRequest = nock(host.fullUrl).get('/index.php?login').reply(200);
9191
const loginRequest = nock(host.fullUrl)
92-
.post('/admin/index.php?login')
92+
.post('/index.php?login')
9393
.reply(
9494
200,
9595
'<html><body><div id="token">abcdefgijklmnopqrstuvwxyzabcdefgijklmnopqrst</div></body></html>'
@@ -115,7 +115,7 @@ describe('Client', () => {
115115
});
116116

117117
test('should throw error if response is non-200', async () => {
118-
teleporter.post('/admin/scripts/pi-hole/php/teleporter.php').reply(500);
118+
teleporter.post('/scripts/pi-hole/php/teleporter.php').reply(500);
119119

120120
const expectError = expect(client.downloadBackup()).rejects;
121121

@@ -132,7 +132,7 @@ describe('Client', () => {
132132

133133
test('should throw error if content type is not gzip', async () => {
134134
teleporter
135-
.post('/admin/scripts/pi-hole/php/teleporter.php')
135+
.post('/scripts/pi-hole/php/teleporter.php')
136136
.reply(200, undefined, { 'content-type': 'text/html' });
137137

138138
const expectError = expect(client.downloadBackup()).rejects;
@@ -153,7 +153,7 @@ describe('Client', () => {
153153

154154
let requestBody = '';
155155
teleporter
156-
.post('/admin/scripts/pi-hole/php/teleporter.php', (body) => (requestBody = body))
156+
.post('/scripts/pi-hole/php/teleporter.php', (body) => (requestBody = body))
157157
.reply(200, undefined, { 'content-type': 'application/gzip' });
158158

159159
const backup = await client.downloadBackup();
@@ -197,7 +197,7 @@ describe('Client', () => {
197197
});
198198

199199
test('should throw error if response is non-200', async () => {
200-
teleporter.post('/admin/scripts/pi-hole/php/teleporter.php').reply(500);
200+
teleporter.post('/scripts/pi-hole/php/teleporter.php').reply(500);
201201

202202
const expectError = expect(client.uploadBackup(backup)).rejects;
203203

@@ -213,7 +213,7 @@ describe('Client', () => {
213213
});
214214

215215
test('should throw error if response does not end with "OK"', async () => {
216-
teleporter.post('/admin/scripts/pi-hole/php/teleporter.php').reply(200);
216+
teleporter.post('/scripts/pi-hole/php/teleporter.php').reply(200);
217217

218218
const expectError = expect(client.uploadBackup(backup)).rejects;
219219

@@ -230,7 +230,7 @@ describe('Client', () => {
230230

231231
test('should throw error if gravity update fails', async () => {
232232
teleporter
233-
.post('/admin/scripts/pi-hole/php/teleporter.php')
233+
.post('/scripts/pi-hole/php/teleporter.php')
234234
.reply(
235235
200,
236236
'Processed adlist (14 entries)<br>\n' +
@@ -248,7 +248,7 @@ describe('Client', () => {
248248
'OK'
249249
);
250250
teleporter
251-
.get('/admin/scripts/pi-hole/php/gravity.sh.php', undefined)
251+
.get('/scripts/pi-hole/php/gravity.sh.php', undefined)
252252
.reply(200, '\ndata: \n\ndata: [✓] TCP (IPv6)\ndata: \ndata: \n\ndata:');
253253

254254
const expectError = expect(client.uploadBackup(backup)).rejects;
@@ -269,7 +269,7 @@ describe('Client', () => {
269269

270270
let requestBody = '';
271271
teleporter
272-
.post('/admin/scripts/pi-hole/php/teleporter.php', (body) => (requestBody = body))
272+
.post('/scripts/pi-hole/php/teleporter.php', (body) => (requestBody = body))
273273
.reply(
274274
200,
275275
'Processed adlist (14 entries)<br>\n' +
@@ -287,7 +287,7 @@ describe('Client', () => {
287287
'OK'
288288
);
289289
teleporter
290-
.get('/admin/scripts/pi-hole/php/gravity.sh.php', undefined)
290+
.get('/scripts/pi-hole/php/gravity.sh.php', undefined)
291291
.reply(
292292
200,
293293
'\ndata: \n\ndata: [✓] TCP (IPv6)\ndata: \ndata: \n\ndata: [✓] Pi-hole blocking is enabled\ndata: \n\ndata:'
@@ -329,7 +329,7 @@ describe('Client', () => {
329329

330330
let requestBody = '';
331331
teleporter
332-
.post('/admin/scripts/pi-hole/php/teleporter.php', (body) => (requestBody = body))
332+
.post('/scripts/pi-hole/php/teleporter.php', (body) => (requestBody = body))
333333
.reply(
334334
200,
335335
'Processed adlist (14 entries)<br>\n' +

src/client.ts

+31-25
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ export class Client {
2121
) { }
2222

2323
public static async create(host: Host): Promise<Client> {
24-
Log.info(chalk.yellow(`➡️ Signing in to ${host.fullUrl}...`));
24+
Log.info(chalk.yellow(`➡️ Signing in to ${host.baseUrl}...`));
2525
const fetch = fetchCookie(nodeFetch);
2626

27-
await fetch(`${host.fullUrl}/admin/index.php?login`, { method: 'GET' });
28-
const response = await fetch(`${host.fullUrl}/admin/index.php?login`, {
27+
await fetch(`${host.fullUrl}/index.php?login`, { method: 'GET' });
28+
const response = await fetch(`${host.fullUrl}/index.php?login`, {
2929
headers: {
3030
'content-type': 'application/x-www-form-urlencoded'
3131
},
@@ -34,17 +34,18 @@ export class Client {
3434
});
3535
if (response.status !== 200)
3636
throw new ErrorNotification({
37-
message: `There was an error logging in to "${host.fullUrl}" - are you able to log in with the configured password?`,
37+
message: `There was an error logging in to "${host.baseUrl}" - are you able to log in with the configured password?`,
3838
verbose: {
39-
host: host.fullUrl,
39+
host: host.baseUrl,
40+
path: host.path,
4041
status: response.status,
4142
responseBody: await response.text()
4243
}
4344
});
4445

4546
const token = this.parseResponseForToken(host, await response.text());
4647

47-
Log.info(chalk.green(`✔️ Successfully signed in to ${host.fullUrl}!`));
48+
Log.info(chalk.green(`✔️ Successfully signed in to ${host.baseUrl}!`));
4849
return new this(fetch, host, token);
4950
}
5051

@@ -53,19 +54,21 @@ export class Client {
5354
const tokenDiv = root.querySelector('#token');
5455
if (!tokenDiv)
5556
throw new ErrorNotification({
56-
message: `No token could be found while logging in to "${host.fullUrl}" - are you able to log in with the configured password?`,
57+
message: `No token could be found while logging in to "${host.baseUrl}" - are you able to log in with the configured password?`,
5758
verbose: {
58-
host: host.fullUrl,
59+
host: host.baseUrl,
60+
path: host.path,
5961
innerHtml: root.innerHTML
6062
}
6163
});
6264

6365
const token = tokenDiv.innerText;
6466
if (token.length != 44)
6567
throw new ErrorNotification({
66-
message: `A token was found but could not be validated while logging in to "${host.fullUrl}" - are you able to log in with the configured password?`,
68+
message: `A token was found but could not be validated while logging in to "${host.baseUrl}" - are you able to log in with the configured password?`,
6769
verbose: {
68-
host: host.fullUrl,
70+
host: host.baseUrl,
71+
path: host.path,
6972
token: token
7073
}
7174
});
@@ -74,11 +77,11 @@ export class Client {
7477
}
7578

7679
public async downloadBackup(): Promise<Blob> {
77-
Log.info(chalk.yellow(`➡️ Downloading backup from ${this.host.fullUrl}...`));
80+
Log.info(chalk.yellow(`➡️ Downloading backup from ${this.host.baseUrl}...`));
7881
const form = this.generateForm();
7982

8083
const response = await this.fetch(
81-
`${this.host.fullUrl}/admin/scripts/pi-hole/php/teleporter.php`,
84+
`${this.host.fullUrl}/scripts/pi-hole/php/teleporter.php`,
8285
{
8386
body: form,
8487
method: 'POST'
@@ -89,29 +92,30 @@ export class Client {
8992
response.headers.get('content-type') !== 'application/gzip'
9093
)
9194
throw new ErrorNotification({
92-
message: `Failed to download backup from "${this.host.fullUrl}".`,
95+
message: `Failed to download backup from "${this.host.baseUrl}".`,
9396
verbose: {
94-
host: this.host.fullUrl,
97+
host: this.host.baseUrl,
98+
path: this.host.path,
9599
status: response.status,
96100
responseBody: await response.text()
97101
}
98102
});
99103

100104
const data = await response.arrayBuffer();
101105

102-
Log.info(chalk.green(`✔️ Backup from ${this.host.fullUrl} completed!`));
106+
Log.info(chalk.green(`✔️ Backup from ${this.host.baseUrl} completed!`));
103107
return new Blob([data]);
104108
}
105109

106110
public async uploadBackup(backup: Blob): Promise<true | never> {
107-
Log.info(chalk.yellow(`➡️ Uploading backup to ${this.host.fullUrl}...`));
111+
Log.info(chalk.yellow(`➡️ Uploading backup to ${this.host.baseUrl}...`));
108112

109113
const form = this.generateForm();
110114
form.append('action', 'in');
111115
form.append('zip_file', backup, 'backup.tar.gz');
112116

113117
const uploadResponse = await this.fetch(
114-
`${this.host.fullUrl}/admin/scripts/pi-hole/php/teleporter.php`,
118+
`${this.host.fullUrl}/scripts/pi-hole/php/teleporter.php`,
115119
{
116120
body: form,
117121
method: 'POST'
@@ -120,21 +124,22 @@ export class Client {
120124
const uploadText = await uploadResponse.text();
121125
if (uploadResponse.status !== 200 || !uploadText.endsWith('OK'))
122126
throw new ErrorNotification({
123-
message: `Failed to upload backup to "${this.host.fullUrl}".`,
127+
message: `Failed to upload backup to "${this.host.baseUrl}".`,
124128
verbose: {
125-
host: this.host.fullUrl,
129+
host: this.host.baseUrl,
130+
path: this.host.path,
126131
status: uploadResponse.status,
127132
responseBody: uploadText
128133
}
129134
});
130135

131-
Log.info(chalk.green(`✔️ Backup uploaded to ${this.host.fullUrl}!`));
136+
Log.info(chalk.green(`✔️ Backup uploaded to ${this.host.baseUrl}!`));
132137
Log.verbose(`Result:\n${chalk.blue(uploadText)}`);
133138

134139
if (Config.updateGravity) {
135-
Log.info(chalk.yellow(`➡️ Updating gravity on ${this.host.fullUrl}...`));
140+
Log.info(chalk.yellow(`➡️ Updating gravity on ${this.host.baseUrl}...`));
136141
const gravityUpdateResponse = await this.fetch(
137-
`${this.host.fullUrl}/admin/scripts/pi-hole/php/gravity.sh.php`,
142+
`${this.host.fullUrl}/scripts/pi-hole/php/gravity.sh.php`,
138143
{ method: 'GET' }
139144
);
140145

@@ -146,15 +151,16 @@ export class Client {
146151
!updateText.endsWith('Pi-hole blocking is enabled')
147152
)
148153
throw new ErrorNotification({
149-
message: `Failed updating gravity on "${this.host.fullUrl}".`,
154+
message: `Failed updating gravity on "${this.host.baseUrl}".`,
150155
verbose: {
151-
host: this.host.fullUrl,
156+
host: this.host.baseUrl,
157+
path: this.host.path,
152158
status: gravityUpdateResponse.status,
153159
eventStream: updateText
154160
}
155161
});
156162

157-
Log.info(chalk.green(`✔️ Gravity updated on ${this.host.fullUrl}!`));
163+
Log.info(chalk.green(`✔️ Gravity updated on ${this.host.baseUrl}!`));
158164
Log.verbose(`Result:\n${chalk.blue(updateText)}`);
159165
}
160166

src/config.test.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ describe('Config', () => {
9191

9292
const expected = {
9393
baseUrl: 'http://10.0.0.2',
94-
fullUrl: 'http://10.0.0.2',
95-
path: '',
94+
fullUrl: 'http://10.0.0.2/admin',
95+
path: '/admin',
9696
password: 'mypassword'
9797
};
9898

@@ -135,8 +135,8 @@ describe('Config', () => {
135135
{
136136
baseUrl: 'http://10.0.0.3',
137137
password: 'mypassword',
138-
fullUrl: 'http://10.0.0.3',
139-
path: ''
138+
fullUrl: 'http://10.0.0.3/admin',
139+
path: '/admin'
140140
}
141141
];
142142

@@ -159,20 +159,20 @@ describe('Config', () => {
159159
{
160160
baseUrl: 'http://10.0.0.3',
161161
password: 'mypassword1',
162-
fullUrl: 'http://10.0.0.3',
163-
path: ''
162+
fullUrl: 'http://10.0.0.3/admin',
163+
path: '/admin'
164164
},
165165
{
166166
baseUrl: 'http://10.0.0.4',
167167
password: 'mypassword2',
168-
fullUrl: 'http://10.0.0.4',
169-
path: ''
168+
fullUrl: 'http://10.0.0.4/admin',
169+
path: '/admin'
170170
},
171171
{
172172
baseUrl: 'http://10.0.0.5',
173173
password: 'mypassword3',
174-
fullUrl: 'http://10.0.0.5',
175-
path: ''
174+
fullUrl: 'http://10.0.0.5/admin',
175+
path: '/admin'
176176
}
177177
]);
178178
});
@@ -187,10 +187,10 @@ describe('Config', () => {
187187
process.env['SECONDARY_HOST_2_BASE_URL'] = 'http://10.0.0.4';
188188
process.env['SECONDARY_HOST_2_PASSWORD'] = 'mypassword3';
189189

190-
expect(Config.allHostBaseUrls).toStrictEqual([
191-
'http://10.0.0.2',
192-
'http://10.0.0.3',
193-
'http://10.0.0.4'
190+
expect(Config.allHostUrls).toStrictEqual([
191+
'http://10.0.0.2/admin',
192+
'http://10.0.0.3/admin',
193+
'http://10.0.0.4/admin'
194194
]);
195195
});
196196
});

src/config.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ export class Config {
4242
return this._secondaryHosts;
4343
}
4444

45-
static get allHostBaseUrls(): string[] {
45+
//TODO look at usages
46+
static get allHostUrls(): string[] {
4647
return [this.primaryHost, ...this.secondaryHosts].map((host) => host.fullUrl);
4748
}
4849

@@ -162,13 +163,13 @@ export interface SyncOptions {
162163
}
163164

164165
export class Host {
165-
private baseUrl: string;
166-
private path: string;
166+
baseUrl: string;
167+
path: string;
167168
fullUrl: string;
168169
password: string;
169170

170171
constructor(baseUrl: string, password: string, path?: string) {
171-
this.path = path ?? '';
172+
this.path = path ?? '/admin';
172173
this.baseUrl = baseUrl;
173174
this.password = password
174175
this.fullUrl = this.baseUrl + this.path

0 commit comments

Comments
 (0)