FederationController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. abort_if(!config('federation.activitypub.outbox'), 404);
  110. // $profile = Profile::whereNull('domain')
  111. // ->whereNull('status')
  112. // ->whereIsPrivate(false)
  113. // ->whereUsername($username)
  114. // ->firstOrFail();
  115. // $key = 'ap:outbox:latest_10:pid:' . $profile->id;
  116. // $ttl = now()->addMinutes(15);
  117. // $res = Cache::remember($key, $ttl, function() use($profile) {
  118. // return Outbox::get($profile);
  119. // });
  120. $res = [];
  121. return response(json_encode($res, JSON_UNESCAPED_SLASHES))->header('Content-Type', 'application/activity+json');
  122. }
  123. public function userInbox(Request $request, $username)
  124. {
  125. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  126. abort_if(!config('federation.activitypub.inbox'), 404);
  127. $headers = $request->headers->all();
  128. $payload = $request->getContent();
  129. if(!$payload || empty($payload)) {
  130. return;
  131. }
  132. $obj = json_decode($payload, true, 8);
  133. if(!isset($obj['id'])) {
  134. return;
  135. }
  136. $domain = parse_url($obj['id'], PHP_URL_HOST);
  137. if(in_array($domain, InstanceService::getBannedDomains())) {
  138. return;
  139. }
  140. if(isset($obj['type']) && $obj['type'] === 'Delete') {
  141. $lockKey = 'pf:ap:del-lock:' . hash('sha256', $obj['id']);
  142. if( isset($obj['actor']) &&
  143. isset($obj['object']) &&
  144. isset($obj['id']) &&
  145. is_string($obj['id']) &&
  146. is_string($obj['actor']) &&
  147. is_string($obj['object']) &&
  148. $obj['actor'] == $obj['object']
  149. ) {
  150. if(Cache::get($lockKey) !== null) {
  151. return;
  152. } else {
  153. Cache::put($lockKey, 1, 3600);
  154. usleep(5000);
  155. }
  156. }
  157. dispatch(new DeleteWorker($headers, $payload))->onQueue('delete');
  158. } else {
  159. $lockKey = 'pf:ap:user-inbox:activity:' . hash('sha256', $obj['id']);
  160. if(Cache::get($lockKey) !== null) {
  161. return;
  162. }
  163. Cache::put($lockKey, 1, 3600);
  164. usleep(5000);
  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. $lockKey = 'pf:ap:del-lock:' . hash('sha256', $obj['id']);
  188. if( isset($obj['actor']) &&
  189. isset($obj['object']) &&
  190. isset($obj['id']) &&
  191. is_string($obj['id']) &&
  192. is_string($obj['actor']) &&
  193. is_string($obj['object']) &&
  194. $obj['actor'] == $obj['object']
  195. ) {
  196. if(Cache::get($lockKey) !== null) {
  197. return;
  198. }
  199. }
  200. Cache::put($lockKey, 1, 3600);
  201. dispatch(new DeleteWorker($headers, $payload))->onQueue('delete');
  202. } else {
  203. dispatch(new InboxWorker($headers, $payload))->onQueue('high');
  204. }
  205. return;
  206. }
  207. public function userFollowing(Request $request, $username)
  208. {
  209. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  210. $obj = [
  211. '@context' => 'https://www.w3.org/ns/activitystreams',
  212. 'id' => $request->getUri(),
  213. 'type' => 'OrderedCollectionPage',
  214. 'totalItems' => 0,
  215. 'orderedItems' => []
  216. ];
  217. return response()->json($obj);
  218. }
  219. public function userFollowers(Request $request, $username)
  220. {
  221. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  222. $obj = [
  223. '@context' => 'https://www.w3.org/ns/activitystreams',
  224. 'id' => $request->getUri(),
  225. 'type' => 'OrderedCollectionPage',
  226. 'totalItems' => 0,
  227. 'orderedItems' => []
  228. ];
  229. return response()->json($obj);
  230. }
  231. }