user  xtreamcodes;
worker_processes  auto;

worker_rlimit_nofile 300000;
events {
  worker_connections  16000;
  use epoll;
  accept_mutex on;
  multi_accept on;
}
thread_pool pool_xtream threads=32 max_queue=0;
http {

  include       mime.types;
  default_type  application/octet-stream;

  sendfile           on;
  tcp_nopush         on;
  tcp_nodelay        on;
  reset_timedout_connection on;
  gzip off;
  fastcgi_read_timeout 200;
  access_log off;
  keepalive_timeout 10;
  include balance.conf;
  send_timeout 20m;	
  sendfile_max_chunk 512k;
  lingering_close off;
  aio threads=pool_xtream;
  client_body_timeout 13s;
  client_header_timeout 13s;
  client_max_body_size 3m;

  limit_req_zone $binary_remote_addr zone=one:30m rate=20r/s;
  limit_req_zone $binary_remote_addr zone=auth:30m rate=3r/m;

  set_real_ip_from 103.21.244.0/22;
  set_real_ip_from 103.22.200.0/22;
  set_real_ip_from 103.31.4.0/22;
  set_real_ip_from 104.16.0.0/12;
  set_real_ip_from 108.162.192.0/18;
  set_real_ip_from 131.0.72.0/22;
  set_real_ip_from 141.101.64.0/18;
  set_real_ip_from 162.158.0.0/15;
  set_real_ip_from 172.64.0.0/13;
  set_real_ip_from 173.245.48.0/20;
  set_real_ip_from 188.114.96.0/20;
  set_real_ip_from 190.93.240.0/20;
  set_real_ip_from 197.234.240.0/22;
  set_real_ip_from 198.41.128.0/17;
  set_real_ip_from 2400:cb00::/32;
  set_real_ip_from 2606:4700::/32;
  set_real_ip_from 2803:f800::/32;
  set_real_ip_from 2405:b500::/32;
  set_real_ip_from 2405:8100::/32;
  set_real_ip_from 2c0f:f248::/32;
  set_real_ip_from 2a06:98c0::/29;
  set_real_ip_from 167.114.185.145;
  set_real_ip_from 134.209.210.108;
 


  #UEMTV REVERSE PROXY
#  set_real_ip_from 62.182.84.131;
  #Ross   
 # set_real_ip_from 138.197.160.15;   

  #TEMP Reverse Proxy (Vsys)
  #set_real_ip_from 31.42.186.180;

  real_ip_header CF-Connecting-IP;
  #real_ip_header X-Forwarded-For; 

  # custom log format to log the vhost
  log_format vhost '$remote_addr - $remote_user $host [$time_local] '
                   '"$request" $status $body_bytes_sent '
                   '"$http_referer" "$http_user_agent"';

  server {
    listen 80;listen 25461;listen 25463 ssl;
    
    ssl_certificate /etc/ssl/certs/ssl-cert-snakeoil.pem;
    ssl_certificate_key /etc/ssl/private/ssl-cert-snakeoil.key;
    
    ssl_protocols SSLv3 TLSv1.1 TLSv1.2;

    index index.php index.html index.htm;

    root /home/xtreamcodes/iptv_xtream_codes/wwwdir/;

    access_log /var/log/nginx/xtream_access.log vhost;
    error_log /var/log/nginx/xtream_error.log info;

    server_tokens off;
    chunked_transfer_encoding off;

    if ( $request_method !~ ^(GET|POST)$ ) {
      return 200;
    }

    rewrite ^/live/(.*)/(.*)/(.*)\.(.*)$ /streaming/clients_live.php?username=$1&password=$2&stream=$3&extension=$4 break;
    rewrite ^/movie/(.*)/(.*)/(.*)$ /streaming/clients_movie.php?username=$1&password=$2&stream=$3&type=movie break;
    rewrite ^/series/(.*)/(.*)/(.*)$ /streaming/clients_movie.php?username=$1&password=$2&stream=$3&type=series break;
    rewrite ^/(.*)/(.*)/(.*).ch$ /streaming/clients_live.php?username=$1&password=$2&stream=$3&extension=ts break;
    rewrite ^/(.*)\.ch$ /streaming/clients_live.php?extension=ts&stream=$1&qs=$query_string break;
    rewrite ^/ch(.*)\.m3u8$ /streaming/clients_live.php?extension=m3u8&stream=$1&qs=$query_string break;
    rewrite ^/hls/(.*)/(.*)/(.*)/(.*)/(.*)$ /streaming/clients_live.php?extension=m3u8&username=$1&password=$2&stream=$3&type=hls&segment=$5&token=$4 break;
    rewrite ^/hlsr/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$ /streaming/clients_live.php?token=$1&username=$2&password=$3&segment=$6&stream=$4&key_seg=$5 break;
    rewrite ^/timeshift/(.*)/(.*)/(.*)/(.*)/(.*)\.(.*)$ /streaming/timeshift.php?username=$1&password=$2&stream=$5&extension=$6&duration=$3&start=$4 break;
    rewrite ^/timeshifts/(.*)/(.*)/(.*)/(.*)/(.*)\.(.*)$ /streaming/timeshift.php?username=$1&password=$2&stream=$4&extension=$6&duration=$3&start=$5 break;

    rewrite ^/(.*)/(.*)/(\d+)$ /streaming/clients_live.php?username=$1&password=$2&stream=$3&extension=ts break;
    #add pvr support
    rewrite ^/server/load.php$ /portal.php break;

    location /stalker_portal/c {
      alias /home/xtreamcodes/iptv_xtream_codes/wwwdir/c;
    }

    # we limit /portal.php -> type=stb && action=mac || action=handshake to 3 requests per minute, per IP
    set $portal_limit 0;

    error_page 429 = @slowportal;

    if ( $arg_action = 'handshake' ) { set $portal_limit 1; }
    if ( $arg_action = 'get_profile' ) { set $portal_limit 1; }
    if ( $arg_mac ) { set $portal_limit 1; }

    location ~ /portal.php {
      if ( $portal_limit ) {
        return 429;
      }
    
      limit_req zone=one burst=6;
      try_files $uri =404;
      fastcgi_pass php;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }

    location @slowportal {
      limit_req zone=auth burst=3 nodelay;

      try_files $uri =404;
      fastcgi_pass php;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }
    # end limit of portal

    #FFmpeg Report Progress
    location = /progress.php {
      allow 127.0.0.1;
      deny all;
      fastcgi_pass php;
      include fastcgi_params;
      fastcgi_ignore_client_abort on;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }


    location ~ \.php$ {
      limit_req zone=one burst=28;
      try_files $uri =404;
      fastcgi_index index.php;
      fastcgi_pass php;
      include fastcgi_params;
      fastcgi_buffering on;
      fastcgi_buffers 96 32k;
      fastcgi_buffer_size 32k;
      fastcgi_max_temp_file_size 0;
      fastcgi_keep_conn on;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }
  }
  server {
    listen 2086;
    index index.php index.html index.htm;

    root /home/xtreamcodes/iptv_xtream_codes/admin/;

		access_log /var/log/nginx/admin_access.log vhost;
		error_log /var/log/nginx/admin_error.log info;

    location ~ \.php$ {
      limit_req zone=one burst=8;
      try_files $uri =404;
      fastcgi_index index.php;
      fastcgi_pass php;
      include fastcgi_params;
      fastcgi_buffering on;
      fastcgi_buffers 96 32k;
      fastcgi_buffer_size 32k;
      fastcgi_max_temp_file_size 0;
      fastcgi_keep_conn on;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }
  }
}
