Skip to content

Commit 37f43f1

Browse files
add support for proxy connections (#12)
* add support for proxy connections
1 parent a554079 commit 37f43f1

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

library/Servicenowimport/Api/Servicenow.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function __construct(string $baseUri, bool $tlsVerify = true, int $timeou
3131
];
3232

3333
$method = $auth['method'] ?? 'UNKNOWN';
34+
$proxyType = $auth['proxy_type'] ?? 'UNKNOWN';
3435

3536
try {
3637
if ($method === 'BASIC') {
@@ -44,6 +45,26 @@ public function __construct(string $baseUri, bool $tlsVerify = true, int $timeou
4445
throw new RuntimeException(sprintf("Failed to set authentication method %s", $e->getMessage()));
4546
}
4647

48+
try {
49+
$proxyAddress = parse_url($auth['proxy_address']);
50+
$proxyHost = $proxyAddress['host'];
51+
$proxyScheme = 'https';
52+
$proxyPort = 443;
53+
54+
if ($proxyType === 'HTTP') {
55+
$proxyScheme = $proxyAddress['scheme'] ?? 'https';
56+
$defaultPort = $proxyScheme === 'http' ? 80 : 443;
57+
$proxyPort = $proxyAddress['port'] ?? $defaultPort;
58+
} elseif ($proxyType === 'SOCKS5') {
59+
$proxyScheme = 'socks5';
60+
$proxyPort = $proxyAddress['port'] ?? 1028;
61+
}
62+
63+
$c['proxy'] = sprintf('%s://%s:%s@%s:%d', $proxyScheme, $auth['proxy_user'], $auth['proxy_password'], $proxyHost, $proxyPort);
64+
} catch (\Exception $e) {
65+
throw new RuntimeException(sprintf("Failed to set proxy method %s", $e->getMessage()));
66+
}
67+
4768
$this->client = new Client($c);
4869
}
4970

library/Servicenowimport/ProvidedHook/Director/ImportSource.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ public static function addSettingsFormFields(QuickForm $form)
135135
. 'https://www.servicenow.com/docs/bundle/yokohama-platform-user-interface/page/use/using-lists/concept/c_EncodedQueryStrings.html',
136136
]
137137
);
138+
139+
static::addProxy($form);
138140
}
139141

140142
/**
@@ -201,6 +203,10 @@ private function getRecordsFromTable(): array
201203
'token' => $this->getSetting('servicenow_token'),
202204
'username' => $this->getSetting('servicenow_username'),
203205
'password' => $this->getSetting('servicenow_password'),
206+
'proxy_type' => $this->getSetting('proxy_type'),
207+
'proxy_address' => $this->getSetting('proxy'),
208+
'proxy_user' => $this->getSetting('proxy_user'),
209+
'proxy_password' => $this->getSetting('proxy_pass'),
204210
];
205211

206212
$client = new Servicenow(
@@ -220,6 +226,50 @@ private function getRecordsFromTable(): array
220226
]
221227
);
222228

229+
230+
223231
return json_decode($result)->result;
224232
}
233+
234+
protected static function addProxy(QuickForm $form)
235+
{
236+
$form->addElement('select', 'proxy_type', [
237+
'label' => $form->translate('Proxy'),
238+
'description' => $form->translate(
239+
'In case your API is only reachable through a proxy, please'
240+
. ' choose it\'s protocol right here'
241+
),
242+
'multiOptions' => $form->optionalEnum([
243+
'HTTP' => $form->translate('HTTP proxy'),
244+
'SOCKS5' => $form->translate('SOCKS5 proxy'),
245+
]),
246+
'class' => 'autosubmit'
247+
]);
248+
249+
$proxyType = $form->getSentOrObjectSetting('proxy_type');
250+
251+
if ($proxyType) {
252+
$form->addElement('text', 'proxy', [
253+
'label' => $form->translate('Proxy Address'),
254+
'description' => $form->translate(
255+
'Hostname, IP or <host>:<port>'
256+
),
257+
'required' => true,
258+
]);
259+
if ($proxyType === 'HTTP') {
260+
$form->addElement('text', 'proxy_user', [
261+
'label' => $form->translate('Proxy Username'),
262+
'description' => $form->translate(
263+
'In case your proxy requires authentication, please'
264+
. ' configure this here'
265+
),
266+
]);
267+
268+
$form->addElement('storedPassword', 'proxy_pass', [
269+
'label' => $form->translate('Proxy Password'),
270+
'required' => strlen((string) $form->getSentOrObjectSetting('proxy_user')) > 0
271+
]);
272+
}
273+
}
274+
}
225275
}

module.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Name: ServiceNow Director Import
22
Module: servicenowimport
3-
Version: 0.2.0
3+
Version: 0.3.0
44
Depends: director (>=1.10.4)
55
Description: Director Import Source for ServiceNow
66
This module fetches data from the ServiceNow API for the Icinga Director import.

0 commit comments

Comments
 (0)