zhoulian/app/src/main/java/com/yingmao/clash/vm/MainAtyVM.kt

392 lines
16 KiB
Kotlin

package com.yingmao.clash.vm
import android.os.Build
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.yingmao.clash.net.http.entity.HeartBeatRsp
import com.orhanobut.logger.Logger
import com.tencent.mmkv.MMKV
import com.yingmao.clash.BuildConfig
import com.yingmao.clash.act.NewMainActivity
import com.yingmao.clash.base.PublicViewMode
import com.cncat.vpn.common.log.Log
import com.google.gson.Gson
import com.yingmao.clash.app.MainApplication
import com.yingmao.clash.conf.BZYConstants
import com.yingmao.clash.entity.CheckLoginRsp
import com.yingmao.clash.entity.ClashNodebBean
import com.yingmao.clash.entity.Node
import com.yingmao.clash.http.OkHttpBase
import com.yingmao.clash.listener.AsyncCallBackListener
import com.yingmao.clash.listener.DownLoadListener
import com.yingmao.clash.net.http.HttpApi
import com.yingmao.clash.net.http.HttpReqType
import com.yingmao.clash.net.http.NetService
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import org.yaml.snakeyaml.Yaml
import java.math.BigDecimal
import java.util.*
import kotlin.concurrent.schedule
class MainAtyVM : PublicViewMode() {
private var heartBeatTimer: Timer?=null
private var heartBeatTask: TimerTask?=null
private fun heartbeat() {
val httpApi = NetService.instances.createHttpsEncodeStr(HttpApi::class.java)
val node = MMKV.defaultMMKV()
.decodeParcelable(BZYConstants.MMKV_KEY_CURRENT_NODE, Node::class.java)
val token = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_PH_TOKEN, "")!!
val phoneNumber =
MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_REMEMBER_USERNAME, "")!!
val params = hashMapOf(
"phToken" to token,
"phoneNumber" to phoneNumber,
"channelId" to BuildConfig.productId,
"plat" to "Android",
"ver" to "${BuildConfig.VERSION_CODE}",
"platver" to Build.VERSION.RELEASE,
"br" to Build.BRAND,
"selectNodeId" to "${node?.id}",
"md" to Build.PRODUCT
)
doAsyncEncodeStr(
HttpReqType.HEARTBEAT,
HeartBeatRsp::class.java
) { httpApi.heartbeat(params) }
}
fun getNoticeInfo() {
val httpApi = NetService.instances.createHttps(HttpApi::class.java)
val userId = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_USER_ID, "")!!
val phToken = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_PH_TOKEN, "")!!
val phone = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_REMEMBER_USERNAME, "")!!
val params = mutableMapOf(
"userId" to userId,
"phoneNumber" to phone,
"phToken" to phToken,
"osType" to "1"
)
doAsync(HttpReqType.PHDOWNLOADURL) { httpApi.noticeAndUpdate(params) }
}
fun getRuningNoticeInfo() {
val httpApi = NetService.instances.createHttps(HttpApi::class.java)
val userId = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_USER_ID, "")!!
val phToken = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_PH_TOKEN, "")!!
val phone = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_REMEMBER_USERNAME, "")!!
val params = mutableMapOf(
"userId" to userId,
"phoneNumber" to phone,
"phToken" to phToken,
// "phToken" to phToken,
// (默认为公告 公告=Bulletin,跑马灯=Running)
"actType" to "Running",
"osType" to "1"
)
doAsync(HttpReqType.GetRuningNotice) { httpApi.noticeAndUpdate(params) }
}
fun checkLogin() {
val httpApi = NetService.instances.createHttpsEncodeStr(HttpApi::class.java)
val token = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_PH_TOKEN, "")!!
val phoneNumber =
MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_REMEMBER_USERNAME, "")!!
val params = mutableMapOf(
"phToken" to token,
"phoneNumber" to phoneNumber,
// "OAID" to MainApplication.OAID,
)
doAsyncEncodeStr(HttpReqType.CHECK_LOGIN, CheckLoginRsp::class.java) {
httpApi.checkLogin(
params
)
}
}
//10分钟一次
fun startHeartBeat() {
// heartBeatTimer.cancel()
if (heartBeatTimer == null) {
heartBeatTimer= Timer()
heartBeatTask = object : TimerTask() {
override fun run() {
Logger.d("心跳")
heartbeat()
}
}
}
heartBeatTimer?.schedule(heartBeatTask,100000,600000)
}
fun stopHeartBeat() {
heartBeatTimer?.cancel()
heartBeatTimer = null
heartBeatTask?.cancel()
heartBeatTask = null
}
fun appCheck() {
val httpApi = NetService.instances.createHttpsEncodeStr(HttpApi::class.java)
val token = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_PH_TOKEN, "")!!
val phoneNumber =
MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_REMEMBER_USERNAME, "")!!
val params = mutableMapOf(
"phToken" to token,
"phoneNumber" to phoneNumber,
"versionNo" to com.yingmao.clash.BuildConfig.VERSION_CODE.toString(),
"versionType" to "1", //android写死
"invitationCode" to BuildConfig.productId,
)
Log.d("appCheck==>$params")
doAsync(HttpReqType.App_VERION_CHECK) {
httpApi.appUpgrade(params)
}
}
fun downLoadFile(url: String, downLoadListener: DownLoadListener) {
NetService.instances.downloadFile(url, downLoadListener)
}
fun getAirNodes() {
val httpApi = NetService.instances.createHttpsEncodeStr(HttpApi::class.java)
val token = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_PH_TOKEN, "")!!
val phoneNumber =
MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_REMEMBER_USERNAME, "")!!
val params = mutableMapOf(
"phToken" to token,
"phoneNumber" to phoneNumber,
)
Log.d("new main getAirportNode")
doAsync(HttpReqType.GET_AIRNODES) {
httpApi.getAirportNode(params)
}
// NetService.instances.postAsync("/netbarcloud/vpn/airportNode.do",params,null,null,myNetCall)
}
fun vipSign() {
val httpApi = NetService.instances.createHttpsEncodeStr(HttpApi::class.java)
val token = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_PH_TOKEN, "")!!
val phoneNumber =
MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_REMEMBER_USERNAME, "")!!
val params = mutableMapOf(
"phToken" to token,
"phoneNumber" to phoneNumber,
)
doAsync(HttpReqType.VIPSIGN) {
httpApi.VipSign(params)
}
// NetService.instances.postAsync("/netbarcloud/vpn/airportNode.do",params,null,null,myNetCall)
}
fun getActivity() {
val httpApi = NetService.instances.createHttps(HttpApi::class.java)
val username =
MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_REMEMBER_USERNAME, "")!!
val phToken = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_PH_TOKEN, "")!!
val params = hashMapOf(
"phoneNumber" to username,
"phToken" to phToken,
"pageNum" to "1",
"pageSize" to "5",
"clientTypeArray" to "1",
"moduleType" to "HOME"
)
doAsync(HttpReqType.GetActivity) { httpApi.getActivity(params) }
}
fun getUploadParameters() {
val httpApi = NetService.instances.createHttps(HttpApi::class.java)
val username =
MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_REMEMBER_USERNAME, "")!!
val phToken = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_PH_TOKEN, "")!!
val params = hashMapOf(
"phoneNumber" to username,
"phToken" to phToken,
)
doAsync(HttpReqType.UploadParameters) { httpApi.getUploadParameters(params) }
}
fun getNodeOther(url: String, listener: AsyncCallBackListener) {
Log.d("get_Node_Other start")
val client = OkHttpClient()
try {
val request = Request.Builder()
.url(url)
.header("User-Agent", "ClashforWindows/0.19.23")
.build()
client.newCall(request).execute().use { response ->
if (!response.isSuccessful || response.headers["subscription-userinfo"] == null) return
var upload: Long = 0
var download: Long = 0
var total: Long = 0
var expire: Long = 0
val userinfo = response.headers["subscription-userinfo"]
// Log.d("updateFlow userinfo==>" + Gson().toJson(userinfo))
if (response.isSuccessful && userinfo != null) {
val flags = userinfo.split(";")
for (flag in flags) {
val info = flag.split("=")
when {
info[0].contains("upload") && info[1].isNotEmpty() -> upload =
BigDecimal(info[1]).longValueExact()
info[0].contains("download") && info[1].isNotEmpty() -> download =
BigDecimal(info[1]).longValueExact()
info[0].contains("total") && info[1].isNotEmpty() -> total =
BigDecimal(info[1]).longValueExact()
info[0].contains("expire") && info[1].isNotEmpty() -> {
if (info[1].isNotEmpty()) {
expire = (info[1].toDouble() * 1000).toLong()
}
}
}
}
Log.d("upload==>${upload}, download==>${download}, total==>${total}, expire==>${expire}")
response.body?.let {
val respStr = it.string()
val conf = Yaml().loadAs(respStr, HashMap::class.java)
val proxies = conf["proxies"];
proxies?.let { pro ->
val proList = pro as ArrayList<*>
val gson = Gson()
val dataList = mutableListOf<ClashNodebBean>()
proList.forEach { item ->
val jo = gson.toJson(item)
val bean = gson.fromJson(jo.toString(), ClashNodebBean::class.java)
dataList.add(bean)
}
listener.suc(dataList)
}
}
} else {
listener.faild(response)
}
}
} catch (e: Exception) {
e.printStackTrace()
Log.d("e==>" + e.localizedMessage)
}
}
fun appStartupStatus() {
val httpApi = NetService.instances.createHttpsEncodeStr(HttpApi::class.java)
val token = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_PH_TOKEN, "")!!
val phoneNumber =
MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_REMEMBER_USERNAME, "")!!
val params = mutableMapOf(
"phToken" to token,
"phoneNumber" to phoneNumber,
)
Log.d("appCheck==>$params")
doAsync(HttpReqType.AppStartupStatus) {
httpApi.appStartupStatus(params)
}
}
fun appStopStatus(addId:Int) {
val httpApi = NetService.instances.createHttpsEncodeStr(HttpApi::class.java)
val token = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_PH_TOKEN, "")!!
val phoneNumber =
MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_REMEMBER_USERNAME, "")!!
val paramsToken = mutableMapOf(
"phToken" to token,
"phoneNumber" to phoneNumber,
)
val params = hashMapOf(
"id" to addId
)
val gson = Gson()
val jsonString = gson.toJson(params)
val requestBody: RequestBody = jsonString.toRequestBody("application/json; charset=utf-8".toMediaType())
doAsync(HttpReqType.AppStopStatus) {
httpApi.appStopStatus(paramsToken,requestBody)
}
}
fun appUpStatus(addId:Int) {
val httpApi = NetService.instances.createHttpsEncodeStr(HttpApi::class.java)
val token = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_PH_TOKEN, "")!!
val phoneNumber =
MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_REMEMBER_USERNAME, "")!!
val paramsToken = mutableMapOf(
"phToken" to token,
"phoneNumber" to phoneNumber,
)
val params = hashMapOf(
"id" to addId
)
val gson = Gson()
val jsonString = gson.toJson(params)
val requestBody: RequestBody = jsonString.toRequestBody("application/json; charset=utf-8".toMediaType())
doAsync(HttpReqType.AppUpStatus) {
httpApi.appUpStatus(paramsToken,requestBody)
}
}
fun getExpensesRecord(page : NewMainActivity.PageInfo){
val httpApi = NetService.instances.createHttps(HttpApi::class.java)
val username = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_REMEMBER_USERNAME,"")!!
val phToken = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_PH_TOKEN,"")!!
val userId = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_USER_ID)!!
val params = hashMapOf<String,String>(
"phoneNumber" to username,
"userId" to userId,
"phToken" to phToken,
"pageno" to "${page.page}",
"pageSize" to "5",
)
doAsync(HttpReqType.EXPENSES_RECORD){httpApi.phOrderList(params)}
}
fun getSelectNodeInfo() {
val httpApi = NetService.instances.createHttps(HttpApi::class.java)
doAsync(HttpReqType.SelectNode) { httpApi.selectNodeInfo(BuildConfig.productId) }
}
fun getLogoutDel(){
val httpApi = NetService.instances.createHttps(HttpApi::class.java)
val username = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_REMEMBER_USERNAME,"")!!
val phToken = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_PH_TOKEN,"")!!
val params = hashMapOf(
"phoneNumber" to username,
"phToken" to phToken,
)
doAsync(HttpReqType.LogOutDel){httpApi.logOutDel(params)}
}
fun getReport(orderId:Int){
val httpApi = NetService.instances.createHttpsEncodeStr(HttpApi::class.java)
val token = MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_PH_TOKEN, "")!!
val phoneNumber =
MMKV.defaultMMKV().decodeString(BZYConstants.MMKV_KEY_REMEMBER_USERNAME, "")!!
val paramsToken = mutableMapOf(
"phToken" to token,
"phoneNumber" to phoneNumber,
)
val params = hashMapOf(
"id" to orderId
)
val gson = Gson()
val jsonString = gson.toJson(params)
val requestBody: RequestBody = jsonString.toRequestBody("application/json; charset=utf-8".toMediaType())
doAsync(HttpReqType.Report){httpApi.getReport(paramsToken,requestBody)}
}
}