Ejemplo n.º 1
0
            public void getitem(CommunicatorConnection cc)
            {
                socket.Send(Encoding.UTF8.GetBytes(cc.Encrypt("get " + path, password, false)));

                byte[] data = new byte[ChunkSize];
                string res  = "";

                res = "";
                int size = socket.Receive(data);

                for (int i = 0; i < size; i++)
                {
                    res += Convert.ToChar(data[i]);
                }

                res = cc.Decrypt(res, password, false);

                data = null;


                string type  = res.Substring(0, res.IndexOf(' '));
                string value = res.Substring(res.IndexOf(' ') + 1);

                if (type == "object")
                {
                    if (value.Length > 0)
                    {
                        string[] values = value.Split(',');
                        for (int i = 0; i < values.Length; i++)
                        {
                            addchild(values[i]);
                        }
                    }
                    ready();
                }
                else
                if (type == "string")
                {
                    this.value = value;
                    ready();
                }
                else
                if (type == "boolean")
                {
                    this.value = value;
                    ready();
                }
                else
                if (type == "number")
                {
                    this.value = Convert.ToDouble(value);
                    ready();
                }
                else
                if (type == "buffer")
                {
                    byte[] buffer = new byte[Convert.ToInt32(value)];
                    int    i      = 0;
                    do
                    {
                        if (i * ChunkSize >= buffer.Length)
                        {
                            break;
                        }
                        socket.Send(Encoding.UTF8.GetBytes(cc.Encrypt("getnextbuffchunk", password, false)));

                        data = new byte[ChunkSize * 2];

                        size = socket.Receive(data);



                        res = "";

                        for (int j = 0; j < size; j++)
                        {
                            res += Convert.ToChar(data[j]);
                        }
                        data = null;
                        var bytes = T1.CoreUtils.Utilities.CryptoUtility.DecryptBytes(res, password, cc.iv);
                        Array.Copy(bytes, 0, buffer, i++ *ChunkSize, Math.Min(ChunkSize, bytes.Length));
                    }while (true);
                    this.value = buffer;
                    File.WriteAllBytes("output.png", buffer);
                    buffer = null;
                    ready();
                }
            }
Ejemplo n.º 2
0
        public void Send(dynamic sendobj, string type)
        {
            dynamic obj = new ExpandoObject();

            obj.type = type;
            obj.val  = sendobj;

            new Task(() =>
            {
                TcpClient client = new TcpClient();
                client.Connect(options.receiver, options.port);

                NetworkStream clientStream = client.GetStream();
                string key = RandomString(12);
                CommunicatorConnection Connection = new CommunicatorConnection();
                Connection.iv = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                connections.Add(key, Connection);
                int connectionsindex = connections.Count - 1;
                byte[] senddata      = Encoding.UTF8.GetBytes(Connection.Encrypt("start", options.password, true));
                clientStream.Write(senddata, 0, senddata.Length);
                clientStream.Flush();
                senddata             = null;
                byte[] sendingbuffer = null;
                int sendingbuffer_i  = 0;

                while (true)
                {
                    byte[] inMessage = new byte[ChunkSize];
                    int bytesRead    = 0;
                    try
                    {
                        bytesRead = clientStream.Read(inMessage, 0, ChunkSize);
                    }
                    catch (Exception e) { /*Catch exceptions and handle them here*/ }

                    string res = Connection.Decrypt(Encoding.ASCII.GetString(inMessage, 0, bytesRead), options.password, true);
                    inMessage  = null;
                    if (res == "close")
                    {
                        break;
                    }
                    else
                    if (res == "getnextbuffchunk")
                    {
                        if ((sendingbuffer_i) * ChunkSize > sendingbuffer.Length)
                        {
                            senddata = Encoding.UTF8.GetBytes(Connection.Encrypt("donebuffer", options.password, true));

                            clientStream.Write(senddata, 0, senddata.Length);
                            clientStream.Flush();

                            continue;
                        }
                        senddata = new byte[Math.Min(ChunkSize, sendingbuffer.Length - sendingbuffer_i * ChunkSize)];
                        Array.Copy(sendingbuffer, sendingbuffer_i * ChunkSize, senddata, 0, Math.Min(ChunkSize, sendingbuffer.Length - sendingbuffer_i * ChunkSize));
                        senddata = Encoding.UTF8.GetBytes(T1.CoreUtils.Utilities.CryptoUtility.EncryptBytes(senddata, options.password, Connection.iv));
                        clientStream.Write(senddata, 0, senddata.Length);
                        clientStream.Flush();
                        sendingbuffer_i++;
                        continue;
                    }

                    string reqtype = res.Substring(0, res.IndexOf(' '));
                    string req     = res.Substring(res.IndexOf(' ') + 1);
                    // Console.WriteLine(req);
                    if (reqtype == "get")
                    {
                        dynamic reqobj = obj;
                        string[] paths = req.Split('/');
                        for (int i = 0; i < paths.Length; i++)
                        {
                            if (paths[i] != "")
                            {
                                if (((object)reqobj).GetType().IsArray)
                                {
                                    reqobj = reqobj[Convert.ToInt32(paths[i])];
                                }
                                else
                                if (reqobj is ExpandoObject)
                                {
                                    reqobj = ((IDictionary <string, object>)reqobj)[paths[i]];
                                }
                                else
                                {
                                    reqobj = ((object)reqobj).GetType()
                                             .GetFields(BindingFlags.Public | BindingFlags.NonPublic |
                                                        BindingFlags.Static | BindingFlags.Instance |
                                                        BindingFlags.DeclaredOnly)
                                             .ToList()
                                             .Find(f => f.Name == paths[i]).GetValue(reqobj);
                                }
                            }
                        }
                        if (reqobj is string)
                        {
                            senddata = Encoding.UTF8.GetBytes(Connection.Encrypt("string " + reqobj, options.password, true));

                            clientStream.Write(senddata, 0, senddata.Length);
                            clientStream.Flush();

                            senddata = null;
                        }
                        else
                        if (
                            reqobj is sbyte ||
                            reqobj is byte ||
                            reqobj is short ||
                            reqobj is ushort ||
                            reqobj is int ||
                            reqobj is uint ||
                            reqobj is long ||
                            reqobj is ulong ||
                            reqobj is float ||
                            reqobj is double ||
                            reqobj is decimal)
                        {
                            senddata = Encoding.UTF8.GetBytes(Connection.Encrypt("number " + reqobj.ToString(), options.password, true));

                            clientStream.Write(senddata, 0, senddata.Length);
                            clientStream.Flush();

                            senddata = null;
                        }
                        else
                        if (reqobj is bool)
                        {
                            senddata = Encoding.UTF8.GetBytes(Connection.Encrypt("boolean " + reqobj ? "true" : "false", options.password, true));

                            clientStream.Write(senddata, 0, senddata.Length);
                            clientStream.Flush();

                            senddata = null;
                        }
                        else
                        if (reqobj is byte[])
                        {
                            sendingbuffer = reqobj;

                            sendingbuffer_i = 0;

                            senddata = Encoding.UTF8.GetBytes(Connection.Encrypt("buffer " + (reqobj.Length).ToString(), options.password, true));

                            clientStream.Write(senddata, 0, senddata.Length);
                            clientStream.Flush();
                        }
                        else
                        if (((object)reqobj).GetType().IsArray)
                        {
                            string str = "";
                            for (int i = 0; i < ((Array)reqobj).Length; i++)
                            {
                                str += i.ToString() + ",";
                            }
                            if (str.Length > 0)
                            {
                                str = str.Substring(0, str.Length - 1);
                            }
                            senddata = Encoding.UTF8.GetBytes(Connection.Encrypt("object !," + str, options.password, true));

                            clientStream.Write(senddata, 0, senddata.Length);
                            clientStream.Flush();

                            senddata = null;
                        }
                        else
                        if (reqobj is object)
                        {
                            string[] properties;
                            string str = "";
                            if (!(reqobj is ExpandoObject))
                            {
                                properties = ((object)reqobj).GetType()
                                             .GetFields(BindingFlags.Public | BindingFlags.NonPublic |
                                                        BindingFlags.Static | BindingFlags.Instance |
                                                        BindingFlags.DeclaredOnly)
                                             .ToList()
                                             .Select(f => f.Name).ToArray();
                            }
                            else
                            {
                                properties = ((IDictionary <string, object>)reqobj).Keys.ToArray();
                            }

                            foreach (var p in properties)
                            {
                                str += p + ",";
                            }
                            str      = str.Substring(0, str.Length - 1);
                            senddata = Encoding.UTF8.GetBytes(Connection.Encrypt("object " + str, options.password, true));

                            clientStream.Write(senddata, 0, senddata.Length);
                            clientStream.Flush();

                            senddata = null;
                        }
                    }
                    else
                    {
                    }
                }
                connections.Remove(key);

                client.Close();
            }).Start();
        }
Ejemplo n.º 3
0
        public void listen(int Port)
        {
            listening = true;
            new Task(() =>
            {
                IPAddress ipAddress = IPAddress.Parse("127.0.0.1");

                TcpListener listener = new TcpListener(ipAddress, Port);

                listener.Start();
                while (listening)
                {
                    Socket client = listener.AcceptSocket();

                    var childSocketThread = new Thread(() =>
                    {
                        string key = RandomString(12);
                        CommunicatorConnection Connection = new CommunicatorConnection();
                        Connection.iv = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                        connections.Add(key, Connection);
                        int connectionsindex = connections.Count - 1;

                        CommunicatorItem mainitem = new CommunicatorItem(client, options.password);
                        byte[] data = new byte[8192];
                        string res  = "";

                        res      = "";
                        int size = client.Receive(data);

                        for (int i = 0; i < size; i++)
                        {
                            res += Convert.ToChar(data[i]);
                        }

                        res = Connection.Decrypt(res, options.password, false);

                        //Console.WriteLine(res);
                        CommunicatorItem notreadyitem = FindNotReady(mainitem);
                        do
                        {
                            notreadyitem.getitem(Connection);
                            notreadyitem = FindNotReady(mainitem);
                        }while (notreadyitem != null);
                        data           = null;
                        dynamic result = mainitem.ToObject();
                        for (int i = 0; i < Callbacks.Count; i++)
                        {
                            if (Callbacks.ElementAt(i).Key == ((IDictionary <string, object>)result)["type"].ToString())
                            {
                                Action <object> cb = null;
                                Callbacks.TryGetValue((string)((IDictionary <string, object>)result)["type"], out cb);

                                cb((ExpandoObject)result.val);
                            }
                        }
                        client.Send(Encoding.UTF8.GetBytes(Connection.Encrypt("close", options.password, false)));
                        client.Close();
                        client.Dispose();

                        connections.Remove(key);
                    });
                    childSocketThread.Start();
                }
            }).Start();
        }