234 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
			
		
		
	
	
			234 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
const { CLIENT_SEND_MESSAGE, CLIENT_UPDATE_PROP } = require("./IPC");
 | 
						||
const stats = require("./stats");
 | 
						||
function Client()
 | 
						||
{
 | 
						||
    /**
 | 
						||
     * @type {string}
 | 
						||
     */
 | 
						||
    this.id = null;
 | 
						||
    /**
 | 
						||
     * @type {import("websocket").connection}
 | 
						||
     */
 | 
						||
    this.socket = null;
 | 
						||
    /**
 | 
						||
     * @type {Date}
 | 
						||
     */
 | 
						||
    this.created_at = null;
 | 
						||
 | 
						||
    /**
 | 
						||
     * @type {Map<string,any>}
 | 
						||
     */
 | 
						||
    this.info = new Map();
 | 
						||
    /**
 | 
						||
     * @type {Map<string,any>}
 | 
						||
     */
 | 
						||
    this.store = new Map();
 | 
						||
    /**
 | 
						||
     * @type {Set<string>}
 | 
						||
     */
 | 
						||
    this.rooms = new Set();
 | 
						||
    /**
 | 
						||
     * @type {Set<string>}
 | 
						||
     */
 | 
						||
    this.pairs = new Set();
 | 
						||
    this.requiredPair = false;
 | 
						||
 | 
						||
    this.APNumber = 0;
 | 
						||
    this.APShortCode = 0;
 | 
						||
    this.APIPAddress = 0;
 | 
						||
    this.isProxy = false;
 | 
						||
    this.proxyProcess = null;
 | 
						||
 | 
						||
    this.sync = function(...args){
 | 
						||
        process.nextTick(()=>{
 | 
						||
            for (const name of args) {
 | 
						||
                CLIENT_UPDATE_PROP(this.id, name, this[name]);
 | 
						||
            }
 | 
						||
        })
 | 
						||
    };
 | 
						||
};
 | 
						||
/**
 | 
						||
 * @type {Map<string, Client>}
 | 
						||
 */
 | 
						||
Client.clients = new Map();
 | 
						||
 | 
						||
/**
 | 
						||
 * @param {Client} client 
 | 
						||
 */
 | 
						||
Client.prototype.peerRequest = function(client){
 | 
						||
    let info = {};
 | 
						||
    this.info.forEach((value, name) => info[name] = value);
 | 
						||
    this.pairs.add(client.id);
 | 
						||
    this.sync('pairs');
 | 
						||
    client.send([
 | 
						||
        { from: this.id },
 | 
						||
        'request/pair'
 | 
						||
    ]);
 | 
						||
};
 | 
						||
 | 
						||
Client.prototype.match = function(filterObject){
 | 
						||
    let keys = Object.keys(filterObject);
 | 
						||
    let size = keys.length;
 | 
						||
    if(size > this.info.size)
 | 
						||
    {
 | 
						||
        return false
 | 
						||
    }
 | 
						||
    for (const key of keys)
 | 
						||
    {
 | 
						||
        if(this.info.has(key))
 | 
						||
        {
 | 
						||
            if(this.info.get(key) != filterObject[key])
 | 
						||
            {
 | 
						||
                return false
 | 
						||
            }
 | 
						||
        }else{
 | 
						||
            return false
 | 
						||
        }
 | 
						||
    };
 | 
						||
    return true
 | 
						||
};
 | 
						||
 | 
						||
/**
 | 
						||
 * @param {Client|string} client 
 | 
						||
 */
 | 
						||
Client.prototype.isSecure = function(client)
 | 
						||
{
 | 
						||
    const { Room } = require("./Services/Room");
 | 
						||
    if(typeof client == "string")
 | 
						||
    {
 | 
						||
        if(Client.clients.has(client))
 | 
						||
        {
 | 
						||
            client = Client.clients.get(client);
 | 
						||
        }else return false;
 | 
						||
    }else if(!(client instanceof Client)){
 | 
						||
        console.error("isSecure Client bir client veri tipinde değil")
 | 
						||
        return false;
 | 
						||
    };
 | 
						||
 | 
						||
    // Eşleştirilmiş kullanıcı
 | 
						||
    if(this.isPaired(client))
 | 
						||
    {
 | 
						||
        return true;
 | 
						||
    }
 | 
						||
 | 
						||
    // Aynı odada bulunan kullanıcı
 | 
						||
    for (const id of this.rooms) {
 | 
						||
        let room = Room.rooms.get(id);
 | 
						||
        if(room)
 | 
						||
        {
 | 
						||
            if(room.clients.has(id))
 | 
						||
            {
 | 
						||
                return true
 | 
						||
            }
 | 
						||
        }
 | 
						||
    };
 | 
						||
    return false;
 | 
						||
}
 | 
						||
/**
 | 
						||
 * @returns {{pairs:Map<string, Client>,roompairs:Map<string, Client>,intersection:Map<string, Client>}}
 | 
						||
 */
 | 
						||
Client.prototype.getSucureClients = function()
 | 
						||
{
 | 
						||
    const { Room } = require("./Services/Room");
 | 
						||
    let pairs = new Map();
 | 
						||
    let roompairs = new Map();
 | 
						||
 | 
						||
    for (const id of this.pairs)
 | 
						||
    {
 | 
						||
        pairs.set(id, Client.clients.get(id))
 | 
						||
    }
 | 
						||
 | 
						||
    // Aynı odada bulunan kullanıcı
 | 
						||
    for (const id of this.rooms) {
 | 
						||
        let room = Room.rooms.get(id);
 | 
						||
        if(room)
 | 
						||
        {
 | 
						||
            for (const [id, client] of room.clients)
 | 
						||
            {
 | 
						||
                if(id == this.id) continue;
 | 
						||
                roompairs.set(id, client)
 | 
						||
            };
 | 
						||
        }
 | 
						||
    };
 | 
						||
    return {
 | 
						||
        pairs,
 | 
						||
        roompairs,
 | 
						||
        intersection : new Map([
 | 
						||
            ...pairs,
 | 
						||
            ...roompairs
 | 
						||
        ])
 | 
						||
    };
 | 
						||
}
 | 
						||
 | 
						||
/**
 | 
						||
 * @param {Client} client 
 | 
						||
 */
 | 
						||
 Client.prototype.acceptPeerRequest = function(client){
 | 
						||
    this.pairs.add(client.id);
 | 
						||
    this.sync('pairs');
 | 
						||
    client.send([{
 | 
						||
        from: this.id
 | 
						||
    },'accepted/pair']);
 | 
						||
};
 | 
						||
/**
 | 
						||
 * @param {Client} client 
 | 
						||
 */
 | 
						||
Client.prototype.rejectPeerRequest = function(client){
 | 
						||
    this.pairs.delete(client.id);
 | 
						||
    client.pairs.delete(this.id);
 | 
						||
    this.sync('pairs');
 | 
						||
    client.send([{
 | 
						||
        from: this.id
 | 
						||
    },'end/pair']);
 | 
						||
};
 | 
						||
/**
 | 
						||
 * @param {Client|string} client 
 | 
						||
 * @returns {Boolean}
 | 
						||
 */
 | 
						||
Client.prototype.isPaired = function(client){
 | 
						||
    if(typeof client == "string")
 | 
						||
    {
 | 
						||
        return Client.clients.get(client)?.pairs.has(this.id) && this.pairs.has(client)
 | 
						||
    }
 | 
						||
    return client.pairs.has(this.id) && this.pairs.has(client.id);
 | 
						||
};
 | 
						||
/**
 | 
						||
 * @returns {string[]}
 | 
						||
 */
 | 
						||
Client.prototype.pairList = function(){
 | 
						||
    return [...this.pairs.values()].filter(e => this.isPaired(e));
 | 
						||
};
 | 
						||
 | 
						||
Client.prototype.send = function(obj){
 | 
						||
    if(this.isProxy)
 | 
						||
    {
 | 
						||
        CLIENT_SEND_MESSAGE(this.id, obj, this.proxyProcess)
 | 
						||
    }else{
 | 
						||
        stats.ws_sended_packs++;
 | 
						||
        if(this.socket.connected){
 | 
						||
            this.socket.sendUTF(JSON.stringify(obj),err => {
 | 
						||
                if(err && this.socket)
 | 
						||
                {
 | 
						||
                    console.error("I/O: Hatalı yazma işlemi yapıldı",err.message)
 | 
						||
                }
 | 
						||
            });
 | 
						||
        }else{
 | 
						||
            console.error("Bağlantısı kopmuş yazma işlemi")
 | 
						||
        }
 | 
						||
    }
 | 
						||
};
 | 
						||
 | 
						||
Client.prototype.packWriteable = function(){
 | 
						||
    return !!this.store.get("packrecaive")
 | 
						||
}
 | 
						||
Client.prototype.packReadable = function(){
 | 
						||
    return !!this.store.get("packsending")
 | 
						||
}
 | 
						||
Client.prototype.peerInfoNotifiable = function(){
 | 
						||
    return !!this.store.get("notifyPairInfo")
 | 
						||
}
 | 
						||
Client.prototype.roomInfoNotifiable = function(){
 | 
						||
    return !!this.store.get("notifyRoomInfo")
 | 
						||
}
 | 
						||
 | 
						||
exports.Client = Client; |