Пример #1
0
        public ActionReply CloseDataStream(DataStream strm)
        {
            ActionReply reply = new ActionReply();

            if (strm == null)
            {
                ActionLog(ActionEventType.Error, "The data stream parameter was null!");
                return(null);
            }

            lock (m_lock)
            {
                try
                {
                    if (IsConnected)
                    {
                        if (strm.CommandStatus.Type == 2)
                        {
                            if (!(reply = GetReply()).Success)
                            {
                                return(reply);
                            }
                        }
                    }
                }
                finally
                {
                }
            }

            return(reply);
        }
Пример #2
0
        public async Task <ActionReply> GetReplyAsync()
        {
            Log.Start();

            ActionReply reply = new ActionReply();
            var         buf   = "";

            if (!IsConnected)
            {
                ActionLog(ActionEventType.Error, "No connection available!");
                return(null);
            }
            //throw new IOException("No connection available, so reply cannot be retrieved!");

            m_stream.ReadTimeout = ConnectionInfo.TimeoutMilliseconds;
            while ((buf = m_stream.ReadLine(TextEncoding)) != null)
            {
                Match m;
                if ((m = Regex.Match(buf, "^(?<code>[0-9]{3}) (?<message>.*)$")).Success)
                {
                    reply.Code    = m.Groups["code"].Value;
                    reply.Message = m.Groups["message"].Value;
                    break;
                }
                reply.InfoMessages += (buf + "\n");
            }

            // log multiline response messages
            if (reply.InfoMessages != null)
            {
                reply.InfoMessages = reply.InfoMessages.Trim();
            }

            return(reply);
        }
Пример #3
0
        public override Task <ActionReply> SayAction(ActionRequest request, ServerCallContext context)
        {
            ActionReply r = new ActionReply();

            r.Risultato = "OK SayAction";

            return(Task.FromResult(r));
        }
Пример #4
0
        public static async void SayActionAsync()
        {
            ActionReply ar = serviceAction.SayAction(new ActionRequest()
            {
                Azione = 3
            });

            Console.WriteLine(ar.Risultato);
        }
Пример #5
0
        private void SysLog(ActionReply mes)
        {
#if DEBUG
            Log.Debug(string.Format("(SystemLog) [ActionReply]{0}{1}", Environment.NewLine, mes.ToString()));
#endif
            LoggerEventArgs args = null;
            args = new LoggerEventArgs(string.Format("[{0}] {1} {2} ({3})", DateTime.Now.ToShortTimeString(),
                                                     mes.Code, mes.Message, mes.InfoMessages));
        }
Пример #6
0
        private ActionReply GetReply(bool isFirstTime = false)
        {
            ActionReply reply = new ActionReply();

            //pretty simple locking mechanism to prevent threads/tasks from
            //going over eachother and makin crazy shit happen...
            lock (m_lock)
            {
                var buf = "";
                if (!isFirstTime)
                {
                    if (!IsConnected)
                    {
                        throw new IOException("No connection available!");
                    }
                }

                if (useStream)
                {
                    stream.ReadTimeout = 15000;
                }

                while ((buf = ReadLine(Encoding.ASCII)) != null)
                {
                    Match m;
                    if ((m = Regex.Match(buf, "^(?<code>[0-9]{3}) (?<message>.*)$")).Success)
                    {
                        reply.Code    = m.Groups["code"].Value;
                        reply.Message = m.Groups["message"].Value;
                        break;
                    }

                    reply.InfoMessages += (buf + "\n");

                    // log multiline response messages
                    if (reply.InfoMessages != null)
                    {
                        reply.InfoMessages = reply.InfoMessages.Trim();
                    }
                }
            }

            return(reply);
        }
Пример #7
0
        public ActionReply SendCommand(string cmd)
        {
            Log.Start();
#if DEBUG
            Log.Debug("(SendCommand) {0}", cmd);
#endif
#if !DEBUG && VERBOSE
            Log.Verbose("(SendCommand) {0}", cmd);
#endif

            ActionReply reply = new ActionReply();
            lock (m_lock)
            {
                if (!IsConnected)
                {
                    if (cmd == "QUIT")
                    {
                        reply.Code    = "200";
                        reply.Message = "Connection already closed...";
                        return(reply);
                    }

                    reply.Code    = "530";
                    reply.Message = "Not connected!";
                    return(reply);
                }

                var cmdToLog = cmd;
                if (cmd.StartsWith("PASS"))
                {
                    cmdToLog = "PASS ***";
                }

                ActionLog(ActionEventType.None, cmdToLog);

                m_stream.WriteLine(TextEncoding, cmd);
                reply = GetReply();
            }

            return(reply);
        }
Пример #8
0
        public void setBinaryMode(BinaryMode mode)
        {
            var rep = new ActionReply();

            switch (mode)
            {
            case BinaryMode.ASCII:
                rep = sendCommand("TYPE A");
                break;

            case BinaryMode.Binary:
                rep = sendCommand("TYPE I");
                break;
            }

            SysLog(rep);
            if (!rep.Success /*.Code != 200*/)
            {
                //throw new IOException(rep.Response.Substring(4));
                SysLog("Unable to set correct binary mode - you sure this server supports either " + '"' + "TYPE A" +
                       '"' + " or " + '"' + "TYPE I" + '"' + "?");
            }
        }
Пример #9
0
 public static bool IsError(this ActionReply actionReply)
 {
     return(actionReply.Error.IsError());
 }
Пример #10
0
        public void Connect()
        {
            if (OnSystemAction == null)
            {
                if (MessageBox.Show("No reference to " + '"' + "OnSystemAction" + '"' + " could be found - do you wish to continue?", "Reference Issue!", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }
            }

            if (OnGeneralAction == null)
            {
                if (MessageBox.Show("No reference to " + '"' + "OnGeneralAction" + '"' + " could be found - do you wish to continue?", "Reference Issue!", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }
            }

            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            var ep = new IPEndPoint(Dns.GetHostEntry(HostInfo.Hostname).AddressList[0], HostInfo.Port);

            try
            {
                client.Connect(ep);
            }
            catch (Exception ex)
            {
                SysLog("Could not reach the server!");
                return;
            }

            var rep = GetReply(true);

            SysLog(rep);
            if (!rep.Success)
            {
                Disconnect();
            }
            else
            {
                IsConnected = true;
                if (HostInfo.Security == SSLType.Unsecure)
                {
                    IsConnected = true;
                }
                else
                {
                    var zx = new ActionReply();
                    switch (HostInfo.Security)
                    {
                    case SSLType.AuthTLSv10:
                    case SSLType.AuthTLSv11:
                    case SSLType.AuthTLSv12:
                    case SSLType.AuthSSL:
                        zx = sendCommand("AUTH SSL");
                        if (zx.Success)
                        {
                            getSslStream(client);
                            useStream   = true;
                            IsConnected = true;
                        }
                        break;
                    }
                }
            }
        }
Пример #11
0
        private void GetServerOptions(ActionReply reply)
        {
            foreach (string feat in reply.InfoMessages.Split('\n'))
            {
                if (feat.ToUpper().Trim().StartsWith("MLST") || feat.ToUpper().Trim().StartsWith("MLSD"))
                {
                    AvailableOptions |= ServerCapabilities.MLSD;
                }
                else if (feat.ToUpper().Trim().StartsWith("MDTM"))
                {
                    AvailableOptions |= ServerCapabilities.MDTM;
                }
                else if (feat.ToUpper().Trim().StartsWith("REST STREAM"))
                {
                    AvailableOptions |= ServerCapabilities.REST;
                }
                else if (feat.ToUpper().Trim().StartsWith("SIZE"))
                {
                    AvailableOptions |= ServerCapabilities.SIZE;
                }
                else if (feat.ToUpper().Trim().StartsWith("UTF8"))
                {
                    AvailableOptions |= ServerCapabilities.UTF8;
                }
                else if (feat.ToUpper().Trim().StartsWith("PRET"))
                {
                    AvailableOptions |= ServerCapabilities.PRET;
                }
                else if (feat.ToUpper().Trim().StartsWith("MFMT"))
                {
                    AvailableOptions |= ServerCapabilities.MFMT;
                }
                else if (feat.ToUpper().Trim().StartsWith("MFCT"))
                {
                    AvailableOptions |= ServerCapabilities.MFCT;
                }
                else if (feat.ToUpper().Trim().StartsWith("MFF"))
                {
                    AvailableOptions |= ServerCapabilities.MFF;
                }
                else if (feat.ToUpper().Trim().StartsWith("MD5"))
                {
                    AvailableOptions |= ServerCapabilities.MD5;
                }
                else if (feat.ToUpper().Trim().StartsWith("XMD5"))
                {
                    AvailableOptions |= ServerCapabilities.XMD5;
                }
                else if (feat.ToUpper().Trim().StartsWith("XCRC"))
                {
                    AvailableOptions |= ServerCapabilities.XCRC;
                }
                else if (feat.ToUpper().Trim().StartsWith("XSHA1"))
                {
                    AvailableOptions |= ServerCapabilities.XSHA1;
                }
                else if (feat.ToUpper().Trim().StartsWith("XSHA256"))
                {
                    AvailableOptions |= ServerCapabilities.XSHA256;
                }
                else if (feat.ToUpper().Trim().StartsWith("XSHA512"))
                {
                    AvailableOptions |= ServerCapabilities.XSHA512;
                }
                else if (feat.ToUpper().Trim().StartsWith("HASH"))
                {
                    Match m;

                    AvailableOptions |= ServerCapabilities.HASH;

                    if ((m = Regex.Match(feat.ToUpper().Trim(), @"^HASH\s+(?<types>.*)$")).Success)
                    {
                        foreach (string type in m.Groups["types"].Value.Split(';'))
                        {
                            switch (type.ToUpper().Trim())
                            {
                            case "SHA-1":
                            case "SHA-1*":
                                UsedHashAlgorithms |= ServerHashAlgorithm.SHA1;
                                break;

                            case "SHA-256":
                            case "SHA-256*":
                                UsedHashAlgorithms |= ServerHashAlgorithm.SHA256;
                                break;

                            case "SHA-512":
                            case "SHA-512*":
                                UsedHashAlgorithms |= ServerHashAlgorithm.SHA512;
                                break;

                            case "MD5":
                            case "MD5*":
                                UsedHashAlgorithms |= ServerHashAlgorithm.MD5;
                                break;

                            case "CRC":
                            case "CRC*":
                                UsedHashAlgorithms |= ServerHashAlgorithm.CRC;
                                break;
                            }
                        }
                    }
                }
            }
        }
Пример #12
0
 private void ActionLog(ActionReply reply)
 {
     ActionLog(reply.Success ? ActionEventType.Success : ActionEventType.Failure, reply);
 }
Пример #13
0
 private void ActionLog(ActionEventType actionType, ActionReply reply)
 {
     ActionLog(actionType, reply, 0, 0);
 }
Пример #14
0
        private void ActionLog(ActionEventType actionType, ActionReply reply, long maxProgress, long curProgress)
        {
            var args = new ActionEventArgs(actionType, reply, "", maxProgress, curProgress);

            SystemActionLog(args);
        }
Пример #15
0
        public virtual async Task ConnectAsync()
        {
            Log.Start();
            ActionReply reply = new ActionReply();

            if (isDisposed)
            {
                throw new ApplicationException("This client has been disposed!");
            }

            if (m_stream == null)
            {
                m_stream = new SocketStream(ConnectionInfo.Security);
            }
            else
            {
                if (IsConnected)
                {
                    await DisconnectAsync();
                }
            }

            if (ConnectionInfo.Hostname == null)
            {
                throw new MissingPrimaryKeyException("Hostname is empty!");
            }
            if (UserInfo == null)
            {
                throw new MissingPrimaryKeyException("No user information has been set!");
            }

            m_stream.ConnectTimeout = ConnectionInfo.TimeoutMilliseconds;
            m_stream.Connect(ConnectionInfo.Hostname, ConnectionInfo.Port);
            m_stream.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive,
                                     ConnectionInfo.KeepAlive);

            if (ConnectionInfo.Security != SSLType.Unsecure)
            {
                switch (ConnectionInfo.Security)
                {
                case SSLType.AuthSSL:
                    reply = await SendCommandAsync("AUTH SSL");

                    if (reply.Success)
                    {
                        m_stream.ActivateEncryption(ConnectionInfo.Hostname, null, SslProtocols.Ssl3);
                    }
                    //else
                    //    throw new SecurityNotAvailableException("SSL call failed!");
                    break;

                case SSLType.AuthTLSv10:
                    reply = await SendCommandAsync("AUTH TLS");

                    if (reply.Success)
                    {
                        m_stream.ActivateEncryption(ConnectionInfo.Hostname, null, SslProtocols.Ssl3 | SslProtocols.Tls);
                    }
                    //else
                    //    throw new SecurityNotAvailableException("TLS call failed!");
                    break;

                case SSLType.AuthTLSv11:
                    reply = await SendCommandAsync("AUTH TLS");

                    if (reply.Success)
                    {
                        m_stream.ActivateEncryption(ConnectionInfo.Hostname, null, SslProtocols.Ssl3 | SslProtocols.Tls11);
                    }
                    //else
                    //    throw new SecurityNotAvailableException("TLS call failed!");
                    break;

                case SSLType.AuthTLSv12:
                    reply = await SendCommandAsync("AUTH TLS");

                    if (reply.Success)
                    {
                        m_stream.ActivateEncryption(ConnectionInfo.Hostname, null, SslProtocols.Ssl3 | SslProtocols.Tls12);
                    }
                    //else
                    //    throw new SecurityNotAvailableException("TLS call failed!");
                    break;

                case SSLType.ImplicitSSL:
                    m_stream.ActivateEncryption(ConnectionInfo.Hostname, null, SslProtocols.Ssl2);
                    break;
                }

                ActionLog(reply);
                if (!reply.Success)
                {
                    await DisconnectAsync();

                    return;
                }
            }

            await HandshakeAsync();
            await LogInAsync();

            if (m_stream.IsEncrypted /* && DataConnectionEncryption*/)
            {
                if (!(reply = await SendCommandAsync("PBSZ 0")).Success)
                {
                    ActionLog(reply);
                    await DisconnectAsync();

                    return;
                    //throw new FtpCommandException(reply);
                }

                if (!(reply = await SendCommandAsync("PROT P")).Success)
                {
                    ActionLog(reply);
                    await DisconnectAsync();

                    return;
                    //throw new FtpCommandException(reply);
                }
            }

            if ((reply = await SendCommandAsync("FEAT")).Success && reply.InfoMessages != null)
            {
                GetServerOptions(reply);
            }

            ActionLog(ActionEventType.None, reply);

            if (TextEncoding == Encoding.ASCII && HasFeature(ServerCapabilities.UTF8))
            {
                TextEncoding = Encoding.UTF8;
                reply        = await SendCommandAsync("OPTS UTF8 ON");

                ActionLog(reply);
            }

            if ((reply = await SendCommandAsync("SYST")).Success)
            {
                ServerSystem = reply.Message;
                ActionLog(reply);
            }

            if (m_stream.IsEncrypted && ConnectionInfo.Security == SSLType.Unsecure)
            {
                if (!(reply = await SendCommandAsync("CCC")).Success)
                {
                    ActionLog(reply);
                    await DisconnectAsync();

                    return;
                }
                else
                {
                    m_stream.DeactivateEncryption();
                    await ReadStaleDataAsync(false, true);
                }
            }

            //list whatever files and shit!
        }