FederationController.php 9.7 KB

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