www/githook.php

113 lines
3.2 KiB
PHP
Raw Permalink Normal View History

2019-03-17 01:31:59 +00:00
<?php
2019-03-22 21:33:06 +00:00
/* gitea deploy webhook for thunix*/
/*
* So, this webhook current accepts hooks for www, ansible, and soon
2019-03-22 16:52:54 +00:00
* gopher. It's pretty extensible, and is currently written for gitea,
* but things like gitlab, github, etc should be feasible, if not
* downright easy.
*
* While this 'should' work fine with numberic keys, and has been
* tested, php's loose casting makes it a crap shoot. We should
* probably not start tokens with a 0, or a number for that matter?
* All project hooks need to use the same key.
*
* Also, tildegit's IP address is hard-wired here, so we only accept
* hooks from tildegit. This will need that change, if it moves.
2019-03-17 01:31:59 +00:00
2019-03-21 14:49:58 +00:00
/* security */
2019-03-22 21:33:06 +00:00
$access_token = "secret";
$www_lastrun = '/dev/shm/www-hook-last-run';
$www_dropfile = '/dev/shm/run-www';
2019-03-22 16:52:54 +00:00
$gopher_lastrun = '/dev/shm/gopher-hook-last-run';
$gopher_dropfile = '/dev/shm/run-gopher';
2019-06-28 16:42:11 +00:00
$wiki_lastrun = '/dev/shm/wiki-hook-last-run';
$wiki_dropfile = '/dev/shm/run-wiki';
2023-07-04 18:03:40 +00:00
$allowedip = '198.50.210.248';
2019-03-22 21:33:06 +00:00
$remoteip = $_SERVER['REMOTE_ADDR'];
$ratelimit = 300;
2019-03-17 01:31:59 +00:00
/* get json data */
$json = file_get_contents('php://input');
$data = json_decode($json, true);
2019-03-22 21:33:06 +00:00
2019-03-22 16:52:54 +00:00
/* check our token */
2019-03-17 01:31:59 +00:00
$client_token = $data["secret"];
2019-03-22 21:33:06 +00:00
if ( strcmp($client_token, $access_token) !== 0 )
2019-03-17 01:31:59 +00:00
{
2019-03-22 16:52:54 +00:00
http_response_code(403);
echo "HTTP 403 - Forbidden, P1.\n";
exit(0);
2019-03-17 01:31:59 +00:00
}
2019-03-22 16:52:54 +00:00
/* check our source ip for the hook */
2019-03-22 21:33:06 +00:00
if ( strcmp($remoteip, $allowedip) !== 0 )
{
2019-03-22 16:52:54 +00:00
http_response_code(403);
echo "HTTP 403 - Forbidden, P2.\n";
exit(0);
}
2019-03-17 01:31:59 +00:00
2019-03-22 16:52:54 +00:00
// Hook for www repo here. Same rules apply, as above, for www. We
// could probably make it able to run more frequently. Backend job is
// just a git pull, and is quick.
elseif ($data["repository"]["full_name"] == 'thunix/www') {
syslog(LOG_INFO, 'WWW Webhook recieved.');
2019-04-13 00:46:07 +00:00
if ( time () - filemtime ( $www_lastrun ) > $ratelimit/30 ) {
2019-03-22 21:40:15 +00:00
touch ( $www_dropfile );
touch ( $www_lastrun );
http_response_code(200);
2019-03-22 16:52:54 +00:00
echo "HTTP 200 - WWW webhook recieved.\n";
}
2019-03-17 01:31:59 +00:00
else {
http_response_code(429);
2019-03-22 16:52:54 +00:00
echo "HTTP 429 - Rate Limited.\n";
exit(0);
}
2019-03-22 16:52:54 +00:00
}
// Hook for gopher. Not implemented on the backend yet.
2019-03-22 21:33:06 +00:00
elseif ($data["repository"]["full_name"] == 'thunix/thunix_gopher') {
2019-03-22 16:52:54 +00:00
syslog(LOG_INFO, 'Gopher Webhook recieved.');
if ( time () - filemtime ( $gopher_lastrun ) > $ratelimit ) {
2019-03-22 21:40:15 +00:00
touch ( $gopher_dropfile );
2019-03-22 16:52:54 +00:00
touch ( $gopher_lastrun );
http_response_code(200);
echo "HTTP 200 - Gopher webhook recieved.\n";
}
else {
http_response_code(429);
echo "HTTP 429 - Rate Limited.\n";
exit(0);
}
}
2019-06-28 16:42:11 +00:00
//Wiki webhook
elseif ($data["repository"]["full_name"] == 'thunix/wiki') {
syslog(LOG_INFO, 'Wiki Webhook recieved.');
if ( time () - filemtime ( $wiki_lastrun ) > $ratelimit/30 ) {
touch ( $wiki_dropfile );
touch ( $wiki_lastrun );
http_response_code(200);
echo "HTTP 200 - Wiki webhook recieved.\n";
}
else {
http_response_code(429);
echo "HTTP 429 - Rate Limited.\n";
exit(0);
}
}
2022-02-07 12:51:21 +00:00
// Easter egg for anyone probing the hook. Enjoy. We're a tea pot
// and not a coffee maker :)
else {
http_response_code(418);
2019-03-22 16:52:54 +00:00
echo "HTTP 418 - I'm a teapot.\n";
2019-03-22 21:43:57 +00:00
syslog(LOG_INFO, "Tea Pot Webhook recieved.\n");
2019-03-17 01:31:59 +00:00
exit(0);
}
2019-04-13 00:46:07 +00:00
2019-03-17 01:31:59 +00:00
?>