添加资源http-dns实现

This commit is contained in:
tongchenfei
2020-12-24 20:30:54 +08:00
parent 7653f28a54
commit a96dcf19e0
20 changed files with 338 additions and 103 deletions

1
.idea/gradle.xml generated
View File

@@ -16,6 +16,7 @@
<option value="$PROJECT_DIR$/app2" />
<option value="$PROJECT_DIR$/foudations" />
<option value="$PROJECT_DIR$/foudations/httpdns-base" />
<option value="$PROJECT_DIR$/foudations/httpdns-mogo" />
<option value="$PROJECT_DIR$/foudations/httpdns-noop" />
<option value="$PROJECT_DIR$/foudations/httpdns-tencent" />
<option value="$PROJECT_DIR$/foudations/mogo-base-services-apk" />

View File

@@ -10,6 +10,7 @@ import androidx.appcompat.app.AppCompatActivity;
import com.alibaba.android.arouter.launcher.ARouter;
import com.mogo.commons.network.SubscribeImpl;
import com.mogo.httpdns.HttpDnsConst;
import com.mogo.httpdns.IHttpDnsCallback;
import com.mogo.httpdns.IMogoHttpDns;
import com.mogo.httpdns.MogoHttpDnsHandler;
@@ -76,7 +77,7 @@ public class MainActivity extends AppCompatActivity {
WorkThreadHandler.getInstance().post( new Runnable() {
@Override
public void run() {
httpDns.getHttpDnsIp( "dnstest.zhidaozhixing.com", false, new IHttpDnsCallback() {
httpDns.getHttpDnsIp( "dnstest.zhidaozhixing.com", HttpDnsConst.HTTP_DNS_ADDRESS_TYPE_HTTP, false, new IHttpDnsCallback() {
@Override
public void onParsed( @Nullable String ip ) {
StringBuilder sb = new StringBuilder( "httpDns ip:\n" );
@@ -100,7 +101,7 @@ public class MainActivity extends AppCompatActivity {
WorkThreadHandler.getInstance().post( new Runnable() {
@Override
public void run() {
httpDns.getHttpDnsIp( "dzt-city.zhidaozhixing.com", false, new IHttpDnsCallback() {
httpDns.getHttpDnsIp( "dzt-city.zhidaozhixing.com", HttpDnsConst.HTTP_DNS_ADDRESS_TYPE_HTTP, false, new IHttpDnsCallback() {
@Override
public void onParsed( @Nullable String ip ) {
StringBuilder sb = new StringBuilder( "httpDns ip:\n" );
@@ -121,7 +122,7 @@ public class MainActivity extends AppCompatActivity {
private void renderResponse( ResponseBody body ) {
StringBuilder sb = new StringBuilder( "httpDns ip:\n" );
String ip = httpDns.getCachedHttpDnsIps( "dnstest.zhidaozhixing.com" );
String ip = httpDns.getCachedHttpDnsIps( "dnstest.zhidaozhixing.com", HttpDnsConst.HTTP_DNS_ADDRESS_TYPE_HTTP );
sb.append( ip ).append( "\n" );
sb.append( "\nserverIp: \n" ).append( body.result.serverIp ).append( "\n" );
content.setText( sb.toString() );

View File

@@ -14,4 +14,8 @@ class HttpDnsConst {
@Keep
public static final String PATH = "/httpdns/api";
public static final int HTTP_DNS_ADDRESS_TYPE_HTTP = 0;
public static final int HTTP_DNS_ADDRESS_TYPE_WS = 1;
public static final int HTTP_DNS_ADDRESS_TYPE_IM = 2;
}

View File

@@ -27,36 +27,68 @@ interface IMogoHttpDns extends IProvider {
@Nullable
HttpDns dns();
// /**
// * 获取缓存中的 dns ip地址
// *
// * @param host
// * @return
// */
// @Nullable
// String getCachedHttpDnsIps( String host );
/**
* 获取缓存中的 dns ip地址
*
* @param host
* 获取缓存中的dns ip地址
* @param host domain
* @param type 类型
* @return
*/
@Nullable
String getCachedHttpDnsIps( String host );
String getCachedHttpDnsIps(String host, int type);
//
// /**
// * dns 解析
// *
// * @param host 域名
// * @param useCache 是否使用缓存,是 - 如果没有缓存,则解析新地址、否 - 解析新地址,并将新地址缓存
// * @param callback
// */
// void getHttpDnsIp( String host, boolean useCache, IHttpDnsCallback callback );
/**
* dns 解析
*
* @param host 域名
* @param useCache 是否使用缓存,是 - 如果没有缓存,则解析新地址、否 - 解析新地址,并将新地址缓存
* dns解析
* @param host
* @param type
* @param useCache
* @param callback
*/
void getHttpDnsIp( String host, boolean useCache, IHttpDnsCallback callback );
void getHttpDnsIp(String host, int type, boolean useCache, IHttpDnsCallback callback);
// /**
// * 监听 ttl 回调
// *
// * @param host 域名
// * @param callback
// */
// void addHttpDnsTtlCallback( String host, IHttpDnsTtlCallback callback );
/**
* 监听 ttl 回调
*
* @param host 域名
* 监听ttl回调
* @param host
* @param type
* @param callback
*/
void addHttpDnsTtlCallback( String host, IHttpDnsTtlCallback callback );
void addHttpDnsTtlCallback(String host, int type, IHttpDnsTtlCallback callback);
// /**
// * 注销 ttl 回调
// *
// * @param host 域名
// */
// void removeHttpDnsTtlCallback( String host );
/**
* 注销 ttl 回调
*
* @param host 域名
* 注销ttl回调
* @param host
* @param type
*/
void removeHttpDnsTtlCallback( String host );
void removeHttpDnsTtlCallback(String host,int type);
}

1
foudations/httpdns-mogo/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/build

View File

@@ -0,0 +1,48 @@
apply plugin: 'com.android.library'
android {
compileSdkVersion rootProject.ext.android.compileSdkVersion
// buildToolsVersion rootProject.ext.android.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.android.minSdkVersion
targetSdkVersion rootProject.ext.android.targetSdkVersion
versionCode Integer.valueOf(VERSION_CODE)
versionName getValueFromRootProperties("${project.name.replace("-", "_").toUpperCase()}_VERSION")
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
javaCompileOptions {
annotationProcessorOptions {
arguments = [AROUTER_MODULE_NAME: project.getName()]
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation rootProject.ext.dependencies.arouter
annotationProcessor rootProject.ext.dependencies.aroutercompiler
implementation 'com.mogo.httpdns:httpdns-helper:1.0.3'
if (Boolean.valueOf(RELEASE)) {
implementation rootProject.ext.dependencies.httpdnsbase
implementation rootProject.ext.dependencies.mogocommons
implementation rootProject.ext.dependencies.modulecommon
} else {
implementation project(':foudations:httpdns-base')
implementation project(':foudations:mogo-commons')
implementation project(':modules:mogo-module-common')
}
}
apply from: new File(rootProject.rootDir, "gradle/upload.gradle").toString()

View File

@@ -0,0 +1,3 @@
GROUP=com.mogo.httpdns
POM_ARTIFACT_ID=httpdns-mogo
VERSION_CODE=1

View File

@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

View File

@@ -0,0 +1,26 @@
package com.mogo.httpdns.mogo;
import android.content.Context;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.mogo.httpdns.mogo.test", appContext.getPackageName());
}
}

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mogo.httpdns.mogo">
</manifest>

View File

@@ -0,0 +1,123 @@
package com.mogo.httpdns.mogo;
import android.content.Context;
import android.text.TextUtils;
import android.util.ArrayMap;
import androidx.annotation.Nullable;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.mogo.commons.network.Utils;
import com.mogo.httpdns.HttpDnsConst;
import com.mogo.httpdns.IHttpDnsCallback;
import com.mogo.httpdns.IHttpDnsTtlCallback;
import com.mogo.httpdns.IMogoHttpDns;
import com.mogo.httpdnshelper.sdk.HttpDnsHelper;
import com.mogo.httpdnshelper.sdk.bean.HttpDnsSimpleLocation;
import com.mogo.httpdnshelper.sdk.listener.IHttpDnsConfig;
import com.mogo.httpdnshelper.sdk.listener.OnAddressChangedListener;
import com.mogo.map.location.MogoLocation;
import com.mogo.module.common.MogoApisHandler;
import com.mogo.utils.network.HttpDns;
import org.jetbrains.annotations.NotNull;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static com.mogo.httpdns.HttpDnsConst.HTTP_DNS_ADDRESS_TYPE_HTTP;
@Route( path = HttpDnsConst.PATH )
public class MogoHttpDns implements IMogoHttpDns, HttpDns,OnAddressChangedListener{
private final HttpDnsHelper httpDnsHelper = new HttpDnsHelper();
@Nullable
@Override
public HttpDns dns() {
return this;
}
@Override
public String getCachedHttpDnsIps(String host, int type) {
return httpDnsHelper.getHttpDnsCachedAddress(type, host);
}
@Override
public void getHttpDnsIp(String host, int type, boolean useCache, IHttpDnsCallback callback) {
if(useCache) {
String address = httpDnsHelper.getHttpDnsCachedAddress(type, host);
if (address != null) {
callback.onParsed(address);
}else{
callback.onParsed(httpDnsHelper.getHttpDnsAddress(type, host));
}
}else {
callback.onParsed(httpDnsHelper.getHttpDnsAddress(type, host));
}
}
private final Map<String, IHttpDnsTtlCallback> ttlCallbackMap = new ArrayMap<>();
@Override
public void addHttpDnsTtlCallback(String host, int type, IHttpDnsTtlCallback callback) {
ttlCallbackMap.put(type + "-" + host, callback);
}
@Override
public void removeHttpDnsTtlCallback(String host, int type) {
ttlCallbackMap.remove(type + "-" + host);
}
@Override
public void init(final Context context) {
httpDnsHelper.init(context, new IHttpDnsConfig() {
@NotNull
@Override
public HttpDnsSimpleLocation getCurrentLocation() {
MogoLocation last = MogoApisHandler.getInstance().getApis().getMapServiceApi().getSingletonLocationClient(context).getLastKnowLocation();
if(last!=null){
return new HttpDnsSimpleLocation(last.getCityCode(), last.getLatitude(), last.getLongitude());
}
return null;
}
@Override
public boolean showDebugLog() {
return true;
}
@NotNull
@Override
public String getSn() {
return Utils.getSn();
}
});
httpDnsHelper.setAddressChangedListener(this);
}
@Override
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
String cacheIp = httpDnsHelper.getHttpDnsCachedAddress(HTTP_DNS_ADDRESS_TYPE_HTTP, hostname);
if ( cacheIp == null || TextUtils.isEmpty( cacheIp ) ) {
return Collections.emptyList();
}
String[] info = cacheIp.split(":");
if (info.length > 1) {
return Arrays.asList( InetAddress.getAllByName( info[0]) );
}else {
return Arrays.asList(InetAddress.getAllByName(cacheIp));
}
}
@Override
public void onAddressChanged(@org.jetbrains.annotations.Nullable Map<String, String> map) {
if (map != null) {
for (String key : map.keySet()) {
ttlCallbackMap.get(key).onTtl();
}
}
}
}

View File

@@ -0,0 +1,17 @@
package com.mogo.httpdns.mogo;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
}

View File

@@ -19,6 +19,9 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static com.mogo.httpdns.HttpDnsConst.HTTP_DNS_ADDRESS_TYPE_IM;
import static com.mogo.httpdns.HttpDnsConst.HTTP_DNS_ADDRESS_TYPE_WS;
public
/**
* @author congtaowang
@@ -50,8 +53,13 @@ class TencentHttpDns implements IMogoHttpDns, HttpDns {
}
@Override
public String getCachedHttpDnsIps( String host ) {
return HttpDnsIpsCache.getHttpDnsIps( host );
public String getCachedHttpDnsIps( String host,int type ) {
String ip = HttpDnsIpsCache.getHttpDnsIps(host);
if (ip == null) {
return null;
} else {
return tailPort(ip, type);
}
}
private String getCacheOrParseIpIfNecessary( String host ) {
@@ -66,7 +74,7 @@ class TencentHttpDns implements IMogoHttpDns, HttpDns {
}
@Override
public void getHttpDnsIp( String host, boolean useCache, IHttpDnsCallback callback ) {
public void getHttpDnsIp( String host,int type, boolean useCache, IHttpDnsCallback callback ) {
String cacheIp = null;
if ( useCache ) {
cacheIp = getCacheOrParseIpIfNecessary( host );
@@ -78,7 +86,7 @@ class TencentHttpDns implements IMogoHttpDns, HttpDns {
}
if ( !TextUtils.isEmpty( cacheIp ) ) {
if ( callback != null ) {
callback.onParsed( cacheIp );
callback.onParsed( tailPort(cacheIp,type) );
return;
}
}
@@ -88,12 +96,12 @@ class TencentHttpDns implements IMogoHttpDns, HttpDns {
}
@Override
public void addHttpDnsTtlCallback( String host, IHttpDnsTtlCallback callback ) {
public void addHttpDnsTtlCallback( String host,int type, IHttpDnsTtlCallback callback ) {
HttpDnsIpsCache.addHttpDnsTtlCallback( host, callback );
}
@Override
public void removeHttpDnsTtlCallback( String host ) {
public void removeHttpDnsTtlCallback( String host,int type ) {
HttpDnsIpsCache.removeHttpDnsTtlCallback( host );
}
@@ -113,4 +121,15 @@ class TencentHttpDns implements IMogoHttpDns, HttpDns {
HttpDnsIpsCache.loopDnsParse();
sInited = true;
}
private String tailPort(String ip, int type) {
switch (type) {
case HTTP_DNS_ADDRESS_TYPE_WS:
return ip + ":4003";
case HTTP_DNS_ADDRESS_TYPE_IM:
return ip + ":4002";
default:
return ip;
}
}
}

View File

@@ -9,8 +9,9 @@ public class WebSocketConstant {
private static final String HOST_QA = "ws://62.234.196.121";
private static final String HOST_DEMO = "ws://62.234.196.121";
private static final String HOST_RELEASE = "ws://62.234.196.121";
// private static final String PORT = ":14001/ws";
public static final String PORT = ":4004/ws";
// private static final String PORT = ":14001/ws";
public static final String PATH = "/ws";
public static final String PORT = ":4004"+PATH;
public static final String PROTOCOL = "ws://";
private static final String DOMAIN_DEV = "dzt-city.zhidaozhixing.com";

View File

@@ -1,6 +1,7 @@
package com.mogo.base.websocket;
import com.alibaba.android.arouter.launcher.ARouter;
import com.mogo.httpdns.HttpDnsConst;
import com.mogo.httpdns.IMogoHttpDns;
import com.mogo.utils.ThreadPoolService;
import com.mogo.utils.logger.Logger;
@@ -27,22 +28,22 @@ public class WebSocketDnsManager {
public void getHttpDnsIp(WebSocketDns webSocketDns) {
this.webSocketDns = webSocketDns;
IMogoHttpDns mogoHttpDns = ARouter.getInstance().navigation(IMogoHttpDns.class);
ThreadPoolService.execute(() -> mogoHttpDns.getHttpDnsIp(WebSocketConstant.getDomain(), false, ip -> {
ThreadPoolService.execute(() -> mogoHttpDns.getHttpDnsIp(WebSocketConstant.getDomain(), HttpDnsConst.HTTP_DNS_ADDRESS_TYPE_WS,false, ip -> {
Logger.d(TAG, "getHttpDnsIp ip : " + ip + " , 得到Dns IP,准备回调 初始化webSocket");
this.cacheIp = ip;
this.webSocketDns.getDnsIp((cacheIp != null ?
WebSocketConstant.PROTOCOL + ip + WebSocketConstant.PORT :
WebSocketConstant.PROTOCOL + ip + WebSocketConstant.PATH :
WebSocketConstant.getSocketServer() + WebSocketConstant.PORT));
}));
mogoHttpDns.addHttpDnsTtlCallback(WebSocketConstant.getDomain(), () -> {
mogoHttpDns.addHttpDnsTtlCallback(WebSocketConstant.getDomain(), HttpDnsConst.HTTP_DNS_ADDRESS_TYPE_WS, () -> {
Logger.d(TAG, "ttl callBack ,ready to getCache Dns IP");
String dnsCacheIp = mogoHttpDns.getCachedHttpDnsIps(WebSocketConstant.getDomain());
String dnsCacheIp = mogoHttpDns.getCachedHttpDnsIps(WebSocketConstant.getDomain(),HttpDnsConst.HTTP_DNS_ADDRESS_TYPE_WS);
if (dnsCacheIp == null) {
return;
}
Logger.d(TAG, "获取缓存Dns IP : " + dnsCacheIp + " , 原缓存 IP " + cacheIp);
if (cacheIp != null && !cacheIp.equals(dnsCacheIp)) {
this.webSocketDns.ttlIp(dnsCacheIp + WebSocketConstant.PORT);
this.webSocketDns.ttlIp(dnsCacheIp + WebSocketConstant.PATH);
}
});
}

View File

@@ -34,7 +34,6 @@ dependencies {
implementation rootProject.ext.dependencies.androidxconstraintlayout
implementation rootProject.ext.dependencies.arouter
implementation rootProject.ext.dependencies.callchatprovider
implementation "com.mogo.httpdns:httpdns-helper:1.0.1"
if (Boolean.valueOf(RELEASE)) {
api rootProject.ext.dependencies.mogomap
api rootProject.ext.dependencies.mogomapapi

View File

@@ -1,64 +0,0 @@
package com.mogo.module.common.utils;
import android.content.Context;
import com.mogo.commons.network.Utils;
import com.mogo.httpdnshelper.sdk.HttpDnsHelper;
import com.mogo.httpdnshelper.sdk.IHttpDnsConfig;
import com.mogo.httpdnshelper.sdk.bean.HttpDnsSimpleLocation;
import com.mogo.map.location.MogoLocation;
import com.mogo.module.common.MogoApisHandler;
import org.jetbrains.annotations.NotNull;
/**
* MogoHttpDnsHelper包装类
*
* @author tongchenfei
*/
public class MogoHttpDnsUtil {
private MogoHttpDnsUtil(){
}
private final static MogoHttpDnsUtil INSTANCE = new MogoHttpDnsUtil();
public static MogoHttpDnsUtil getInstance(){
return INSTANCE;
}
public void init(Context context){
HttpDnsHelper.INSTANCE.init(context, new IHttpDnsConfig() {
@NotNull
@Override
public HttpDnsSimpleLocation getCurrentLocation() {
MogoLocation last = MogoApisHandler.getInstance().getApis().getMapServiceApi().getSingletonLocationClient(context).getLastKnowLocation();
if(last!=null){
return new HttpDnsSimpleLocation(last.getCityCode(), last.getLatitude(), last.getLongitude());
}
return null;
}
@Override
public boolean showDebugLog() {
return true;
}
@NotNull
@Override
public String getSn() {
return Utils.getSn();
}
});
}
public String getHttpAddress(){
return HttpDnsHelper.INSTANCE.getHttpDnsAddress(HttpDnsHelper.HTTP_DNS_TYPE_HTTP);
}
public String getWsAddress(){
return HttpDnsHelper.INSTANCE.getHttpDnsAddress(HttpDnsHelper.HTTP_DNS_TYPE_WS);
}
public void release(){
HttpDnsHelper.INSTANCE.release();
}
}

View File

@@ -12,7 +12,6 @@ import com.mogo.map.location.IMogoLocationClient;
import com.mogo.map.location.IMogoLocationListener;
import com.mogo.map.location.MogoLocation;
import com.mogo.module.common.MogoApisHandler;
import com.mogo.module.common.utils.MogoHttpDnsUtil;
import com.mogo.module.main.EventDispatchCenter;
import com.mogo.module.main.cards.MogoModulesManager;
import com.mogo.module.main.delaycheck.DelayCheckUtil;
@@ -53,8 +52,6 @@ class MogoMainService extends Service implements IMogoLocationListener {
// 开启延时检测
DelayCheckUtil delayCheckUtil = new DelayCheckUtil(this);
delayCheckUtil.waitingForCheck();
MogoHttpDnsUtil.getInstance().init(this);
}
@Nullable
@@ -111,6 +108,5 @@ class MogoMainService extends Service implements IMogoLocationListener {
mLocationClient = null;
}
mServiceApis = null;
MogoHttpDnsUtil.getInstance().release();
}
}

View File

@@ -1,3 +1,4 @@
include ':foudations:httpdns-mogo'
include ':modules:mogo-module-smp'
include ':app2'
include ':foudations:httpdns-base'