Skip to content

Instantly share code, notes, and snippets.

@the-eric-kwok
Last active March 22, 2023 05:46
Show Gist options
  • Save the-eric-kwok/98e7a610aa629d493686acf99b6ad91d to your computer and use it in GitHub Desktop.
Save the-eric-kwok/98e7a610aa629d493686acf99b6ad91d to your computer and use it in GitHub Desktop.
在任意浏览器上启用新必应

首先需要安装 mitmproxy

在 7890 端口架设好一个外网的 http 代理

接着将以下内容写入 mitm_new_bing.md

#!/usr/bin/env python3
# encoding: utf-8

import logging


class AnyBrowserAsEdge:
    def request(self, flow):
        if "bing.com" in flow.request.host:
            flow.request.headers["User-Agent"] = \
                "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) " + \
                "Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.41"


class RemoveChineseBingCookie:
    def request(self, flow):
        if "bing.com" in flow.request.host:
            if "Cookie" in flow.request.headers and "&mkt=zh-CN" in flow.request.headers["Cookie"]:
                cookie_before = flow.request.headers["Cookie"]
                flow.request.headers["Cookie"] = flow.request.headers["Cookie"].replace("&mkt=zh-CN", "")
                if (cookie_before != flow.request.headers["Cookie"]):
                    logging.info(f"Cookie after: '{flow.request.headers['Cookie']}'")


addons = [
    AnyBrowserAsEdge(),
    RemoveChineseBingCookie()
]

在终端中运行

mitmdump -s ./mitm_new_bing.py --mode upstream:http://localhost:7890

最后将系统代理配置到 http://localhost:8080 就可以使用了,mitmproxy 的默认代理端口为 8080,如需修改请参照文档,此处略。

你还可以写一个服务来让 launchd 或 systemd 管理自启动之类的,此处略。

效果: image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment