CI上动态配置是否支持https7.0抓包

转载请注明出处:
http://blog.csdn.net/brucehurrican/article/details/51778319

最近遇到个需求,需要抓取https包,在windows下,主要是通过 fiddler工具抓包,验证服务器返回的数据是否正确。需要注意的地方是,因为google在7.0之后修改了安全权限,对于非CA授权的https证书,需要手动设置
按照官方建议如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
    <application android:networkSecurityConfig="@xml/network_security_config"
                    ... >
        ...
    </application>
</manifest>

res/xml/network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <debug-overrides>
        <trust-anchors>
            <certificates src="@raw/debug_cas"/>
        </trust-anchors>
    </debug-overrides>
</network-security-config>

因为需要在灰度阶段检测,所以只能打 release包。

为了检测这种场景,可以有多种方法,一种是在gradle中配置 buildType 新增一种类型,将上述设置代码放入相应类型下面,具体方法可以参考 android打包之重叠包技术浅谈 。今天我要介绍另一种方法,对于改动文件比较少的时候适用,像上面只需要改动两个文件 (AndroidManifest.xml 和 network_security_config.xml ) 就比较适合。

思路如下:
通过某种方法检测用户当前输入,是否支持7.0 https 抓包,来决定是否修改上述两个文件

代码如下 isCancelSupport7.0Https.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os;
import re;
import sys;

appManifestFileName="app/src/main/AndroidManifest.xml"
appNetworkSecurityConfigFileName="app/src/main/res/xml/network_security_config.xml"
manifestHttpsDebugSupportAfterNougatContent="android:networkSecurityConfig=\"@xml/network_security_config\""
removeSupportContent=""
isRemoveSupportCatchbag=sys.argv[1]==str(True) or (sys.argv[1]==str('true'))
print (sys.argv[1]==str(True)) or (sys.argv[1]==str('true'))
if(isRemoveSupportCatchbag):
    print "移除7.0以后系统可以抓取https功能"
    manifesFile=open(appManifestFileName,"r")
    lines=manifesFile.read()
    manifesFile.close()
    manifesFile=open(appManifestFileName,"w")
    manifesFile.writelines(re.sub(manifestHttpsDebugSupportAfterNougatContent,removeSupportContent,lines))
    manifesFile.close()
    print "AndroidManifest.xml 文件修改完成"
    print "删除抓取7.0功能配置文件..."
    os.remove(appNetworkSecurityConfigFileName)
    print "删除抓取7.0功能配置文件-->完成"
    # for line in manifesFile.readlines():
    #    print line
    # print manifesFile.next()
    # manifesFile.close()
else:
    print "用户开启7.0https抓包功能"

在命令行中输入 python isCancelSupport7.0Https.py false

这样在执行编译命令后,输出的就是支持 7.0https的 release包。

可以将该脚本和jenkins结合,在build 命令前执行,由打包人员动态设置是否需要支持 https 7.0 抓包。

参考资料:

网络安全性配置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值