feat: nextdns features

This commit is contained in:
Saahil 2024-10-28 23:40:41 -04:00 committed by GitHub
parent bf922fae42
commit 60eae376bd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 66 additions and 0 deletions

View file

@ -11,6 +11,7 @@ import { getJellyfinStatus, getSpotifyStatus } from "./modules/status";
import { getResponse } from "./modules/randomResponseSystem";
import * as utils from "./modules/index";
import howWasYourDay from "./modules/howWasYourDay";
import { myPrivateDNS } from "./modules/nextdns";
const db = new JSONdb("data.json");
app.start(process.env.PORT || 3000).then(async (d) => {
@ -25,6 +26,7 @@ app.start(process.env.PORT || 3000).then(async (d) => {
text: `Starting Slack Bot :D`,
});
init(app);
myPrivateDNS(app);
});
// app.client.cha
const cmdLoader = new Loader(app, path.join(__dirname, "commands"));

64
src/modules/nextdns.ts Normal file
View file

@ -0,0 +1,64 @@
import { ModifiedApp } from "./slackapp"
export interface Root {
timestamp: string
domain: string
root: string
encrypted: boolean
protocol: string
clientIp: string
device: Device
status: string
reasons: Reason[]
}
export interface Reason {
id: string
name: string
}
export interface Device {
id: string
name: string
}
export function myPrivateDNS(app: ModifiedApp) {
fetch(`https://api.nextdns.io/profiles/${process.env.MY_NEXTDNS}/logs/stream`,
{ headers:
{ "X-Api-Key": process.env.NEXTDNS_API_KEY }}
).then(r=>{
const reader = r.body?.getReader();
const rs = require("stream").Readable();
rs._read = async () => {
const result = await reader?.read();
if(!result?.done){
//@ts-ignore
rs.push(Buffer.from(result.value));
}else{
rs.push(null);
return;
}
};
//@ts-ignore
rs.on('data', (d) => {
const str = d.toString()
if(str.includes(':keepalive')) return;
console.debug(str)
let splits = str.split('\n')
let id = splits[0].split(/ +/)[1]
let data = splits[1].split(":").slice(1).join(":")
if(!id || !data) return;
const realData:Root = JSON.parse(data)
console.log(realData)
//@ts-ignore
delete realData.clientIp;
// console.log(`${realData.status == 'blocked' ? ':x:' : ":white_check_mark:"} ${realData.encrypted ? ":lock: " : ""} - ${realData.domain} `)
app.client.chat.postMessage({
channel: `C07LT7XS28Z`,
text: `${realData.status == 'blocked' ? ':x:' : ":white_check_mark:"} ${realData.encrypted ? ":lock: " : ""} - ${realData.domain} `,
});
})
rs.on('end', () => {
console.log('end')
})
})
}