【解決方法】暗号化プロキシ サービス

プログラミングQA


こんにちは
私の名前はアリで、イランに住んでいます。ご存じのように、私たちの政府は私たちのインターネットを制限しています。
私たちは検閲を受けており、フェイスブックにさえ行くことができません。
そのため、vpn やプロキシなどを使用する必要があります。
最近、接続できない PPTP および L2TP プロトコルがブロックされました。
(このブロックは、私たちの選挙が行われることです)
したがって、vpn をブロックできる場合は、現在使用している https プロキシもブロックできます。
私はプロキシのようなものを設計しましたが、暗号化 MD5 を使用して接続し、すべてが接続で暗号化されます。
私は何かを設計しましたが、完全に機能していません。誰かが私を助けることができるかどうか疑問に思っていました

それはクライアント側のコードです:

C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;

namespace httpsProxyClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("CLIENT");
            TcpListener listener = new TcpListener(IPAddress.Any, 8889);
            listener.Start();
            while (true)
            {
                Socket socket = listener.AcceptSocket();
                ParameterizedThreadStart pts = new ParameterizedThreadStart(IncommingRequest);
                Thread td = new Thread(pts);
                td.Start(socket);
            }
        }

        const int datalenth = 1048576;

        static void IncommingRequest(object objclientSocket)
        {
            while (true)
            {
                
                    Socket clientSocket = (Socket)objclientSocket;
                    byte[] buff2 = ReciveData(clientSocket);
                    
                    string request = Encoding.ASCII.GetString(buff2);
                    string sUrl = "";
                    bool isHTTPRequest = true;
                    if (request.ToLower().StartsWith("connect"))
                    {
                        sUrl = request.Substring(request.IndexOf(" ") + 1, request.IndexOf(":") - request.IndexOf(" ") - 1);
                        isHTTPRequest = false;
                    }
                   

                    /////////////////////////////////////////////////////////////////////
                    string proxy = "192.168.1.7";//host;
                    int proxyPort = 243;//443;
                    byte[] buffer = new byte[1048576];
                    int bytes;

                    // Connect socket
                    TcpClient client = new TcpClient(proxy, proxyPort);
                    NetworkStream stream = client.GetStream();

                    // Establish Tcp tunnel
                    byte[] tunnelRequest = Encoding.UTF8.GetBytes(request);

                    stream.Write(tunnelRequest, 0, tunnelRequest.Length);
                    stream.Flush();

                    // Read response to CONNECT request
                    // There should be loop that reads multiple packets
                    bytes = stream.Read(buffer, 0, buffer.Length);
                    clientSocket.Send(buffer, 0, bytes, SocketFlags.None);
                    Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));
                    
                    //reading users new request
                    buffer = ReciveData(clientSocket);
                    // Wrap in SSL stream
                    //SslStream sslStream = new SslStream(stream);
                    SslStream sslStream = new SslStream(stream, false, new RemoteCertificateValidationCallback(CertificateValidationCallback));
                    sslStream.AuthenticateAsClient(sUrl);

                    // Send request
                    byte[] brequest = buffer;
                    sslStream.Write(brequest, 0, brequest.Length);
                    sslStream.Flush();
                    byte[] bt = new byte[1048576];
                    int cor = sslStream.Read(bt, 0, bt.Length);
                    int total = cor;
                    List<byte[]> lst = new List<byte[]>();
                    lst.Add(cutArray(bt, cor));
                    //Console.Write(Encoding.UTF8.GetString(bt, 0, cor));
                    // clientSocket.Send(bt, 0, cor, SocketFlags.None);
                    // Read response
                    do
                    {
                        bytes = sslStream.Read(bt, 0, bt.Length);
                        lst.Add(cutArray(bt, bytes));
                        total += bytes;
                        Console.WriteLine(bytes);
                        // Console.Write(Encoding.UTF8.GetString(bt, 0, bytes));
                        //clientSocket.Send(bt, 0, bytes, SocketFlags.None);

                    } while (bytes == bt.Length);

                    byte[] sending = new byte[total];
                    int corsur = 0;
                    foreach (byte[] data in lst)
                    {
                        for (int i = 0; i < data.Length; i++)
                        {
                            sending[corsur] = data[i];
                            corsur++;
                        }
                    }
                    clientSocket.Send(sending, 0, sending.Length, SocketFlags.None);
                    Console.Write("****"+Encoding.UTF8.GetString(sending, 0, sending.Length));
                    /////////////////////////////////////////////////////////////////////

                    //Console.Write(sUrl);
               /* }
                catch (Exception ex)
                { Console.WriteLine(ex.Message); break; }*/
            }
        }

        static bool CertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }

        static byte[] cutArray(byte[] toCut, int count)
        {
            byte[] tmp = new byte[count];
            for (int i = 0; i < count; i++)
            {
                tmp[i] = toCut[i];
            }
            return tmp;
        }

        private static byte[] ReciveData(Socket clientSocket)
        {
            int total = 0;
            byte[] data = new byte[datalenth];
            byte[] buff1 = new byte[0];
            int recint = 0;
            do
            {
                recint = clientSocket.Receive(data);
                total += recint;
                byte[] temp = buff1;
                buff1 = new byte[total];
                int cursor = 0;
                for (; cursor < temp.Length; cursor++)
                {
                    buff1[cursor] = temp[cursor];

                }
                for (int i = 0; i < recint; i++)
                {
                    buff1[i + cursor] = data[i];
                }
            } while (recint == datalenth);
            return buff1;
        }
    }
}

サーバー側は次のとおりです。

C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Security;
using System.Net.Sockets;
using System.Net;
using System.Threading;


namespace httpsProxServer
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.Write("Port to Listen:");
            TcpListener listener = new TcpListener(IPAddress.Any, int.Parse(Console.ReadLine()));
            listener.Start();
            while (true)
            {
                Console.WriteLine("Server Started");
                Console.WriteLine("Waiting for client...");
                TcpClient client = listener.AcceptTcpClient();
                Console.WriteLine("Client connected:" + client.Client.RemoteEndPoint.ToString());
                ParameterizedThreadStart pts = new ParameterizedThreadStart(IncommingClient);
                Thread td = new Thread(pts);
                td.Start(client);
            }
        }

        private static void IncommingClient(object objclient)
        {
            TcpClient client = (TcpClient)objclient;
            byte[] data = new byte[1048576];
            int recData = client.Client.Receive(data);
            data = cutArray(data, recData);
            string strData = Encoding.ASCII.GetString(data);
            Console.WriteLine(strData);
            string host = strData.Substring(strData.IndexOf(" ") + 1, strData.IndexOf(":") - strData.IndexOf(" ") - 1);
            int port = int.Parse(strData.Substring(strData.IndexOf(":") + 1, strData.IndexOf(" ", strData.IndexOf(":")) - strData.IndexOf(":") - 1));

            IPHostEntry hosts = Dns.GetHostEntry(host);
            Socket ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //FIX IT LATER
            int addr = 0;
            connectAgain:
            try
            {
                ServerSocket.Connect(hosts.AddressList[addr], port);
            }
            catch 
            {
                if (addr >= hosts.AddressList.Length)
                {
                    Console.WriteLine("No response from {0} Servers", addr);
                    return;
                }
                addr++;
                goto connectAgain;
            }
            //
            client.Client.Send(Encoding.UTF8.GetBytes("HTTP/1.0 200 Connection established" + Environment.NewLine + "Proxy-agent: ALProxy 0.62" + Environment.NewLine + Environment.NewLine));

            ParameterizedThreadStart pts = new ParameterizedThreadStart(reception);
            Thread thrdReception = new Thread(pts);
            thrdTrasfer tras = new thrdTrasfer(ServerSocket, client);
            thrdReception.Start(tras);

            while (true)
            {
                try
                {
                    byte[] rec = new byte[20480];
                    int i = client.Client.Receive(rec);
                    rec = cutArray(rec, i);
                    ServerSocket.Send(rec);
                    Console.WriteLine("Client----->Server");
                }
                catch (SocketException ex)
                {
                    Console.WriteLine(ex.Message);
                    break;
                }
            }
        }

        static void reception(object objSocket)
        {
            thrdTrasfer serverSocket = (thrdTrasfer)objSocket;
            int ohtimes = 0;
            const int maxtokill = 10;
            while (true)
            {
                try
                {
                    byte[] buff = ReciveData(serverSocket.serverSocket);
                    if (buff.Length == 0)
                    {
                        if (ohtimes >= maxtokill)
                            return;
                        ohtimes++;
                    }
                    serverSocket.Client.Client.Send(buff);
                    Console.WriteLine("Server----->Client:" + buff.Length);
                    
                }
                catch (Exception ec)
                {
                    Console.WriteLine(ec.Message);
                }
            }
        }

        private static byte[] ReciveData(Socket clientSocket)
        {
            int total = 0;
            byte[] data = new byte[1048576];
            byte[] buff1 = new byte[0];
            int recint = 0;
            do
            {
                recint = clientSocket.Receive(data);
                total += recint;
                byte[] temp = buff1;
                buff1 = new byte[total];
                int cursor = 0;
                for (; cursor < temp.Length; cursor++)
                {
                    buff1[cursor] = temp[cursor];

                }
                for (int i = 0; i < recint; i++)
                {
                    buff1[i + cursor] = data[i];
                }
            } while (recint == 1048576);
            return buff1;
        }

        static byte[] cutArray(byte[] toCut, int count)
        {
            byte[] tmp = new byte[count];
            for (int i = 0; i < count; i++)
            {
                tmp[i] = toCut[i];
            }
            return tmp;
        }
    }
    struct thrdTrasfer
    {
        public Socket serverSocket;
        public TcpClient Client;
        public thrdTrasfer(Socket s,TcpClient c)
        {
            serverSocket = s;
            Client = c;
        }
    }
}

これを読んでくれてありがとう
良い1日を
PS: このコードには暗号化はありません。 私は現在暗号化クラスを持っていますが、このコードは機能していません:(

解決策 1

最初に学ぶ必要があるのは、MD5 は暗号化アルゴリズムではないということです。

ハッシュアルゴリズムです。 違いは、暗号化は元に戻すことができますが、ハッシュは元に戻せないことです。
ハッシュは破壊的です。情報を破棄して、データの検証に使用できる小さくて高速なコードを生成しますが、暗号化には使用できません。 代わりに、TripleDES などを使用することを検討してください。MD5 を中心にシステムを設計した場合、これは決して機能しないことに気付くでしょう。 これは実際にはあなたの問題かもしれません…

解決策 2

あなたは質問を完全に読んでいませんでした。
私の友人の暗号化に問題はありません。 暗号化コードは含まれていません いいえ 働く
参考までに、私は MD5 で TripleDES を使用しています

ともあれ、ありがとう

解決策 4

平和アリ
これがあなたの答えです

よい時間を

コメント

タイトルとURLをコピーしました