public void Send(PierceMessage message) { message.SenderId = _id; var makeParcel = message.MakeParcel(); _sender.Send(makeParcel); Stats.AddPacket(PacketType.Sent); Stats.AddBytes(makeParcel.Length, ByteType.Sent); }
/// <summary> /// Sends a message to all connected nodes /// </summary> /// <param name="message">The message to send</param> public void SendMessage(string message) { var pierceMessage = new PierceMessage(PierceHeader.Message); pierceMessage.Message = message; byte[] parcel = pierceMessage.MakeParcel(); _tcpServer.Send(parcel); }
public override byte[] ParseIncomingSSL(int read, byte[] initialBuffer, SslStream stream, ClientContext context) { Stats.AddBytes(read, ByteType.Received); Stats.AddPacket(PacketType.Received); if (read != InitialBufferSize) { Logger.Log("Initial buffer incorrect size", Severity.Warning); return(null); } if (initialBuffer[0] == 0x02 && initialBuffer[1] == 0x07) { short int16 = BitConverter.ToInt16(initialBuffer, 2); if (Enum.TryParse(int16.ToString(), out PierceHeader header)) { int len = BitConverter.ToInt32(initialBuffer, 8); //Logger.Log(header.ToString(), Severity.Info); //Logger.Log("Length: " + len, Severity.Info); var list = new List <byte>(); list.AddRange(initialBuffer); var payload = new byte[len]; //int readback = stream.Read(payload, 0, len); int bytesread = 0; do { int readback = stream.Read(payload, bytesread, len - bytesread); Stats.AddBytes(readback, ByteType.Received); bytesread += readback; } while (bytesread < len); list.AddRange(payload); if (bytesread != len) { Logger.Log("Full message not read or too much read!", Severity.Warning); return(null); } var endbit = new byte[4]; var endread = stream.Read(endbit, 0, 4); Stats.AddBytes(endread, ByteType.Received); if (endread != 4) { Logger.Log("End bit is wrong length!", Severity.Warning); return(null); } list.AddRange(endbit); PierceMessage message = PierceMessage.Parse(list.ToArray()); //Logger.Log("Message: " + message, Severity.Info); //Logger.Log(BitConverter.ToString(payload), Severity.Info); switch (message.Header) { case PierceHeader.Invalid: Logger.Log("Invalid header received (0x00)", Severity.Warning); return(null); case PierceHeader.Handshake: Logger.Log("Handshake received, version " + BitConverter.ToString(payload), Severity.Info); uint id = (uint)new Random().Next(); while (RemoteClientList.ContainsKey(id)) { id = (uint)new Random().Next(); } if (!RemoteClientList.TryAdd(id, new RemoteClientInfo(id, context))) { throw new Exception("Conflicting key on creation, this is impossible"); } var pierceMessage = new PierceMessage(PierceHeader.HandshakeOK); pierceMessage.Payload = BitConverter.GetBytes(id); byte[] bytes = pierceMessage.MakeParcel(); stream.Write(bytes); // return back to server for stat collection return(list.ToArray()); case PierceHeader.HandshakeOK: break; case PierceHeader.Message: break; case PierceHeader.ScreenShot: break; case PierceHeader.RemoteDeskRequest: OnRemoteDeskRequestReceived(null); break; case PierceHeader.Socks5: var sockParcel = SockParcel.DeSerialize(message.Payload); if (RemoteClientList.ContainsKey(message.SenderId)) { OnSockParcelReceived(RemoteClientList[message.SenderId], sockParcel); } else { Logger.Log(message.SenderId + " is an unrecognized Client ID", Severity.Warning); } break; case PierceHeader.RoundTrip: if (RemoteClientList.ContainsKey(message.SenderId)) { OnRoundTripReceived(RemoteClientList[message.SenderId], message.Payload); } else { Logger.Log(message.SenderId + " is an unrecognized Client ID", Severity.Warning); } break; default: throw new ArgumentOutOfRangeException(); } } else { Logger.Log("Invalid header received", Severity.Warning); return(null); } } else { Logger.Log("Invalid magic bytes received", Severity.Warning); return(null); } return(initialBuffer); }
private void ConnectCallback(IAsyncResult ar) { try { _client.EndConnect(ar); } catch (SocketException e) { Logger.Log(e); Reconnect(); return; } SslStream ssl = new SslStream(_client.GetStream(), false, ValidateServerCertificate, null); if (_sender == null) { _sender = new ConcurrentSender(ssl); _sender.StatusUpdate += (sender, s) => OnSenderStatusUpdate(s); } else { _sender.SetStream(ssl); if (_sender.Paused) { _sender.UnPause(); } } //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; var x509Certificate2Collection = new X509Certificate2Collection(Cert); try { ssl.AuthenticateAsClient(Ip, x509Certificate2Collection, SslProtocols.Tls12, false); } catch (Exception e) { Logger.Log(e); Reconnect(); return; } Logger.Log("Connected to " + Ip + ":" + Port, Severity.Info); var pierceMessage = new PierceMessage(PierceHeader.Handshake); byte[] parcel = pierceMessage.MakeParcel(); var tuple = new Tuple <byte[], SslStream>(parcel, ssl); try { ssl.BeginWrite(parcel, 0, parcel.Length, WriteCallBack, tuple); } catch (Exception e) { Logger.Log(e); Reconnect(); return; } }
public void Send(PierceMessage message, RemoteClientInfo client) { _tcpServer.Send(message.MakeParcel(), client.Context); }