コード例 #1
0
ファイル: Local.cs プロジェクト: yaoliyc/shadowsocks-csharp
        private void BeginConnect(IPAddress ipAddress, int serverPort)
        {
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, serverPort);
            remoteUDPEndPoint = remoteEP;

            if (server.tcp_over_udp && connectionUDP == null)
            {
                remoteTDP = new TDPHandler();
            }
            if (socks5RemotePort != 0
                || connectionUDP == null && !server.tcp_over_udp
                || connectionUDP != null && server.udp_over_tcp)
            {
                remote = new Socket(ipAddress.AddressFamily,
                    SocketType.Stream, ProtocolType.Tcp);
                remote.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
            }

            if (connectionUDP != null && !server.udp_over_tcp)
            {
                try
                {
                    remoteUDP = new Socket(ipAddress.AddressFamily,
                        SocketType.Dgram, ProtocolType.Udp);
                    remoteUDP.Bind(new IPEndPoint(ipAddress.AddressFamily == AddressFamily.InterNetworkV6 ? IPAddress.IPv6Any : IPAddress.Any, 0));
                }
                catch (SocketException)
                {
                    remoteUDP = null;
                }
            }

            if (remoteTDP != null && server.tcp_over_udp && socks5RemotePort == 0)
            {
                speedTester.BeginConnect();
                remoteTDP.BeginConnect(server.method, server.password, remoteEP, "", 0,
                    new AsyncCallback(ConnectCallback), null);
            }
            else
            {
                // Connect to the remote endpoint.
                if (socks5RemotePort == 0 && connectionUDP != null && !server.udp_over_tcp)
                {
                    ConnectState _state = this.State;
                    if (_state == ConnectState.CONNECTING)
                    {
                        this.State = ConnectState.CONNECTED;
                        StartPipe();
                    }
                    else if (_state == ConnectState.CONNECTED)
                    {
                        //ERROR
                    }
                }
                else
                {
                    speedTester.BeginConnect();
                    remote.BeginConnect(remoteEP,
                        new AsyncCallback(ConnectCallback), null);
                }
            }
        }
コード例 #2
0
ファイル: Local.cs プロジェクト: yaoliyc/shadowsocks-csharp
        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();
            }
        }
コード例 #3
0
ファイル: Local.cs プロジェクト: GreyC/shadowsocks-csharp
        private void BeginConnect(IPAddress ipAddress, int serverPort)
        {
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, serverPort);
            remoteTCPEndPoint = remoteEP;
            remoteUDPEndPoint = remoteEP;

            if (server.tcp_over_udp && connectionUDP == null)
            {
                remoteTDP = new TDPHandler();
            }
            if (socks5RemotePort != 0
                || connectionUDP == null && !server.tcp_over_udp
                || connectionUDP != null && server.udp_over_tcp)
            {
                remote = new Socket(ipAddress.AddressFamily,
                    SocketType.Stream, ProtocolType.Tcp);
                remote.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
            }

            if (connectionUDP != null && !server.udp_over_tcp)
            {
                try
                {
                    remoteUDP = new Socket(ipAddress.AddressFamily,
                        SocketType.Dgram, ProtocolType.Udp);
                    remoteUDP.Bind(new IPEndPoint(ipAddress.AddressFamily == AddressFamily.InterNetworkV6 ? IPAddress.IPv6Any : IPAddress.Any, 0));
                }
                catch (SocketException)
                {
                    remoteUDP = null;
                }
            }

            if (remoteTDP != null && server.tcp_over_udp && socks5RemotePort == 0)
            {
                speedTester.BeginConnect();
                remoteTDP.BeginConnect(server.method, server.password, remoteEP, "", 0,
                    new AsyncCallback(ConnectCallback), null);
            }
            else
            {
                // Connect to the remote endpoint.
                if (socks5RemotePort == 0 && connectionUDP != null && !server.udp_over_tcp)
                {
                    ConnectState _state = this.State;
                    if (_state == ConnectState.CONNECTING)
                    {
                        StartPipe();
                    }
                    else if (_state == ConnectState.CONNECTED)
                    {
                        //ERROR
                    }
                }
                else
                {
                    speedTester.BeginConnect();
                    IAsyncResult result = remote.BeginConnect(remoteEP,
                        new AsyncCallback(ConnectCallback), null);
                    if (reconnectTimesRemain + reconnectTimes > 0)
                    {
                        bool success = result.AsyncWaitHandle.WaitOne((int)(5 * 1000), true);
                        if (!success)
                        {
                            remote.Close();
                            lastErrCode = 8;
                            server.ServerSpeedLog().AddTimeoutTimes();
                        }
                    }
                }
            }
        }
コード例 #4
0
ファイル: Local.cs プロジェクト: yaoliyc/shadowsocks-csharp
        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);
            }
        }
コード例 #5
0
ファイル: Local.cs プロジェクト: GreyC/shadowsocks-csharp
 private void Connect()
 {
     remote = null;
     remoteUDP = null;
     remoteTDP = null;
     closed = false;
     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.protocol = ObfsFactory.GetObfs(server.protocol);
     this.obfs = ObfsFactory.GetObfs(server.obfs);
     {
         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;
             }
         }
         BeginConnect(ipAddress, serverPort);
     }
 }