Skip to content

Commit 84a4229

Browse files
committed
fix bug for request tag
1 parent 66dce7f commit 84a4229

File tree

11 files changed

+76
-35
lines changed

11 files changed

+76
-35
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ dependencies {
3434
implementation 'androidx.core:core-ktx:1.3.2'
3535
implementation 'androidx.appcompat:appcompat:1.2.0'
3636
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
37+
3738
testImplementation 'junit:junit:4.12'
3839
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
3940
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

app/src/main/java/com/forgetsky/domainhttptest/net/RetrofitManager.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.forgetsky.domainhttptest.net
22

33
import android.util.Log
4+
import com.forgetsky.domainhttp.BuildConfig
45
import com.forgetsky.domainhttp.dns.CacheDns
56
import com.forgetsky.domainhttp.dns.DnsRetryInterceptor
67
import com.forgetsky.domainhttp.domain.Domain
@@ -104,8 +105,8 @@ object RetrofitManager {
104105
}
105106
}, object : LoggingInterceptor.DecryptAdapter {
106107
override fun decrypt(chain: Interceptor.Chain, inputStream: InputStream): String {
107-
if (isEncryptInterface(chain.request()) &&
108-
!isHandShakeInterface(chain.request())) {
108+
if (chain.request().isEncryptInterface() &&
109+
!chain.request().isHandShakeInterface()) {
109110
return try {
110111
val domainPolicy: IDomainPolicy? =
111112
DomainManager.getDomainPolicy(chain.request())

app/src/main/java/com/forgetsky/domainhttptest/net/domain1/EncryptUtil.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ internal object EncryptUtil {
6868
*/
6969
fun decryptResultForLog(result: ByteArray?): String {
7070
try {
71-
decryptResult(result)
72-
return String()
71+
return decryptResult(result)
7372
} catch (e: Exception) {
7473
Log.e("decryptResultForLog", "e:" + e.message)
7574
}

app/src/main/java/com/forgetsky/domainhttptest/net/domain2/EncryptUtil.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ internal object EncryptUtil {
6868
*/
6969
fun decryptResultForLog(result: ByteArray?): String {
7070
try {
71-
decryptResult(result)
72-
return String()
71+
return decryptResult(result)
7372
} catch (e: Exception) {
7473
Log.e("decryptResultForLog", "e:" + e.message)
7574
}

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22
buildscript {
33
ext.kotlin_version = "1.3.72"
44
repositories {
5+
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
56
google()
67
jcenter()
78
}
89
dependencies {
910
classpath "com.android.tools.build:gradle:4.0.0"
1011
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
11-
12+
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
1213
// NOTE: Do not place your application dependencies here; they belong
1314
// in the individual module build.gradle files
1415
}
1516
}
1617

1718
allprojects {
1819
repositories {
20+
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
1921
google()
2022
jcenter()
2123
}

domainhttp/bintray.gradle

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//================================bintray 上传插件配置 start=========================================
2+
apply plugin: 'com.github.panpf.bintray-publish'
3+
4+
//防止中文注释出现错误
5+
allprojects {
6+
tasks.withType(Javadoc) {
7+
options.addStringOption('Xdoclint:none', '-quiet')
8+
options.addStringOption('encoding', 'UTF-8')
9+
}
10+
}
11+
12+
buildscript {
13+
repositories {
14+
jcenter()
15+
}
16+
dependencies {
17+
//上传插件
18+
classpath 'com.github.panpf.bintray-publish:bintray-publish:1.0.0'
19+
}
20+
}
21+
22+
publish {
23+
userOrg = 'forgetsky' //bintray注册的用户名
24+
groupId = 'com.forgetsky' //compile引用时的第1部分groupId
25+
artifactId = 'domainhttp' //compile引用时的第2部分项目名
26+
publishVersion = '1.0.0' //compile引用时的第3部分版本号
27+
desc = '一个基于OkHttp的多域名加密网络请求库'
28+
website = 'https://github.com/ForgetSky/DomainHttp' // 项目的主页
29+
}
30+
//================================bintray 上传插件配置 end=========================================

domainhttp/build.gradle

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apply plugin: 'com.android.library'
22
apply plugin: 'kotlin-android'
33
apply plugin: 'kotlin-android-extensions'
4-
4+
apply from: 'jitpack.gradle'
55
android {
66
compileSdkVersion 29
77
buildToolsVersion "29.0.3"
@@ -10,7 +10,7 @@ android {
1010
minSdkVersion 21
1111
targetSdkVersion 29
1212
versionCode 1
13-
versionName "1.0"
13+
versionName "1.0.0"
1414

1515
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1616
consumerProguardFiles "consumer-rules.pro"
@@ -36,4 +36,6 @@ dependencies {
3636
api 'com.squareup.okhttp3:okhttp:4.9.0'
3737
implementation("com.squareup.okhttp3:logging-interceptor:4.9.0")
3838

39-
}
39+
}
40+
41+

domainhttp/jitpack.gradle

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//==============================JitPack 上传插件配置 start=====================
2+
apply plugin: 'com.github.dcendents.android-maven'
3+
//组名 com.github是固定的,后面的是Github的用户名,替换成你自己的就可以了
4+
group='com.github.forgetsky'
5+
6+
buildscript {
7+
repositories {
8+
jcenter()
9+
}
10+
dependencies {
11+
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
12+
}
13+
}
14+
//==============================JitPack 上传插件配置 end=======================

domainhttp/src/main/java/com/forgetsky/domainhttp/domain/BaseEncryptPolicy.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ abstract class BaseEncryptPolicy :
3232
processResponse(domain, response)
3333
} catch (e: AesException) {
3434
//如果是握手接口,则抛出异常给握手方法tryHandShake()去处理
35-
if (isHandShakeInterface(request)) throw e
35+
if (request.isHandShakeInterface()) throw e
3636
//请求时间早于握手成功时间,则不用重新握手,直接重新发起请求
3737
if (requestTime > handShakeSuccessTime) {
3838
handShakeSuccess = false
@@ -50,8 +50,8 @@ abstract class BaseEncryptPolicy :
5050
return builder.also {
5151
addHeaders(domain, request, it)
5252
//如果是加密环境,且是POST接口,则执行加密或握手相关的逻辑
53-
if (isEncryptInterface(request, domain) && request.isPost()) {
54-
if (isHandShakeInterface(request)) {
53+
if (request.isEncryptInterface() && request.isPost()) {
54+
if (request.isHandShakeInterface()) {
5555
processHandShakeRequest(domain, it.build(), it)
5656
} else {
5757
//如果不是握手接口,则添加公共请求参数,对请求体加密
@@ -65,11 +65,11 @@ abstract class BaseEncryptPolicy :
6565
@Throws(IOException::class)
6666
override fun processResponse(domain: Domain, response: Response): Response {
6767
//如果是加密环境,且是POST接口,则执行解密密或握手相关的逻辑
68-
if (isEncryptInterface(response.request, domain) && response.request.isPost()) {
68+
if (response.request.isEncryptInterface() && response.request.isPost()) {
6969
val code = response.code
7070
if (response.isSuccessful) {
7171
//如果是握手接口
72-
return if (isHandShakeInterface(response.request)){
72+
return if (response.request.isHandShakeInterface()){
7373
processHandShakeResponse(domain, response)
7474
} else {
7575
//如果不是握手接口,则对响应体解密

domainhttp/src/main/java/com/forgetsky/domainhttp/domain/DomainPolicyInterceptor.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,28 @@ class DomainPolicyInterceptor : Interceptor {
3030
val domainKeyFromHeader = request.header(KEY_DOMAIN)
3131
domainKeyFromHeader?.let {
3232
domainKey = domainKeyFromHeader
33-
//后面的流程中统一使用tag来判断
34-
requestBuilder.tag(DomainTag::class.java, DomainTag(domainKey))
3533
requestBuilder.removeHeader(KEY_DOMAIN)
3634
}
35+
//后面的流程中统一使用tag来判断
36+
requestBuilder.tag(DomainTag::class.java, DomainTag(domainKey))
3737
} else {
3838
domainKey = domainTag.domain
3939
}
4040
val domain = DomainManager.mDomainMap[domainKey]
4141
?: throw IllegalArgumentException("No domain for $domainKey")
4242

43-
//针对单个接口处理是否加密,可以通过tag或者header配置
43+
//接口是否加密,可以通过tag或者header配置
44+
var isEncrypt = domain.isEncrypt
4445
val encryptTag = request.tag(EncryptTag::class.java)
4546
if (encryptTag == null) {
4647
val isEncryptHeader = request.header(KEY_ENCRYPT_HEADER)
4748
isEncryptHeader?.let {
48-
//后面的流程中统一使用tag来判断
49-
requestBuilder.tag(EncryptTag::class.java, EncryptTag(isEncryptHeader == "true"))
49+
isEncrypt = isEncryptHeader == "true"
5050
requestBuilder.removeHeader(KEY_ENCRYPT_HEADER)
5151
}
52+
53+
//后面的流程中统一使用tag来判断
54+
requestBuilder.tag(EncryptTag::class.java, EncryptTag(isEncrypt))
5255
}
5356

5457
//处理接口是否是握手接口,可以通过tag或者header配置

0 commit comments

Comments
 (0)