diff options
-rw-r--r-- | lib/Api/v1/Circles.php | 4 | ||||
-rw-r--r-- | lib/AppInfo/Application.php | 4 | ||||
-rw-r--r-- | lib/Controller/BaseController.php | 12 | ||||
-rw-r--r-- | lib/Controller/CirclesController.php | 4 | ||||
-rw-r--r-- | lib/Controller/FederatedController.php | 14 | ||||
-rw-r--r-- | lib/Controller/SharesController.php | 4 | ||||
-rw-r--r-- | lib/Service/ConfigService.php | 21 | ||||
-rw-r--r-- | lib/Service/FederatedLinkService.php (renamed from lib/Service/FederatedService.php) | 187 | ||||
-rw-r--r-- | lib/Service/SharesService.php | 189 |
9 files changed, 227 insertions, 212 deletions
diff --git a/lib/Api/v1/Circles.php b/lib/Api/v1/Circles.php index d324676b..0ece8294 100644 --- a/lib/Api/v1/Circles.php +++ b/lib/Api/v1/Circles.php @@ -34,7 +34,7 @@ use OCA\Circles\Model\FederatedLink; use OCA\Circles\Model\Member; use OCA\Circles\Model\SharingFrame; use OCA\Circles\Service\CirclesService; -use OCA\Circles\Service\FederatedService; +use OCA\Circles\Service\FederatedLinkService; use OCA\Circles\Service\MembersService; use OCA\Circles\Service\MiscService; use OCA\Circles\Service\SharesService; @@ -370,7 +370,7 @@ class Circles { public static function linkCircle($circleUniqueId, $remote) { $c = self::getContainer(); - return $c->query(FederatedService::class) + return $c->query(FederatedLinkService::class) ->linkCircle($circleUniqueId, $remote); } diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 720ae4dd..7eb0b731 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -42,7 +42,7 @@ use OCA\Circles\Events\UserEvents; use OCA\Circles\Service\BroadcastService; use OCA\Circles\Service\CirclesService; use OCA\Circles\Service\EventsService; -use OCA\Circles\Service\FederatedService; +use OCA\Circles\Service\FederatedLinkService; use OCA\Circles\Service\GroupsService; use OCA\Circles\Service\MembersService; use OCA\Circles\Service\ConfigService; @@ -57,6 +57,8 @@ class Application extends App { const APP_NAME = 'circles'; + const REMOTE_URL_LINK = '/index.php/apps/circles/v1/link'; + const REMOTE_URL_PAYLOAD = '/index.php/apps/circles/v1/payload'; /** * @param array $params diff --git a/lib/Controller/BaseController.php b/lib/Controller/BaseController.php index d83949bd..ca76f691 100644 --- a/lib/Controller/BaseController.php +++ b/lib/Controller/BaseController.php @@ -30,7 +30,7 @@ use OC\AppFramework\Http; use OCA\Circles\Service\BroadcastService; use OCA\Circles\Service\CirclesService; use OCA\Circles\Service\ConfigService; -use OCA\Circles\Service\FederatedService; +use OCA\Circles\Service\FederatedLinkService; use OCA\Circles\Service\GroupsService; use OCA\Circles\Service\MembersService; @@ -71,8 +71,8 @@ class BaseController extends Controller { /** @var BroadcastService */ protected $broadcastService; - /** @var FederatedService */ - protected $federatedService; + /** @var FederatedLinkService */ + protected $federatedLinkService; /** @var MiscService */ protected $miscService; @@ -92,7 +92,7 @@ class BaseController extends Controller { * @param GroupsService $groupsService * @param SharesService $sharesService * @param BroadcastService $broadcastService - * @param FederatedService $federatedService + * @param FederatedLinkService $federatedLinkService * @param MiscService $miscService */ public function __construct( @@ -107,7 +107,7 @@ class BaseController extends Controller { GroupsService $groupsService, SharesService $sharesService, BroadcastService $broadcastService, - FederatedService $federatedService, + FederatedLinkService $federatedLinkService, MiscService $miscService ) { parent::__construct($appName, $request); @@ -121,7 +121,7 @@ class BaseController extends Controller { $this->groupsService = $groupsService; $this->sharesService = $sharesService; $this->broadcastService = $broadcastService; - $this->federatedService = $federatedService; + $this->federatedLinkService = $federatedLinkService; $this->miscService = $miscService; } diff --git a/lib/Controller/CirclesController.php b/lib/Controller/CirclesController.php index 14b13c7a..be577c18 100644 --- a/lib/Controller/CirclesController.php +++ b/lib/Controller/CirclesController.php @@ -203,7 +203,7 @@ class CirclesController extends BaseController { */ public function link($uniqueId, $remote) { try { - $link = $this->federatedService->linkCircle($uniqueId, $remote); + $link = $this->federatedLinkService->linkCircle($uniqueId, $remote); $links = $this->circlesService->detailsCircle($uniqueId) ->getLinks(); @@ -244,7 +244,7 @@ class CirclesController extends BaseController { } try { - $links = $this->federatedService->linkStatus($linkId, $status); + $links = $this->federatedLinkService->linkStatus($linkId, $status); return $this->success(['link_id' => $linkId, 'links' => $links]); } catch (\Exception $e) { diff --git a/lib/Controller/FederatedController.php b/lib/Controller/FederatedController.php index 26e8d253..80660d72 100644 --- a/lib/Controller/FederatedController.php +++ b/lib/Controller/FederatedController.php @@ -36,7 +36,7 @@ use OCA\Circles\Model\FederatedLink; use OCA\Circles\Model\SharingFrame; use OCA\Circles\Service\CirclesService; use OCA\Circles\Service\ConfigService; -use OCA\Circles\Service\FederatedService; +use OCA\Circles\Service\FederatedLinkService; use OCA\Circles\Service\MembersService; use OCA\Circles\Service\MiscService; use OCA\Circles\Service\SharesService; @@ -63,8 +63,8 @@ class FederatedController extends BaseController { /** @var SharesService */ protected $sharesService; - /** @var FederatedService */ - protected $federatedService; + /** @var FederatedLinkService */ + protected $federatedLinkService; /** @var MiscService */ protected $miscService; @@ -105,7 +105,7 @@ class FederatedController extends BaseController { ->setRemoteCircleName($sourceName) ->setAddress($address); - $this->federatedService->initiateLink($circle, $link); + $this->federatedLinkService->initiateLink($circle, $link); return $this->federatedSuccess( ['status' => $link->getStatus(), 'uniqueId' => $circle->getUniqueId(true)], $link @@ -147,7 +147,7 @@ class FederatedController extends BaseController { try { Circles::compareVersion($apiVersion); $frame = SharingFrame::fromJSON($item); - $this->federatedService->receiveFrame($token, $uniqueId, $frame); + $this->sharesService->receiveFrame($token, $uniqueId, $frame); } catch (SharingFrameAlreadyExistException $e) { return $this->federatedSuccess(); } catch (Exception $e) { @@ -156,7 +156,7 @@ class FederatedController extends BaseController { $this->miscService->asyncAndLeaveClientOutOfThis('done'); $this->broadcastService->broadcastFrame($frame); - $this->federatedService->sendRemoteShare($frame); + $this->sharesService->forwardSharingFrame($frame); exit(); } @@ -184,7 +184,7 @@ class FederatedController extends BaseController { try { Circles::compareVersion($apiVersion); - $link = $this->federatedService->updateLinkFromRemote($token, $uniqueId, $status); + $link = $this->federatedLinkService->updateLinkFromRemote($token, $uniqueId, $status); } catch (Exception $e) { return $this->federatedFail($e->getMessage()); } diff --git a/lib/Controller/SharesController.php b/lib/Controller/SharesController.php index f69adaa5..77615487 100644 --- a/lib/Controller/SharesController.php +++ b/lib/Controller/SharesController.php @@ -112,9 +112,9 @@ class SharesController extends BaseController { $this->broadcastService->broadcastFrame($frame); // TODO - do not update cloudId to avoid duplicate, use it's own field and keep cloudId - $this->federatedService->updateFrameWithCloudId($frame); + $this->sharesService->updateFrameWithCloudId($frame); if ($this->configService->isFederatedCirclesAllowed()) { - $this->federatedService->sendRemoteShare($frame); + $this->sharesService->forwardSharingFrame($frame); } exit(); diff --git a/lib/Service/ConfigService.php b/lib/Service/ConfigService.php index 77ee9cd7..d8dfcb55 100644 --- a/lib/Service/ConfigService.php +++ b/lib/Service/ConfigService.php @@ -172,6 +172,27 @@ class ConfigService { return ($this->allowedNonSSLLinks === 1); } + + + + /** + * @param string $remote + * + * @return string + */ + public function generateRemoteHost($remote) { + if ((!$this->isNonSSLLinksAllowed() || strpos($remote, 'http://') !== 0) + && strpos($remote, 'https://') !== 0 + ) { + $remote = 'https://' . $remote; + } + + return rtrim($remote, '/'); + } + + + + /** * Get a value by key * diff --git a/lib/Service/FederatedService.php b/lib/Service/FederatedLinkService.php index 8de3a275..25deca0e 100644 --- a/lib/Service/FederatedService.php +++ b/lib/Service/FederatedLinkService.php @@ -28,34 +28,26 @@ namespace OCA\Circles\Service; use Exception; -use OC\Http\Client\ClientService; use OCA\Circles\Api\v1\Circles; +use OCA\Circles\AppInfo\Application; use OCA\Circles\Db\CirclesRequest; use OCA\Circles\Db\FederatedLinksRequest; -use OCA\Circles\Exceptions\CircleDoesNotExistException; +use OCA\Circles\Exceptions\CircleTypeNotValidException; use OCA\Circles\Exceptions\FederatedCircleLinkFormatException; use OCA\Circles\Exceptions\FederatedCircleNotAllowedException; -use OCA\Circles\Exceptions\CircleTypeNotValidException; use OCA\Circles\Exceptions\FederatedCircleStatusUpdateException; +use OCA\Circles\Exceptions\FederatedLinkCreationException; use OCA\Circles\Exceptions\FederatedLinkUpdateException; use OCA\Circles\Exceptions\FederatedRemoteCircleDoesNotExistException; use OCA\Circles\Exceptions\FederatedRemoteDoesNotAllowException; use OCA\Circles\Exceptions\FederatedRemoteIsDownException; -use OCA\Circles\Exceptions\PayloadDeliveryException; -use OCA\Circles\Exceptions\SharingFrameAlreadyExistException; -use OCA\Circles\Exceptions\FederatedLinkCreationException; use OCA\Circles\Exceptions\MemberIsNotAdminException; -use OCA\Circles\Exceptions\SharingFrameDoesNotExistException; use OCA\Circles\Model\Circle; use OCA\Circles\Model\FederatedLink; -use OCA\Circles\Model\SharingFrame; use OCP\Http\Client\IClientService; use OCP\IL10N; -class FederatedService { - - const REMOTE_URL_LINK = '/index.php/apps/circles/v1/link'; - const REMOTE_URL_PAYLOAD = '/index.php/apps/circles/v1/payload'; +class FederatedLinkService { /** @var string */ private $userId; @@ -81,9 +73,6 @@ class FederatedService { /** @var EventsService */ private $eventsService; - /** @var string */ - private $serverHost; - /** @var IClientService */ private $clientService; @@ -119,7 +108,6 @@ class FederatedService { $this->broadcastService = $broadcastService; $this->federatedLinksRequest = $federatedLinksRequest; $this->eventsService = $eventsService; - $this->serverHost = $this->configService->getLocalAddress(); $this->clientService = $clientService; $this->miscService = $miscService; @@ -280,7 +268,7 @@ class FederatedService { $link = new FederatedLink(); $link->setCircleId($circleUniqueId) - ->setLocalAddress($this->serverHost) + ->setLocalAddress($this->configService->getLocalAddress()) ->setAddress($remoteAddress) ->setRemoteCircleName($remoteCircle) ->setStatus(FederatedLink::STATUS_LINK_SETUP) @@ -306,33 +294,7 @@ class FederatedService { * @return string */ private function generateLinkRemoteURL($remote) { - return $this->generateRemoteHost($remote) . self::REMOTE_URL_LINK; - } - - - /** - * @param string $remote - * - * @return string - */ - private function generatePayloadDeliveryURL($remote) { - return $this->generateRemoteHost($remote) . self::REMOTE_URL_PAYLOAD; - } - - - /** - * @param string $remote - * - * @return string - */ - private function generateRemoteHost($remote) { - if ((!$this->configService->isNonSSLLinksAllowed() || strpos($remote, 'http://') !== 0) - && strpos($remote, 'https://') !== 0 - ) { - $remote = 'https://' . $remote; - } - - return rtrim($remote, '/'); + return $this->configService->generateRemoteHost($remote) . Application::REMOTE_URL_LINK; } @@ -684,39 +646,6 @@ class FederatedService { /** - * @param string $token - * @param string $uniqueId - * @param SharingFrame $frame - * - * @return bool - * @throws Exception - */ - public function receiveFrame($token, $uniqueId, SharingFrame &$frame) { - try { - $link = $this->circlesRequest->getLinkFromToken((string)$token, (string)$uniqueId); - } catch (Exception $e) { - throw $e; - } - - try { - $this->circlesRequest->getFrame($link->getCircleId(), $frame->getUniqueId()); - throw new SharingFrameAlreadyExistException('shares_is_already_known'); - } catch (SharingFrameDoesNotExistException $e) { - } - - try { - $circle = $this->circlesRequest->forceGetCircle($link->getCircleId()); - } catch (CircleDoesNotExistException $e) { - throw new CircleDoesNotExistException('unknown_circle'); - } - - $frame->setCircle($circle); - $this->circlesRequest->saveFrame($frame); - - return true; - } - - /** * @param string $circleUniqueId * @param string $uniqueId * @@ -736,108 +665,4 @@ class FederatedService { return $this->federatedLinksRequest->getLinked($circleUniqueId); } - - /** - * @param string $circleUniqueId - * @param string $frameUniqueId - * - * @return bool - * @throws Exception - */ - public function initiateShare($circleUniqueId, $frameUniqueId) { - $args = [ - 'circleId' => $circleUniqueId, - 'frameId' => $frameUniqueId - ]; - - $client = $this->clientService->newClient(); - try { - $client->post( - $this->generatePayloadDeliveryURL($this->serverHost), [ - 'body' => $args, - 'timeout' => 10, - 'connect_timeout' => 10, - ] - ); - -// $result = json_decode($request->getBody(), true); -// $this->miscService->log( -// "initiateRemoteShare result: " . $uniqueId . ' ---- ' . var_export($result, true) -// ); - - return true; - } catch (Exception $e) { - throw $e; - } - } - - - /** - * @param SharingFrame $frame - * - * @throws Exception - */ - public function sendRemoteShare(SharingFrame $frame) { - - try { - $circle = $this->circlesRequest->forceGetCircle( - $frame->getCircle() - ->getUniqueId() - ); - } catch (CircleDoesNotExistException $e) { - throw new CircleDoesNotExistException('unknown_circle'); - } - - $links = $this->getLinksFromCircle( - $frame->getCircle() - ->getUniqueId() - ); - - foreach ($links AS $link) { - - - $args = [ - 'apiVersion' => Circles::version(), - 'token' => $link->getToken(true), - 'uniqueId' => $circle->getUniqueId(true), - 'item' => json_encode($frame) - ]; - - $client = $this->clientService->newClient(); - try { - $request = $client->put( - $this->generatePayloadDeliveryURL($link->getAddress()), [ - 'body' => $args, - 'timeout' => 10, - 'connect_timeout' => 10, - ] - ); - - $result = json_decode($request->getBody(), true); - if ($result['status'] === -1) { - throw new PayloadDeliveryException($result['reason']); - } - - } catch (Exception $e) { - $this->miscService->log( - 'Issue while sending sharing frame to ' . $link->getAddress() . ' - ' - . $e->getMessage() - ); - } - } - } - - - /** - * generateHeaders() - * - * Generate new headers for the current Payload, and save them in the SharingFrame. - * - * @param SharingFrame $frame - */ - public function updateFrameWithCloudId(SharingFrame $frame) { - $frame->setCloudId($this->serverHost); - $this->circlesRequest->updateFrame($frame); - } - }
\ No newline at end of file diff --git a/lib/Service/SharesService.php b/lib/Service/SharesService.php index 860846e2..9c6a5bc2 100644 --- a/lib/Service/SharesService.php +++ b/lib/Service/SharesService.php @@ -28,14 +28,18 @@ namespace OCA\Circles\Service; use Exception; +use OC\Http\Client\ClientService; +use OCA\Circles\Api\v1\Circles; +use OCA\Circles\AppInfo\Application; use OCA\Circles\Db\CirclesRequest; -use OCA\Circles\Exceptions\BroadcasterIsNotCompatibleException; +use OCA\Circles\Exceptions\CircleDoesNotExistException; use OCA\Circles\Exceptions\MemberDoesNotExistException; +use OCA\Circles\Exceptions\PayloadDeliveryException; use OCA\Circles\Exceptions\SharingFrameAlreadyDeliveredException; +use OCA\Circles\Exceptions\SharingFrameAlreadyExistException; use OCA\Circles\Exceptions\SharingFrameDoesNotExistException; -use OCA\Circles\IBroadcaster; use OCA\Circles\Model\Circle; -use OCA\Circles\Model\Member; +use OCA\Circles\Model\FederatedLink; use OCA\Circles\Model\SharingFrame; @@ -53,8 +57,11 @@ class SharesService { /** @var BroadcastService */ private $broadcastService; - /** @var FederatedService */ - private $federatedService; + /** @var FederatedLinkService */ + private $federatedLinkService; + + /** @var ClientService */ + private $clientService; /** @var MiscService */ private $miscService; @@ -67,7 +74,8 @@ class SharesService { * @param ConfigService $configService * @param CirclesRequest $circlesRequest * @param BroadcastService $broadcastService - * @param FederatedService $federatedService + * @param FederatedLinkService $federatedLinkService + * @param ClientService $clientService * @param MiscService $miscService */ public function __construct( @@ -75,14 +83,16 @@ class SharesService { ConfigService $configService, CirclesRequest $circlesRequest, BroadcastService $broadcastService, - FederatedService $federatedService, + FederatedLinkService $federatedLinkService, + ClientService $clientService, MiscService $miscService ) { $this->userId = (string)$userId; $this->configService = $configService; $this->circlesRequest = $circlesRequest; $this->broadcastService = $broadcastService; - $this->federatedService = $federatedService; + $this->federatedLinkService = $federatedLinkService; + $this->clientService = $clientService; $this->miscService = $miscService; } @@ -113,9 +123,7 @@ class SharesService { $this->generateHeaders($frame, $circle, $broadcast); $this->circlesRequest->saveFrame($frame); - $this->federatedService->initiateShare( - $circle->getUniqueId(), $frame->getUniqueId() - ); + $this->initiateShare($circle->getUniqueId(), $frame->getUniqueId()); } catch (Exception $e) { throw $e; } @@ -173,4 +181,163 @@ class SharesService { } + /** + * @param string $token + * @param string $uniqueId + * @param SharingFrame $frame + * + * @return bool + * @throws Exception + */ + public function receiveFrame($token, $uniqueId, SharingFrame &$frame) { + try { + $link = $this->circlesRequest->getLinkFromToken((string)$token, (string)$uniqueId); + } catch (Exception $e) { + throw $e; + } + + try { + $this->circlesRequest->getFrame($link->getCircleId(), $frame->getUniqueId()); + throw new SharingFrameAlreadyExistException('shares_is_already_known'); + } catch (SharingFrameDoesNotExistException $e) { + } + + try { + $circle = $this->circlesRequest->forceGetCircle($link->getCircleId()); + } catch (CircleDoesNotExistException $e) { + throw new CircleDoesNotExistException('unknown_circle'); + } + + $frame->setCircle($circle); + $this->circlesRequest->saveFrame($frame); + + return true; + } + + + /** + * @param string $circleUniqueId + * @param string $frameUniqueId + * + * @return bool + * @throws Exception + */ + public function initiateShare($circleUniqueId, $frameUniqueId) { + $args = [ + 'circleId' => $circleUniqueId, + 'frameId' => $frameUniqueId + ]; + + $client = $this->clientService->newClient(); + try { + $client->post( + $this->generatePayloadDeliveryURL($this->configService->getLocalAddress()), [ + 'body' => $args, + 'timeout' => 10, + 'connect_timeout' => 10, + ] + ); + + return true; + } catch (Exception $e) { + throw $e; + } + } + + + /** + * @param string $remote + * + * @return string + */ + private function generatePayloadDeliveryURL($remote) { + return $this->configService->generateRemoteHost($remote) . Application::REMOTE_URL_PAYLOAD; + } + + + /** + * @param SharingFrame $frame + * + * @throws Exception + */ + public function forwardSharingFrame(SharingFrame $frame) { + + try { + $circle = $this->circlesRequest->forceGetCircle( + $frame->getCircle() + ->getUniqueId() + ); + } catch (CircleDoesNotExistException $e) { + throw new CircleDoesNotExistException('unknown_circle'); + } + + $links = $this->federatedLinkService->getLinksFromCircle( + $frame->getCircle() + ->getUniqueId() + ); + + $this->forwardSharingFrameToFederatedLinks($circle, $frame, $links); + } + + + /** + * @param Circle $circle + * @param SharingFrame $frame + * @param FederatedLink[] $links + */ + private function forwardSharingFrameToFederatedLinks(Circle $circle, SharingFrame $frame, $links) { + + $args = [ + 'apiVersion' => Circles::version(), + 'uniqueId' => $circle->getUniqueId(true), + 'item' => json_encode($frame) + ]; + + foreach ($links AS $link) { + $args['token'] = $link->getToken(true); + $this->deliverSharingFrameToLink($link, $args); + } + } + + + /** + * sendRemoteShareToLinks(); + * + * @param FederatedLink $link + * @param array $args + */ + private function deliverSharingFrameToLink($link, $args) { + + $client = $this->clientService->newClient(); + try { + $request = $client->put( + $this->generatePayloadDeliveryURL($link->getAddress()), [ + 'body' => $args, + 'timeout' => 10, + 'connect_timeout' => 10, + ] + ); + + $result = json_decode($request->getBody(), true); + if ($result['status'] === -1) { + throw new PayloadDeliveryException($result['reason']); + } + + } catch (Exception $e) { + $this->miscService->log( + 'fail to send frame to ' . $link->getAddress() . ' - ' . $e->getMessage() + ); + } + } + + + /** + * @param SharingFrame $frame + */ + public function updateFrameWithCloudId(SharingFrame $frame) { + $frame->setCloudId($this->configService->getLocalAddress()); + $this->circlesRequest->updateFrame($frame); + } + + }
\ No newline at end of file |