示例#1
0
        private void ConnectAsync(Action callback, Action <TcpTransportResult> faultCallback)
        {
            WRITE_LOG(string.Format("Socket.ConnectAsync[#3] {0} ({1}:{2})", Id, Host, Port));

            TLSocks5Proxy socks5Proxy = ProxyConfig != null && ProxyConfig.IsEnabled.Value && !ProxyConfig.IsEmpty
                ? ProxyConfig.GetProxy() as TLSocks5Proxy
                : null;

            if (socks5Proxy != null)
            {
                try
                {
                    ActualHost = StaticHost;
                    ActualPort = StaticPort;

                    RaiseConnectingAsync();

                    SocksProxy.ConnectToSocks5Proxy(_socket, socks5Proxy.Server.ToString(), (ushort)socks5Proxy.Port.Value, StaticHost, (ushort)StaticPort, socks5Proxy.Username.ToString(), socks5Proxy.Password.ToString());

                    OnConnected(new SocketAsyncEventArgs {
                        SocketError = SocketError.Success
                    }, callback, faultCallback);
                }
                catch (Exception ex)
                {
                    faultCallback.SafeInvoke(new TcpTransportResult(SocketAsyncOperation.Connect, ex));

                    WRITE_LOG("Socket.ConnectAsync[#3]", ex);
                }
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("  Connecting mtproto=[server={0} port={1}]", Host, Port);

                var args = new SocketAsyncEventArgs
                {
                    RemoteEndPoint = new IPEndPoint(_address, Port)
                };

                args.Completed += (o, e) => OnConnected(e, callback, faultCallback);

                try
                {
                    ActualHost = Host;
                    ActualPort = Port;

                    RaiseConnectingAsync();
                    _socket.ConnectAsync(args);
                }
                catch (Exception ex)
                {
                    faultCallback.SafeInvoke(new TcpTransportResult(SocketAsyncOperation.Connect, ex));

                    WRITE_LOG("Socket.ConnectAsync[#3]", ex);
                }
            }
        }
示例#2
0
        public void Done()
        {
            var proxyConfig = _transportService.GetProxyConfig() as TLProxyConfig76 ?? TLProxyConfigBase.Empty as TLProxyConfig76;

            if (proxyConfig == null)
            {
                return;
            }
            if (!IsDoneEnabled)
            {
                return;
            }

            if (IsSocks5Proxy)
            {
                TLSocks5Proxy proxy = null;
                // new proxy
                if (_proxy == null)
                {
                    proxy = new TLSocks5Proxy {
                        CustomFlags = new TLLong(0)
                    };
                    proxy.Server = new TLString(Server);
                    var port = 0;
                    proxy.Port     = int.TryParse(Port, out port) ? new TLInt(port) : new TLInt(0);
                    proxy.Username = new TLString(Username);
                    proxy.Password = new TLString(Password);

                    _proxy = proxy;
                    proxyConfig.Items.Add(_proxy);
                    proxyConfig.SelectedIndex = new TLInt(proxyConfig.Items.IndexOf(_proxy));
                    proxyConfig.IsEnabled     = TLBool.True;

                    _transportService.SetProxyConfig(proxyConfig);
                    _transportService.Close();
                    MTProtoService.PingAsync(TLLong.Random(), result => { });

                    EventAggregator.Publish(new ProxyChangedEventArgs(proxyConfig));
                    NavigationService.GoBack();

                    return;
                }

                proxy = _proxy as TLSocks5Proxy;
                // change type
                if (proxy == null)
                {
                    proxy = new TLSocks5Proxy {
                        CustomFlags = new TLLong(0)
                    };
                    proxy.Server = new TLString(Server);
                    var port = 0;
                    proxy.Port     = int.TryParse(Port, out port) ? new TLInt(port) : new TLInt(0);
                    proxy.Username = new TLString(Username);
                    proxy.Password = new TLString(Password);

                    _proxy = proxy;
                    proxyConfig.Items.Add(_proxy);
                    proxyConfig.SelectedIndex = new TLInt(proxyConfig.Items.IndexOf(_proxy));

                    _transportService.SetProxyConfig(proxyConfig);
                    _transportService.Close();
                    MTProtoService.PingAsync(TLLong.Random(), result => { });

                    EventAggregator.Publish(new ProxyChangedEventArgs(proxyConfig));
                    NavigationService.GoBack();

                    return;
                }
                // same type
                else
                {
                    proxy.Server = new TLString(Server);
                    var port = 0;
                    proxy.Port     = int.TryParse(Port, out port) ? new TLInt(port) : new TLInt(0);
                    proxy.Username = new TLString(Username);
                    proxy.Password = new TLString(Password);

                    _proxy = proxy;
                    if (proxyConfig.SelectedIndex.Value == proxyConfig.Items.IndexOf(_proxy))
                    {
                        _transportService.SetProxyConfig(proxyConfig);
                        _transportService.Close();
                        MTProtoService.PingAsync(TLLong.Random(), result => { });
                    }

                    EventAggregator.Publish(new ProxyChangedEventArgs(proxyConfig));
                    NavigationService.GoBack();

                    return;
                }
            }
            else
            {
                TLMTProtoProxy proxy = null;
                // new proxy
                if (_proxy == null)
                {
                    proxy = new TLMTProtoProxy {
                        CustomFlags = new TLLong(0)
                    };
                    proxy.Server = new TLString(Server);
                    var port = 0;
                    proxy.Port   = int.TryParse(Port, out port) ? new TLInt(port) : new TLInt(0);
                    proxy.Secret = new TLString(Secret);

                    _proxy = proxy;
                    proxyConfig.Items.Add(_proxy);
                    proxyConfig.SelectedIndex = new TLInt(proxyConfig.Items.IndexOf(_proxy));
                    proxyConfig.IsEnabled     = TLBool.True;

                    _transportService.SetProxyConfig(proxyConfig);
                    _transportService.Close();
                    MTProtoService.PingAsync(TLLong.Random(), result => { });

                    EventAggregator.Publish(new ProxyChangedEventArgs(proxyConfig));
                    NavigationService.GoBack();

                    return;
                }

                proxy = _proxy as TLMTProtoProxy;
                // change type
                if (proxy == null)
                {
                    proxy = new TLMTProtoProxy {
                        CustomFlags = new TLLong(0)
                    };
                    proxy.Server = new TLString(Server);
                    var port = 0;
                    proxy.Port   = int.TryParse(Port, out port) ? new TLInt(port) : new TLInt(0);
                    proxy.Secret = new TLString(Secret);

                    _proxy = proxy;
                    proxyConfig.Items.Add(_proxy);
                    proxyConfig.SelectedIndex = new TLInt(proxyConfig.Items.IndexOf(_proxy));

                    _transportService.SetProxyConfig(proxyConfig);
                    _transportService.Close();
                    MTProtoService.PingAsync(TLLong.Random(), result => { });

                    EventAggregator.Publish(new ProxyChangedEventArgs(proxyConfig));
                    NavigationService.GoBack();

                    return;
                }
                // same type
                else
                {
                    proxy.Server = new TLString(Server);
                    var port = 0;
                    proxy.Port   = int.TryParse(Port, out port) ? new TLInt(port) : new TLInt(0);
                    proxy.Secret = new TLString(Secret);

                    _proxy = proxy;
                    if (proxyConfig.SelectedIndex.Value == proxyConfig.Items.IndexOf(_proxy))
                    {
                        _transportService.SetProxyConfig(proxyConfig);
                        _transportService.Close();
                        MTProtoService.PingAsync(TLLong.Random(), result => { });
                    }

                    EventAggregator.Publish(new ProxyChangedEventArgs(proxyConfig));
                    NavigationService.GoBack();

                    return;
                }
            }
        }
        private async Task <bool> ConnectAsync(double timeout, Action <TcpTransportResult> faultCallback)
        {
            TLSocks5Proxy socks5Proxy = ProxyConfig != null && ProxyConfig.IsEnabled.Value && !ProxyConfig.IsEmpty
                ? ProxyConfig.GetProxy() as TLSocks5Proxy
                : null;

            if (socks5Proxy != null)
            {
                try
                {
                    ActualHost = StaticHost;
                    ActualPort = StaticPort;

                    RaiseConnectingAsync();

                    await SocksProxy.ConnectToSocks5Proxy(timeout, _socket, _dataWriter, _dataReader, socks5Proxy.Server.ToString(), (ushort)socks5Proxy.Port.Value, StaticHost, (ushort)StaticPort, socks5Proxy.Username.ToString(), socks5Proxy.Password.ToString());

                    lock (_dataWriterSyncRoot)
                    {
                        var buffer = GetInitBufferInternal();
                        _dataWriter.WriteBytes(buffer);

                        var result = _dataWriter.StoreAsync().AsTask().Result;
                    }

                    lock (_isConnectedSyncRoot)
                    {
                        _isConnecting = false;
                        _isConnected  = true;
                    }
                }
                catch (Exception ex)
                {
                    var status = SocketError.GetStatus(ex.HResult);
                    WRITE_LOG("TCPTransportWinRT.ConnectAsync " + status, ex);

                    var error = SocketError.GetStatus(ex.HResult);
                    faultCallback.SafeInvoke(new TcpTransportResult(ex));
                    return(false);
                }
            }
            else
            {
                try
                {
                    ActualHost = Host;
                    ActualPort = Port;

                    RaiseConnectingAsync();

                    //var address = IPAddress.IsValidIPv6(Host);
                    await _socket.ConnectAsync(new HostName(Host), Port.ToString(CultureInfo.InvariantCulture)).WithTimeout(timeout);

                    lock (_dataWriterSyncRoot)
                    {
                        var buffer = GetInitBufferInternal();
                        _dataWriter.WriteBytes(buffer);
                    }

                    lock (_isConnectedSyncRoot)
                    {
                        _isConnecting = false;
                        _isConnected  = true;
                    }
                }
                catch (Exception ex)
                {
                    var status = SocketError.GetStatus(ex.HResult);
                    WRITE_LOG("TCPTransportWinRT.ConnectAsync " + status, ex);

                    var error = SocketError.GetStatus(ex.HResult);
                    faultCallback.SafeInvoke(new TcpTransportResult(ex));
                    return(false);
                }
            }

            return(true);
        }