Untitled

 avatar
user_2299906
plain_text
a year ago
13 kB
2
Indexable
Never
public class Test
    {
        public void tes()
        {
            
            string cookie = "";
            string UID = "100034645878878";
            RequestFB request = new RequestFB(cookie);
            string idpost = "789207278560725";
            string idcom = "789209021893884";
            string mess = set_tag_uid("chấm @[100007363965466:Hiếu Nguyễn Minh]");
            string test = request.GraphApi("&variables={\"displayCommentsFeedbackContext\":null,\"displayCommentsContextEnableComment\":null,\"displayCommentsContextIsAdPreview\":null,\"displayCommentsContextIsAggregatedShare\":null,\"displayCommentsContextIsStorySet\":null,\"feedLocation\":\"GROUP\",\"feedbackSource\":69,\"focusCommentID\":null,\"groupID\":null,\"includeNestedComments\":false,\"input\":{\"attachments\":null,\"feedback_id\":\"" + ($"feedback:{idpost}_{idcom}").Base64Encode() + "\",\"formatting_style\":null,\"message\":" + mess + ",\"reply_comment_parent_fbid\":\"" + ($"comment:{idpost}_{idcom}").Base64Encode() + "\",\"reply_target_clicked\":true,\"attribution_id_v2\":\"\",\"is_tracking_encrypted\":true,\"tracking\":[\"\",\"{\\\"assistant_caller\\\":\\\"comet_above_composer\\\",\\\"conversation_guide_session_id\\\":\\\"" + Guid.NewGuid() + "\\\",\\\"conversation_guide_shown\\\":null}\"],\"feedback_source\":\"GROUPS_TARGETED_TAB\",\"idempotence_token\":\"client:" + Guid.NewGuid() + "\",\"session_id\":\"" + Guid.NewGuid() + "\",\"actor_id\":\"" + UID + "\",\"client_mutation_id\":\"10\"},\"inviteShortLinkKey\":null,\"renderLocation\":null,\"scale\":1,\"useDefaultActor\":false,\"UFI2CommentsProvider_commentsKey\":null,\"__relay_internal__pv__StoriesRingrelayprovider\":false}&doc_id=5828809580549986").Result;
            string ketqua = Regex.Match(test, "url\":\"([^\"]+)reply_comment_id=(\\d+)").Value;
            if (!string.IsNullOrEmpty(ketqua)) ketqua = ketqua.Replace("url\":\"", "");
        }
        string jsonencode(string input)
        {
            if (string.IsNullOrEmpty(input)) return "";           
            byte[] bytes = Encoding.UTF8.GetBytes(input);
            input = Encoding.UTF8.GetString(bytes);
            return JsonConvert.SerializeObject(input).TrimEnd('\"').TrimStart('\"');
        }
        string set_tag_uid(string Content)
        {
            Random rd = new Random();
            string cont = jsonencode(Content).Replace("\\r", "").Replace("\\n\\n", "\\n");         
            
            string ketqua = $"message%22%3A%7B%22ranges%22%3A%5B%5D%2C%22text%22%3A%22{cont}%22%7D%2C%22";
            if (Regex.IsMatch(cont, "@[[](\\d+):(.*?)[]]"))
            {
                string tmp = cont;
                int bonus = 0;
                List<string> ranges = new List<string>();
                Regex reg = new Regex("@[[](\\d+):(.*?)[]]");
                Match res = reg.Match(cont);
                do
                {
                    tmp = cont.Replace(res.Value, res.Groups[2].Value);
                    int tmpunicode = Regex.Matches(tmp.Substring(0, res.Index), "\\p{Cs}").Count / 2;
                    tmpunicode += Regex.Matches(Regex.Unescape(tmp.Substring(0, res.Index)), "\\n").Count;
                    ranges.Add("%7B%22entity%22%3A%7B%22id%22%3A%22" + res.Groups[1].Value + "%22%7D%2C%22length%22%3A" + res.Groups[2].Value.ToString().Length + "%2C%22offset%22%3A" + (res.Index - tmpunicode - bonus) + "%7D");
                    bonus += res.Value.Length - res.Groups[2].Value.Length;
                    res = res.NextMatch();
                } while (res != Match.Empty);
                ketqua = "%7B%22ranges%22%3A%5B" + string.Join("%2C", ranges) + $"%5D%2C%22text%22%3A%22{tmp}%22%7D";
            }
            return ketqua;
        }
        public class RequestFB
        {
            public RestClient Client { set; get; }
            private RestClientOptions options;
            private RestRequest request;
            public string LSD { set; get; }
            public string Fb_dtsg { set; get; }
            public string Jazoest { set; get; }
            public string ResponseURI { set; get; }
            public string User { set; get; }
            public string UID { set; get; }
            public string datapost { set; get; }

            private string html;
            public int _Timeout { set; get; } = 60;
            string UserAgentDefaut = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36";

            Random rd = new Random(Guid.NewGuid().GetHashCode());

            public RequestFB(string cookies = null, string userAgent = null, string ProxyUrl = null)
            {
                datapost = "";
                User = "";
                Setoptions(userAgent);
                SetProxy(ProxyUrl);
                Client = new RestClient(options);
                SetCookie(cookies);
            }
            public void SetProxy(string ProxyUrl)
            {
                if (!string.IsNullOrEmpty(ProxyUrl))
                {
                    string[] prox = ProxyUrl.Replace(" ", "").Replace("|", ":").Split(':');
                    string proxyUserName = "";
                    string proxyPassword = "";
                    string proxyHost = prox[0];
                    string proxyPort = "";
                    if (!string.IsNullOrEmpty(proxyHost) && prox.Length >= 2)
                    {
                        proxyPort = prox[1];
                        //var t = new Socks5ProxyClient();
                        var proxy = new WebProxy(proxyHost, int.Parse(proxyPort)); //(new Uri("https://" + proxyHost + ":" + proxyPort));//
                        if (prox.Length > 2)
                        {
                            proxyUserName = prox[2];
                            proxyPassword = prox[3];
                            proxy.Credentials = new NetworkCredential(userName: proxyUserName, password: proxyPassword);
                        }
                        options.Proxy = proxy;
                    }
                }
            }
            private void Setoptions(string userAgent)
            {                             
                if (string.IsNullOrEmpty(userAgent)) userAgent = UserAgentDefaut;                
                if (options == null) options = new RestClientOptions() { MaxTimeout = _Timeout * 1000, CookieContainer = new System.Net.CookieContainer(), UserAgent = userAgent, Encoding = Encoding.UTF8, Expect100Continue = true, MaxRedirects = 100, FollowRedirects = true, ThrowOnAnyError = false, FailOnDeserializationError = false, ThrowOnDeserializationError = false, RemoteCertificateValidationCallback = new RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => { return true; }), PreAuthenticate = true };
            }
            public string _Cookie { set; get; }
            private void SetCookie(string cookies)
            {
                if (string.IsNullOrEmpty(cookies)) cookies = "locale=vi_VN;";
                _Cookie = cookies;
                if (string.IsNullOrEmpty(UID)) UID = Regex.Match(cookies, "c_user=(\\d+)").Groups[1].Value;
                foreach (string coo in cookies.Split(new[] { ";", " " }, StringSplitOptions.RemoveEmptyEntries))
                {
                    if (coo.Contains("useragent=")) continue;
                    if (coo.Contains("i_user")) continue;
                    string[] temp = coo.Split('=');
                    try
                    {
                        if (temp.Length == 2 && !string.IsNullOrEmpty(temp[0]) && !string.IsNullOrEmpty(temp[1])) Client.CookieContainer.Add(new Cookie(temp[0], temp[1], "/", "facebook.com"));

                    }
                    catch { }
                }
            }
            public Task<string> GraphApi(string data)
            {
                var restk = Task.Run(async () =>
                {
                    if (string.IsNullOrEmpty(LSD) && _Cookie != "locale=vi_VN;")
                    {
                        Get("https://www.facebook.com/").Wait();
                    }
                    request = new RestRequest("https://www.facebook.com/api/graphql/"); request.Timeout = -1;
                    request.AddHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
                    request.AddHeader("sec-fetch-site", "same-origin");
                    request.AddHeader("sec-fetch-dest", "empty");
                    request.AddHeader("accept-language", "en-US,en;q=0.9");
                    request.AddHeader("sec-fetch-mode", "cors");
                    request.AddHeader("referer", "https://www.facebook.com/");
                    request.AddHeader("sec-ch-ua-platform", "\"Windows\"");
                    if (!string.IsNullOrEmpty(LSD))
                    {
                        request.AddHeader("x-fb-lsd", LSD);
                        if (string.IsNullOrEmpty(User)) User = UID;
                        if (string.IsNullOrEmpty(datapost) || User != UID) datapost = $"av={User}&__user={UID}&__a=1&__dyn=&__csr=&__req=&__hs=&dpr=&__ccg=&__rev=&__s=&__hsi=&__comet_req=15&fb_dtsg={Fb_dtsg}&jazoest={Jazoest}&lsd={LSD}&__spin_r=&__spin_b=&__spin_t=&fb_api_caller_class=RelayModern";
                    }
                    if (!string.IsNullOrEmpty(data)) data = data.Replace("{Var:UID}", UID);
                    if (!data.Contains("&__user=") && !string.IsNullOrEmpty(UID)) data = datapost + "&" + data.TrimStart('&');
                    else data = data.Replace("{Var:Fb_dtsg}", Fb_dtsg).Replace("{Var:LSD}", LSD).Replace("{Var:Jazoest}", Jazoest);
                    request.AddStringBody(data, "application/x-www-form-urlencoded");
                    request.Method = Method.Post;
                    var res = await Client.ExecuteAsync(request);
                    html = res?.Content;                 
                    return res?.Content;
                });
                return restk;
            }
            public Task<string> Get(string url)
            {
                var restk = Task.Run(async () =>
                {
                    request = new RestRequest(url.Replace("{Var:Fb_dtsg}", Fb_dtsg).Replace("{Var:LSD}", LSD).Replace("{Var:Jazoest}", Jazoest));
                    request.Timeout = -1;
                    request.AddHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
                    request.AddHeader("sec-fetch-site", "same-origin");
                    request.AddHeader("sec-fetch-dest", "empty");
                    request.AddHeader("accept-language", "en-US,en;q=0.9");
                    request.AddHeader("sec-fetch-mode", "cors");
                    request.AddHeader("referer", "https://www.facebook.com/");
                    request.AddHeader("sec-ch-ua-platform", "\"Windows\"");
                    request.Method = Method.Get;
                    var res = await Client.ExecuteAsync(request);

                    try
                    {
                        if (res.ResponseUri != null)
                            ResponseURI = res.ResponseUri.OriginalString;
                    }
                    catch { }
                    if (res.StatusCode != HttpStatusCode.OK)
                    {
                        return res?.Content;
                    }
                    html = res?.Content;
                    if (string.IsNullOrEmpty(LSD))
                    {
                        string temp = res?.Content;
                        if (!string.IsNullOrEmpty(temp))
                        {
                            ResetToken(temp);
                        }
                    }
                    return res?.Content;
                });
                return restk;
            }
            public void ResetToken(string HTML = "")
            {
                if (!string.IsNullOrEmpty(HTML)) html = HTML;
                if (!string.IsNullOrEmpty(html))
                {
                    Fb_dtsg = Regex.Match(html.Replace("\\", string.Empty), "DTSGInitialData\",(.*?),{\"token\":\"(.*?)\"").Groups[2].Value;
                    Jazoest = Regex.Match(html.Replace("\\", string.Empty), "jazoest=(\\d+)").Groups[1].Value;
                    if (string.IsNullOrEmpty(Jazoest)) Jazoest = Regex.Match(html.Replace("\\", string.Empty), "jazoest\",\"version\":2,\"should_randomize\":false},(\\d+)").Groups[1].Value;
                    LSD = Regex.Match(html.Replace("\\", string.Empty), "LSD\",(.*?),{\"token\":\"(.*?)\"").Groups[2].Value;
                }
            }
        }
    }