|  | @@ -1,11 +1,10 @@
 | 
	
		
			
				|  |  | +let argv = require('minimist')(process.argv.slice(2));
 | 
	
		
			
				|  |  |  const cron = require("node-cron");
 | 
	
		
			
				|  |  |  const fs = require('fs');
 | 
	
		
			
				|  |  |  const express = require('express');
 | 
	
		
			
				|  |  |  const app = express();
 | 
	
		
			
				|  |  | -const app_serve = express();
 | 
	
		
			
				|  |  |  const mysql = require('mysql2')
 | 
	
		
			
				|  |  |  const SendSeekable = require('send-seekable');
 | 
	
		
			
				|  |  | -const WebSocket = require('ws');
 | 
	
		
			
				|  |  |  const pool = mysql.createPool({
 | 
	
		
			
				|  |  |      keepAliveInitialDelay: 10000, enableKeepAlive: true,
 | 
	
		
			
				|  |  |  })
 | 
	
	
		
			
				|  | @@ -17,16 +16,15 @@ const nodemailer = require('nodemailer')
 | 
	
		
			
				|  |  |  const bodyParser = require("body-parser");
 | 
	
		
			
				|  |  |  const nrc = require('node-run-cmd')
 | 
	
		
			
				|  |  |  const https = require("node:https");
 | 
	
		
			
				|  |  | -const {WebSocketServer} = require("ws");
 | 
	
		
			
				|  |  | -const http = require("node:http");
 | 
	
		
			
				|  |  |  const {OpenCC} = require("opencc");
 | 
	
		
			
				|  |  |  const converter = new OpenCC('s2t.json')
 | 
	
		
			
				|  |  |  app.use(bodyParser.json({"limit": "200mb"}));
 | 
	
		
			
				|  |  |  app.use(express.json());
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -app_serve.use(bodyParser.json({"limit": "200mb"}));
 | 
	
		
			
				|  |  | -app_serve.use(express.json());
 | 
	
		
			
				|  |  | -app_serve.use(SendSeekable)
 | 
	
		
			
				|  |  | +app.use(SendSeekable)
 | 
	
		
			
				|  |  | +let availCache = {}
 | 
	
		
			
				|  |  | +cron.schedule('* */12 * * *', () => {
 | 
	
		
			
				|  |  | +    availCache = {}
 | 
	
		
			
				|  |  | +})
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  const transporter = nodemailer.createTransport({
 | 
	
		
			
				|  |  |      host: 'smtp.qq.com', port: 465, secure: true, auth: {
 | 
	
	
		
			
				|  | @@ -34,31 +32,48 @@ const transporter = nodemailer.createTransport({
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  })
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +/// TODO
 | 
	
		
			
				|  |  | +let ncmAPIUrl = "http://localhost:5999";
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -app.post('/submit', function (req, res) {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +function Submit(req, res) {
 | 
	
		
			
				|  |  |      let uuid = crypto.randomUUID()
 | 
	
		
			
				|  |  |      db.connect(function (err) {
 | 
	
		
			
				|  |  |          if (err) {
 | 
	
		
			
				|  |  |              console.log(err)
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |      })
 | 
	
		
			
				|  |  | +    try {
 | 
	
		
			
				|  |  | +        res.header("Access-Control-Allow-Origin", "*");
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    res.header("Access-Control-Allow-Origin", "*");
 | 
	
		
			
				|  |  | +    } catch (e) {
 | 
	
		
			
				|  |  | +        console.log(e)
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      let albumCover = null;
 | 
	
		
			
				|  |  |      try {
 | 
	
		
			
				|  |  |          fetch(req.body.file).then(res => {
 | 
	
		
			
				|  |  |              res.arrayBuffer().then(r => fs.writeFileSync("./" + uuid, Buffer.from(r)));
 | 
	
		
			
				|  |  |          })
 | 
	
		
			
				|  |  | -        fetch(req.body.albumCover).then(res=>{
 | 
	
		
			
				|  |  | -            res.arrayBuffer().then(r=>{
 | 
	
		
			
				|  |  | +        fetch(req.body.albumCover).then(res => {
 | 
	
		
			
				|  |  | +            res.arrayBuffer().then(r => {
 | 
	
		
			
				|  |  |                  albumCover = r
 | 
	
		
			
				|  |  |              })
 | 
	
		
			
				|  |  |          })
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        res.end("api_success");
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            res.end("api_success")
 | 
	
		
			
				|  |  | +        } catch (e) {
 | 
	
		
			
				|  |  | +            console.log(e)
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      } catch (err) {
 | 
	
		
			
				|  |  | -        res.status(500).send("Server Error");
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            res.status(500).send("Server Error");
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        } catch (e) {
 | 
	
		
			
				|  |  | +            console.log(e)
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |          return
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -133,7 +148,7 @@ app.post('/submit', function (req, res) {
 | 
	
		
			
				|  |  |                  to: req.body.email,
 | 
	
		
			
				|  |  |                  subject: "你的音频已处理完成。",
 | 
	
		
			
				|  |  |                  text: "你的音频已处理完成。",
 | 
	
		
			
				|  |  | -                html: fs.readFileSync("./Template.html", "utf8").toString().replace("{song_name}", req.body.name).replace("{href_link}", "https://andyxie.cn:4000/player?play="+uuid)
 | 
	
		
			
				|  |  | +                html: fs.readFileSync("./Template.html", "utf8").toString().replace("{song_name}", req.body.name).replace("{href_link}", "https://andyxie.cn:4000/player?play=" + uuid)
 | 
	
		
			
				|  |  |              }).then((result) => {
 | 
	
		
			
				|  |  |                  console.log("Message sent: %s", result.messageId)
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -144,15 +159,17 @@ app.post('/submit', function (req, res) {
 | 
	
		
			
				|  |  |      })
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -})
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +app.post('/submit', Submit)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  app.options('/submit', function (req, res) {
 | 
	
		
			
				|  |  |      res.header("Access-Control-Allow-Origin", "*");
 | 
	
		
			
				|  |  |      res.header("Access-Control-Allow-Headers", "Content-Type");
 | 
	
		
			
				|  |  |      res.end()
 | 
	
		
			
				|  |  |  })
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  app.post('/search_api', async function (req, res) {
 | 
	
		
			
				|  |  |      async function OnFetched(err, rows) {
 | 
	
		
			
				|  |  |          try {
 | 
	
	
		
			
				|  | @@ -178,7 +195,6 @@ app.post('/search_api', async function (req, res) {
 | 
	
		
			
				|  |  |                     or artist like '%${await converter.convertPromise(req.body.searchStr)}%'`, OnFetched)
 | 
	
		
			
				|  |  |      db.unprepare()
 | 
	
		
			
				|  |  |  })
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  app.options('/search_api', function (req, res) {
 | 
	
		
			
				|  |  |      res.header("Access-Control-Allow-Origin", "*");
 | 
	
		
			
				|  |  |      res.header("Access-Control-Allow-Headers", "Content-Type");
 | 
	
	
		
			
				|  | @@ -206,13 +222,60 @@ app.get("/getSingle", function (req, res) {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  })
 | 
	
		
			
				|  |  | -var availCache = {}
 | 
	
		
			
				|  |  | -cron.schedule('* */12 * * *', () => {
 | 
	
		
			
				|  |  | -    availCache = {}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +// 163
 | 
	
		
			
				|  |  | +app.options('/ncm/url', function (req, res) {
 | 
	
		
			
				|  |  | +    res.header("Access-Control-Allow-Origin", "*");
 | 
	
		
			
				|  |  | +    res.header("Access-Control-Allow-Headers", "Content-Type");
 | 
	
		
			
				|  |  | +    res.end()
 | 
	
		
			
				|  |  |  })
 | 
	
		
			
				|  |  | -app_serve.get('/:uuid', function (req, res, next) {
 | 
	
		
			
				|  |  | +app.post("/ncm/url", function (req, res) {
 | 
	
		
			
				|  |  | +    if (req.body.id) {
 | 
	
		
			
				|  |  | +        let id = req.body.id
 | 
	
		
			
				|  |  | +        fetch(
 | 
	
		
			
				|  |  | +            ncmAPIUrl + "/song/download/url/v1?id=" + id + "&level=hires", {
 | 
	
		
			
				|  |  | +                headers: {
 | 
	
		
			
				|  |  | +                    Cookie: "MUSIC_U=0087F9D8E102A1C1661EBE1792412F3351DA64D1BD3D862BA77E45E9024524725F3A1983345D9B5A4014C725D19C069DD71081F6FE3659F9E1FD412DC427FB809FAF7789AEEA10E9DE6F06C58D1959BA209D2A83C3FA753261036C4CFD0D143B6C7748B8A6D2DD5C2E96E75D1E847E4AAE035CB2C86B175D9AFC6A164C522ED76E24AE654740AB6BAF5B29597F7E3B0158B2EC1C37F2688279871873FA7ADAEF8280A059E84C4BBFB9E4F225F9A2065DF652247D5496587A7B1E3D35DB0CD3F825C06FE5BFE5CFEF1770847099704360504B73C9B396E37CECE4F9DDEE6001588C3C4F5B2861D9ADF339FC47DD480858CA800620785EA032215B63B81025304DB3331F384793FF8EE681247E34C7931176F2F618B66C122F0602F1EA15F963E422DEC79C257F3577A197BECE71E316C751C3B9F5F3CD07BFDC0270A287A1BB6576"
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        ).then(async result => {
 | 
	
		
			
				|  |  | +            let result_json = await result.json()
 | 
	
		
			
				|  |  | +            if (result.status === 200) {
 | 
	
		
			
				|  |  | +                let infos = await (await fetch(ncmAPIUrl + "/song/detail?ids=" + id)).json();
 | 
	
		
			
				|  |  | +                req.body.file = "data:audio/flac;base64,"+Buffer.from(await (await fetch(result_json.data.url)).arrayBuffer()).toString("base64")
 | 
	
		
			
				|  |  | +                req.body.name = infos.songs[0].name;
 | 
	
		
			
				|  |  | +                req.body.albumName = infos.songs[0].al.name
 | 
	
		
			
				|  |  | +                req.body.albumCover = infos.songs[0].al.picUrl
 | 
	
		
			
				|  |  | +                req.body.link = result_json.data.url
 | 
	
		
			
				|  |  | +                req.body.artist = infos.songs[0].ar[0].name
 | 
	
		
			
				|  |  | +                /// Complete
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                Submit(req, res)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        })
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    } else {
 | 
	
		
			
				|  |  | +        res.contentType("application/json");
 | 
	
		
			
				|  |  | +        res.header("Access-Control-Allow-Origin", "*");
 | 
	
		
			
				|  |  | +        res.header("Access-Control-Allow-Headers", "Content-Type");
 | 
	
		
			
				|  |  | +        res.end("{}")
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +})
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +app.get("/favicon.ico", function (req, res) {
 | 
	
		
			
				|  |  | +    res.end("");
 | 
	
		
			
				|  |  | +})
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +// Fetch
 | 
	
		
			
				|  |  | +app.get('/:uuid', function (req, res) {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      let uuid = req.params.uuid;
 | 
	
		
			
				|  |  | +    console.log(uuid);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      function Provider(err, rows) {
 | 
	
		
			
				|  |  |          try {
 | 
	
	
		
			
				|  | @@ -255,17 +318,15 @@ app_serve.get('/:uuid', function (req, res, next) {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  })
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -// TODO Always use HTTPS before publishing.
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | - https.createServer({
 | 
	
		
			
				|  |  | -     key: fs.readFileSync('andyxie.cn.key'), cert: fs.readFileSync('andyxie.cn.pem')
 | 
	
		
			
				|  |  | -}, app).listen(8080)
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | - https.createServer({
 | 
	
		
			
				|  |  | -     key: fs.readFileSync('andyxie.cn.key'), cert: fs.readFileSync('andyxie.cn.pem')
 | 
	
		
			
				|  |  | +if (argv.https === "true") {
 | 
	
		
			
				|  |  | +    https.createServer({
 | 
	
		
			
				|  |  | +        key: fs.readFileSync('andyxie.cn.key'), cert: fs.readFileSync('andyxie.cn.pem')
 | 
	
		
			
				|  |  | +    }, app).listen(8080)
 | 
	
		
			
				|  |  | +    console.log("Listening on port 8080 with TLS")
 | 
	
		
			
				|  |  | +} else {
 | 
	
		
			
				|  |  | +    app.listen(8080)
 | 
	
		
			
				|  |  | +    console.log("Listening on port 8080")
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | - }, app_serve).listen(8079)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -// app.listen(8080)
 | 
	
		
			
				|  |  | -// app_serve.listen(8079)
 |