FederationController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Jobs\InboxPipeline\DeleteWorker;
  4. use App\Jobs\InboxPipeline\InboxValidator;
  5. use App\Jobs\InboxPipeline\InboxWorker;
  6. use App\Profile;
  7. use App\Services\AccountService;
  8. use App\Services\InstanceService;
  9. use App\Status;
  10. use App\Util\Lexer\Nickname;
  11. use App\Util\Site\Nodeinfo;
  12. use App\Util\Webfinger\Webfinger;
  13. use Cache;
  14. use Illuminate\Http\Request;
  15. class FederationController extends Controller
  16. {
  17. public function nodeinfoWellKnown()
  18. {
  19. abort_if(! config('federation.nodeinfo.enabled'), 404);
  20. return response()->json(Nodeinfo::wellKnown(), 200, [], JSON_UNESCAPED_SLASHES)
  21. ->header('Access-Control-Allow-Origin', '*');
  22. }
  23. public function nodeinfo()
  24. {
  25. abort_if(! config('federation.nodeinfo.enabled'), 404);
  26. return response()->json(Nodeinfo::get(), 200, [], JSON_UNESCAPED_SLASHES)
  27. ->header('Access-Control-Allow-Origin', '*');
  28. }
  29. public function webfinger(Request $request)
  30. {
  31. if (! config('federation.webfinger.enabled') ||
  32. ! $request->has('resource') ||
  33. ! $request->filled('resource')
  34. ) {
  35. return response('', 400);
  36. }
  37. $resource = $request->input('resource');
  38. $domain = config('pixelfed.domain.app');
  39. if (config('federation.activitypub.sharedInbox') &&
  40. $resource == 'acct:'.$domain.'@'.$domain) {
  41. $res = [
  42. 'subject' => 'acct:'.$domain.'@'.$domain,
  43. 'aliases' => [
  44. 'https://'.$domain.'/i/actor',
  45. ],
  46. 'links' => [
  47. [
  48. 'rel' => 'http://webfinger.net/rel/profile-page',
  49. 'type' => 'text/html',
  50. 'href' => 'https://'.$domain.'/site/kb/instance-actor',
  51. ],
  52. [
  53. 'rel' => 'self',
  54. 'type' => 'application/activity+json',
  55. 'href' => 'https://'.$domain.'/i/actor',
  56. ],
  57. ],
  58. ];
  59. return response()->json($res, 200, [], JSON_UNESCAPED_SLASHES);
  60. }
  61. $hash = hash('sha256', $resource);
  62. $key = 'federation:webfinger:sha256:'.$hash;
  63. if ($cached = Cache::get($key)) {
  64. return response()->json($cached, 200, [], JSON_UNESCAPED_SLASHES);
  65. }
  66. if (strpos($resource, $domain) == false) {
  67. return response('', 400);
  68. }
  69. $parsed = Nickname::normalizeProfileUrl($resource);
  70. if (empty($parsed) || $parsed['domain'] !== $domain) {
  71. return response('', 400);
  72. }
  73. $username = $parsed['username'];
  74. $profile = Profile::whereNull('domain')->whereUsername($username)->first();
  75. if (! $profile || $profile->status !== null) {
  76. return response('', 400);
  77. }
  78. $webfinger = (new Webfinger($profile))->generate();
  79. Cache::put($key, $webfinger, 1209600);
  80. return response()->json($webfinger, 200, [], JSON_UNESCAPED_SLASHES)
  81. ->header('Access-Control-Allow-Origin', '*');
  82. }
  83. public function hostMeta(Request $request)
  84. {
  85. abort_if(! config('federation.webfinger.enabled'), 404);
  86. $path = route('well-known.webfinger');
  87. $xml = '<?xml version="1.0" encoding="UTF-8"?><XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0"><Link rel="lrdd" type="application/xrd+xml" template="'.$path.'?resource={uri}"/></XRD>';
  88. return response($xml)->header('Content-Type', 'application/xrd+xml');
  89. }
  90. public function userOutbox(Request $request, $username)
  91. {
  92. abort_if(! (bool) config_cache('federation.activitypub.enabled'), 404);
  93. if (! $request->wantsJson()) {
  94. return redirect('/'.$username);
  95. }
  96. $id = AccountService::usernameToId($username);
  97. abort_if(! $id, 404);
  98. $account = AccountService::get($id);
  99. abort_if(! $account || ! isset($account['statuses_count']), 404);
  100. $res = [
  101. '@context' => 'https://www.w3.org/ns/activitystreams',
  102. 'id' => 'https://'.config('pixelfed.domain.app').'/users/'.$username.'/outbox',
  103. 'type' => 'OrderedCollection',
  104. 'totalItems' => $account['statuses_count'] ?? 0,
  105. ];
  106. return response(json_encode($res, JSON_UNESCAPED_SLASHES))->header('Content-Type', 'application/activity+json');
  107. }
  108. public function userInbox(Request $request, $username)
  109. {
  110. abort_if(! (bool) config_cache('federation.activitypub.enabled'), 404);
  111. abort_if(! config('federation.activitypub.inbox'), 404);
  112. $headers = $request->headers->all();
  113. $payload = $request->getContent();
  114. if (! $payload || empty($payload)) {
  115. return;
  116. }
  117. $obj = json_decode($payload, true, 8);
  118. if (! isset($obj['id'])) {
  119. return;
  120. }
  121. $domain = parse_url($obj['id'], PHP_URL_HOST);
  122. if (in_array($domain, InstanceService::getBannedDomains())) {
  123. return;
  124. }
  125. if (isset($obj['type']) && $obj['type'] === 'Delete') {
  126. if (isset($obj['object']) && isset($obj['object']['type']) && isset($obj['object']['id'])) {
  127. if ($obj['object']['type'] === 'Person') {
  128. if (Profile::whereRemoteUrl($obj['object']['id'])->exists()) {
  129. dispatch(new DeleteWorker($headers, $payload))->onQueue('inbox');
  130. return;
  131. }
  132. }
  133. if ($obj['object']['type'] === 'Tombstone') {
  134. if (Status::whereObjectUrl($obj['object']['id'])->exists()) {
  135. dispatch(new DeleteWorker($headers, $payload))->onQueue('delete');
  136. return;
  137. }
  138. }
  139. if ($obj['object']['type'] === 'Story') {
  140. dispatch(new DeleteWorker($headers, $payload))->onQueue('story');
  141. return;
  142. }
  143. }
  144. return;
  145. } elseif (isset($obj['type']) && in_array($obj['type'], ['Follow', 'Accept'])) {
  146. dispatch(new InboxValidator($username, $headers, $payload))->onQueue('follow');
  147. } else {
  148. dispatch(new InboxValidator($username, $headers, $payload))->onQueue('high');
  149. }
  150. }
  151. public function sharedInbox(Request $request)
  152. {
  153. abort_if(! (bool) config_cache('federation.activitypub.enabled'), 404);
  154. abort_if(! config('federation.activitypub.sharedInbox'), 404);
  155. $headers = $request->headers->all();
  156. $payload = $request->getContent();
  157. if (! $payload || empty($payload)) {
  158. return;
  159. }
  160. $obj = json_decode($payload, true, 8);
  161. if (! isset($obj['id'])) {
  162. return;
  163. }
  164. $domain = parse_url($obj['id'], PHP_URL_HOST);
  165. if (in_array($domain, InstanceService::getBannedDomains())) {
  166. return;
  167. }
  168. if (isset($obj['type']) && $obj['type'] === 'Delete') {
  169. if (isset($obj['object']) && isset($obj['object']['type']) && isset($obj['object']['id'])) {
  170. if ($obj['object']['type'] === 'Person') {
  171. if (Profile::whereRemoteUrl($obj['object']['id'])->exists()) {
  172. dispatch(new DeleteWorker($headers, $payload))->onQueue('inbox');
  173. return;
  174. }
  175. }
  176. if ($obj['object']['type'] === 'Tombstone') {
  177. if (Status::whereObjectUrl($obj['object']['id'])->exists()) {
  178. dispatch(new DeleteWorker($headers, $payload))->onQueue('delete');
  179. return;
  180. }
  181. }
  182. if ($obj['object']['type'] === 'Story') {
  183. dispatch(new DeleteWorker($headers, $payload))->onQueue('story');
  184. return;
  185. }
  186. }
  187. return;
  188. } elseif (isset($obj['type']) && in_array($obj['type'], ['Follow', 'Accept'])) {
  189. dispatch(new InboxWorker($headers, $payload))->onQueue('follow');
  190. } else {
  191. dispatch(new InboxWorker($headers, $payload))->onQueue('shared');
  192. }
  193. }
  194. public function userFollowing(Request $request, $username)
  195. {
  196. abort_if(! (bool) config_cache('federation.activitypub.enabled'), 404);
  197. $id = AccountService::usernameToId($username);
  198. abort_if(! $id, 404);
  199. $account = AccountService::get($id);
  200. abort_if(! $account || ! isset($account['following_count']), 404);
  201. $obj = [
  202. '@context' => 'https://www.w3.org/ns/activitystreams',
  203. 'id' => $request->getUri(),
  204. 'type' => 'OrderedCollection',
  205. 'totalItems' => $account['following_count'] ?? 0,
  206. ];
  207. return response()->json($obj)->header('Content-Type', 'application/activity+json');
  208. }
  209. public function userFollowers(Request $request, $username)
  210. {
  211. abort_if(! (bool) config_cache('federation.activitypub.enabled'), 404);
  212. $id = AccountService::usernameToId($username);
  213. abort_if(! $id, 404);
  214. $account = AccountService::get($id);
  215. abort_if(! $account || ! isset($account['followers_count']), 404);
  216. $obj = [
  217. '@context' => 'https://www.w3.org/ns/activitystreams',
  218. 'id' => $request->getUri(),
  219. 'type' => 'OrderedCollection',
  220. 'totalItems' => $account['followers_count'] ?? 0,
  221. ];
  222. return response()->json($obj)->header('Content-Type', 'application/activity+json');
  223. }
  224. }