Ejemplo n.º 1
0
        public override void Stop()
        {
            lock (_stateLock)
            {
                lock (this.ThisLock)
                {
                    if (this.State == ManagerState.Stop)
                    {
                        return;
                    }
                    _state = ManagerState.Stop;
                }

                _watchThread.Join();
                _watchThread = null;

                lock (_samClientLock)
                {
                    if (_samSession != null)
                    {
                        _samSession.Dispose();
                    }

                    _samSession = null;
                }

                lock (_samServerLock)
                {
                    if (_samListener != null)
                    {
                        _samListener.Dispose();
                    }

                    _samListener = null;

                    _oldSamBridgeUri = null;
                }

                lock (this.ThisLock)
                {
                    if (_settings.I2pUri != null)
                    {
                        if (this.RemoveUri(_settings.I2pUri))
                        {
                            Log.Information(string.Format("Remove Node uri: {0}", _settings.I2pUri));
                        }
                    }
                    _settings.I2pUri = null;
                }
            }
        }
Ejemplo n.º 2
0
        private CapBase CreateCap(object sender, string uri)
        {
            if (_disposed)
            {
                return(null);
            }
            if (this.State == ManagerState.Stop)
            {
                return(null);
            }

            if (!uri.StartsWith("i2p:"))
            {
                return(null);
            }

            List <IDisposable> garbages = new List <IDisposable>();

            try
            {
                string scheme = null;
                string host   = null;

                {
                    Regex regex = new Regex(@"(.*?):(.*)");
                    var   match = regex.Match(uri);

                    if (match.Success)
                    {
                        scheme = match.Groups[1].Value;
                        host   = match.Groups[2].Value;
                    }
                }

                if (host == null)
                {
                    return(null);
                }

                {
                    string proxyScheme = null;
                    string proxyHost   = null;
                    int    proxyPort   = -1;

                    {
                        Regex regex = new Regex(@"(.*?):(.*):(\d*)");
                        var   match = regex.Match(this.SamBridgeUri);

                        if (match.Success)
                        {
                            proxyScheme = match.Groups[1].Value;
                            proxyHost   = match.Groups[2].Value;
                            proxyPort   = int.Parse(match.Groups[3].Value);
                        }
                    }

                    if (proxyHost == null)
                    {
                        return(null);
                    }

                    if (scheme == "i2p")
                    {
                        SamV3Connector connector = null;

                        try
                        {
                            SamV3Session session;

                            lock (_samClientLock)
                            {
                                session = _samSession;

                                if (session == null)
                                {
                                    Socket proxySocket = null;

                                    try
                                    {
                                        proxySocket = OverlayNetworkManager.Connect(new IPEndPoint(OverlayNetworkManager.GetIpAddress(proxyHost), proxyPort), new TimeSpan(0, 0, 10));

                                        string   caption = "Amoeba_Client";
                                        string[] options = new string[]
                                        {
                                            "inbound.nickname=" + caption,
                                            "outbound.nickname=" + caption
                                        };
                                        string optionsString = string.Join(" ", options);

                                        session = new SamV3Session(proxyHost, proxyPort, null, proxySocket);

                                        session.Handshake();
                                        session.Create(null, optionsString);

                                        _samSession = session;
                                    }
                                    catch (Exception)
                                    {
                                        if (session != null)
                                        {
                                            session.Dispose();
                                        }
                                        if (proxySocket != null)
                                        {
                                            proxySocket.Dispose();
                                        }

                                        throw;
                                    }
                                }
                            }

                            connector = new SamV3Connector(session);
                            connector.Handshake();
                            connector.Connect(host);
                        }
                        catch (SamBridgeErrorException ex)
                        {
                            bool isLog = true;
                            if (connector != null)
                            {
                                connector.Dispose();
                            }

                            string result = ex.Reply.UniqueValue("RESULT", string.Empty).Value;

                            if (result == SamBridgeErrorMessage.INVALID_ID)
                            {
                                lock (_samClientLock)
                                {
                                    if (_samSession != null)
                                    {
                                        _samSession.Dispose();
                                        _samSession = null;
                                    }
                                }

                                isLog = false;
                            }
                            else if (result == SamBridgeErrorMessage.CANT_REACH_PEER ||
                                     result == SamBridgeErrorMessage.PEER_NOT_FOUND)
                            {
                                isLog = false;
                            }

                            if (isLog)
                            {
                                Debug.WriteLine(ex);
                            }

                            throw;
                        }
                        catch (SamException ex)
                        {
                            Debug.WriteLine(ex);
                            if (connector != null)
                            {
                                connector.Dispose();
                            }

                            throw;
                        }

                        return(new SocketCap(connector.BridgeSocket));
                    }
                }
            }
            catch (Exception)
            {
                foreach (var item in garbages)
                {
                    item.Dispose();
                }
            }

            return(null);
        }