FederationController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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/activity+json');
  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. if(isset($obj['object']) && isset($obj['object']['type']) && isset($obj['object']['id'])) {
  140. if($obj['object']['type'] === 'Person') {
  141. if(Profile::whereRemoteUrl($obj['object']['id'])->exists()) {
  142. dispatch(new DeleteWorker($headers, $payload))->onQueue('inbox');
  143. return;
  144. }
  145. }
  146. if($obj['object']['type'] === 'Tombstone') {
  147. if(Status::whereObjectUrl($obj['object']['id'])->exists()) {
  148. dispatch(new DeleteWorker($headers, $payload))->onQueue('delete');
  149. return;
  150. }
  151. }
  152. if($obj['object']['type'] === 'Story') {
  153. dispatch(new DeleteWorker($headers, $payload))->onQueue('story');
  154. return;
  155. }
  156. }
  157. return;
  158. } else if( isset($obj['type']) && in_array($obj['type'], ['Follow', 'Accept'])) {
  159. dispatch(new InboxValidator($username, $headers, $payload))->onQueue('follow');
  160. } else {
  161. dispatch(new InboxValidator($username, $headers, $payload))->onQueue('high');
  162. }
  163. return;
  164. }
  165. public function sharedInbox(Request $request)
  166. {
  167. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  168. abort_if(!config('federation.activitypub.sharedInbox'), 404);
  169. $headers = $request->headers->all();
  170. $payload = $request->getContent();
  171. if(!$payload || empty($payload)) {
  172. return;
  173. }
  174. $obj = json_decode($payload, true, 8);
  175. if(!isset($obj['id'])) {
  176. return;
  177. }
  178. $domain = parse_url($obj['id'], PHP_URL_HOST);
  179. if(in_array($domain, InstanceService::getBannedDomains())) {
  180. return;
  181. }
  182. if(isset($obj['type']) && $obj['type'] === 'Delete') {
  183. if(isset($obj['object']) && isset($obj['object']['type']) && isset($obj['object']['id'])) {
  184. if($obj['object']['type'] === 'Person') {
  185. if(Profile::whereRemoteUrl($obj['object']['id'])->exists()) {
  186. dispatch(new DeleteWorker($headers, $payload))->onQueue('inbox');
  187. return;
  188. }
  189. }
  190. if($obj['object']['type'] === 'Tombstone') {
  191. if(Status::whereObjectUrl($obj['object']['id'])->exists()) {
  192. dispatch(new DeleteWorker($headers, $payload))->onQueue('delete');
  193. return;
  194. }
  195. }
  196. if($obj['object']['type'] === 'Story') {
  197. dispatch(new DeleteWorker($headers, $payload))->onQueue('story');
  198. return;
  199. }
  200. }
  201. return;
  202. } else if( isset($obj['type']) && in_array($obj['type'], ['Follow', 'Accept'])) {
  203. dispatch(new InboxWorker($headers, $payload))->onQueue('follow');
  204. } else {
  205. dispatch(new InboxWorker($headers, $payload))->onQueue('shared');
  206. }
  207. return;
  208. }
  209. public function userFollowing(Request $request, $username)
  210. {
  211. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  212. $obj = [
  213. '@context' => 'https://www.w3.org/ns/activitystreams',
  214. 'id' => $request->getUri(),
  215. 'type' => 'OrderedCollectionPage',
  216. 'totalItems' => 0,
  217. 'orderedItems' => []
  218. ];
  219. return response()->json($obj);
  220. }
  221. public function userFollowers(Request $request, $username)
  222. {
  223. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  224. $obj = [
  225. '@context' => 'https://www.w3.org/ns/activitystreams',
  226. 'id' => $request->getUri(),
  227. 'type' => 'OrderedCollectionPage',
  228. 'totalItems' => 0,
  229. 'orderedItems' => []
  230. ];
  231. return response()->json($obj);
  232. }
  233. }