aaPanel_Kern I tried to protect the urls of the websites a bit so that they do not access inappropriate things. They are in the configuration file that I uploaded earlier
basically with these two files I control what enters the server
config domain file
server
{
listen 80;
listen 888;
listen 443 ssl http2;
server_name animalitos-pro.com;
index index.php index.html index.htm default.htm default.html;
# root /www/wwwroot/animalitos-pro.com;
#SSL-START SSL related configuration
#error_page 404/404.html;
ssl_certificate /www/server/panel/vhost/cert/animalitos/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/animalitos/privkey.pem;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
add_header Strict-Transport-Security "max-age=31536000";
error_page 497 https://$host$request_uri;
#SSL-END
#ERROR-PAGE-START Error page related configuration
#error_page 404 /404.html;
#error_page 502 /502.html;
#ERROR-PAGE-END
#phpmyadmin
#end
#REWRITE-START Pseudo-static related configuration
include /www/server/panel/vhost/rewrite/node_animalitos.conf;
#REWRITE-END
#Files or directories forbidden to access
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md|package.json|package-lock.json|\.env|node_modules) {
return 404;
}
#One-click application for SSL certificate verification directory related settings
location /.well-known/ {
root /www/wwwroot/animalitos-pro.com;
}
# HTTP reverse proxy related settings begin >>>
location ~ /purge(/.*) {
proxy_cache_purge cache_one $host$request_uri$is_args$args;
}
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
add_header X-Cache $upstream_cache_status;
proxy_connect_timeout 30s;
proxy_read_timeout 86400s;
proxy_send_timeout 30s;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
# End of HTTP reverse proxy related settings <<<
access_log /www/wwwlogs/animalitos.log;
error_log /www/wwwlogs/animalitos.error.log;
}
app js
import fs from 'fs';
import Url from 'url';
import Path from 'path';
import http from 'http';
import express from 'express';
import bodyParser from 'body-parser';
import fetch from 'node-fetch';
import * as socketIO from "socket.io";
const __dirname = Path.dirname( Url.fileURLToPath( import.meta.url ) );
// npm i -S express body-parser node-fetch
// npm i -D nodemon
// fuser -k 80/tcp
import { ticks, toDate } from "./Frame.js"
process.env.TZ = "America/Caracas";
const port = 5000;
const app = express();
const server = http.createServer( app );
const io = new socketIO.Server( server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
credentials: true
}
} );
function getCurrentFile( _Date = null ){
let date = _Date || new Date();
let day = ''+( date.getDate() );
let year = ''+( date.getFullYear() );
let month = ''+( date.getMonth() + 1 );
let base = 'resultados';
let name = 'index.html';
let path = Path.join( __dirname, base, year, month, day );
let file = Path.join( path, name );
return { path, file, name };
};
function getCurrentDay(){
let day = 0;
let next = true;
let date = new Date();
let arr = date.toLocaleString('en-US', { hour: '2-digit', minute: '2-digit' }).split(' ');
let [ h, m ] = arr[0].split(':').map( e => parseInt( e ) );
h = arr[1] == 'PM' ? h + 12: h;
if( h >= 9 && h <= 19 ){
next = false;
if( ( h == 9 && m < 5 ) || ( h >= 19 && m > 5 ) ){
next = true;
}
}
if( next ){
if( arr[1] == 'AM' ){
day = 1;
}
}
date.setDate( date.getDate() - day );
return { next, day, date };
};
app.set('trust proxy', true);
app.use( bodyParser.urlencoded({ extended: false }) ).use( bodyParser.json() ).use( ( req, res, next ) => {
res.setHeader( 'Access-Control-Allow-Origin', req.headers.origin ?? '*' );
res.setHeader( 'Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE' );
res.setHeader( 'Access-Control-Allow-Headers', '*' );
next();
});
/*
app.get('/', function( req, res ){
//res.end('Hola');
//res.status(200).send('Hola!');
//var file = Path.join(__dirname, 'index.html');
var file = Path.join(__dirname, 'public', 'index.html');
res.sendFile( file );
});*/
function GET( url ){
return fetch(url, {
headers: {
'User-Agent': '',
},
}).then(async function(r){
var html = null;
try{
html = await r.text();
}catch(ex){ }
return html;
});
};
function animalitos( req, res ){
var status = 200;
var list = [];
var temp = ( req.params[0] ?? '/' ).trim();
var url = 'https://www.tuazar.com/loteria/animalitos/resultados' + temp;
let { date } = getCurrentDay();
let { path, file } = getCurrentFile( date );
console.log({ temp, path });
if( fs.existsSync( file ) && temp == '/' ){
fs.createReadStream( file ).pipe( res );
console.log('cargado desde cache.');
return;
}
console.log('descargando desde otro server.');
GET( url ).then(function( html ){
if( html == null ){
status = 501;
}
if( html != null && temp == '/' ){
if( !fs.existsSync( path ) ){
fs.mkdirSync( path, { recursive: true } );
}
fs.writeFileSync( file, html);
}
res.status( status ).send( html );
});
};
app.get(/animalitos(.*)/, animalitos);
app.use( express.static(__dirname + '/.well-known') );
app.use( express.static(__dirname + '/public') );
//127.0.0.1/js
app.all('*', function( req, res ){
res.status(404).send('Que paso marico, sete perdio algo?');
});
async function wsConnection( socket ){
console.log("User connected via socket.io");
socket.emit('all', [1,2,3,4,5]);
};
io.on("connection", wsConnection);
server.listen(port, async function(){
//startCron();
console.log('Servidor corriendo en el puerto:', port);
async function updateAll( ){
console.log('fetch request to update data.');
var url = 'https://www.tuazar.com/loteria/animalitos/resultados/';
GET( url ).then(function( html ){
if( html != null ){
let { path, file } = getCurrentFile();
if( !fs.existsSync( path ) ){
fs.mkdirSync( path, { recursive: true } );
}
fs.writeFileSync( file, html);
try{
io.emit('update', html);
}catch(ex){}
}
//res.status( status ).send( html );
});
};
[].slice.call( Object.keys( ticks ) ).map( str => {
ticks[ str ] = updateAll;
});
//console.log('ticks:', ticks);
const sleep = ( ms = 1000 / 16 ) => new Promise( R => setTimeout( R, ms ) );
var aux = toDate();
function onupdate(){
let str = toDate();
if( str != aux ){
aux = str;
if( typeof ticks[ str ] === 'function' ){
ticks[ str ]( );
}
//console.log('str:', str);
try{
io.emit('time', str);
}catch(ex){}
}
};
while( !0 ){
onupdate();
await sleep( 1000 / 20 );
}
/*
var frame = new AnimationFrame();
frame.fps_limit = 4;
frame.onupdate = onupdate;
frame.start();
*/
});