V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
cmos
V2EX  ›  问与答

求解 golang 访问 cloudflare CDN 的 cookies 问题

  •  
  •   cmos · Jul 4, 2023 · 1493 views
    This topic created in 1038 days ago, the information mentioned may be changed or developed.

    有一个使用 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 replies    2023-07-04 13:43:51 +08:00
    abersheeran
        1
    abersheeran  
       Jul 4, 2023   ❤️ 1
    这不是老问题了吗。golang 标准库的 TLS 握手有特征,cloudflare 针对这个做了屏蔽。
    cmos
        2
    cmos  
    OP
       Jul 4, 2023 via Android
    @abersheeran 1# 感谢!原来如此!我 golang 纯新手,还不知道这个,再次感谢!
    abersheeran
        3
    abersheeran  
       Jul 4, 2023
    @imes 不客气。
    Dreax
        4
    Dreax  
       Jul 4, 2023   ❤️ 1
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   935 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 20:30 · PVG 04:30 · LAX 13:30 · JFK 16:30
    ♥ Do have faith in what you're doing.