Exemple #1
0
            public async Task Invoke(OwinEnvironment ctx)
            {
                var cancel_token = ctx.Request.CallCancelled;
                var localpath    = Path.GetFullPath(CombinePath(LocalPath, ctx.Request.Path));

                if (Directory.Exists(localpath))
                {
                    localpath = Path.Combine(localpath, "index.html");
                    if (!File.Exists(localpath))
                    {
                        ctx.Response.StatusCode = HttpStatusCode.Forbidden;
                        return;
                    }
                }
                if (File.Exists(localpath))
                {
                    var contents     = File.ReadAllBytes(localpath);
                    var content_desc = GetFileDesc(Path.GetExtension(localpath));
                    ctx.Response.ContentType   = content_desc.MimeType;
                    ctx.Response.ContentLength = contents.LongLength;
                    var acinfo = ctx.GetAccessControlInfo();
                    if (acinfo?.AuthenticationKey != null)
                    {
                        ctx.Response.Headers.Add("Set-Cookie", $"auth={acinfo.AuthenticationKey.GetToken()}; Path=/");
                    }
                    await ctx.Response.WriteAsync(contents, cancel_token).ConfigureAwait(false);
                }
                else
                {
                    ctx.Response.StatusCode = HttpStatusCode.NotFound;
                }
            }
        public async Task Invoke(OwinEnvironment ctx)
        {
            var cancel_token = ctx.Request.CallCancelled;

            ctx.Response.ContentType   = "application/javascript";
            ctx.Response.ContentLength = contents.LongLength;
            var acinfo = ctx.GetAccessControlInfo();

            if (acinfo?.AuthenticationKey != null)
            {
                ctx.Response.Headers.Add("Set-Cookie", $"auth={acinfo.AuthenticationKey.GetToken()}; Path=/");
            }
            await ctx.Response.WriteAsync(contents, cancel_token).ConfigureAwait(false);
        }
Exemple #3
0
        private static async Task InvokeRedirect(OwinEnvironment ctx)
        {
            var cancel_token = ctx.Request.CallCancelled;

            ctx.Response.ContentType = "text/plain;charset=utf-8";
            ctx.Response.Headers.Set("Location", "/html/index.html");
            var acinfo = ctx.GetAccessControlInfo();

            if (acinfo?.AuthenticationKey != null)
            {
                ctx.Response.Headers.Add("Set-Cookie", $"auth={acinfo.AuthenticationKey.GetToken()}; Path=/");
            }
            ctx.Response.StatusCode = HttpStatusCode.Moved;
            await ctx.Response.WriteAsync("Moving...", cancel_token).ConfigureAwait(false);
        }
Exemple #4
0
        private static async Task InvokeIndexTXT(OwinEnvironment ctx)
        {
            var cancel_token = ctx.Request.CallCancelled;

            ctx.Response.ContentType = "text/plain;charset=utf-8";
            var acinfo = ctx.GetAccessControlInfo();

            if (acinfo?.AuthenticationKey != null)
            {
                ctx.Response.Headers.Add("Set-Cookie", $"auth={acinfo.AuthenticationKey.GetToken()}; Path=/");
            }
            var peercast = ctx.GetPeerCast();
            var indextxt = String.Join("\r\n", peercast.Channels.Select(c => BuildIndexTXTEntry(peercast, c)));
            await ctx.Response.WriteAsync(indextxt, cancel_token).ConfigureAwait(false);
        }
Exemple #5
0
        private async Task Invoke(OwinEnvironment ctx)
        {
            var cancel_token = ctx.Request.CallCancelled;
            var channel_list = await GetYPChannelsAsync(cancel_token).ConfigureAwait(false);

            var contents = System.Text.Encoding.UTF8.GetBytes(ChannelsToIndex(channel_list));

            ctx.Response.ContentType   = "text/plain;charset=utf-8";
            ctx.Response.ContentLength = contents.LongLength;
            var acinfo = ctx.GetAccessControlInfo();

            if (acinfo?.AuthenticationKey != null)
            {
                ctx.Response.Headers.Add("Set-Cookie", $"auth={acinfo.AuthenticationKey.GetToken()}; Path=/");
            }
            await ctx.Response.WriteAsync(contents, cancel_token).ConfigureAwait(false);
        }
        private async Task PlayListHandler(OwinEnvironment ctx, ParsedRequest req, Channel channel)
        {
            var ct      = ctx.Request.CallCancelled;
            var session = req.Session;

            if (String.IsNullOrWhiteSpace(session))
            {
                var source = new IPEndPoint(IPAddress.Parse(ctx.Request.RemoteIpAddress), ctx.Request.RemotePort ?? 0).ToString();
                using (var md5 = System.Security.Cryptography.MD5.Create()) {
                    session =
                        md5
                        .ComputeHash(System.Text.Encoding.ASCII.GetBytes(source))
                        .Aggregate(new System.Text.StringBuilder(), (builder, v) => builder.Append(v.ToString("X2")))
                        .ToString();
                }
                var location = new UriBuilder(ctx.Request.Uri);
                if (String.IsNullOrEmpty(location.Query))
                {
                    location.Query = $"session={session}";
                }
                else
                {
                    location.Query = location.Query.Substring(1) + $"&session={session}";
                }
                ctx.Response.Redirect(location.Uri.ToString());
                return;
            }
            var pls = new M3U8PlayList(ctx.Request.Query.Get("scheme"), channel);

            ctx.Response.StatusCode = HttpStatusCode.OK;
            ctx.Response.Headers.Add("Cache-Control", "private");
            ctx.Response.Headers.Add("Cache-Disposition", "inline");
            ctx.Response.Headers.Add("Access-Control-Allow-Origin", "*");
            ctx.Response.ContentType = pls.MIMEType;
            using (var subscription = HLSChannelSink.GetSubscription(this, channel, ctx, session)) {
                subscription.Stopped.ThrowIfCancellationRequested();
                byte[] body;
                try {
                    var baseuri = new Uri(
                        new Uri(ctx.Request.Uri.GetComponents(UriComponents.SchemeAndServer | UriComponents.UserInfo, UriFormat.UriEscaped)),
                        "hls/");
                    var acinfo = ctx.GetAccessControlInfo();
                    using (var cts = CancellationTokenSource.CreateLinkedTokenSource(ct, subscription.Stopped)) {
                        cts.CancelAfter(10000);
                        if (acinfo.AuthorizationRequired)
                        {
                            var parameters = new Dictionary <string, string>()
                            {
                                { "auth", acinfo.AuthenticationKey.GetToken() },
                                { "session", subscription.SessionId },
                            };
                            body = await pls.CreatePlayListAsync(baseuri, parameters, subscription.Segmenter, cts.Token).ConfigureAwait(false);
                        }
                        else
                        {
                            var parameters = new Dictionary <string, string>()
                            {
                                { "session", subscription.SessionId },
                            };
                            body = await pls.CreatePlayListAsync(baseuri, parameters, subscription.Segmenter, cts.Token).ConfigureAwait(false);
                        }
                    }
                }
                catch (OperationCanceledException) {
                    ctx.Response.StatusCode = HttpStatusCode.GatewayTimeout;
                    return;
                }
                ctx.Response.StatusCode = HttpStatusCode.OK;
                await ctx.Response.WriteAsync(body, ct).ConfigureAwait(false);
            }
        }
    private static async Task PlayListHandler(OwinEnvironment ctx)
    {
      var ct = ctx.Request.CallCancelled;
      var req = ParsedRequest.Parse(ctx);
      if (!req.IsValid) {
        ctx.Response.StatusCode = req.Status;
        return;
      }
      Channel channel;
      try {
        channel = await GetChannelAsync(ctx, req, ct).ConfigureAwait(false);
      }
      catch (TaskCanceledException) {
        ctx.Response.StatusCode = HttpStatusCode.GatewayTimeout;
        return;
      }
      if (channel==null) {
        ctx.Response.StatusCode = HttpStatusCode.NotFound;
        return;
      }

      var fmt = ctx.Request.Query.Get("pls") ?? req.Extension;
      //m3u8のプレイリストを要求された時はHLS用のパスに転送する
      if (fmt?.ToLowerInvariant()=="m3u8") {
        var location = new UriBuilder(ctx.Request.Uri);
        location.Path = $"/hls/{req.ChannelId:N}";
        if (location.Query.Contains("pls=m3u8")) {
          var queries = location.Query.Substring(1).Split('&').Where(seg => seg!="pls=m3u8").ToArray();
          if (queries.Length>0) {
            location.Query = String.Join("&", queries);
          }
          else {
            location.Query = null;
          }
        }
        ctx.Response.Redirect(HttpStatusCode.MovedPermanently, location.Uri.ToString());
        return;
      }

      var scheme = ctx.Request.Query.Get("scheme");
      var pls = CreatePlaylist(channel, fmt, scheme);
      ctx.Response.StatusCode = HttpStatusCode.OK;
      ctx.Response.Headers.Add("Cache-Control", "private");
      ctx.Response.Headers.Add("Cache-Disposition", "inline");
      ctx.Response.Headers.Add("Access-Control-Allow-Origin", "*");
      ctx.Response.ContentType = pls.MIMEType;
      byte[] body;
      try {
        var baseuri = new Uri(
          new Uri(ctx.Request.Uri.GetComponents(UriComponents.SchemeAndServer | UriComponents.UserInfo, UriFormat.UriEscaped)),
          "stream/");
        var acinfo = ctx.GetAccessControlInfo();
        using (var cts=CancellationTokenSource.CreateLinkedTokenSource(ct)) {
          cts.CancelAfter(10000);
          if (acinfo.AuthorizationRequired) {
            var parameters = new Dictionary<string, string>() {
              { "auth", acinfo.AuthenticationKey.GetToken() },
            };
            body = await pls.CreatePlayListAsync(baseuri, parameters, cts.Token).ConfigureAwait(false);
          }
          else {
            body = await pls.CreatePlayListAsync(baseuri, Enumerable.Empty<KeyValuePair<string,string>>(), cts.Token).ConfigureAwait(false);
          }
        }
      }
      catch (OperationCanceledException) {
        ctx.Response.StatusCode = HttpStatusCode.GatewayTimeout;
        return;
      }
      ctx.Response.StatusCode = HttpStatusCode.OK;
      await ctx.Response.WriteAsync(body, ct).ConfigureAwait(false);
    }