Beispiel #1
0
        /// <summary>
        /// 与ss服务器连接后的回调方法
        /// </summary>
        /// <param name="ar"></param>
        private void ConnectCallback(IAsyncResult ar)
        {
            Server server = null;

            if (closed)
            {
                return;
            }
            try
            {
                // 关闭用于检查连接状态的定时器
                ServerTimer timer = (ServerTimer)ar.AsyncState;
                server         = timer.Server;
                timer.Elapsed -= connectTimer_Elapsed;
                timer.Enabled  = false;
                timer.Dispose();

                // Complete the connection.
                remote.EndConnect(ar);

                connected = true;

                //Console.WriteLine("Socket connected to {0}",
                //    remote.RemoteEndPoint.ToString());

                var       latency  = DateTime.Now - _startConnectTime;
                IStrategy strategy = controller.GetCurrentStrategy();
                if (strategy != null)
                {
                    strategy.UpdateLatency(server, latency);
                }

                // 启动ss隧道
                StartPipe();
            }
            catch (ArgumentException)
            {
            }
            catch (Exception e)
            {
                if (server != null)
                {
                    IStrategy strategy = controller.GetCurrentStrategy();
                    if (strategy != null)
                    {
                        strategy.SetFailure(server);
                    }
                }
                Logging.LogUsefulException(e);
                RetryConnect();
            }
        }
        private void ConnectCallback(IAsyncResult ar)
        {
            if (_closed)
            {
                return;
            }
            try
            {
                var         session = (AsyncSession <ServerTimer>)ar.AsyncState;
                ServerTimer timer   = session.State;
                _server        = timer.Server;
                timer.Elapsed -= DestConnectTimer_Elapsed;
                timer.Enabled  = false;
                timer.Dispose();

                var remote = session.Remote;
                // Complete the connection.
                remote.EndConnectDest(ar);

                _destConnected = true;

                if (_config.isVerboseLogging)
                {
                    Logging.Info($"Socket connected to ss server: {_server.FriendlyName()}");
                }

                var       latency  = DateTime.Now - _startConnectTime;
                IStrategy strategy = _controller.GetCurrentStrategy();
                strategy?.UpdateLatency(_server, latency);
                _tcprelay.UpdateLatency(_server, latency);

                StartPipe(session);
            }
            catch (ArgumentException)
            {
            }
            catch (Exception e)
            {
                if (_server != null)
                {
                    IStrategy strategy = _controller.GetCurrentStrategy();
                    strategy?.SetFailure(_server);
                }
                Logging.LogUsefulException(e);
                Close();
            }
        }
        private void ConnectCallback(IAsyncResult ar)
        {
            Server server = null;

            if (closed)
            {
                return;
            }
            try
            {
                ServerTimer timer = (ServerTimer)ar.AsyncState;
                server         = timer.Server;
                timer.Elapsed -= connectTimer_Elapsed;
                timer.Enabled  = false;
                timer.Dispose();

                // Complete the connection.
                remote.EndConnect(ar);

                connected = true;

                Logging.Debug($"Socket connected to {remote.RemoteEndPoint}");

                var       latency  = DateTime.Now - _startConnectTime;
                IStrategy strategy = controller.GetCurrentStrategy();
                strategy?.UpdateLatency(server, latency);
                tcprelay.UpdateLatency(server, latency);

                StartPipe();
            }
            catch (ArgumentException)
            {
            }
            catch (Exception e)
            {
                if (server != null)
                {
                    IStrategy strategy = controller.GetCurrentStrategy();
                    if (strategy != null)
                    {
                        strategy.SetFailure(server);
                    }
                }
                Logging.LogUsefulException(e);
                RetryConnect();
            }
        }
        private void ConnectCallback(object sender, SocketAsyncEventArgs e)
        {
            using (e)
            {
                if (_closed)
                {
                    return;
                }
                try
                {
                    if (e.SocketError != SocketError.Success)
                    {
                        Logging.LogUsefulException(new Exception(e.SocketError.ToString()));
                        Close();
                        return;
                    }

                    if (e.BytesTransferred <= 0)
                    {
                        // close
                        Close();
                        return;
                    }

                    ServerTimer timer = (ServerTimer)e.UserToken;
                    timer.Enabled  = false;
                    timer.Elapsed -= DestConnectTimer_Elapsed;
                    timer.Dispose();

                    if (_config.isVerboseLogging)
                    {
                        Logging.Info($"Socket connected to ss server: {_server.FriendlyName()}");
                    }

                    _destConnected = true;

                    StartPipe();
                }
                catch (Exception ex)
                {
                    Logging.LogUsefulException(ex);
                    Close();
                }
            }
        }
Beispiel #5
0
        private void ConnectCallback(IAsyncResult ar)
        {
            if (_closed)
            {
                return;
            }

            try
            {
                AsyncSession <ServerTimer> session = (AsyncSession <ServerTimer>)ar.AsyncState;
                ServerTimer timer = session.State;
                _server        = timer.Server;
                timer.Elapsed -= DestConnectTimer_Elapsed;
                timer.Enabled  = false;
                timer.Dispose();

                IProxy remote = session.Remote;
                // Complete the connection.
                remote.EndConnectDest(ar);

                _destConnected = true;

                Logger.Debug($"Socket connected to ss server: {_server.ToString()}");

                TimeSpan latency = DateTime.Now - _startConnectTime;

                OnConnected?.Invoke(this, new SSTCPConnectedEventArgs(_server, latency));

                StartPipe(session);
            }
            catch (ArgumentException)
            {
            }
            catch (Exception e)
            {
                if (_server != null)
                {
                    OnFailed?.Invoke(this, new SSRelayEventArgs(_server));
                }
                ErrorClose(e);
            }
        }
Beispiel #6
0
        private void DestConnectTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            ServerTimer timer = (ServerTimer)sender;

            timer.Elapsed -= DestConnectTimer_Elapsed;
            timer.Enabled  = false;
            timer.Dispose();

            if (_destConnected || _closed)
            {
                return;
            }

            AsyncSession session = timer.Session;
            Server       server  = timer.Server;

            OnFailed?.Invoke(this, new SSRelayEventArgs(_server));
            Logger.Info($"{server.ToString()} timed out");
            session.Remote.Close();
            Close();
        }