Browse Source

added config options to sonarr socks api endpoint

CauseFX 5 years ago
parent
commit
8cb6040b30
3 changed files with 69 additions and 25 deletions
  1. 30 0
      api/classes/organizr.class.php
  2. 2 0
      api/config/default.php
  3. 37 25
      api/v2/routes/socks.php

+ 30 - 0
api/classes/organizr.class.php

@@ -5734,6 +5734,36 @@ class Organizr
 							'value' => $this->config['sonarrToken']
 						)
 					),
+					'API SOCKS' => array(
+						array(
+							'type' => 'html',
+							'override' => 12,
+							'label' => '',
+							'html' => '
+							<div class="panel panel-default">
+								<div class="panel-wrapper collapse in">
+									<div class="panel-body">
+										<h3 lang="en">Sonarr SOCKS API Connection</h3>
+										<p>Using this feature allows you to access the Sonarr API without having to reverse proxy it.  Just access it from: </p>
+										<code>' . $this->getServerPath() . 'api/v2/socks/sonarr/</code>
+									</div>
+								</div>
+							</div>'
+						),
+						array(
+							'type' => 'switch',
+							'name' => 'sonarrSocksEnabled',
+							'label' => 'Enable',
+							'value' => $this->config['sonarrSocksEnabled']
+						),
+						array(
+							'type' => 'select',
+							'name' => 'sonarrSocksAuth',
+							'label' => 'Minimum Authentication',
+							'value' => $this->config['sonarrSocksAuth'],
+							'options' => $groups
+						),
+					),
 					'Queue' => array(
 						array(
 							'type' => 'switch',

+ 2 - 0
api/config/default.php

@@ -57,6 +57,8 @@ return array(
 	'sonarrURL' => '',
 	'sonarrUnmonitored' => false,
 	'sonarrToken' => '',
+	'sonarrSocksEnabled' => false,
+	'sonarrSocksAuth' => '999',
 	'lidarrURL' => '',
 	'lidarrToken' => '',
 	'radarrURL' => '',

+ 37 - 25
api/v2/routes/socks.php

@@ -1,33 +1,45 @@
 <?php
 $app->any('/socks/sonarr/{route:.*}', function ($request, $response) {
 	$Organizr = ($request->getAttribute('Organizr')) ?? new Organizr();
-	$new = str_ireplace('/api/v2/socks/sonarr', '', $request->getUri()->getPath());
-	$getParams = ($_GET) ? '?' . http_build_query($_GET) : '';
-	$url = $Organizr->qualifyURL($Organizr->config['sonarrURL']) . $new . $getParams;
-	$url = $Organizr->cleanPath($url);
-	$options = ($Organizr->localURL($url)) ? array('verify' => false) : array();
-	$headers = [];
-	if ($request->hasHeader('X-Api-Key')) {
-		$headerKey = $request->getHeaderLine('X-Api-Key');
-		$headers['X-Api-Key'] = $headerKey;
+	$error = false;
+	if (!$Organizr->config['sonarrSocksEnabled']) {
+		$error = true;
+		$Organizr->setAPIResponse('error', 'Sonarr SOCKS module is not enabled', 409);
 	}
-	switch ($request->getMethod()) {
-		case 'GET':
-			$call = Requests::get($url, $headers, $options);
-			break;
-		case 'POST':
-			$call = Requests::post($url, $headers, $Organizr->apiData($request), $options);
-			break;
-		case 'DELETE':
-			$call = Requests::delete($url, $headers, $options);
-			break;
-		case 'PUT':
-			$call = Requests::put($url, $headers, $Organizr->apiData($request), $options);
-			break;
-		default:
-			$call = Requests::get($url, $headers, $options);
+	if (!$Organizr->qualifyRequest($Organizr->config['sonarrSocksAuth'], true)) {
+		$error = true;
+	}
+	if (!$error) {
+		$new = str_ireplace('/api/v2/socks/sonarr', '', $request->getUri()->getPath());
+		$getParams = ($_GET) ? '?' . http_build_query($_GET) : '';
+		$url = $Organizr->qualifyURL($Organizr->config['sonarrURL']) . $new . $getParams;
+		$url = $Organizr->cleanPath($url);
+		$options = ($Organizr->localURL($url)) ? array('verify' => false) : array();
+		$headers = [];
+		if ($request->hasHeader('X-Api-Key')) {
+			$headerKey = $request->getHeaderLine('X-Api-Key');
+			$headers['X-Api-Key'] = $headerKey;
+		}
+		switch ($request->getMethod()) {
+			case 'GET':
+				$call = Requests::get($url, $headers, $options);
+				break;
+			case 'POST':
+				$call = Requests::post($url, $headers, $Organizr->apiData($request), $options);
+				break;
+			case 'DELETE':
+				$call = Requests::delete($url, $headers, $options);
+				break;
+			case 'PUT':
+				$call = Requests::put($url, $headers, $Organizr->apiData($request), $options);
+				break;
+			default:
+				$call = Requests::get($url, $headers, $options);
+		}
+		$response->getBody()->write($call->body);
+	} else {
+		$response->getBody()->write(jsonE($GLOBALS['api']));
 	}
-	$response->getBody()->write($call->body);
 	return $response
 		->withHeader('Content-Type', 'application/json;charset=UTF-8')
 		->withStatus($GLOBALS['responseCode']);