104 lines
3.0 KiB
JavaScript
104 lines
3.0 KiB
JavaScript
const spawn=require('child_process').spawn
|
|
const crypto=require('crypto')
|
|
const promisify=require('util').promisify
|
|
const fs=require('fs')
|
|
const path=require('path')
|
|
|
|
const request=require('request')
|
|
|
|
const express=require('express')
|
|
const multer=require('multer')
|
|
const maxmind=require('maxmind')
|
|
|
|
const countryLookup=maxmind.openSync('GeoLite2-Country.mmdb')
|
|
const app=express()
|
|
let packageListHash=null
|
|
|
|
function GetRequestIP(req) {
|
|
return req.headers['x-forwarded-for'] ||
|
|
req.connection.remoteAddress ||
|
|
req.socket.remoteAddress ||
|
|
req.connection.socket.remoteAddress || ''
|
|
}
|
|
|
|
function GetIPCountry(ip) {
|
|
let x=countryLookup.get(ip)
|
|
if(x) return x.country.iso_code
|
|
else return null
|
|
}
|
|
|
|
app.get("/gateway",async (req,res)=>{
|
|
let ip=GetRequestIP(req)
|
|
let country=GetIPCountry(ip)
|
|
console.log(`Client IP: ${ip} Country: ${country}`)
|
|
res.end(country)
|
|
})
|
|
|
|
app.get("/listver",(req,res)=>{
|
|
res.writeHead(200,'OK')
|
|
res.end(packageListHash)
|
|
})
|
|
|
|
app.use("/list",express.static("tmp"))
|
|
|
|
function GetFileHash(filepath) {
|
|
return new Promise((resolve)=>{
|
|
let hash=crypto.createHash('sha256')
|
|
let input=fs.createReadStream(filepath)
|
|
input.on('data',(data)=>{
|
|
hash.update(data)
|
|
})
|
|
input.on('end',()=>{
|
|
return resolve(hash.digest('hex'))
|
|
})
|
|
})
|
|
}
|
|
|
|
function UpdatePackageList() {
|
|
return new Promise((resolve,reject)=>{
|
|
request('http://kiritow.com:3000/gpm/list/raw/master/programs.info',(err,res,body)=>{
|
|
if(err) return reject(err)
|
|
if(!res || res.statusCode!=200 ) return reject("Invalid response.")
|
|
fs.writeFile(path.join('tmp','programs.info.tmp'),body,(err)=>{
|
|
if(err) return reject(err)
|
|
GetFileHash(path.join('tmp','programs.info.tmp')).then((hash)=>{
|
|
fs.rename(path.join('tmp','programs.info.tmp'),path.join('tmp',hash),(err)=>{
|
|
if(err) return reject(err)
|
|
packageListHash=hash
|
|
return resolve()
|
|
})
|
|
}).catch((err)=>{
|
|
return reject(err)
|
|
})
|
|
})
|
|
})
|
|
})
|
|
}
|
|
|
|
async function ClearTmp() {
|
|
let files=await promisify(fs.readdir)('tmp')
|
|
let parr=new Array
|
|
files.forEach((name)=>{
|
|
parr.push(promisify(fs.unlink)(path.join('tmp',name)))
|
|
})
|
|
await Promise.all(parr)
|
|
return parr.length
|
|
}
|
|
|
|
async function main() {
|
|
let _tmBefore=new Date()
|
|
console.log("[Init] Grab Server Starting...")
|
|
console.log("[Init] Checking directories...")
|
|
fs.mkdirSync("tmp",{recursive:true})
|
|
console.log("[Init] Cleaning tmp...")
|
|
let count=await ClearTmp()
|
|
console.log(`${count} files removed.`)
|
|
console.log("[Init] Initializing package list...")
|
|
UpdatePackageList()
|
|
|
|
console.log("[Working] Starting web server...")
|
|
app.listen(7887)
|
|
console.log(`[Done] Server started in ${(new Date()-_tmBefore)/1000}s`)
|
|
}
|
|
|
|
main() |