Example #1
0
        /// <summary>
        /// Support proxy protocol version 1 header for use with HAProxy.
        /// Documented at http://www.haproxy.org/download/1.8/doc/proxy-protocol.txt
        /// </summary>
        /// <param name="command"></param>
        /// <param name="errorResponse"></param>
        /// <returns></returns>
        public bool TryMakeProxy(out SmtpCommand command, out SmtpResponse errorResponse)
        {
            command       = null;
            errorResponse = null;

            Enumerator.Take();
            Enumerator.Skip(TokenKind.Space);

            if (TryMake(TryMakeProxyProtoTypeString, out string inetProto) == false)
            {
                return(false);
            }

            if (inetProto == "UNKNOWN")
            {
                // IF INET PROTO IS UNKNOWN REST OF THIS LINE SHOULD BE IGNORED.
                command = new ProxyCommand(_options, null, null);
                return(true);
            }

            Enumerator.Skip(TokenKind.Space);

            IPAddress sourceIp;
            IPAddress destinationIp;

            // read addresses if IPv4
            if (inetProto == "TCP4")
            {
                if (TryMakeProxyFromAndToAddresses(TryMakeIpv4AddressLiteral, out sourceIp, out destinationIp) == false)
                {
                    return(false);
                }
            }
            else if (inetProto == "TCP6")
            {
                if (TryMakeProxyFromAndToAddresses(TryMakeIpv6AddressLiteral, out sourceIp, out destinationIp) == false)
                {
                    return(false);
                }
            }
            else
            {
                // Unexpected  / Invalid protocol in proxy protocol format.
                return(false);
            }

            Enumerator.Skip(TokenKind.Space);

            // Read ports
            if (TryMakeShortNum(out var sourcePort) == false)
            {
                return(false);
            }

            Enumerator.Skip(TokenKind.Space);

            if (TryMakeShortNum(out var destinationPort) == false)
            {
                return(false);
            }

            command = new ProxyCommand(_options, new IPEndPoint(sourceIp, sourcePort),
                                       new IPEndPoint(destinationIp, destinationPort));
            return(true);
        }
 /// <summary>
 /// Visit an PROXY command.
 /// </summary>
 /// <param name="command">The command that is being visited.</param>
 protected virtual void Visit(ProxyCommand command)
 {
 }