FederationController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. // Instance Actor
  40. if (
  41. config('federation.activitypub.sharedInbox') &&
  42. $resource == 'acct:'.$domain.'@'.$domain
  43. ) {
  44. $res = [
  45. 'subject' => 'acct:'.$domain.'@'.$domain,
  46. 'aliases' => [
  47. 'https://'.$domain.'/i/actor',
  48. ],
  49. 'links' => [
  50. [
  51. 'rel' => 'http://webfinger.net/rel/profile-page',
  52. 'type' => 'text/html',
  53. 'href' => 'https://'.$domain.'/site/kb/instance-actor',
  54. ],
  55. [
  56. 'rel' => 'self',
  57. 'type' => 'application/activity+json',
  58. 'href' => 'https://'.$domain.'/i/actor',
  59. ],
  60. ],
  61. ];
  62. return response()->json($res, 200, [], JSON_UNESCAPED_SLASHES);
  63. }
  64. if(str_starts_with($resource, 'https://')) {
  65. if(str_starts_with($resource, 'https://' . $domain . '/users/')) {
  66. $username = str_replace('https://' . $domain . '/users/', '', $resource);
  67. if(strlen($username) > 15) {
  68. return response('', 400);
  69. }
  70. $stripped = str_replace(['_', '.', '-'], '', $username);
  71. if(!ctype_alnum($stripped)) {
  72. return response('', 400);
  73. }
  74. $key = 'federation:webfinger:sha256:url-username:'.$username;
  75. if ($cached = Cache::get($key)) {
  76. return response()->json($cached, 200, [], JSON_UNESCAPED_SLASHES);
  77. }
  78. $profile = Profile::whereUsername($username)->first();
  79. if (! $profile || $profile->status !== null || $profile->domain) {
  80. return response('', 400);
  81. }
  82. $webfinger = (new Webfinger($profile))->generate();
  83. Cache::put($key, $webfinger, 1209600);
  84. return response()->json($webfinger, 200, [], JSON_UNESCAPED_SLASHES)
  85. ->header('Access-Control-Allow-Origin', '*');
  86. } else {
  87. return response('', 400);
  88. }
  89. }
  90. $hash = hash('sha256', $resource);
  91. $key = 'federation:webfinger:sha256:'.$hash;
  92. if ($cached = Cache::get($key)) {
  93. return response()->json($cached, 200, [], JSON_UNESCAPED_SLASHES);
  94. }
  95. if (strpos($resource, $domain) == false) {
  96. return response('', 400);
  97. }
  98. $parsed = Nickname::normalizeProfileUrl($resource);
  99. if (empty($parsed) || $parsed['domain'] !== $domain) {
  100. return response('', 400);
  101. }
  102. $username = $parsed['username'];
  103. $profile = Profile::whereUsername($username)->first();
  104. if (! $profile || $profile->status !== null || $profile->domain) {
  105. return response('', 400);
  106. }
  107. $webfinger = (new Webfinger($profile))->generate();
  108. Cache::put($key, $webfinger, 1209600);
  109. return response()->json($webfinger, 200, [], JSON_UNESCAPED_SLASHES)
  110. ->header('Access-Control-Allow-Origin', '*');
  111. }
  112. public function hostMeta(Request $request)
  113. {
  114. abort_if(! config('federation.webfinger.enabled'), 404);
  115. $path = route('well-known.webfinger');
  116. $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>';
  117. return response($xml)->header('Content-Type', 'application/xrd+xml');
  118. }
  119. public function userOutbox(Request $request, $username)
  120. {
  121. abort_if(! (bool) config_cache('federation.activitypub.enabled'), 404);
  122. if (! $request->wantsJson()) {
  123. return redirect('/'.$username);
  124. }
  125. $id = AccountService::usernameToId($username);
  126. abort_if(! $id, 404);
  127. $account = AccountService::get($id);
  128. abort_if(! $account || ! isset($account['statuses_count']), 404);
  129. $res = [
  130. '@context' => 'https://www.w3.org/ns/activitystreams',
  131. 'id' => 'https://'.config('pixelfed.domain.app').'/users/'.$username.'/outbox',
  132. 'type' => 'OrderedCollection',
  133. 'totalItems' => $account['statuses_count'] ?? 0,
  134. ];
  135. return response(json_encode($res, JSON_UNESCAPED_SLASHES))->header('Content-Type', 'application/activity+json');
  136. }
  137. public function userInbox(Request $request, $username)
  138. {
  139. abort_if(! (bool) config_cache('federation.activitypub.enabled'), 404);
  140. abort_if(! config('federation.activitypub.inbox'), 404);
  141. $headers = $request->headers->all();
  142. $payload = $request->getContent();
  143. if (! $payload || empty($payload)) {
  144. return;
  145. }
  146. $obj = json_decode($payload, true, 8);
  147. if (! isset($obj['id'])) {
  148. return;
  149. }
  150. $domain = parse_url($obj['id'], PHP_URL_HOST);
  151. if (in_array($domain, InstanceService::getBannedDomains())) {
  152. return;
  153. }
  154. if (isset($obj['type']) && $obj['type'] === 'Delete') {
  155. if (isset($obj['object']) && isset($obj['object']['type']) && isset($obj['object']['id'])) {
  156. if ($obj['object']['type'] === 'Person') {
  157. if (Profile::whereRemoteUrl($obj['object']['id'])->exists()) {
  158. dispatch(new DeleteWorker($headers, $payload))->onQueue('inbox');
  159. return;
  160. }
  161. }
  162. if ($obj['object']['type'] === 'Tombstone') {
  163. if (Status::whereObjectUrl($obj['object']['id'])->exists()) {
  164. dispatch(new DeleteWorker($headers, $payload))->onQueue('delete');
  165. return;
  166. }
  167. }
  168. if ($obj['object']['type'] === 'Story') {
  169. dispatch(new DeleteWorker($headers, $payload))->onQueue('story');
  170. return;
  171. }
  172. }
  173. return;
  174. } elseif (isset($obj['type']) && in_array($obj['type'], ['Follow', 'Accept'])) {
  175. dispatch(new InboxValidator($username, $headers, $payload))->onQueue('follow');
  176. } else {
  177. dispatch(new InboxValidator($username, $headers, $payload))->onQueue('high');
  178. }
  179. }
  180. public function sharedInbox(Request $request)
  181. {
  182. abort_if(! (bool) config_cache('federation.activitypub.enabled'), 404);
  183. abort_if(! config('federation.activitypub.sharedInbox'), 404);
  184. $headers = $request->headers->all();
  185. $payload = $request->getContent();
  186. if (! $payload || empty($payload)) {
  187. return;
  188. }
  189. $obj = json_decode($payload, true, 8);
  190. if (! isset($obj['id'])) {
  191. return;
  192. }
  193. $domain = parse_url($obj['id'], PHP_URL_HOST);
  194. if (in_array($domain, InstanceService::getBannedDomains())) {
  195. return;
  196. }
  197. if (isset($obj['type']) && $obj['type'] === 'Delete') {
  198. if (isset($obj['object']) && isset($obj['object']['type']) && isset($obj['object']['id'])) {
  199. if ($obj['object']['type'] === 'Person') {
  200. if (Profile::whereRemoteUrl($obj['object']['id'])->exists()) {
  201. dispatch(new DeleteWorker($headers, $payload))->onQueue('inbox');
  202. return;
  203. }
  204. }
  205. if ($obj['object']['type'] === 'Tombstone') {
  206. if (Status::whereObjectUrl($obj['object']['id'])->exists()) {
  207. dispatch(new DeleteWorker($headers, $payload))->onQueue('delete');
  208. return;
  209. }
  210. }
  211. if ($obj['object']['type'] === 'Story') {
  212. dispatch(new DeleteWorker($headers, $payload))->onQueue('story');
  213. return;
  214. }
  215. }
  216. return;
  217. } elseif (isset($obj['type']) && in_array($obj['type'], ['Follow', 'Accept'])) {
  218. dispatch(new InboxWorker($headers, $payload))->onQueue('follow');
  219. } else {
  220. dispatch(new InboxWorker($headers, $payload))->onQueue('shared');
  221. }
  222. }
  223. public function userFollowing(Request $request, $username)
  224. {
  225. abort_if(! (bool) config_cache('federation.activitypub.enabled'), 404);
  226. $id = AccountService::usernameToId($username);
  227. abort_if(! $id, 404);
  228. $account = AccountService::get($id);
  229. abort_if(! $account || ! isset($account['following_count']), 404);
  230. $obj = [
  231. '@context' => 'https://www.w3.org/ns/activitystreams',
  232. 'id' => $request->getUri(),
  233. 'type' => 'OrderedCollection',
  234. 'totalItems' => $account['following_count'] ?? 0,
  235. ];
  236. return response()->json($obj)->header('Content-Type', 'application/activity+json');
  237. }
  238. public function userFollowers(Request $request, $username)
  239. {
  240. abort_if(! (bool) config_cache('federation.activitypub.enabled'), 404);
  241. $id = AccountService::usernameToId($username);
  242. abort_if(! $id, 404);
  243. $account = AccountService::get($id);
  244. abort_if(! $account || ! isset($account['followers_count']), 404);
  245. $obj = [
  246. '@context' => 'https://www.w3.org/ns/activitystreams',
  247. 'id' => $request->getUri(),
  248. 'type' => 'OrderedCollection',
  249. 'totalItems' => $account['followers_count'] ?? 0,
  250. ];
  251. return response()->json($obj)->header('Content-Type', 'application/activity+json');
  252. }
  253. }