Пример #1
0
        public void Close()
        {
            _socket.Close();

            if (_protocol != null)
            {
                _protocol.Dispose();
                _protocol = null;
            }
            if (_obfs != null)
            {
                _obfs.Dispose();
                _obfs = null;
            }

            lock (_encryptionLock)
            {
                lock (_decryptionLock)
                {
                    if (_encryptor != null)
                    {
                        ((IDisposable)_encryptor).Dispose();
                    }
                }
            }
        }
        public void Close()
        {
            _socket.Close();

            if (_protocol != null)
            {
                _protocol.Dispose();
                _protocol = null;
            }
            if (_obfs != null)
            {
                _obfs.Dispose();
                _obfs = null;
            }

            lock (_encryptionLock)
            {
                lock (_decryptionLock)
                {
                    if (_encryptor != null)
                    {
                        ((IDisposable)_encryptor).Dispose();
                        _encryptor = null;
                    }
                }
            }

            _socket              = null;
            SendEncryptBuffer    = null;
            ReceiveDecryptBuffer = null;
        }
 public override void SetServerInfo(ServerInfo serverInfo)
 {
     if (subObfs == null && serverInfo.param != null && serverInfo.param.Length > 0)
     {
         try
         {
             string[] paramsList = serverInfo.param.Split(",".ToCharArray(), 2);
             string   subParam   = "";
             if (paramsList.Length > 1)
             {
                 subObfs  = ObfsFactory.GetObfs(paramsList[0]);
                 subParam = paramsList[1];
             }
             else
             {
                 subObfs = ObfsFactory.GetObfs(serverInfo.param);
             }
             if (((SubEncodeObfs)serverInfo.data).subObfs == null)
             {
                 ((SubEncodeObfs)serverInfo.data).subObfs = subObfs.InitData();
             }
             subObfs.SetServerInfo(new ServerInfo(serverInfo.host, serverInfo.port, serverInfo.tcp_mss, subParam, ((SubEncodeObfs)serverInfo.data).subObfs));
         }
         catch (Exception)
         {
             // do nothing
         }
         serverInfo.param = null;
     }
     base.SetServerInfo(serverInfo);
 }
Пример #4
0
        public static IObfs GetObfs(string method)
        {
            if (string.IsNullOrEmpty(method))
            {
                method = "plain";
            }
            method = method.ToLowerInvariant();
            Type            t      = _registeredObfs[method];
            ConstructorInfo c      = t.GetConstructor(_constructorTypes);
            IObfs           result = (IObfs)c.Invoke(new object[] { method });

            return(result);
        }
        public override byte[] ClientPreEncrypt(byte[] plaindata, int datalength, out int outlength)
        {
            byte[] outdata  = new byte[datalength + datalength / 10 + 32];
            byte[] packdata = new byte[9000];
            byte[] data     = plaindata;
            outlength = 0;
            const int unit_len = 8100;

            while (datalength > unit_len)
            {
                int outlen;
                PackData(data, unit_len, packdata, out outlen);
                if (outdata.Length < outlength + outlen)
                {
                    Array.Resize(ref outdata, (outlength + outlen) * 2);
                }
                Array.Copy(packdata, 0, outdata, outlength, outlen);
                outlength  += outlen;
                datalength -= unit_len;
                byte[] newdata = new byte[datalength];
                Array.Copy(data, unit_len, newdata, 0, newdata.Length);
                data = newdata;
            }
            if (datalength > 0)
            {
                int outlen;
                PackData(data, datalength, packdata, out outlen);
                if (outdata.Length < outlength + outlen)
                {
                    Array.Resize(ref outdata, (outlength + outlen) * 2);
                }
                Array.Copy(packdata, 0, outdata, outlength, outlen);
                outlength += outlen;
            }
            if (subObfs == null && Server.param != null && Server.param.Length > 0)
            {
                try
                {
                    subObfs = ObfsFactory.GetObfs(Server.param);
                }
                catch (Exception)
                {
                    // do nothing
                }
                Server.param = null;
            }
            return(outdata);
        }
Пример #6
0
 public void SetObfs(IObfs obfs)
 {
     _obfs = obfs;
 }
Пример #7
0
 public void SetProtocol(IObfs protocol)
 {
     _protocol = protocol;
 }
Пример #8
0
 private void Connect()
 {
     lock (server)
     {
         server.ServerSpeedLog().AddConnectTimes();
         if (this.State == ConnectState.HANDSHAKE)
         {
             this.State = ConnectState.CONNECTING;
         }
         server.GetConnections().AddRef(this.connection);
         encryptor = EncryptorFactory.GetEncryptor(server.method, server.password);
         encryptorUDP = EncryptorFactory.GetEncryptor(server.method, server.password);
     }
     this.obfs = ObfsFactory.GetObfs(server.obfs);
     closed = false;
     {
         IPAddress ipAddress;
         string serverURI = server.server;
         int serverPort = server.server_port;
         if (socks5RemotePort > 0)
         {
             serverURI = socks5RemoteHost;
             serverPort = socks5RemotePort;
         }
         bool parsed = IPAddress.TryParse(serverURI, out ipAddress);
         if (!parsed)
         {
             //IPHostEntry ipHostInfo = Dns.GetHostEntry(serverURI);
             //ipAddress = ipHostInfo.AddressList[0];
             if (server.DnsBuffer().isExpired(serverURI))
             {
                 Dns.BeginGetHostEntry(serverURI, new AsyncCallback(DnsCallback), null);
                 return;
             }
             else
             {
                 ipAddress = server.DnsBuffer().ip;
             }
         }
         //else
         BeginConnect(ipAddress, serverPort);
     }
 }
Пример #9
0
        public void ReConnect()
        {
            ResetTimeout(0);

            reconnectTimesRemain--;
            reconnectTimes++;

            lock (server)
            {
                server.ServerSpeedLog().AddDisconnectTimes();
                server.GetConnections().DecRef(this.connection);
            }

            if (reconnectTimes < 2)
            {
                server = this.getCurrentServer(true);
            }
            else
            {
                server = this.getCurrentServer(true, true);
            }

            CloseSocket(ref remote);
            CloseSocket(ref remoteUDP);
            if (remoteTDP != null)
            {
                try
                {
                    remoteTDP.Shutdown();
                    //remoteTDP.Close();
                }
                catch (Exception e)
                {
                    Logging.LogUsefulException(e);
                }
                remoteTDP = null;
            }
            if (obfs != null)
            {
                obfs.Dispose();
                obfs = null;
            }

            connectionShutdown = false;
            remoteShutdown = false;

            speedTester.sizeUpload = 0;
            speedTester.sizeDownload = 0;

            lastErrCode = 0;
            obfs = ObfsFactory.GetObfs(server.obfs);

            try
            {
                Connect();
            }
            catch (Exception e)
            {
                LogSocketException(e);
                if (!Logging.LogSocketException(server.remarks, server.server, e))
                    Logging.LogUsefulException(e);
                this.Close();
            }
        }
Пример #10
0
        public void Close()
        {
            lock (this)
            {
                if (closed)
                {
                    return;
                }
                closed = true;
            }
            try
            {
                if (TryReconnect())
                    return;
                lock (server)
                {
                    if (this.State != ConnectState.END)
                    {
                        this.State = ConnectState.END;
                        server.ServerSpeedLog().AddDisconnectTimes();
                        server.GetConnections().DecRef(this.connection);
                        server.ServerSpeedLog().AddSpeedLog(new TransLog((int)speedTester.GetAvgDownloadSpeed(), DateTime.Now));
                    }
                    getCurrentServer = null;
                    ResetTimeout(0);
                    speedTester = null;
                }
                CloseSocket(ref connection);
                CloseSocket(ref connectionUDP);
                CloseSocket(ref remote);
                CloseSocket(ref remoteUDP);
                if (remoteTDP != null)
                {
                    try
                    {
                        remoteTDP.Shutdown();
                        //remoteTDP.Close();
                    }
                    catch (Exception e)
                    {
                        Logging.LogUsefulException(e);
                    }
                    remoteTDP = null;
                }

                if (obfs != null)
                {
                    obfs.Dispose();
                    obfs = null;
                }
                lock (encryptionLock)
                {
                    lock (decryptionLock)
                    {
                        if (encryptor != null)
                            ((IDisposable)encryptor).Dispose();
                        if (encryptorUDP != null)
                            ((IDisposable)encryptorUDP).Dispose();
                    }
                }
            }
            catch (Exception e)
            {
                Logging.LogUsefulException(e);
            }
        }
Пример #11
0
        public void ReConnect()
        {
            ResetTimeout(0);

            reconnectTimesRemain--;
            reconnectTimes++;

            if (this.State != ConnectState.HANDSHAKE && this.State != ConnectState.READY)
            {
                lock (server)
                {
                    server.ServerSpeedLog().AddDisconnectTimes();
                    server.GetConnections().DecRef(this.connection);
                }
            }

            if (reconnectTimes < 2)
            {
                server = this.getCurrentServer(true);
            }
            else
            {
                server = this.getCurrentServer(true, true);
            }

            CloseSocket(ref remote);
            CloseSocket(ref remoteUDP);
            if (remoteTDP != null)
            {
                try
                {
                    remoteTDP.Shutdown();
                    //remoteTDP.Close();
                }
                catch (Exception e)
                {
                    Logging.LogUsefulException(e);
                }
                //remoteTDP = null;
            }
            if (obfs != null)
            {
                obfs.Dispose();
                obfs = null;
            }
            if (protocol != null)
            {
                protocol.Dispose();
                protocol = null;
            }

            connectionShutdown = false;
            remoteShutdown = false;

            speedTester.sizeUpload = 0;
            speedTester.sizeDownload = 0;

            lastErrCode = 0;
            Thread.Sleep(100);

            try
            {
                Connect();
            }
            catch (Exception e)
            {
                LogSocketException(e);
                if (!Logging.LogSocketException(server.remarks, server.server, e))
                    Logging.LogUsefulException(e);
                this.Close();
            }
        }
Пример #12
0
        public void Close()
        {
            lock (this)
            {
                if (closed)
                {
                    return;
                }
                closed = true;
            }
            if (lastErrCode == 0)
            {
                if (speedTester.sizeRecv == 0 && speedTester.sizeUpload > 0)
                    server.ServerSpeedLog().AddErrorEmptyTimes();
                else
                    server.ServerSpeedLog().AddNoErrorTimes();
            }
            try
            {
                if (TryReconnect())
                    return;
                lock (server)
                {
                    if (this.State != ConnectState.END)
                    {
                        if (this.State != ConnectState.HANDSHAKE && this.State != ConnectState.READY)
                        {
                            server.ServerSpeedLog().AddDisconnectTimes();
                            server.GetConnections().DecRef(this.connection);
                        }
                        server.ServerSpeedLog().AddSpeedLog(new TransLog((int)speedTester.GetAvgDownloadSpeed(), DateTime.Now));
                        this.State = ConnectState.END;
                    }
                    getCurrentServer = null;
                    ResetTimeout(0);
                }
                CloseSocket(ref connection);
                CloseSocket(ref connectionUDP);
                CloseSocket(ref remote);
                CloseSocket(ref remoteUDP);
                if (remoteTDP != null)
                {
                    try
                    {
                        remoteTDP.Shutdown();
                        //remoteTDP.Close();
                    }
                    catch (Exception e)
                    {
                        Logging.LogUsefulException(e);
                    }
                }

                if (obfs != null)
                {
                    obfs.Dispose();
                    obfs = null;
                }
                if (protocol != null)
                {
                    protocol.Dispose();
                    protocol = null;
                }
                lock (encryptionLock)
                {
                    lock (decryptionLock)
                    {
                        if (encryptor != null)
                            ((IDisposable)encryptor).Dispose();
                        if (encryptorUDP != null)
                            ((IDisposable)encryptorUDP).Dispose();
                    }
                }
            }
            catch (Exception e)
            {
                Logging.LogUsefulException(e);
            }
        }
Пример #13
0
 public override void SetServerInfo(ServerInfo serverInfo)
 {
     if (subObfs == null && serverInfo.param != null && serverInfo.param.Length > 0)
     {
         try
         {
             string[] paramsList = serverInfo.param.Split(",".ToCharArray(), 2);
             string subParam = "";
             if (paramsList.Length > 1)
             {
                 subObfs = ObfsFactory.GetObfs(paramsList[0]);
                 subParam = paramsList[1];
             }
             else
             {
                 subObfs = ObfsFactory.GetObfs(serverInfo.param);
             }
             if (((SubEncodeObfs)serverInfo.data).subObfs == null)
                 ((SubEncodeObfs)serverInfo.data).subObfs = subObfs.InitData();
             subObfs.SetServerInfo(new ServerInfo(serverInfo.host, serverInfo.port, serverInfo.tcp_mss, subParam, ((SubEncodeObfs)serverInfo.data).subObfs));
         }
         catch (Exception)
         {
             // do nothing
         }
         serverInfo.param = null;
     }
     base.SetServerInfo(serverInfo);
 }
Пример #14
0
 public override byte[] ClientPreEncrypt(byte[] plaindata, int datalength, out int outlength)
 {
     byte[] outdata = new byte[datalength + datalength / 10 + 32];
     byte[] packdata = new byte[9000];
     byte[] data = plaindata;
     outlength = 0;
     const int unit_len = 8100;
     while (datalength > unit_len)
     {
         int outlen;
         PackData(data, unit_len, packdata, out outlen);
         if (outdata.Length < outlength + outlen)
             Array.Resize(ref outdata, (outlength + outlen) * 2);
         Array.Copy(packdata, 0, outdata, outlength, outlen);
         outlength += outlen;
         datalength -= unit_len;
         byte[] newdata = new byte[datalength];
         Array.Copy(data, unit_len, newdata, 0, newdata.Length);
         data = newdata;
     }
     if (datalength > 0)
     {
         int outlen;
         PackData(data, datalength, packdata, out outlen);
         if (outdata.Length < outlength + outlen)
             Array.Resize(ref outdata, (outlength + outlen) * 2);
         Array.Copy(packdata, 0, outdata, outlength, outlen);
         outlength += outlen;
     }
     if (subObfs == null && Server.param != null && Server.param.Length > 0)
     {
         try
         {
             subObfs = ObfsFactory.GetObfs(Server.param);
         }
         catch(Exception)
         {
             // do nothing
         }
         Server.param = null;
     }
     return outdata;
 }