第一种方式 启动浏览器添加命令行
格式 --user-agent="自定义的UA"
1、C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe --user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 jdtest"
2、启动浏览器f12查看效果
可以看到 net render 和主进程以及f12开发者工具中 都已经追加成功自定义ua。
原因是 翻看源码【本例子122核】可以看到每次取UA优先从命令行,然后是默认UA
// A string used to override the default user agent with a custom one.
const char kUserAgent[] = "user-agent";
components\embedder_support\user_agent_utils.cc
absl::optional<std::string> GetUserAgentFromCommandLine() {
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(kUserAgent)) {
std::string ua = command_line->GetSwitchValueASCII(kUserAgent);
if (net::HttpUtil::IsValidHeaderValue(ua)) {
return ua;
}
LOG(WARNING) << "Ignored invalid value for flag --" << kUserAgent;
}
return absl::nullopt;
}
std::string GetUserAgent(
UserAgentReductionEnterprisePolicyState user_agent_reduction) {
//从命令行获取UA
absl::optional<std::string> custom_ua = GetUserAgentFromCommandLine();
if (custom_ua.has_value()) {
return custom_ua.value();
}
//获取默认UA
return GetUserAgentInternal(user_agent_reduction);
}
第二种方法如下:
1、在components\embedder_support\user_agent_utils.cc和content\common\user_agent.cc中直接修改源码:
1、components\embedder_support\user_agent_utils.cc
// Internal function to handle return the full or "reduced" user agent string,
// depending on the UserAgentReduction enterprise policy.
std::string GetUserAgentInternal(
UserAgentReductionEnterprisePolicyState user_agent_reduction) {
std::string product = GetProductAndVersion(user_agent_reduction);
if (base::CommandLine::ForCurrentProcess()->HasSwitch(kHeadless)) {
product.insert(0, "Headless");
}
#if BUILDFLAG(IS_ANDROID)
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kUseMobileUserAgent))
product += " Mobile";
#endif
// In User-Agent reduction phase 5, only apply the <unifiedPlatform> to
// desktop UA strings.
// In User-Agent reduction phase 6, only apply the <unifiedPlatform> to
// android UA strings.
return ShouldSendUserAgentUnifiedPlatform(user_agent_reduction)
? content::BuildUnifiedPlatformUserAgentFromProduct(product)
: content::BuildUserAgentFromProduct(product);
}
absl::optional<std::string> GetUserAgentFromCommandLine() {
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(kUserAgent)) {
std::string ua = command_line->GetSwitchValueASCII(kUserAgent);
if (net::HttpUtil::IsValidHeaderValue(ua)) {
return ua;
}
LOG(WARNING) << "Ignored invalid value for flag --" << kUserAgent;
}
return absl::nullopt;
}
//主进程 net进程 和render进程都会调用此函数,稍后看调试堆栈
std::string GetUserAgent(
UserAgentReductionEnterprisePolicyState user_agent_reduction) {
absl::optional<std::string> custom_ua = GetUserAgentFromCommandLine();
if (custom_ua.has_value()) {
return custom_ua.value();
}
return GetUserAgentInternal(user_agent_reduction);
}
2、content::BuildUserAgentFromProduct函数实现在
content\common\user_agent.cc
std::string BuildUserAgentFromProduct(const std::string& product) {
std::string os_info;
base::StringAppendF(&os_info, "%s%s", GetUserAgentPlatform().c_str(),
BuildOSCpuInfo(IncludeAndroidBuildNumber::Exclude,
IncludeAndroidModel::Include)
.c_str());
return BuildUserAgentFromOSAndProduct(os_info, product);
}
//此处代码是否有种似曾相识感觉,其他的自行看源码吧。
std::string BuildUserAgentFromOSAndProduct(const std::string& os_info,
const std::string& product) {
// Derived from Safari's UA string.
// This is done to expose our product name in a manner that is maximally
// compatible with Safari, we hope!!
std::string user_agent;
base::StringAppendF(&user_agent,
"Mozilla/5.0 (%s) AppleWebKit/537.36 (KHTML, like Gecko) "
"%s Safari/537.36",
os_info.c_str(), product.c_str());
return user_agent;
}
2、看下调用GetUserAgent的堆栈
1)、地址栏输入www.baidu.com render进程调用GetUserAgent初始化UA
2)、我将UA追加 jdtest 编译看下效果:
至此:两种方法定义UA已经介绍完毕,至于动态定义UA读者可自行实现。