FederationController.php 6.9 KB

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