Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
com.ccwangluo.accelerator
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sheteng
com.ccwangluo.accelerator
Commits
78f244a9
Commit
78f244a9
authored
Mar 22, 2022
by
sheteng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
加速修改模式切换3
parent
b9828bfe
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
247 additions
and
184 deletions
+247
-184
app/src/main/java/com/ccwangluo/accelerator/ui/home/AcceleratorFragment.kt
.../com/ccwangluo/accelerator/ui/home/AcceleratorFragment.kt
+14
-14
core/src/main/java/com/github/shadowsocks/aidl/ShadowsocksConnection.kt
...java/com/github/shadowsocks/aidl/ShadowsocksConnection.kt
+2
-6
core/src/main/java/com/github/shadowsocks/bg/AuthManager.kt
core/src/main/java/com/github/shadowsocks/bg/AuthManager.kt
+63
-164
core/src/main/java/com/github/shadowsocks/http/HttpUtils.kt
core/src/main/java/com/github/shadowsocks/http/HttpUtils.kt
+168
-0
No files found.
app/src/main/java/com/ccwangluo/accelerator/ui/home/AcceleratorFragment.kt
View file @
78f244a9
...
@@ -247,20 +247,20 @@ class AcceleratorFragment : XPageFragment() {
...
@@ -247,20 +247,20 @@ class AcceleratorFragment : XPageFragment() {
AcceleratorUtils
.
getGameList
(
this
)
{
AcceleratorUtils
.
getGameList
(
this
)
{
if
(
it
)
{
if
(
it
)
{
//
//
//
AcceleratorUtils.game?.let {
AcceleratorUtils
.
game
?.
let
{
//
if (!SysUtils.isPackageInstall(requireContext(), it.packageName)) {
if
(!
SysUtils
.
isPackageInstall
(
requireContext
(),
it
.
packageName
))
{
//
binding.quickTopToast.visibility = View.VISIBLE
binding
.
quickTopToast
.
visibility
=
View
.
VISIBLE
//
binding.quickTopToastTx.setText("请先安装游戏后加速")
binding
.
quickTopToastTx
.
setText
(
"请先安装游戏后加速"
)
//
binding.quickTopToastTx.postDelayed({
binding
.
quickTopToastTx
.
postDelayed
({
//
binding.quickTopToast.visibility = View.GONE
binding
.
quickTopToast
.
visibility
=
View
.
GONE
//
}, 2000)
},
2000
)
//
DataRePortUtils.report(
DataRePortUtils
.
report
(
//
"st_speed_result",
"st_speed_result"
,
//
mapOf("result" to 0, "cause" to "1")
mapOf
(
"result"
to
0
,
"cause"
to
"1"
)
//
)
)
//
return@getGameList
return
@getGameList
//
}
}
//
}
}
//用户是否有使用时长
//用户是否有使用时长
LoginUtils
.
getUserHasTimer
(
this
@AcceleratorFragment
)
{
LoginUtils
.
getUserHasTimer
(
this
@AcceleratorFragment
)
{
if
(
it
)
{
if
(
it
)
{
...
...
core/src/main/java/com/github/shadowsocks/aidl/ShadowsocksConnection.kt
View file @
78f244a9
...
@@ -26,6 +26,7 @@ import android.content.Intent
...
@@ -26,6 +26,7 @@ import android.content.Intent
import
android.content.ServiceConnection
import
android.content.ServiceConnection
import
android.os.IBinder
import
android.os.IBinder
import
android.os.RemoteException
import
android.os.RemoteException
import
com.github.shadowsocks.bg.AuthManager
import
com.github.shadowsocks.bg.AuthManager.ACC_TIME
import
com.github.shadowsocks.bg.AuthManager.ACC_TIME
import
com.github.shadowsocks.bg.AuthManager.Last_ACC_TIme
import
com.github.shadowsocks.bg.AuthManager.Last_ACC_TIme
import
com.github.shadowsocks.bg.BaseService
import
com.github.shadowsocks.bg.BaseService
...
@@ -62,14 +63,9 @@ class ShadowsocksConnection(private var listenForDeath: Boolean = false) : Servi
...
@@ -62,14 +63,9 @@ class ShadowsocksConnection(private var listenForDeath: Boolean = false) : Servi
private
val
serviceCallback
=
object
:
IShadowsocksServiceCallback
.
Stub
()
{
private
val
serviceCallback
=
object
:
IShadowsocksServiceCallback
.
Stub
()
{
override
fun
stateChanged
(
state
:
Int
,
profileName
:
String
?,
msg
:
String
?)
{
override
fun
stateChanged
(
state
:
Int
,
profileName
:
String
?,
msg
:
String
?)
{
val
callback
=
callback
?:
return
val
callback
=
callback
?:
return
if
(
AuthManager
.
isreload
)
return
GlobalScope
.
launch
(
Dispatchers
.
Main
.
immediate
)
{
GlobalScope
.
launch
(
Dispatchers
.
Main
.
immediate
)
{
callback
.
stateChanged
(
BaseService
.
State
.
values
()[
state
],
profileName
,
msg
)
callback
.
stateChanged
(
BaseService
.
State
.
values
()[
state
],
profileName
,
msg
)
if
(
BaseService
.
State
.
values
()[
state
]
==
BaseService
.
State
.
Connected
)
{
ACC_TIME
=
System
.
currentTimeMillis
()
}
else
{
ACC_TIME
=
0L
Last_ACC_TIme
=
System
.
currentTimeMillis
()
}
}
}
}
}
override
fun
trafficUpdated
(
profileId
:
Long
,
stats
:
TrafficStats
)
{
override
fun
trafficUpdated
(
profileId
:
Long
,
stats
:
TrafficStats
)
{
...
...
core/src/main/java/com/github/shadowsocks/bg/AuthManager.kt
View file @
78f244a9
...
@@ -37,23 +37,10 @@ object AuthManager {
...
@@ -37,23 +37,10 @@ object AuthManager {
var
scheduleAtFixedRate
:
ScheduledFuture
<
*
>?
=
null
var
scheduleAtFixedRate
:
ScheduledFuture
<
*
>?
=
null
var
scheduleAtFixedRate1
:
ScheduledFuture
<
*
>?
=
null
var
scheduleAtFixedRate1
:
ScheduledFuture
<
*
>?
=
null
private
val
okHttpClient
:
OkHttpClient
val
gson
:
Gson
val
mmkv
=
MMKV
.
mmkvWithID
(
"InterProcessKV"
,
MMKV
.
MULTI_PROCESS_MODE
)
val
mmkv
=
MMKV
.
mmkvWithID
(
"InterProcessKV"
,
MMKV
.
MULTI_PROCESS_MODE
)
private
const
val
KEY_TOKEN
=
"com.ccwangluo.KEY_TOKEN"
private
const
val
KEY_TOKEN
=
"com.ccwangluo.KEY_TOKEN"
init
{
okHttpClient
=
OkHttpClient
().
newBuilder
()
.
readTimeout
(
60
,
TimeUnit
.
SECONDS
)
//设置读取超时时间
.
writeTimeout
(
60
,
TimeUnit
.
SECONDS
)
//设置写的超时时间
.
connectTimeout
(
60
,
TimeUnit
.
SECONDS
)
//设置连接超时时间
//https支持
.
hostnameVerifier
{
hostname
,
session
->
true
}
.
sslSocketFactory
(
initSSLSocketFactory
(),
initTrustManager
())
.
build
()
gson
=
Gson
()
}
var
gameId
:
String
?
var
gameId
:
String
?
get
()
=
mmkv
.
decodeString
(
"CONFIG_GAMEID"
,
null
)
get
()
=
mmkv
.
decodeString
(
"CONFIG_GAMEID"
,
null
)
...
@@ -96,12 +83,19 @@ object AuthManager {
...
@@ -96,12 +83,19 @@ object AuthManager {
class
AuthException
(
msg
:
String
)
:
IOException
(
msg
),
BaseService
.
ExpectedException
class
AuthException
(
msg
:
String
)
:
IOException
(
msg
),
BaseService
.
ExpectedException
var
isreload
=
false
private
var
currentNetwork
:
Int
=
-
1
suspend
fun
initAuthProfile
(
profile
:
Profile
)
{
suspend
fun
initAuthProfile
(
profile
:
Profile
)
{
withContext
(
Dispatchers
.
IO
)
{
withContext
(
Dispatchers
.
IO
)
{
val
gameId
:
Int
=
gameId
?.
toInt
()
?:
throw
AuthException
(
"游戏不存在"
)
val
gameId
:
Int
=
gameId
?.
toInt
()
?:
throw
AuthException
(
"游戏不存在"
)
val
lineConfigResult
=
val
lineConfigResult
=
HttpUtils
.
postSync
<
LineConfig
>(
postSync
<
LineConfig
>(
"/api/acc/line"
,
authToken
,
LineConfigReq
(
gameId
,
area
))
"/api/acc/line"
,
authToken
,
LineConfigReq
(
gameId
,
area
)
)
if
(
lineConfigResult
.
isSuccessful
())
{
if
(
lineConfigResult
.
isSuccessful
())
{
val
lineConfig
=
lineConfigResult
.
unwrapData
()
val
lineConfig
=
lineConfigResult
.
unwrapData
()
profile
.
host
=
lineConfig
.
host
profile
.
host
=
lineConfig
.
host
...
@@ -121,7 +115,18 @@ object AuthManager {
...
@@ -121,7 +115,18 @@ object AuthManager {
}
}
fun
updateConfig
(
data
:
BaseService
.
Data
)
{
fun
updateConfig
(
data
:
BaseService
.
Data
)
{
if
(
data
.
state
!=
BaseService
.
State
.
Connected
)
{
Timber
.
d
(
"updateConfig ${data.state.name} + $isreload"
)
if
(
data
.
state
==
BaseService
.
State
.
Connected
)
{
if
(!
isreload
)
{
ACC_TIME
=
System
.
currentTimeMillis
()
}
else
{
isreload
=
false
}
}
else
{
if
(!
isreload
)
{
ACC_TIME
=
0L
Last_ACC_TIme
=
System
.
currentTimeMillis
()
}
return
return
}
}
NetworkAdapter
.
initNetworks
(
Core
.
app
)
NetworkAdapter
.
initNetworks
(
Core
.
app
)
...
@@ -130,9 +135,9 @@ object AuthManager {
...
@@ -130,9 +135,9 @@ object AuthManager {
scheduleAtFixedRate
=
executor
.
scheduleAtFixedRate
({
scheduleAtFixedRate
=
executor
.
scheduleAtFixedRate
({
if
(
data
.
state
==
BaseService
.
State
.
Connected
)
{
if
(
data
.
state
==
BaseService
.
State
.
Connected
)
{
data
.
proxy
?.
let
{
proxyInstance
->
data
.
proxy
?.
let
{
proxyInstance
->
val
refreshTokenResult
=
postSync
<
RefreshTokenRes
>(
val
refreshTokenResult
=
HttpUtils
.
postSync
<
RefreshTokenRes
>(
"/api/acc/line/refresh"
,
authToken
,
"/api/acc/line/refresh"
,
authToken
,
RefreshTokenReq
(
proxyInstance
.
profile
.
token
?:
""
)
RefreshTokenReq
(
proxyInstance
.
profile
.
token
?:
""
)
)
)
Timber
.
d
(
"refreshTokenResult: $refreshTokenResult"
)
Timber
.
d
(
"refreshTokenResult: $refreshTokenResult"
)
if
(
refreshTokenResult
.
isSuccessful
())
{
if
(
refreshTokenResult
.
isSuccessful
())
{
...
@@ -159,28 +164,34 @@ object AuthManager {
...
@@ -159,28 +164,34 @@ object AuthManager {
},
1
,
1
,
TimeUnit
.
MINUTES
)
},
1
,
1
,
TimeUnit
.
MINUTES
)
scheduleAtFixedRate1
?.
cancel
(
false
)
scheduleAtFixedRate1
?.
cancel
(
false
)
scheduleAtFixedRate1
=
executor
.
scheduleAtFixedRate
(
scheduleAtFixedRate1
=
executor
.
scheduleAtFixedRate
(
{
{
Timber
.
d
(
"testLatency"
)
Timber
.
d
(
"testLatency"
)
GlobalScope
.
launch
{
GlobalScope
.
launch
{
testLatency
()
testLatency
()
if
(
wifiLatencyList
.
size
==
5
)
{
if
(
wifiLatencyList
.
size
==
5
)
{
val
wifiavg
=
wifiLatencyList
.
average
().
toInt
()
val
wifiavg
=
wifiLatencyList
.
average
().
toInt
()
val
celavg
=
cellularLatencyList
.
average
().
toInt
()
val
celavg
=
cellularLatencyList
.
average
().
toInt
()
if
(
wifiavg
>
celavg
&&
wifiavg
>
200
&&
celavg
!=
-
1
)
{
if
(
wifiavg
>
celavg
&&
wifiavg
>
100
&&
celavg
!=
-
1
&&
currentNetwork
<
0
)
{
NetworkAdapter
.
useCellular
(
Core
.
deviceStorage
)
currentNetwork
=
0
Timber
.
d
(
"switch to cellular"
)
NetworkAdapter
.
useCellular
(
Core
.
deviceStorage
)
}
else
if
(
celavg
>
wifiavg
&&
wifiavg
!=
-
1
)
{
isreload
=
true
NetworkAdapter
.
useWifi
(
Core
.
deviceStorage
)
Core
.
reloadService
()
Timber
.
d
(
"switch to wifi"
)
Timber
.
d
(
"switch to cellular"
)
}
wifiLatencyList
.
clear
()
cellularLatencyList
.
clear
()
}
}
// else if (celavg > wifiavg && wifiavg != -1) {
// NetworkAdapter.useWifi(Core.deviceStorage)
// isreload = true
// Core.reloadService()
// Timber.d("switch to wifi")
// }
wifiLatencyList
.
clear
()
cellularLatencyList
.
clear
()
}
}
},
}
1
,
},
1
,
5
,
TimeUnit
.
SECONDS
5
,
TimeUnit
.
SECONDS
)
)
}
}
...
@@ -189,18 +200,18 @@ object AuthManager {
...
@@ -189,18 +200,18 @@ object AuthManager {
private
suspend
fun
testLatency
()
{
private
suspend
fun
testLatency
()
{
val
networkAutoSwitch
=
val
networkAutoSwitch
=
isSmartAccOpen
// 根据用户设置测试延迟 ,过高时切换 connection.bindNetwork(NetworkMode.CELLULAR.code)
isSmartAccOpen
// 根据用户设置测试延迟 ,过高时切换 connection.bindNetwork(NetworkMode.CELLULAR.code)
if
(
networkAutoSwitch
)
{
if
(
networkAutoSwitch
)
{
val
wifiLatency
:
Int
=
doTestLatency
(
val
wifiLatency
:
Int
=
doTestLatency
(
"114.114.114.114"
,
"114.114.114.114"
,
53
,
53
,
NetworkAdapter
.
wifiNetwork
NetworkAdapter
.
wifiNetwork
)
)
wifiLatencyList
.
add
(
wifiLatency
)
wifiLatencyList
.
add
(
wifiLatency
)
val
cellularLatency
:
Int
=
doTestLatency
(
val
cellularLatency
:
Int
=
doTestLatency
(
"114.114.114.114"
,
"114.114.114.114"
,
53
,
53
,
NetworkAdapter
.
cellularNetwork
NetworkAdapter
.
cellularNetwork
)
)
cellularLatencyList
.
add
(
cellularLatency
)
cellularLatencyList
.
add
(
cellularLatency
)
Timber
.
d
(
"wifi latency: $wifiLatency, cellular latency: $cellularLatency"
)
Timber
.
d
(
"wifi latency: $wifiLatency, cellular latency: $cellularLatency"
)
...
@@ -208,10 +219,10 @@ object AuthManager {
...
@@ -208,10 +219,10 @@ object AuthManager {
}
}
private
suspend
fun
doTestLatency
(
private
suspend
fun
doTestLatency
(
pingHost
:
String
,
pingHost
:
String
,
pingPort
:
Int
,
pingPort
:
Int
,
network
:
Network
?,
network
:
Network
?,
systemNetwork
:
Boolean
=
false
systemNetwork
:
Boolean
=
false
):
Int
{
):
Int
{
return
withTimeoutOrNull
(
DataStore
.
pingTimeoutMillis
.
toLong
())
{
return
withTimeoutOrNull
(
DataStore
.
pingTimeoutMillis
.
toLong
())
{
try
{
try
{
...
@@ -228,8 +239,8 @@ object AuthManager {
...
@@ -228,8 +239,8 @@ object AuthManager {
}
}
val
start
=
System
.
currentTimeMillis
()
val
start
=
System
.
currentTimeMillis
()
socketAddress
.
connect
(
socketAddress
.
connect
(
InetSocketAddress
(
pingHost
,
pingPort
),
InetSocketAddress
(
pingHost
,
pingPort
),
DataStore
.
pingTimeoutMillis
DataStore
.
pingTimeoutMillis
)
)
(
System
.
currentTimeMillis
()
-
start
).
toInt
()
(
System
.
currentTimeMillis
()
-
start
).
toInt
()
}
catch
(
_
:
Exception
)
{
}
catch
(
_
:
Exception
)
{
...
@@ -240,117 +251,5 @@ object AuthManager {
...
@@ -240,117 +251,5 @@ object AuthManager {
var
loginAuthToken
:
String
=
"1"
var
loginAuthToken
:
String
=
"1"
private
val
jsonType
get
()
=
"application/json; charset=utf-8"
.
toMediaTypeOrNull
()
private
inline
fun
<
reified
T
>
postSync
(
url
:
String
,
token
:
String
?,
params
:
Any
):
ApiResult
<
T
>
{
val
body
:
RequestBody
=
gson
.
toJson
(
params
).
toString
().
toRequestBody
(
jsonType
)
val
request
=
Request
.
Builder
()
.
url
(
if
(
url
.
startsWith
(
"http"
))
url
else
HttpConfig
.
baseUrl
+
url
)
.
post
(
body
)
token
?.
let
{
request
.
addHeader
(
"Authorization"
,
it
)
}
val
call
=
okHttpClient
.
newCall
(
request
.
build
())
val
resultType
=
object
:
TypeToken
<
ApiResult
<
T
>>()
{}.
type
try
{
call
.
execute
().
use
{
response
->
val
string
=
response
.
body
?.
string
()
println
(
string
)
return
gson
.
fromJson
(
string
,
resultType
)
}
}
catch
(
e
:
IOException
)
{
Timber
.
e
(
e
,
"AuthManager 调用接口错误"
)
return
ApiResult
(
ApiError
.
NetworkError
)
}
}
private
fun
initSSLSocketFactory
():
SSLSocketFactory
{
var
sslContext
:
SSLContext
?
=
null
try
{
sslContext
=
SSLContext
.
getInstance
(
"SSL"
)
val
xTrustArray
=
arrayOf
(
initTrustManager
())
sslContext
.
init
(
null
,
xTrustArray
,
SecureRandom
()
)
}
catch
(
e
:
Exception
)
{
e
.
printStackTrace
()
}
return
AutoSwitchNetworkSocketFactory
(
sslContext
!!
.
socketFactory
)
}
private
class
AutoSwitchNetworkSocketFactory
(
private
val
delegate
:
SSLSocketFactory
)
:
SSLSocketFactory
()
{
override
fun
createSocket
(
s
:
Socket
?,
host
:
String
?,
port
:
Int
,
autoClose
:
Boolean
):
Socket
{
val
bindNetwork
=
NetworkAdapter
.
bindNetwork
val
createSocket
=
delegate
.
createSocket
(
s
,
host
,
port
,
autoClose
)
bindNetwork
?.
bindSocket
(
createSocket
)
return
createSocket
}
override
fun
createSocket
(
host
:
String
?,
port
:
Int
):
Socket
{
val
bindNetwork
=
NetworkAdapter
.
bindNetwork
return
if
(
bindNetwork
!=
null
)
{
bindNetwork
.
socketFactory
.
createSocket
(
host
,
port
)
}
else
{
delegate
.
createSocket
(
host
,
port
)
}
}
override
fun
createSocket
(
host
:
String
?,
port
:
Int
,
localHost
:
InetAddress
?,
localPort
:
Int
):
Socket
{
val
bindNetwork
=
NetworkAdapter
.
bindNetwork
return
if
(
bindNetwork
!=
null
)
{
bindNetwork
.
socketFactory
.
createSocket
(
host
,
port
,
localHost
,
localPort
)
}
else
{
delegate
.
createSocket
(
host
,
port
,
localHost
,
localPort
)
}
}
override
fun
createSocket
(
host
:
InetAddress
?,
port
:
Int
):
Socket
{
val
bindNetwork
=
NetworkAdapter
.
bindNetwork
return
if
(
bindNetwork
!=
null
)
{
bindNetwork
.
socketFactory
.
createSocket
(
host
,
port
)
}
else
{
delegate
.
createSocket
(
host
,
port
)
}
}
override
fun
createSocket
(
address
:
InetAddress
?,
port
:
Int
,
localAddress
:
InetAddress
?,
localPort
:
Int
):
Socket
{
val
bindNetwork
=
NetworkAdapter
.
bindNetwork
return
if
(
bindNetwork
!=
null
)
{
bindNetwork
.
socketFactory
.
createSocket
(
address
,
port
,
localAddress
,
localPort
)
}
else
{
delegate
.
createSocket
(
address
,
port
,
localAddress
,
localPort
)
}
}
override
fun
getDefaultCipherSuites
():
Array
<
String
>
{
return
delegate
.
defaultCipherSuites
}
override
fun
getSupportedCipherSuites
():
Array
<
String
>
{
return
delegate
.
supportedCipherSuites
}
}
private
fun
initTrustManager
():
X509TrustManager
{
return
object
:
X509TrustManager
{
override
fun
getAcceptedIssuers
():
Array
<
X509Certificate
>
{
return
arrayOf
()
}
override
fun
checkServerTrusted
(
chain
:
Array
<
out
X509Certificate
>?,
authType
:
String
?)
{
}
override
fun
checkClientTrusted
(
chain
:
Array
<
out
X509Certificate
>?,
authType
:
String
?)
{
}
}
}
}
}
\ No newline at end of file
core/src/main/java/com/github/shadowsocks/http/HttpUtils.kt
0 → 100644
View file @
78f244a9
package
com.github.shadowsocks.http
import
com.ccnet.acc.NetworkAdapter
import
com.github.shadowsocks.bg.AuthManager
import
com.google.gson.Gson
import
com.google.gson.reflect.TypeToken
import
okhttp3.MediaType.Companion.toMediaTypeOrNull
import
okhttp3.OkHttpClient
import
okhttp3.Request
import
okhttp3.RequestBody
import
okhttp3.RequestBody.Companion.toRequestBody
import
timber.log.Timber
import
java.io.IOException
import
java.net.InetAddress
import
java.net.Socket
import
java.security.SecureRandom
import
java.security.cert.X509Certificate
import
java.util.concurrent.TimeUnit
import
javax.net.ssl.SSLContext
import
javax.net.ssl.SSLSocketFactory
import
javax.net.ssl.X509TrustManager
object
HttpUtils
{
val
okHttpClient
:
OkHttpClient
val
gson
:
Gson
init
{
okHttpClient
=
OkHttpClient
().
newBuilder
()
.
readTimeout
(
60
,
TimeUnit
.
SECONDS
)
//设置读取超时时间
.
writeTimeout
(
60
,
TimeUnit
.
SECONDS
)
//设置写的超时时间
.
connectTimeout
(
60
,
TimeUnit
.
SECONDS
)
//设置连接超时时间
//https支持
.
hostnameVerifier
{
hostname
,
session
->
true
}
.
sslSocketFactory
(
initSSLSocketFactory
(),
initTrustManager
())
.
build
()
gson
=
Gson
()
}
val
jsonType
get
()
=
"application/json; charset=utf-8"
.
toMediaTypeOrNull
()
inline
fun
<
reified
T
>
postSync
(
url
:
String
,
token
:
String
?,
params
:
Any
):
ApiResult
<
T
>
{
val
body
:
RequestBody
=
gson
.
toJson
(
params
).
toString
().
toRequestBody
(
jsonType
)
val
request
=
Request
.
Builder
()
.
url
(
if
(
url
.
startsWith
(
"http"
))
url
else
HttpConfig
.
baseUrl
+
url
)
.
post
(
body
)
token
?.
let
{
request
.
addHeader
(
"Authorization"
,
it
)
}
val
call
=
okHttpClient
.
newCall
(
request
.
build
())
val
resultType
=
object
:
TypeToken
<
ApiResult
<
T
>>()
{}.
type
try
{
call
.
execute
().
use
{
response
->
val
string
=
response
.
body
?.
string
()
println
(
string
)
return
gson
.
fromJson
(
string
,
resultType
)
}
}
catch
(
e
:
IOException
)
{
Timber
.
e
(
e
,
"AuthManager 调用接口错误"
)
return
ApiResult
(
ApiError
.
NetworkError
)
}
}
private
fun
initSSLSocketFactory
():
SSLSocketFactory
{
var
sslContext
:
SSLContext
?
=
null
try
{
sslContext
=
SSLContext
.
getInstance
(
"SSL"
)
val
xTrustArray
=
arrayOf
(
initTrustManager
())
sslContext
.
init
(
null
,
xTrustArray
,
SecureRandom
()
)
}
catch
(
e
:
Exception
)
{
e
.
printStackTrace
()
}
return
AutoSwitchNetworkSocketFactory
(
sslContext
!!
.
socketFactory
)
}
private
class
AutoSwitchNetworkSocketFactory
(
private
val
delegate
:
SSLSocketFactory
)
:
SSLSocketFactory
()
{
override
fun
createSocket
(
s
:
Socket
?,
host
:
String
?,
port
:
Int
,
autoClose
:
Boolean
):
Socket
{
val
bindNetwork
=
NetworkAdapter
.
bindNetwork
val
createSocket
=
delegate
.
createSocket
(
s
,
host
,
port
,
autoClose
)
bindNetwork
?.
bindSocket
(
createSocket
)
return
createSocket
}
override
fun
createSocket
(
host
:
String
?,
port
:
Int
):
Socket
{
val
bindNetwork
=
NetworkAdapter
.
bindNetwork
return
if
(
bindNetwork
!=
null
)
{
bindNetwork
.
socketFactory
.
createSocket
(
host
,
port
)
}
else
{
delegate
.
createSocket
(
host
,
port
)
}
}
override
fun
createSocket
(
host
:
String
?,
port
:
Int
,
localHost
:
InetAddress
?,
localPort
:
Int
):
Socket
{
val
bindNetwork
=
NetworkAdapter
.
bindNetwork
return
if
(
bindNetwork
!=
null
)
{
bindNetwork
.
socketFactory
.
createSocket
(
host
,
port
,
localHost
,
localPort
)
}
else
{
delegate
.
createSocket
(
host
,
port
,
localHost
,
localPort
)
}
}
override
fun
createSocket
(
host
:
InetAddress
?,
port
:
Int
):
Socket
{
val
bindNetwork
=
NetworkAdapter
.
bindNetwork
return
if
(
bindNetwork
!=
null
)
{
bindNetwork
.
socketFactory
.
createSocket
(
host
,
port
)
}
else
{
delegate
.
createSocket
(
host
,
port
)
}
}
override
fun
createSocket
(
address
:
InetAddress
?,
port
:
Int
,
localAddress
:
InetAddress
?,
localPort
:
Int
):
Socket
{
val
bindNetwork
=
NetworkAdapter
.
bindNetwork
return
if
(
bindNetwork
!=
null
)
{
bindNetwork
.
socketFactory
.
createSocket
(
address
,
port
,
localAddress
,
localPort
)
}
else
{
delegate
.
createSocket
(
address
,
port
,
localAddress
,
localPort
)
}
}
override
fun
getDefaultCipherSuites
():
Array
<
String
>
{
return
delegate
.
defaultCipherSuites
}
override
fun
getSupportedCipherSuites
():
Array
<
String
>
{
return
delegate
.
supportedCipherSuites
}
}
private
fun
initTrustManager
():
X509TrustManager
{
return
object
:
X509TrustManager
{
override
fun
getAcceptedIssuers
():
Array
<
X509Certificate
>
{
return
arrayOf
()
}
override
fun
checkServerTrusted
(
chain
:
Array
<
out
X509Certificate
>?,
authType
:
String
?)
{
}
override
fun
checkClientTrusted
(
chain
:
Array
<
out
X509Certificate
>?,
authType
:
String
?)
{
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment