Skip to content

Instantly share code, notes, and snippets.

@anikishov
Last active February 26, 2020 21:23
Show Gist options
  • Save anikishov/d7604e88c7dd9f0841ec19b07bc06bac to your computer and use it in GitHub Desktop.
Save anikishov/d7604e88c7dd9f0841ec19b07bc06bac to your computer and use it in GitHub Desktop.
auth_backend & insert ads
Hi,
For configuring auth_backend & inserting ads on PHP needed to use web server, for example, nginx
# install php and nginx
apt update
apt install nginx php7.2-fpm
# turn on socket listening in php-fpm
echo "listen = /run/php/php7.2-fpm.sock" >> /etc/php/7.2/fpm/php-fpm.conf
# type next command:
nano /etc/nginx/sites-available/auth_backend
# Ctrl+O - for save file
# Ctrl+X - close editor
# copy in here follow code:
server {
listen 80;
server_name <BACKEND_SITE_DOMAIN>;
root /var/www/auth_backend/;
index index.php;
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
}
# turn on auth_backend site in nginx (create simlink)
ln -s /etc/nginx/sites-available/auth_backend /etc/nginx/sites-enabled/
# Create a directory for the site and put here auth_backend script
mkdir /var/www/auth_backend
nano /var/www/auth_backend/auth_backend.php
<?php
# auth_backend code
$get = print_r($_GET, true);
$token = $_GET["token"];
if(!$token || !strlen($token)) {
header('HTTP/1.0 403 Forbidden');
error_log("No token provided", 4);
die();
}
$tokens = array();
$contents = explode("\n", file_get_contents("auth.txt"));
foreach($contents as $line) {
if(strlen($line) > 3) {
$parts = explode(":", $line);
$tokens[$parts[1]] = $parts[0];
}
}
if($tokens[$token]) {
header("HTTP/1.0 200 OK");
header("X-UserId: ".$tokens[$token]."\r\n");
# header("X-Max-Sessions: 5\r\n"); // Turn this on to protect from multiscreen
} else {
header('HTTP/1.0 403 Forbidden');
}
# code to insert ads
header('Content-type: application/json');
$user_ads = [
"preroll" => "vod/bunny.mp4",
"midroll_interval" => 180,
"midroll" => ["vod/bunny.mp4", "vod/bunny.mp4"]
];
echo json_encode(array("ad_inject" => $user_ads));
?>
# Ctrl+O - for save file
# Ctrl+X - close editor
# File with tokens and users:
nano /var/www/auth_backend/auth.txt
user1:token1
user2:token2
user3:token3
# Ctrl+O - for save file
# Ctrl+X - close editor
chmod 755 -R /var/www/auth_backend/
# For apply changes restart nginx
service nginx restart
# For test perform follow command:
curl -v http://<BACKEND_SITE_DOMAIN>/auth_backend.php
* Trying 127.0.1.1...
* TCP_NODELAY set
* Connected to an1.e (127.0.1.1) port 8080 (#0)
> GET /auth_backend.php HTTP/1.1
> Host: <BACKEND_SITE_DOMAIN>
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 403 Forbidden
< Server: nginx/1.14.0 (Ubuntu)
< Date: Sat, 18 May 2019 07:51:33 GMT
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
# Configure the Flussonic,
nano /etc/flussonic/flussonic.conf
# type here following code:
auth_backend main {
backend http://<BACKEND_SITE_DOMAIN>/auth_backend.php;
}
stream ads_insert {
url fake://fake;
auth auth://main;
}
# apply Flussonic configuration
service flussonic reload
# For test open url
http://flussonic-ip/ads_insert/embed.html?token=token1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment