FederationController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. $res = [
  114. '@context' => 'https://www.w3.org/ns/activitystreams',
  115. 'id' => 'https://' . config('pixelfed.domain.app') . '/users/' . $username . '/outbox',
  116. 'type' => 'OrderedCollection',
  117. 'totalItems' => 0,
  118. 'orderedItems' => []
  119. ];
  120. return response(json_encode($res, JSON_UNESCAPED_SLASHES))->header('Content-Type', 'application/activity+json');
  121. }
  122. public function userInbox(Request $request, $username)
  123. {
  124. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  125. abort_if(!config('federation.activitypub.inbox'), 404);
  126. $headers = $request->headers->all();
  127. $payload = $request->getContent();
  128. if(!$payload || empty($payload)) {
  129. return;
  130. }
  131. $obj = json_decode($payload, true, 8);
  132. if(!isset($obj['id'])) {
  133. return;
  134. }
  135. $domain = parse_url($obj['id'], PHP_URL_HOST);
  136. if(in_array($domain, InstanceService::getBannedDomains())) {
  137. return;
  138. }
  139. if(isset($obj['type']) && $obj['type'] === 'Delete') {
  140. if(isset($obj['object']) && isset($obj['object']['type']) && isset($obj['object']['id'])) {
  141. if($obj['object']['type'] === 'Person') {
  142. if(Profile::whereRemoteUrl($obj['object']['id'])->exists()) {
  143. dispatch(new DeleteWorker($headers, $payload))->onQueue('inbox');
  144. return;
  145. }
  146. }
  147. if($obj['object']['type'] === 'Tombstone') {
  148. if(Status::whereObjectUrl($obj['object']['id'])->exists()) {
  149. dispatch(new DeleteWorker($headers, $payload))->onQueue('delete');
  150. return;
  151. }
  152. }
  153. if($obj['object']['type'] === 'Story') {
  154. dispatch(new DeleteWorker($headers, $payload))->onQueue('story');
  155. return;
  156. }
  157. }
  158. return;
  159. } else if( isset($obj['type']) && in_array($obj['type'], ['Follow', 'Accept'])) {
  160. dispatch(new InboxValidator($username, $headers, $payload))->onQueue('follow');
  161. } else {
  162. dispatch(new InboxValidator($username, $headers, $payload))->onQueue('high');
  163. }
  164. return;
  165. }
  166. public function sharedInbox(Request $request)
  167. {
  168. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  169. abort_if(!config('federation.activitypub.sharedInbox'), 404);
  170. $headers = $request->headers->all();
  171. $payload = $request->getContent();
  172. if(!$payload || empty($payload)) {
  173. return;
  174. }
  175. $obj = json_decode($payload, true, 8);
  176. if(!isset($obj['id'])) {
  177. return;
  178. }
  179. $domain = parse_url($obj['id'], PHP_URL_HOST);
  180. if(in_array($domain, InstanceService::getBannedDomains())) {
  181. return;
  182. }
  183. if(isset($obj['type']) && $obj['type'] === 'Delete') {
  184. if(isset($obj['object']) && isset($obj['object']['type']) && isset($obj['object']['id'])) {
  185. if($obj['object']['type'] === 'Person') {
  186. if(Profile::whereRemoteUrl($obj['object']['id'])->exists()) {
  187. dispatch(new DeleteWorker($headers, $payload))->onQueue('inbox');
  188. return;
  189. }
  190. }
  191. if($obj['object']['type'] === 'Tombstone') {
  192. if(Status::whereObjectUrl($obj['object']['id'])->exists()) {
  193. dispatch(new DeleteWorker($headers, $payload))->onQueue('delete');
  194. return;
  195. }
  196. }
  197. if($obj['object']['type'] === 'Story') {
  198. dispatch(new DeleteWorker($headers, $payload))->onQueue('story');
  199. return;
  200. }
  201. }
  202. return;
  203. } else if( isset($obj['type']) && in_array($obj['type'], ['Follow', 'Accept'])) {
  204. dispatch(new InboxWorker($headers, $payload))->onQueue('follow');
  205. } else {
  206. dispatch(new InboxWorker($headers, $payload))->onQueue('shared');
  207. }
  208. return;
  209. }
  210. public function userFollowing(Request $request, $username)
  211. {
  212. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  213. $id = AccountService::usernameToId($username);
  214. abort_if(!$id, 404);
  215. $account = AccountService::get($id);
  216. abort_if(!$account || !isset($account['following_count']), 404);
  217. $obj = [
  218. '@context' => 'https://www.w3.org/ns/activitystreams',
  219. 'id' => $request->getUri(),
  220. 'type' => 'OrderedCollection',
  221. 'totalItems' => $account['following_count'] ?? 0,
  222. ];
  223. return response()->json($obj);
  224. }
  225. public function userFollowers(Request $request, $username)
  226. {
  227. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  228. $id = AccountService::usernameToId($username);
  229. abort_if(!$id, 404);
  230. $account = AccountService::get($id);
  231. abort_if(!$account || !isset($account['followers_count']), 404);
  232. $obj = [
  233. '@context' => 'https://www.w3.org/ns/activitystreams',
  234. 'id' => $request->getUri(),
  235. 'type' => 'OrderedCollection',
  236. 'totalItems' => $account['followers_count'] ?? 0,
  237. ];
  238. return response()->json($obj);
  239. }
  240. }