The development of mobile apps and H5 applications often need to separate testing on the local and online environment, generally think of the operation is to modify the hosts, I have been doing so, but the mobile phone to modify the hosts is required root authority, so too troublesome, there is the risk of changing bricks, and some phones simply can not root.
Busy and an afternoon, and finally fully achieve the expected, a little harvest, the following is said to focus, mainly two:
- Local DNS Service
- Network Agent
Do a local DNS service, a bit like the sense of DNS hijacking, this is the way to once and for all
First, you need a Linux virtual machine or a server
My following configuration uses the Centos7.0 system, the version may be somewhat different.
First, install the DNSMASQ
# Install Yum install dnsmasq-y service dnsmasq start# Edit configuration Parameters vi/etc/dnsmasq.conf# Find Resolv-file Remove Comment, add File path # indicates that DNSMASQ will be from this Locate the upstream DNS server in the specified file resolv-file=/etc/resolv.conf# find no-hosts, make sure that there is a # sign in front (indicating that it is commented out), otherwise the hosts configuration does not work # listener iplisten-address= 127.0.0.1,192.168.0.103strict-orderaddn-hosts=/etc/hostscache-size=32768# configuration to the network card parameters, CENTOS7 and 6 of the network card name is different, with ifconfig View Replacement # CentOS 7 before vi/etc/sysconfig/network-scripts/ifcfg-eth0# Centos7 not necessarily the same vi/etc/sysconfig/network-scripts/ ifcfg-enp3s0# can only write a maximum of 3, note that the original configuration if there is a need to adjust dns1=127.0.0.1dns2=192.168.0.103dns3=202.101.172.35#dns3= 202.101.172.47# after saving, resolv.conf file contents will be updated automatically cat/etc/resolv.conf# see if DNS has been added # Registration service chkconfig DNSMASQ on# Restart/etc/init.d/ DNSMASQ restart# centos7 command systemctl Restart dnsmasq# Check for successful installation netstat-tunlp|grep 53yum install bind-utils# test whether DNS is enabled Configure the Dig m.ai9475.com# edit Hosts file to configure the local DNS hijacking domain name IP mappings vi/etc/hosts192.168.0.105 m.ai9475.com
In this way, the server's DNS hijacking is basically configured , the next step is to set up the DNS corresponding IP on the phone,
Open Network settings, Wi-Fi connection, modify network, advanced, static IP mode, modify the first NS IP, the second available normal public network DNS
Such as:
Last Note: After you modify the hosts, you need to restart the DNSMASQ and the network is best to restart.
Second, the use of local network agent
This method is more limited, the H5 Web application can also, for some of the native app may not be valid oh, but there is a good thing is to cooperate with some software can do the front-end capture of the package view, test interface and so on to view data is useful
I've used two software,
1. Fiddler
This software is very convenient to use, there are many introductions on the Internet, this is not said, but is said to support only the Windows platform
2. Whistle
This is open source project, cross-platform, I am using this now, configuration is very simple
The following is simply how to install the use
Rely on Nodejs, so install Nodejs first
I have a good picture here, and I installed it in Yum's way.
Curl--silent--location https://rpm.nodesource.com/setup_4.x | Bash-yum install-y Nodejs
Download speed may be a little busy, 10kb/s downloaded long time just finished, patience, etc.
NPM Install cnpm-g--registry=https://registry.npm.taobao.orgcnpm install-g whistlew2 start
This completes the ...
To view the proxy request data is to grab the packet data
http://127.0.0.1:8900
http://127.0.0.1:8899
: 8899 is the port of the agent, but can also view the packet capture data
Two ports can be seen, see the effect first (image can be dragged directly to see the larger image)
The other is directly read the official documentation, Chinese, very clear
Https://avwo.github.io/whistle/install.html
The following is the phone on the operation and the above DNS settings are the same place
Open Network settings, WiFi connection, modify network, Advanced Agent (manual configuration), complete the IP of the server in the LAN , such as 192.168. 0. 103, Port 8899
Such as:
The last is to modify the hosts on the server, refer to the above DNS configuration hosts operation is the same
It's almost all set up.
The last thing to note is to clear the cache on the phone, be sure to close the browser and app, clean up the memory and then open, or the phone's DNS and network settings may not be switched over.
The above configuration I have been tested on Android devices, IOS is not applied to test, but it should be said to be the same effect.
Although the proxy method is not necessarily useful for some apps, it is recommended that DNS and proxies come together ... That's it.
You can test your Android, Apple app, and H5 apps locally without having to root and change hosts.