mirror of
https://codeberg.org/SafeTwitch/safetwitch.git
synced 2024-12-22 13:22:58 -05:00
Working IRC proxy
This commit is contained in:
parent
0facf85528
commit
0c38345bd1
2 changed files with 77 additions and 5 deletions
|
@ -1,6 +1,7 @@
|
||||||
import { Router, Response, Request, NextFunction } from 'express'
|
import { Router, Response, Request, NextFunction } from 'express'
|
||||||
import { TwitchAPI } from '../util/scraping/extractor';
|
import { TwitchAPI } from '../util/scraping/extractor';
|
||||||
import ws from 'ws';
|
import ws, { WebSocket } from 'ws';
|
||||||
|
import { TwitchChat } from '../util/scraping/chat/chat';
|
||||||
|
|
||||||
const proxyRouter = Router();
|
const proxyRouter = Router();
|
||||||
const twitch = new TwitchAPI()
|
const twitch = new TwitchAPI()
|
||||||
|
@ -56,12 +57,76 @@ proxyRouter.get('/hls/:encodedUrl' , async (req: Request, res: Response, next: N
|
||||||
|
|
||||||
|
|
||||||
// IRC PROXY
|
// IRC PROXY
|
||||||
|
interface ExtWebSocket extends WebSocket {
|
||||||
|
id: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const chat = new TwitchChat({
|
||||||
|
login: {
|
||||||
|
username: 'justinfan23423',
|
||||||
|
password: 'none'
|
||||||
|
},
|
||||||
|
channels: []
|
||||||
|
})
|
||||||
|
chat.connect()
|
||||||
|
|
||||||
|
const clients : { [k:string]: ExtWebSocket[] } = {}
|
||||||
|
import { randomUUID } from 'crypto';
|
||||||
|
|
||||||
|
const findClientsForStreamer = async (streamerName: string) => {
|
||||||
|
if(!clients[streamerName]) return Promise.reject(new Error('No clients following streamer'))
|
||||||
|
|
||||||
|
return clients[streamerName]
|
||||||
|
}
|
||||||
|
|
||||||
export const wsServer = new ws.Server({ noServer: true });
|
export const wsServer = new ws.Server({ noServer: true });
|
||||||
wsServer.on('connection', (socket: ws.WebSocket) => {
|
wsServer.on('connection', (ws: ExtWebSocket) => {
|
||||||
socket.send('Welcome! Send a comma seperated list to get the IRC')
|
const socket = ws as ExtWebSocket
|
||||||
socket.on('message', message => console.log(message.toString()));
|
socket.on('message', (message) => {
|
||||||
|
const data = message.toString()
|
||||||
|
const splitted = data.split(' ')
|
||||||
|
|
||||||
|
if(splitted.length > 2) socket.close()
|
||||||
|
if(splitted[0] !== 'JOIN') socket.close()
|
||||||
|
|
||||||
|
const streamersToJoin = splitted[1].split(',')
|
||||||
|
if(streamersToJoin.length > 1) socket.close()
|
||||||
|
|
||||||
|
const id = randomUUID()
|
||||||
|
for (let streamer of streamersToJoin) {
|
||||||
|
chat.addStreamer(streamer)
|
||||||
|
|
||||||
|
if(clients[streamer]) {
|
||||||
|
clients[streamer].push(socket)
|
||||||
|
} else {
|
||||||
|
clients[streamer] = [socket]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
socket.id = id
|
||||||
|
socket.send('OK')
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on('close', () => {
|
||||||
|
if(socket.id) {
|
||||||
|
}
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
chat.on('PRIVMSG', async (username, type, channel, message) => {
|
||||||
|
const socketsToSend = await findClientsForStreamer(channel)
|
||||||
|
for(let socket of socketsToSend) {
|
||||||
|
let payload = {
|
||||||
|
username,
|
||||||
|
type,
|
||||||
|
channel,
|
||||||
|
message
|
||||||
|
}
|
||||||
|
socket.send(JSON.stringify(payload))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export default proxyRouter
|
export default proxyRouter
|
|
@ -11,6 +11,7 @@ export class TwitchChat extends EventEmitter{
|
||||||
public channels: string[]
|
public channels: string[]
|
||||||
private url = 'wss://irc-ws.chat.twitch.tv:443'
|
private url = 'wss://irc-ws.chat.twitch.tv:443'
|
||||||
private ws: WebSocket | null;
|
private ws: WebSocket | null;
|
||||||
|
private isConnected: boolean = false
|
||||||
|
|
||||||
constructor(options: TwitchChatOptions) {
|
constructor(options: TwitchChatOptions) {
|
||||||
super()
|
super()
|
||||||
|
@ -31,7 +32,7 @@ export class TwitchChat extends EventEmitter{
|
||||||
let parsedMetadata: Metadata = {
|
let parsedMetadata: Metadata = {
|
||||||
username: parseUsername(metadata[0]),
|
username: parseUsername(metadata[0]),
|
||||||
messageType: metadata[1],
|
messageType: metadata[1],
|
||||||
channel: metadata[2],
|
channel: metadata[2].replace('#', ''),
|
||||||
message: message
|
message: message
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,6 +46,7 @@ export class TwitchChat extends EventEmitter{
|
||||||
|
|
||||||
public async connect() {
|
public async connect() {
|
||||||
this.ws = new WebSocket(this.url)
|
this.ws = new WebSocket(this.url)
|
||||||
|
this.isConnected = true
|
||||||
|
|
||||||
this.ws.on('open', () => {
|
this.ws.on('open', () => {
|
||||||
if(this.ws) {
|
if(this.ws) {
|
||||||
|
@ -62,4 +64,9 @@ export class TwitchChat extends EventEmitter{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public addStreamer(streamerName: string) {
|
||||||
|
if(!this.isConnected) return;
|
||||||
|
this.ws!.send(`JOIN #${streamerName}`)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue