V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
imes
V2EX  ›  问与答

求解 golang 访问 cloudflare CDN 的 cookies 问题

  •  
  •   imes · 299 天前 · 662 次点击
    这是一个创建于 299 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有一个使用 cloudflare 的 CDN 进行保护的网站,想访问的时候,需要在 cookies 中附带session__cf_bm数据。
    之前使用 Python requests ,设置如下,可以正常抓取数据。

    cookie = {
        'session': SESSION,
        '__cf_bm': CF_BM
    }
    response = requests.post(url, cookies=cookie)
    

    最近想使用 golang 重写,代码如下,结果发现页面反馈Please enable cookies,直接被 cloudflare 拦截,无法正常访问。

    cookie := cookies{
    	session: session,
    	__cf_bm: CF_BM,
    }
    req.Header.Add("Cookie", "session="+cookie.session+"; theme="+cookie.theme+"; __cf_bm="+cookie.__cf_bm)
    

    第一反应以为是代码写错了,用`https://httpbin.org/cookies`作为 url ,发现反馈的 cookies 正常,内容如下。结果对比,和 python 的反馈值一直,无区别。
    {
      "cookies": {
        "__cf_bm": "ehOUuRclb1sJgFygWWcSli", 
        "session": "7586800e10"
      }
    }
    

    随后在 stackoverflow 上找了了类似案例,[Python requests works fine, when trying same request in golang is not working as expected]( https://stackoverflow.com/questions/76503246/python-requests-works-fine-when-trying-same-request-in-golang-is-not-working-as)

    目前状况

    只能猜测 clouflare 对 golang 进行了额外的限制,学艺不精者使用 golang 访问就会失败。
    想问问 V2 上有没有同行遇到了类似现象,泪目了~

    4 条回复    2023-07-04 13:43:51 +08:00
    abersheeran
        1
    abersheeran  
       299 天前   ❤️ 1
    这不是老问题了吗。golang 标准库的 TLS 握手有特征,cloudflare 针对这个做了屏蔽。
    imes
        2
    imes  
    OP
       299 天前 via Android
    @abersheeran 1# 感谢!原来如此!我 golang 纯新手,还不知道这个,再次感谢!
    abersheeran
        3
    abersheeran  
       299 天前
    @imes 不客气。
    Dreax
        4
    Dreax  
       299 天前   ❤️ 1
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5392 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 09:31 · PVG 17:31 · LAX 02:31 · JFK 05:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.