Beispiel #1
0
        /// <summary>
        /// Runs the logic for Client Hello
        /// </summary>
        /// <param name="ch"><see cref="ClientHello"/></param>
        public static void Run(RequestWithHeader <ClientHelloHeader> ch)
        {
#if DEBUG
            Tuple <string, bool> auth = new Tuple <string, bool>("", true);
#else
            Tuple <string, bool> auth = Authentification.AuthenticateClient(ch.header.ClientIP, ch.header.ClientToken);
#endif
            string sha     = "";
            bool   aproval = false;
            if (auth.Item2 == true)
            {
                aproval = DecideAproval(ch.header);

                if (aproval == false)
                {
                    Log.Warning("Order was not approved, fetching already existing sha!");
                    sha  = GetExistingSha(ch.header);
                    auth = new Tuple <string, bool>("Error: Existing order with sha:" + sha, false);
                }
                else
                {
                    sha = CreateSha(ch.header);
                    Log.Information("Order was approved, new sha :{0}", sha);
                }
            }

            if (auth.Item1.Contains("Token:"))
            {
                StorageProvider.GetInstance().RegisterClientToken(ch.header.ClientIP, auth.Item1.Split(':')[1]);
            }

            // Wrap the ServerHello Header
            WraperHeader wraper = new WraperHeader
            {
                Type = HeaderTypes.ServerHelloHeader,
                Data = ByteArrayUtils.ToByteArray(CreateServerHelloHeader(aproval, sha, auth.Item1))
            };

            Sender.Send(wraper, ch.Client);

            // Wait for the orderpost
            if (aproval == true)
            {
                OrderReciever.ReicieveClientHello(ch, sha);
            }
            // If aproval is false, the connection will close
        }
        public static void Run(RequestWithHeader <RequestFinishedOrderHeader> rfo)
        {
#if DEBUG
            Tuple <string, bool> auth = new Tuple <string, bool>("", true);
#else
            Tuple <string, bool> auth = Authentification.AuthenticateClient(rfo.header.Ip, rfo.header.ClientToken);
#endif
            FinishedOrderHeader foh = new FinishedOrderHeader();

            bool   sendData   = auth.Item2 == true && !StorageProvider.GetInstance().ClientHasOrder(rfo.header.Ip);
            string orderPath  = Path.Combine(ConfigReader.GetInstance().Config.SavePath, rfo.header.Sha);
            string resultPath = Path.Combine(orderPath, "result");
            string zipPath    = Path.Combine(orderPath, "result.zip");
            if (sendData)
            {
                if (!File.Exists(zipPath))
                {
                    ZipFile.CreateFromDirectory(resultPath, zipPath);
                }
                long size = new FileInfo(zipPath).Length;
                foh.Size    = size;
                foh.Message = rfo.header.Sha;
            }
            else
            {
                foh.Size    = 0;
                foh.Message = auth.Item1 == "" ? "You already have an order in progress" : auth.Item1;
            }


            WraperHeader wh = new WraperHeader()
            {
                Data = ByteArrayUtils.ToByteArray(foh),
                Type = HeaderTypes.FinishedOrderHeader
            };

            Sender.Send(wh, rfo.Client);

            if (sendData)
            {
                Sender.SendZip(zipPath, rfo.Client.GetStream());
            }
        }