Example #1
0
 /// <summary>
 /// Create a new user
 /// </summary>
 /// <param name="connectionId">The SignalR connection ID of the user</param>
 /// <param name="mediaServerUuid">The UUID of the media server that this User is connected to</param>
 /// <param name="username">The username of the user</param>
 public User(string connectionId, int mediaServerUuid, string username)
 {
     ConnectionId    = connectionId;
     MediaServerUuid = mediaServerUuid;
     Username        = username;
     UserUuid        = Uuid.GetUuid();
 }
Example #2
0
 /// <summary>
 /// Creates a new file object, generating a new UUID for it
 /// </summary>
 /// <param name="videoUrl">The url of the video</param>
 /// <param name="audioUrl">The url of the audio</param>
 /// <param name="title">The title of the file</param>
 /// <param name="sha1">The sha1 hash of the file</param>
 /// <param name="subtitles">The subtitles of this file</param>
 /// <param name="duration">The duration of the file</param>
 /// <param name="isFileAvailable">Whether the file is available</param>
 /// <param name="type">Whether it's stored offline or it's downloaded</param>
 public ServerFile(string videoUrl, string audioUrl, string title, string sha1, SubtitleInfo[] subtitles, double duration, bool isFileAvailable, FileType type) : base(videoUrl, audioUrl, title, sha1, subtitles, duration, isFileAvailable, type)
 {
     UUID = Uuid.GetUuid();
 }
        /// <summary>
        /// Downloads a file from a URL
        /// </summary>
        /// <param name="url">The URL to download from</param>
        /// <returns>True if it was successful, false if not</returns>
        private async Task <bool> DownloadUrl(string url)
        {
            //Get the info
            DownloadInfo info = await Downloader.GetDownloadInfoFromUrl(url);

            //Validate
            if (info == null)
            {
                return(false);
            }
            if (info.VideoCodec == null && info.AudioCodec == null)
            {
                return(false);
            }
            if (info.RequestedUrls == null && info.Url == null)
            {
                return(false);
            }

            //Create a new file object
            string httpVideoUrl = "";
            string httpAudioUrl = "";
            int    uuid         = Uuid.GetUuid();

            if (!string.IsNullOrEmpty(info.VideoCodec) && info.VideoCodec != "none")
            {
                httpVideoUrl = "http://" + MediaserverIp + ":" + MediaserverPort + "/download/" + uuid + "." + info.Extension;
            }
            else
            {
                httpAudioUrl = "http://" + MediaserverIp + ":" + MediaserverPort + "/download/" + uuid + "." + info.Extension;
            }

            PlayableFile newFile = new PlayableFile(httpVideoUrl, httpAudioUrl, info.Title, null, new SubtitleInfo[0], info.Duration, false, FileType.Downloaded);

            //Add it to the list of files
            Files.Add(newFile);

            //Send available files updated message
            WebServer.SendRawData("AvailableFilesUpdated", new byte[0]);

            //Download the file to the temporary folder
            bool isSuccess = await Downloader.DownloadFileFromUrl(url, DownloadFilesPath + "\\incomplete\\" + uuid + "." + info.Extension);

            //If it was a success, change the availability to true, and add to the http server
            if (isSuccess)
            {
                File.Move(DownloadFilesPath + "\\incomplete\\" + uuid + "." + info.Extension, DownloadFilesPath + "\\" + uuid + "." + info.Extension);
                //SetHttpServerFiles();
            }
            //If it wasn't a success, remove it from the list of files
            else
            {
                Files.Remove(newFile);
                newFile.IsAvailable = true;
                WebServer.SendRawData("AvailableFilesUpdated", new byte[0]);
            }

            //Send available files updated message
            //WebServer.SendRawData("AvailableFilesUpdated", new byte[0]);

            //Return whether it was success
            return(isSuccess);
        }
Example #4
0
        public async Task <IActionResult> Login([FromBody] ModelLogin model)
        {
            // Check request legality.
            if (!ModelState.IsValid)
            {
                return(StatusCode(403, new JsonResult(new ViewModelCode403("Model"))));
            }

            var recaptcha = await Recaptcha.Validate(Request);

            if (!recaptcha.success)
            {
                return(StatusCode(403, new JsonResult(new ViewModelCode403("reCaptcha"))));
            }

            // Do login. Check password.
            var users = from u in Db.Users
                        where u.Username == model.Username &&
                        u.Password == Password.HashPassword(model.Password, u.PasswordSalt)
                        select u;

            var user = users.FirstOrDefault();

            if (user == null)
            {
                return(StatusCode(403, new JsonResult(new ViewModelCode403("Password"))));
            }

            // Check ip. Unused yet.
            var clientIp = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();

            /*
             * var securities = from s in Db.UsersSecurity
             *       where s.Id == user.Id
             *       select s;
             *
             * var security = securities.FirstOrDefault();
             * if (security == null)
             * {
             *  Db.UsersSecurity.Add(new UsersSecurity()
             *  {
             *      Id = user.Id
             *  });
             *  Db.SaveChanges();
             * }
             *
             * if (!string.IsNullOrWhiteSpace(security.LastLoginIp))
             * {
             *  if (security.LastLoginIp != clientIp)
             *  {
             *
             *  }
             * }
             */

            // Login successful.
            // Expire expired tokens.
            var timeNow = long.Parse(Time.GetTimeStamp13());

            var tokens = from t in Db.Tokens
                         where t.ExpireTime < timeNow
                         select t;

            foreach (var t in tokens)
            {
                t.Status = 3;
            }
            Db.SaveChanges();

            // Temp expire current user other token.
            tokens = from t in Db.Tokens
                     where t.UserId == user.Id &&
                     t.Status == 1
                     select t;

            foreach (var t in tokens)
            {
                t.Status = 2;
            }

            // Generate and save new token.
            var accessToken = Uuid.GetUuid();

            Db.Tokens.Add(new Tokens()
            {
                UserId      = user.Id,
                AccessToken = accessToken,
                ClientToken = model.ClientToken,
                ClientIp    = clientIp,
                IssueTime   = timeNow,
                ExpireTime  = timeNow + (int.Parse(Config["Security:TokenExpireDays"]) * 24 * 60 * 60),
                Status      = 1
            });
            Db.SaveChanges();

            // Return results.
            var result = new Login()
            {
                AccessToken = accessToken,
                ClientToken = model.ClientToken,
                User        = new User()
                {
                    UserId = user.OpenUserId
                }
            };

            return(new JsonResult(result));
        }