public override string Invoke()
        {
            _tcpClientActor.Action(TelnetAction.MoveLeft);

            _tcpClientActor.WriteLine("Enter data for the new rule.");
            var publicHostRequested = RequestEntry("Public Host URL (empty for 'Any'): ");

            var publicPortRequestedEntry = RequestEntry("Public Port: ");
            int publicPortRequested;
            if (!int.TryParse(publicPortRequestedEntry, out publicPortRequested))
                return string.Format("Command aborted. Cannot parse {0} as an integer.", publicPortRequestedEntry);

            var internalTargetAddressEntry = RequestEntry("Internal Target IP: ");
            IPAddress internalTargetAddress;
            if (!IPAddress.TryParse(internalTargetAddressEntry, out internalTargetAddress))
                return string.Format("Command aborted. Cannot parse {0} as a IP Address", internalTargetAddressEntry);

            var internalTargetPortEntry = RequestEntry("Internal Target Port: ");
            int internalTargetPort;
            if (!int.TryParse(internalTargetPortEntry, out internalTargetPort))
                return string.Format("Command aborted. Cannot parse {0} as an integer.", internalTargetPort);

            var redirectRule = new RedirectRule(IPAddress.Any, publicHostRequested, publicPortRequested, internalTargetAddress, internalTargetPort);

            _tcpClientActor.WriteLine(string.Format("The new rule will be {0}:{1} sent to {2}:{3}.", redirectRule.PublicRequestHost, redirectRule.PublicRequestPort, redirectRule.InternalTargetAddress, redirectRule.InternalTargetPort));

            string responseMessage;
            if (ConfirmAction(string.Format("The change will take effect immediately! Are you sure that the rule is to be added?"), out responseMessage))
            {
                RedirectRuleManager.Instance.Add(redirectRule, true);

                responseMessage = string.Format("New rule was added.");
            }
            return responseMessage;
        }
        private PortListener(RedirectRule redirectRule)
        {
            TcpActor.TcpActorStoppedEvent += this.TcpActor_TcpActorStoppedEvent;

            _listenerTask = new Task(ListenForClients);
            _redirectRules = new List<RedirectRule>();
            Add(redirectRule);
            _tcpListener = TcpListenerProvider.Create();
        }
        public void Add(RedirectRule redirectRule, bool save)
        {
            //Check so that incoming rule is not duplicated in the redirect rule
            if ( this._redirectRules.Any(x => x.PublicRequestHost == redirectRule.PublicRequestHost && x.PublicRequestPort == redirectRule.PublicRequestPort) )
                throw new InvalidOperationException(string.Format("There is already a redirect rule for host {0} and port {1}.", string.IsNullOrEmpty(redirectRule.PublicRequestHost) ? "'Any'" : redirectRule.PublicRequestHost, redirectRule.PublicRequestPort));

            _redirectRules.Add(redirectRule);
            PerformaceCounters.Instance.RedirectRuleCounterIncrement();
            OnRedirectRuleAddedEvent(new RedirectRuleAddedEventArgs(redirectRule));

            if (save)
                Save();
        }
        public bool Remove(RedirectRule redirectRule)
        {
            if (!_redirectRules.Remove(redirectRule))
            {
                return(false);
            }

            PerformaceCounters.Instance.RedirectRuleCounterDecrement();
            OnRedirectRuleRemovedEvent(new RedirectRuleRemovedEventArgs(redirectRule));

            Save();

            return(true);
        }
        public void Add(RedirectRule redirectRule, bool save)
        {
            //Check so that incoming rule is not duplicated in the redirect rule
            if (this._redirectRules.Any(x => x.PublicRequestHost == redirectRule.PublicRequestHost && x.PublicRequestPort == redirectRule.PublicRequestPort))
            {
                throw new InvalidOperationException(string.Format("There is already a redirect rule for host {0} and port {1}.", string.IsNullOrEmpty(redirectRule.PublicRequestHost) ? "'Any'" : redirectRule.PublicRequestHost, redirectRule.PublicRequestPort));
            }

            _redirectRules.Add(redirectRule);
            PerformaceCounters.Instance.RedirectRuleCounterIncrement();
            OnRedirectRuleAddedEvent(new RedirectRuleAddedEventArgs(redirectRule));

            if (save)
            {
                Save();
            }
        }
Example #6
0
        public override string Invoke()
        {
            _tcpClientActor.Action(TelnetAction.MoveLeft);

            _tcpClientActor.WriteLine("Enter data for the new rule.");
            var publicHostRequested = RequestEntry("Public Host URL (empty for 'Any'): ");

            var publicPortRequestedEntry = RequestEntry("Public Port: ");
            int publicPortRequested;

            if (!int.TryParse(publicPortRequestedEntry, out publicPortRequested))
            {
                return(string.Format("Command aborted. Cannot parse {0} as an integer.", publicPortRequestedEntry));
            }

            var       internalTargetAddressEntry = RequestEntry("Internal Target IP: ");
            IPAddress internalTargetAddress;

            if (!IPAddress.TryParse(internalTargetAddressEntry, out internalTargetAddress))
            {
                return(string.Format("Command aborted. Cannot parse {0} as a IP Address", internalTargetAddressEntry));
            }

            var internalTargetPortEntry = RequestEntry("Internal Target Port: ");
            int internalTargetPort;

            if (!int.TryParse(internalTargetPortEntry, out internalTargetPort))
            {
                return(string.Format("Command aborted. Cannot parse {0} as an integer.", internalTargetPort));
            }

            var redirectRule = new RedirectRule(IPAddress.Any, publicHostRequested, publicPortRequested, internalTargetAddress, internalTargetPort);

            _tcpClientActor.WriteLine(string.Format("The new rule will be {0}:{1} sent to {2}:{3}.", redirectRule.PublicRequestHost, redirectRule.PublicRequestPort, redirectRule.InternalTargetAddress, redirectRule.InternalTargetPort));

            string responseMessage;

            if (ConfirmAction(string.Format("The change will take effect immediately! Are you sure that the rule is to be added?"), out responseMessage))
            {
                RedirectRuleManager.Instance.Add(redirectRule, true);

                responseMessage = string.Format("New rule was added.");
            }
            return(responseMessage);
        }
        public bool Remove(RedirectRule redirectRule)
        {
            if (!_redirectRules.Remove(redirectRule))
                return false;

            //LogHelper.LogMessage(string.Format("Host {0} removed from listener with port {1}. Redirects to {2}:{3}.", string.IsNullOrEmpty(redirectRule.PublicRequestHost) ? "'Any'" : redirectRule.PublicRequestHost, redirectRule.PublicRequestPort, redirectRule.InternalTargetAddress, redirectRule.InternalTargetPort), Issue.IssueLevel.Information);

            return true;
        }
        public void Add(RedirectRule redirectRule)
        {
            if (this._redirectRules.Count > 0)
            {
                if (PublicPortRequested != redirectRule.PublicRequestPort)
                    throw new InvalidOperationException(string.Format("Cannot add redirect rule on different ports to the same listener. Current port is {0} and the requests added port {1}.", PublicPortRequested, redirectRule.PublicRequestPort));
            }

            _redirectRules.Add(redirectRule);
            //LogHelper.LogMessage(string.Format("Host {0} added to listener with port {1}. Redirects to {2}:{3}.", string.IsNullOrEmpty(redirectRule.PublicRequestHost) ? "'Any'" : redirectRule.PublicRequestHost, redirectRule.PublicRequestPort, redirectRule.InternalTargetAddress, redirectRule.InternalTargetPort), Issue.IssueLevel.Information);
        }
 public static PortListener Create(RedirectRule redirectRule)
 {
     return new PortListener(redirectRule);
 }
Example #10
0
        private void FromClientToTarget()
        {
            try
            {
                LogHelper.ShowMessage(string.Format("Connection {0} started to read.", _connectionIndex));
                var messageChunk = new byte[MessageChunkSize];

                while (_state == TcpActorState.Running)
                {
                    var bytesRead = _tcpClientActor.Read(messageChunk, 0, messageChunk.Length);
                    PerformaceCounters.Instance.Rx(bytesRead);
                    _totalRx += bytesRead;

                    //If there is no _tcpTargetActor, try to get one.
                    if (_tcpTargetActor == null)
                    {
                        _currentRedirectRule = GetTargetInfo(messageChunk, bytesRead);
                        if (_currentRedirectRule != null)
                            _tcpTargetActor = new TcpClientActor(new TcpClient(_currentRedirectRule.InternalTargetAddress.ToString(), _currentRedirectRule.InternalTargetPort));
                        _targetRecievedEvent.Set();
                    }

                    if (_tcpTargetActor == null)
                    {
                        //If there still is no target actor, exit the loop.
                        LogHelper.LogMessage("The reader could not find a target actor to forward the traffic to.", Issue.IssueLevel.Warning);
                        break;
                    }

                    _tcpTargetActor.Write(messageChunk, 0, bytesRead);

                    //TODO: Here is the place to log throughput from client to server. At this point data has been transferred from the client to the target.
                }
            }
            catch (System.IO.IOException exp)
            {
                //Expected when the client closes.
                //exp = {"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."}
                LogHelper.LogException(exp, false, LogHelper.ExceptionSeverity.InformationException);
            }
            catch (ObjectDisposedException exp)
            {
                LogHelper.LogException(exp, false, LogHelper.ExceptionSeverity.ErrorException);
            }
            catch (InvalidOperationException exp)
            {
                LogHelper.LogException(exp, false, LogHelper.ExceptionSeverity.ErrorException);
            }
            catch (SocketException exp)
            {
                //Expected when there is no server listening.
                LogHelper.LogException(exp, true, LogHelper.ExceptionSeverity.WarningException);
                _staticResponse = exp.Message; //This message is sent back as response to the client
                _targetRecievedEvent.Set();
            }
            catch (Exception exp)
            {
                LogHelper.LogException(exp, false, LogHelper.ExceptionSeverity.ErrorException);
                throw;
            }
            finally
            {
                _state = TcpActorState.RequestStop;
                LogHelper.ShowMessage(string.Format("Connection {0} stopped to read. (Rx: {1})", _connectionIndex, _totalRx));
            }
        }
        public bool Remove(RedirectRule redirectRule)
        {
            if (!_redirectRules.Remove(redirectRule))
                return false;

            PerformaceCounters.Instance.RedirectRuleCounterDecrement();
            OnRedirectRuleRemovedEvent(new RedirectRuleRemovedEventArgs(redirectRule));

            Save();

            return true;
        }
 public RedirectRuleRemovedEventArgs(RedirectRule redirectRule)
 {
     this.RedirectRule = redirectRule;
 }
 public RedirectRuleAddedEventArgs(RedirectRule redirectRule)
 {
     this.RedirectRule = redirectRule;
 }
 public RedirectRuleRemovedEventArgs(RedirectRule redirectRule)
 {
     this.RedirectRule = redirectRule;
 }
 public RedirectRuleAddedEventArgs(RedirectRule redirectRule)
 {
     this.RedirectRule = redirectRule;
 }