Kingkong直播弹幕姬分析(三)

科技是第一生产力

Posted by 恋 on January 25, 2019

Kingkong直播

Stage 7

摸了一天鱼。我们用好几天前的WebSocketSharp,复制了一下他们的demo加了自己的代码跑了起来。

首先NuGet Install-Package WebSocketSharp -Version 1.0.3-rc11

然后复制代码.jpg

using System;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using WebSocketSharp;

class Program
{
    static void Main(string[] args)
    {
        byte[] GetBytes(string s) => Encoding.UTF8.GetBytes(s);
        string Base64(byte[] b) => Convert.ToBase64String(b).Replace("=", "").Replace("+", "-").Replace("/", "_");
        var roomId = int.Parse(Console.ReadLine());
        var dc = new WebClient().DownloadString("https://www.kingkong.com.tw/" + roomId);
        var m = Regex.Match(dc, $"liveId":"({roomId}\\w+)"");
        var liveId = m.Success ? m.Groups[1].Value : throw new Exception("liveId miss.");
        m = Regex.Match(dc, "liveKey":"(\\w+)"");
        var liveKey = m.Success ? m.Groups[1].Value : throw new Exception("liveKey miss.");
        var guid = Guid.NewGuid().ToString().ToLower().Replace("-", "");
        var part12 = $"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.{Base64(GetBytes($"{{\"live_id\":\"{liveId}\",\"pfid\":\"{guid}\",\"name\":\"訪客{guid.Substring(27)}\",\"access_token\":null,\"lv\":1,\"from\":1,\"from_seq\":1,\"channel_id\":0,\"client_type\":\"web\"}}"))}";
        var token = $"{part12}.{Base64(new HMACSHA256(GetBytes("")).ComputeHash(GetBytes(part12)))}";
        var access = $"42/chat_nsp,[\"authentication\",{{\"live_id\":\"{liveId}\",\"anchor_pfid\":{roomId},\"access_token\":\"{token}\",\"token\":\"{token}\",\"from\":\"WEB\",\"client_type\":\"web\",\"r\":0}}]";
        using (var ws = new WebSocket("wss://cht.ws.kingkong.com.tw/chat_nsp/?EIO=3&transport=websocket"))
        {
            ws.OnMessage += (sender, e) =>
            {
                Console.WriteLine("Receive: " + e.Data);
                if (e.Data == "40/chat_nsp")
                {
                    Console.WriteLine("Auth pending");
                    ws.Send(access);
                }
            };
            ws.Connect();
            ws.Send("40/chat_nsp,");
            Console.WriteLine("Enter to quit.");
            Console.ReadLine();
        }
    }
}

一切看起来都十分靠谱,但是问题来了:我还是获取不到国内的弹幕… 根据数位热心群友提供的样本,我发现这里的各类magic number都没有区别(包括我怀疑的channel_id)…

大概是他们服务端判定根本不肯给我吧。伤心。

另:每过一会儿要发个心跳包,很简单的。但是既然已经知道没办法获取另一个区的,我就不想写了。

据说手机版能同时看到两个区的弹幕,等我不咸鱼了再更。

(待续)