add new func of httpdns switch city to fit socketserver

This commit is contained in:
zhongchao
2021-07-30 17:22:37 +08:00
100 changed files with 1985 additions and 2080 deletions

View File

@@ -1,5 +1,8 @@
package com.mogo.commons;
import static com.mogo.commons.debug.DebugConfig.CAR_MACHINE_TYPE_SELF_INNOVATE;
import static com.mogo.httpdns.HttpDnsConst.HTTP_DNS_ADDRESS_TYPE_HTTP;
import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
@@ -11,7 +14,8 @@ import android.widget.ImageView;
import android.widget.TextView;
import com.alibaba.android.arouter.launcher.ARouter;
import com.amap.api.maps.TextureMapView;
import com.amap.api.navi.AMapNaviView;
import com.mogo.cloud.httpdns.MogoHttpDnsClient;
import com.mogo.commons.analytics.AnalyticsUtils;
import com.mogo.commons.debug.DebugConfig;
import com.mogo.commons.device.Devices;
@@ -20,7 +24,6 @@ import com.mogo.commons.network.Constants;
import com.mogo.commons.network.ParamsUtil;
import com.mogo.commons.network.X509TrustManagerImpl;
import com.mogo.commons.storage.SpStorage;
import com.mogo.httpdns.HttpDnsConst;
import com.mogo.httpdns.IMogoHttpDns;
import com.mogo.httpdns.MogoHttpDnsHandler;
import com.mogo.utils.ThreadPoolService;
@@ -59,30 +62,30 @@ public abstract class AbsMogoApplication extends Application {
return sApp;
}
public static TextureMapView aMapNaviView;
public static AMapNaviView aMapNaviView;
public static TextureMapView getMapNaviView() {
public static AMapNaviView getMapNaviView() {
return aMapNaviView;
}
@Override
public void onCreate() {
super.onCreate();
aMapNaviView = new TextureMapView(this);
aMapNaviView = new AMapNaviView(this);
aMapNaviView.onCreate(null);
// 在设置皮肤布局填充器之前进行克隆一个出来
mLayoutInflaterNoSkin = LayoutInflater.from(new ContextThemeWrapper(this, R.style.Theme_AppCompat)).cloneInContext(new ContextThemeWrapper(this, R.style.Theme_AppCompat));
sApp = this;
initARouter();
if ( shouldInit() ) {
if (shouldInit()) {
init();
}
}
private void initARouter() {
ARouter.init( sApp );
ARouter.init(sApp);
// 初始化 arouter
if ( DebugConfig.isDebug() ) {
if (DebugConfig.isDebug()) {
ARouter.openDebug();
ARouter.openLog();
}
@@ -97,26 +100,26 @@ public abstract class AbsMogoApplication extends Application {
}
protected void initTipToast() {
TipToast.init( this, ( ( context, message, tipDrawable ) -> {
if ( TextUtils.isEmpty( message ) ) {
TipToast.init(this, ((context, message, tipDrawable) -> {
if (TextUtils.isEmpty(message)) {
return null;
}
try {
View contentView = generateToastView( context, message, tipDrawable );
View contentView = generateToastView(context, message, tipDrawable);
return contentView;
} catch ( Exception e ) {
} catch (Exception e) {
}
return null;
} ) );
}));
}
private View generateToastView( Context context, String message, TipDrawable tipDrawable){
private View generateToastView(Context context, String message, TipDrawable tipDrawable) {
View contentView;
if(tipDrawable==null) {
if (tipDrawable == null) {
contentView = LayoutInflater.from(context).inflate(R.layout.module_commons_layout_toast, null);
TextView txt = contentView.findViewById(R.id.module_commons_toast_msg);
txt.setText(message);
}else{
} else {
// 有图片,使用带图片的布局,当前只实现了左侧图片
contentView = LayoutInflater.from(context).inflate(R.layout.module_commons_layout_toast_with_left_drawable, null);
TextView txt = contentView.findViewById(R.id.module_commons_toast_msg);
@@ -138,20 +141,22 @@ public abstract class AbsMogoApplication extends Application {
*/
private static SSLContext getSslContext() throws Exception {
SSLContext sc = null;
sc = SSLContext.getInstance( "SSL" );
sc.init( null, new TrustManager[]{new X509TrustManagerImpl()}, new SecureRandom() );
sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[]{new X509TrustManagerImpl()}, new SecureRandom());
return sc;
}
protected void asyncInit() {
ThreadPoolService.execute( () -> {
ThreadPoolService.execute(() -> {
initNetConfig();
// 初始化埋点
AnalyticsUtils.init(sApp);
Devices.init( getApp() );
Devices.checkBindState();
if (DebugConfig.getCarMachineType() == CAR_MACHINE_TYPE_SELF_INNOVATE) {
Devices.init(getApp());
Devices.checkBindState();
}
asyncInitImpl();
} );
});
}
protected void asyncInitImpl() {
@@ -162,35 +167,55 @@ public abstract class AbsMogoApplication extends Application {
try {
SSLContext sc = getSslContext();
NetConfig.instance().setSslContext( sc );
} catch ( Exception e ) {
NetConfig.instance().setSslContext(sc);
} catch (Exception e) {
}
IMogoHttpDns dns = MogoHttpDnsHandler.getHttpDnsApi();
if(dns == null){
Logger.d(TAG,"dns is null");
if (dns == null) {
Logger.d(TAG, "dns is null");
}
NetConfig.instance().setSignaturePrefix( Constants.SIGN_PREFIX )
.setPublicParams( ParamsUtil.getStaticParams() )
.setHostnameVerifier( new AllAllowedHostnameVerifier() )
.addNetworkInterceptor( chain -> {
NetConfig.instance().setSignaturePrefix(Constants.SIGN_PREFIX)
.setPublicParams(ParamsUtil.getStaticParams())
.setHostnameVerifier(new AllAllowedHostnameVerifier())
.addNetworkInterceptor(chain -> {
Request original = chain.request();
Request request = original.newBuilder()
.header( "token", SpStorage.getTicket() )
.method( original.method(), original.body() )
.header("token", SpStorage.getTicket())
.method(original.method(), original.body())
.build();
return chain.proceed( request );
} )
return chain.proceed(request);
})
// 增加域名->域名的转换方式暂时去掉httpdns方式
.addInterceptor(chain -> {
Request request = chain.request();
String path = request.url().encodedPath();
String host = "http://" + dns.syncGetHttpDns(request.url().host().replace("http://", "").replace("https://", ""), HttpDnsConst.HTTP_DNS_ADDRESS_TYPE_HTTP, true);
String host = "http://" + dns.syncGetHttpDns(request.url().host().replace("http://", "").replace("https://", ""), HTTP_DNS_ADDRESS_TYPE_HTTP, true);
String url = host + path;
Logger.d("DomainExchange", "oriHost: " + request.url().host() + " newHost: " + host+" \r\n newUrl: "+url);
Logger.d("DomainExchange", "oriHost: " + request.url().host() + " newHost: " + host + " \r\n newUrl: " + url);
return chain.proceed(request.newBuilder().url(url).build());
})
.setHttpDns( null )
.setLoggable( DebugConfig.isDebug() );
.setHttpDns(null)
.setLoggable(DebugConfig.isDebug());
}
private String cacheIp = null;
protected void registerSocketHttpDnsTTL(String host) {
IMogoHttpDns mogoHttpDns = MogoHttpDnsHandler.getHttpDnsApi();
mogoHttpDns.addressChangedListener(map -> {
Logger.d("TEST-SOCKET", "ttl callBack ,ready to getCache Dns IP");
String dnsCacheIp = mogoHttpDns.getCachedHttpDnsIps(host, HTTP_DNS_ADDRESS_TYPE_HTTP);
if (dnsCacheIp == null) {
return;
}
Logger.d("TEST-SOCKET", "获取缓存Dns IP : " + dnsCacheIp + " , 原缓存 IP " + cacheIp);
if (!dnsCacheIp.equals(cacheIp)) {
socketTTL();
this.cacheIp = dnsCacheIp;
}
});
}
protected abstract void socketTTL();
}

View File

@@ -58,7 +58,7 @@ public abstract class MvpActivity< V extends IView, P extends Presenter< V > >
try {
if (DebugConfig.getCarMachineType() == DebugConfig.CAR_MACHINE_TYPE_LENOVO) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.zhidao.speech.voice.pad",
intent.setComponent(new ComponentName("com.zhidao.speech",
"com.zhidao.speech.MainActivity"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);