FederationController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. class FederationController extends Controller
  32. {
  33. public function nodeinfoWellKnown()
  34. {
  35. abort_if(!config('federation.nodeinfo.enabled'), 404);
  36. return response()->json(Nodeinfo::wellKnown(), 200, [], JSON_UNESCAPED_SLASHES)
  37. ->header('Access-Control-Allow-Origin','*');
  38. }
  39. public function nodeinfo()
  40. {
  41. abort_if(!config('federation.nodeinfo.enabled'), 404);
  42. return response()->json(Nodeinfo::get(), 200, [], JSON_UNESCAPED_SLASHES)
  43. ->header('Access-Control-Allow-Origin','*');
  44. }
  45. public function webfinger(Request $request)
  46. {
  47. if (!config('federation.webfinger.enabled') ||
  48. !$request->has('resource') ||
  49. !$request->filled('resource')
  50. ) {
  51. return response('', 400);
  52. }
  53. $resource = $request->input('resource');
  54. $domain = config('pixelfed.domain.app');
  55. if(config('federation.activitypub.sharedInbox') &&
  56. $resource == 'acct:' . $domain . '@' . $domain) {
  57. $res = [
  58. 'subject' => 'acct:' . $domain . '@' . $domain,
  59. 'aliases' => [
  60. 'https://' . $domain . '/i/actor'
  61. ],
  62. 'links' => [
  63. [
  64. 'rel' => 'http://webfinger.net/rel/profile-page',
  65. 'type' => 'text/html',
  66. 'href' => 'https://' . $domain . '/site/kb/instance-actor'
  67. ],
  68. [
  69. 'rel' => 'self',
  70. 'type' => 'application/activity+json',
  71. 'href' => 'https://' . $domain . '/i/actor'
  72. ]
  73. ]
  74. ];
  75. return response()->json($res, 200, [], JSON_UNESCAPED_SLASHES);
  76. }
  77. $hash = hash('sha256', $resource);
  78. $key = 'federation:webfinger:sha256:' . $hash;
  79. if($cached = Cache::get($key)) {
  80. return response()->json($cached, 200, [], JSON_UNESCAPED_SLASHES);
  81. }
  82. if(strpos($resource, $domain) == false) {
  83. return response('', 400);
  84. }
  85. $parsed = Nickname::normalizeProfileUrl($resource);
  86. if(empty($parsed) || $parsed['domain'] !== $domain) {
  87. return response('', 400);
  88. }
  89. $username = $parsed['username'];
  90. $profile = Profile::whereNull('domain')->whereUsername($username)->first();
  91. if(!$profile || $profile->status !== null) {
  92. return response('', 400);
  93. }
  94. $webfinger = (new Webfinger($profile))->generate();
  95. Cache::put($key, $webfinger, 1209600);
  96. return response()->json($webfinger, 200, [], JSON_UNESCAPED_SLASHES)
  97. ->header('Access-Control-Allow-Origin','*');
  98. }
  99. public function hostMeta(Request $request)
  100. {
  101. abort_if(!config('federation.webfinger.enabled'), 404);
  102. $path = route('well-known.webfinger');
  103. $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>';
  104. return response($xml)->header('Content-Type', 'application/xrd+xml');
  105. }
  106. public function userOutbox(Request $request, $username)
  107. {
  108. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  109. if(!$request->wantsJson()) {
  110. return redirect('/' . $username);
  111. }
  112. $res = [
  113. '@context' => 'https://www.w3.org/ns/activitystreams',
  114. 'id' => 'https://' . config('pixelfed.domain.app') . '/users/' . $username . '/outbox',
  115. 'type' => 'OrderedCollection',
  116. 'totalItems' => 0,
  117. 'orderedItems' => []
  118. ];
  119. return response(json_encode($res, JSON_UNESCAPED_SLASHES))->header('Content-Type', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"');
  120. }
  121. public function userInbox(Request $request, $username)
  122. {
  123. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  124. abort_if(!config('federation.activitypub.inbox'), 404);
  125. $headers = $request->headers->all();
  126. $payload = $request->getContent();
  127. if(!$payload || empty($payload)) {
  128. return;
  129. }
  130. $obj = json_decode($payload, true, 8);
  131. if(!isset($obj['id'])) {
  132. return;
  133. }
  134. $domain = parse_url($obj['id'], PHP_URL_HOST);
  135. if(in_array($domain, InstanceService::getBannedDomains())) {
  136. return;
  137. }
  138. if(isset($obj['type']) && $obj['type'] === 'Delete') {
  139. $lockKey = 'pf:ap:del-lock:' . hash('sha256', $obj['id']);
  140. if( isset($obj['actor']) &&
  141. isset($obj['object']) &&
  142. isset($obj['id']) &&
  143. is_string($obj['id']) &&
  144. is_string($obj['actor']) &&
  145. is_string($obj['object']) &&
  146. $obj['actor'] == $obj['object']
  147. ) {
  148. if(Cache::get($lockKey) !== null) {
  149. return;
  150. } else {
  151. Cache::put($lockKey, 1, 3600);
  152. usleep(5000);
  153. }
  154. }
  155. dispatch(new DeleteWorker($headers, $payload))->onQueue('delete');
  156. } else {
  157. $lockKey = 'pf:ap:user-inbox:activity:' . hash('sha256', $obj['id']);
  158. if(Cache::get($lockKey) !== null) {
  159. return;
  160. }
  161. Cache::put($lockKey, 1, 3600);
  162. usleep(5000);
  163. dispatch(new InboxValidator($username, $headers, $payload))->onQueue('high');
  164. }
  165. return;
  166. }
  167. public function sharedInbox(Request $request)
  168. {
  169. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  170. abort_if(!config('federation.activitypub.sharedInbox'), 404);
  171. $headers = $request->headers->all();
  172. $payload = $request->getContent();
  173. if(!$payload || empty($payload)) {
  174. return;
  175. }
  176. $obj = json_decode($payload, true, 8);
  177. if(!isset($obj['id'])) {
  178. return;
  179. }
  180. $domain = parse_url($obj['id'], PHP_URL_HOST);
  181. if(in_array($domain, InstanceService::getBannedDomains())) {
  182. return;
  183. }
  184. if(isset($obj['type']) && $obj['type'] === 'Delete') {
  185. $lockKey = 'pf:ap:del-lock:' . hash('sha256', $obj['id']);
  186. if( isset($obj['actor']) &&
  187. isset($obj['object']) &&
  188. isset($obj['id']) &&
  189. is_string($obj['id']) &&
  190. is_string($obj['actor']) &&
  191. is_string($obj['object']) &&
  192. $obj['actor'] == $obj['object']
  193. ) {
  194. if(Cache::get($lockKey) !== null) {
  195. return;
  196. }
  197. }
  198. Cache::put($lockKey, 1, 3600);
  199. dispatch(new DeleteWorker($headers, $payload))->onQueue('delete');
  200. } else {
  201. dispatch(new InboxWorker($headers, $payload))->onQueue('high');
  202. }
  203. return;
  204. }
  205. public function userFollowing(Request $request, $username)
  206. {
  207. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  208. $obj = [
  209. '@context' => 'https://www.w3.org/ns/activitystreams',
  210. 'id' => $request->getUri(),
  211. 'type' => 'OrderedCollectionPage',
  212. 'totalItems' => 0,
  213. 'orderedItems' => []
  214. ];
  215. return response()->json($obj);
  216. }
  217. public function userFollowers(Request $request, $username)
  218. {
  219. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  220. $obj = [
  221. '@context' => 'https://www.w3.org/ns/activitystreams',
  222. 'id' => $request->getUri(),
  223. 'type' => 'OrderedCollectionPage',
  224. 'totalItems' => 0,
  225. 'orderedItems' => []
  226. ];
  227. return response()->json($obj);
  228. }
  229. }