新增凭证绑定HTTP服务接口

This commit is contained in:
sxy0 2024-11-06 11:44:40 +08:00
parent 147ac597fa
commit 0a20d978ef
19 changed files with 258 additions and 138 deletions

View File

@ -225,7 +225,10 @@ dependencies {
implementation 'com.shuyu:gsyVideoPlayer-java:8.1.2'
implementation 'org.java-websocket:Java-WebSocket:1.5.7'
//webserver
implementation 'com.koushikdutta.async:androidasync:3.1.0'
// implementation 'org.java-websocket:Java-WebSocket:1.5.7'
//ijk模式的so
implementation 'com.shuyu:gsyVideoPlayer-armv5:8.1.2'

View File

@ -9,6 +9,7 @@ import android.util.Log
import cn.trans88.kurotool.util.LogLevel
import cn.trans88.kurotool.util.LogUtil
import cn.trans88.taxiappkotlin.ext.logd
import cn.trans88.taxiappkotlin.ledok.HttpServer
import cn.trans88.taxiappkotlin.logic.dao.DaoMaster
import cn.trans88.taxiappkotlin.logic.dao.DaoSession
import cn.trans88.taxiappkotlin.logic.dao.HelperDaoDB
@ -226,6 +227,12 @@ class TaxiApp : Application() {
registerNetworkChangeReceiver()
Log.d(TAG, "onCreate: httpServer")
Thread {
HttpServer().listen()
}.start()
// HttpServer().start()
//配置exoPlayer的player
// initPlayer()
//TODO

View File

@ -0,0 +1,81 @@
package cn.trans88.taxiappkotlin.ledok
import android.util.Log
import cn.trans88.taxiappkotlin.logic.model.BindModel
import cn.trans88.taxiappkotlin.logic.network.ConnManger
import com.google.gson.Gson
import com.koushikdutta.async.http.body.AsyncHttpRequestBody
import com.koushikdutta.async.http.body.JSONObjectBody
import com.koushikdutta.async.http.body.StringBody
import com.koushikdutta.async.http.server.AsyncHttpServerRequest
import com.koushikdutta.async.http.server.AsyncHttpServerResponse
import com.koushikdutta.async.http.server.HttpServerRequestCallback
import org.json.JSONException
import org.json.JSONObject
/**
* Created by Administrator on 2017/3/29 0029.
*/
class ActionRequest : HttpServerRequestCallback {
private val TAG = this.javaClass.simpleName
override fun onRequest(request: AsyncHttpServerRequest, response: AsyncHttpServerResponse) {
val gson = Gson()
try {
val hs = response.headers
hs.add("Access-Control-Allow-Origin", "*")
hs.add("Access-Control-Allow-Methods", "POST") //让ajax跨域
val obj = getJSONFromReq(request)
val actionName = obj!!.getString("action")
val json = obj.toString()
Log.i(TAG, "get POST: $json")
if ("InvokeTaxiAppFunction" == actionName) {
val bindModel =
gson.fromJson(obj.get("jsonCommand").toString(), BindModel::class.java)
Log.d(TAG, "listen: httpServer ${bindModel}")
if (bindModel?.server != null && bindModel?.server != "") {
bindModel.server = bindModel?.server + "/"
}
if (bindModel?.server != null && bindModel.server != "") {
bindModel.tlsServer = bindModel?.server+ "/"
}
val result = ConnManger.mbindAccount(bindModel);
if (result) {
val map = HashMap<String, Any>()
map["code"] = 200
map["message"] = "Success"
response.code(200).send("application/json", gson.toJson(map));
} else {
val map = HashMap<String, Any>()
map["code"] = 400
map["message"] = "Failed"
response.code(400).send("application/json", gson.toJson(map));
}
}
} catch (e: JSONException) {
throw RuntimeException(e)
Log.d(TAG, "onRequest: httpServer e")
val map = HashMap<String, Any>()
map["code"] = 500
map["message"] = gson.toJson(e)
response.code(500).send("application/json", gson.toJson(map));
}
}
private fun getJSONFromReq(req: AsyncHttpServerRequest): JSONObject? {
var obj: JSONObject? = null
val body = req.getBody<AsyncHttpRequestBody<*>>()
if (body is JSONObjectBody) {
obj = body.get()
} else if (body is StringBody) {
try {
obj = JSONObject(body.get())
} catch (e: JSONException) {
e.printStackTrace()
}
}
return obj
}
}

View File

@ -0,0 +1,47 @@
package cn.trans88.taxiappkotlin.ledok
import android.util.Log
import cn.trans88.taxiappkotlin.logic.model.BindModel
import cn.trans88.taxiappkotlin.logic.network.ConnManger
import com.google.gson.Gson
import com.koushikdutta.async.http.body.AsyncHttpRequestBody
import com.koushikdutta.async.http.body.JSONObjectBody
import com.koushikdutta.async.http.body.StringBody
import com.koushikdutta.async.http.server.AsyncHttpServer
import com.koushikdutta.async.http.server.AsyncHttpServerRequest
import org.json.JSONException
import org.json.JSONObject
class HttpServer {
val TAG = this.javaClass::class.simpleName
lateinit var server: AsyncHttpServer
fun listen() {
server = AsyncHttpServer()
server.get("/") { _, resp ->
run {
resp.send("Hello")
}
}
server.post("/", ActionRequest())
server.listen(3389)
Log.d(TAG, "run: httpServer start... listen 3389")
}
private fun getJSONFromReq(req: AsyncHttpServerRequest): JSONObject? {
var obj: JSONObject? = null
val body = req.getBody<AsyncHttpRequestBody<*>>()
if (body is JSONObjectBody) {
obj = body.get()
} else if (body is StringBody) {
try {
obj = JSONObject(body.get())
} catch (e: JSONException) {
e.printStackTrace()
}
}
return obj
}
}

View File

@ -11,14 +11,14 @@ data class LedOkAdvertise(
data class LedOkBindModel(
val _type: String,
val bindModel: BindModel
val bindModel: cn.trans88.taxiappkotlin.ledok.BindModel
)
data class BingModel(
data class BindModel(
val action: String,
val accountIdToken: String,
val server: String,
val tlsServer: String
var server: String,
var tlsServer: String
)
data class Task(

View File

@ -2,33 +2,16 @@ package cn.trans88.taxiappkotlin.ledok
import android.content.Intent
import android.os.Build
import android.os.Environment
import android.os.FileUtils
import android.util.Base64
import android.util.Log
import androidx.annotation.RequiresApi
import cn.trans88.taxiappkotlin.R
import cn.trans88.taxiappkotlin.TaxiApp
import cn.trans88.taxiappkotlin.TaxiApp.Companion.instance
import cn.trans88.taxiappkotlin.logic.network.JoeyDownloadListener
import cn.trans88.taxiappkotlin.play.kuroPlay.IKuroPlay
import com.google.gson.Gson
import com.liulishuo.filedownloader.BaseDownloadTask
import com.liulishuo.filedownloader.FileDownloadListener
import com.liulishuo.filedownloader.FileDownloader
import org.java_websocket.WebSocket
import java.io.BufferedReader
import java.io.File
import java.io.FileOutputStream
import java.io.FileReader
import java.io.IOException
import java.io.OutputStream
import java.net.NetworkInterface
import java.net.SocketException
import java.nio.ByteBuffer
import java.util.concurrent.CountDownLatch
import kotlin.reflect.jvm.internal.impl.load.kotlin.JvmType
object LedOkCommands {
@ -112,105 +95,105 @@ object LedOkCommands {
/**
* ledok平台的命令
*/
fun executeByLedOk(conn: WebSocket, command: String) {
Log.d(TAG, "executeByLedOk: command: $command")
try {
val ledOkAdvertise = gson.fromJson(command, LedOkAdvertise::class.java)
Log.d(TAG, "executeByLedOk: baskTask: $ledOkAdvertise")
if (ledOkAdvertise.type != null) {
when (ledOkAdvertise.type) {
"consult" -> consult(conn, ledOkAdvertise)
"proStart" -> proStart(conn, ledOkAdvertise)
"fileStart" -> fileStart(conn, ledOkAdvertise)
"fileEnd" -> fileEnd(conn, ledOkAdvertise)
"proEnd" -> proEnd(conn, ledOkAdvertise)
else -> Log.w(TAG, "executeByLedOk: 三乐 未知命令")
}
}
} catch (e: Exception) {
Log.e(TAG, "executeByLedOk: 三乐长连接执行命令出现异常: ${e.printStackTrace()}")
}
}
fun consult(conn: WebSocket, ledOkAdvertise: LedOkAdvertise) {
// if ("program".equals(ledOkAdvertise.proName)) {
val items = ledOkAdvertise.task.items
val idList: MutableList<String> = mutableListOf()
playlist.clear()
for (item in items) {
val file = File("${downloadDir.absolutePath}/${item.itemId}")
if (!file.exists()) {
idList.add(item.itemId)
}
item.path = file.absolutePath
playlist.add(item)
}
val resMap = HashMap<String, Any?>()
resMap["type"] = "consult"
resMap["idList"] = idList
conn.send(gson.toJson(resMap))
// fun executeByLedOk(conn: WebSocket, command: String) {
// Log.d(TAG, "executeByLedOk: command: $command")
// try {
// val ledOkAdvertise = gson.fromJson(command, LedOkAdvertise::class.java)
// Log.d(TAG, "executeByLedOk: baskTask: $ledOkAdvertise")
// if (ledOkAdvertise.type != null) {
// when (ledOkAdvertise.type) {
// "consult" -> consult(conn, ledOkAdvertise)
// "proStart" -> proStart(conn, ledOkAdvertise)
// "fileStart" -> fileStart(conn, ledOkAdvertise)
// "fileEnd" -> fileEnd(conn, ledOkAdvertise)
// "proEnd" -> proEnd(conn, ledOkAdvertise)
// else -> Log.w(TAG, "executeByLedOk: 三乐 未知命令")
// }
}
fun proStart(conn: WebSocket, ledOkAdvertise: LedOkAdvertise) {
Log.d(TAG, "proStart: ")
}
fun fileStart(conn: WebSocket, ledOkAdvertise: LedOkAdvertise) {
Log.d(TAG, "fileStart: ")
currentFile = ledOkAdvertise.task.items[0].itemId
Log.d(TAG, "fileStart: currentFIle: $currentFile")
}
fun fileEnd(conn: WebSocket, ledOkAdvertise: LedOkAdvertise) {
Log.d(TAG, "fileEnd: ")
val currentFilePath = File("${downloadDir.absolutePath}/$currentFile")
Log.d(TAG, "fileEnd: currentFilePath: $currentFilePath")
// if (!currentFilePath.exists()) {
// }
// } catch (e: Exception) {
// Log.e(TAG, "executeByLedOk: 三乐长连接执行命令出现异常: ${e.printStackTrace()}")
// }
// }
//
// fun consult(conn: WebSocket, ledOkAdvertise: LedOkAdvertise) {
//// if ("program".equals(ledOkAdvertise.proName)) {
// val items = ledOkAdvertise.task.items
// val idList: MutableList<String> = mutableListOf()
// playlist.clear()
// for (item in items) {
// val file = File("${downloadDir.absolutePath}/${item.itemId}")
// if (!file.exists()) {
// idList.add(item.itemId)
// }
// item.path = file.absolutePath
// playlist.add(item)
// }
// val resMap = HashMap<String, Any?>()
// resMap["type"] = "consult"
// resMap["idList"] = idList
// conn.send(gson.toJson(resMap))
//// }
// }
//
// fun proStart(conn: WebSocket, ledOkAdvertise: LedOkAdvertise) {
// Log.d(TAG, "proStart: ")
// }
//
//
// fun fileStart(conn: WebSocket, ledOkAdvertise: LedOkAdvertise) {
// Log.d(TAG, "fileStart: ")
// currentFile = ledOkAdvertise.task.items[0].itemId
// Log.d(TAG, "fileStart: currentFIle: $currentFile")
// }
//
// fun fileEnd(conn: WebSocket, ledOkAdvertise: LedOkAdvertise) {
// Log.d(TAG, "fileEnd: ")
// val currentFilePath = File("${downloadDir.absolutePath}/$currentFile")
// Log.d(TAG, "fileEnd: currentFilePath: $currentFilePath")
//// if (!currentFilePath.exists()) {
//// val resMap = HashMap<String, Any?>()
//// resMap["type"] = "AckFailed"
//// conn.send(gson.toJson(resMap))
//// } else {
//// currentFile = null
//// }
// currentFile = null
// }
//
// fun proEnd(conn: WebSocket, ledOkAdvertise: LedOkAdvertise) {
// Log.d(TAG, "proEnd: ")
// playAdvertise(ledOkAdvertise)
// val resMap = HashMap<String, Any?>()
// resMap["type"] = "AckSuccess"
// conn.send(gson.toJson(resMap))
// }
//
//
// fun receiveFile(conn: WebSocket, message: ByteBuffer) {
// Log.d(TAG, "executeByLedOk: command: ")
// try {
// val data = ByteArray(message.remaining());
// message.get(data)
// val downloadDir = File(
// TaxiApp.instance().filesDir,
// TaxiApp.instance().getString(R.string.dir_program)
// )
// if (!downloadDir.exists()) {
// downloadDir.mkdirs()
// }
// Log.d(TAG, "prepareAdvertise: downloadDir: " + downloadDir.absolutePath)
// val downloadFile = File("${downloadDir.absolutePath}/$currentFile")
// FileOutputStream(downloadFile).use { outputStream ->
// outputStream.write(data)
// }
// } catch (e: Exception) {
// Log.e(TAG, "executeByLedOk: 三乐长连接执行命令出现异常: ${e.printStackTrace()}")
// val resMap = HashMap<String, Any?>()
// resMap["type"] = "AckFailed"
// conn.send(gson.toJson(resMap))
// } else {
// currentFile = null
// }
currentFile = null
}
fun proEnd(conn: WebSocket, ledOkAdvertise: LedOkAdvertise) {
Log.d(TAG, "proEnd: ")
playAdvertise(ledOkAdvertise)
val resMap = HashMap<String, Any?>()
resMap["type"] = "AckSuccess"
conn.send(gson.toJson(resMap))
}
fun receiveFile(conn: WebSocket, message: ByteBuffer) {
Log.d(TAG, "executeByLedOk: command: ")
try {
val data = ByteArray(message.remaining());
message.get(data)
val downloadDir = File(
TaxiApp.instance().filesDir,
TaxiApp.instance().getString(R.string.dir_program)
)
if (!downloadDir.exists()) {
downloadDir.mkdirs()
}
Log.d(TAG, "prepareAdvertise: downloadDir: " + downloadDir.absolutePath)
val downloadFile = File("${downloadDir.absolutePath}/$currentFile")
FileOutputStream(downloadFile).use { outputStream ->
outputStream.write(data)
}
} catch (e: Exception) {
Log.e(TAG, "executeByLedOk: 三乐长连接执行命令出现异常: ${e.printStackTrace()}")
val resMap = HashMap<String, Any?>()
resMap["type"] = "AckFailed"
conn.send(gson.toJson(resMap))
}
}
// }
//
/**
* 播放广告

View File

@ -1,16 +1,10 @@
package cn.trans88.taxiappkotlin.ledok
import android.content.Context
import android.content.Intent
import android.util.Log
import cn.trans88.taxiappkotlin.R
import cn.trans88.taxiappkotlin.TaxiApp
import cn.trans88.taxiappkotlin.TaxiApp.Companion.instance
import cn.trans88.taxiappkotlin.ledok.LedOkCommands.PLAYLIST
import cn.trans88.taxiappkotlin.ledok.LedOkCommands.playlist
import cn.trans88.taxiappkotlin.logic.model.BindModel
import cn.trans88.taxiappkotlin.logic.network.ConnManger
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.trs88.kurolibrary.log.KuroLog
import java.io.BufferedReader
@ -79,7 +73,7 @@ class TaskSocketServer(private val context: Context, port: Int) {
try {
//只保持一个链接,新的进来就把老的断开
val socket = soc!!.accept()
socket.soTimeout = 5000
socket.soTimeout = 5000 * 5
if (client != null) {
client!!.close()
}
@ -454,16 +448,20 @@ class TaskSocketServer(private val context: Context, port: Int) {
dos = null
file = null
//TaskProcessor.getInstance(context).updateSourceFile(new SourceFile(cmd.id, servant.getCanUsedPath(context), FileState.Is_Downloaded));
} else if (cmd._type == "BindAccount") {
Log.d(TAG, "processPackage: BindAccount")
// val bindModel = cmd.bindModel
}
// else if (cmd._type == "BindAccount") {
// Log.d(TAG, "processPackage: BindAccount")
// val server = cmd.server!!
// val tlsServer = cmd.tlsServer!!
// val accountIdToken = cmd.accountIdToken!!
// val bindModel = BindModel("", accountIdToken, server, tlsServer)!!
// if (bindModel?.server != null && bindModel?.server != "") {
// bindModel?.server = bindModel?.server + "/"
// }
// if (bindModel?.server != null && bindModel.server != "") {
// bindModel.tlsServer = bindModel.tlsServer + "/"
// }
// val mbindAccount = bindModel?.let { ConnManger.mbindAccount(it) }
// val mbindAccount = ConnManger.mbindAccount(bindModel)
// if (mbindAccount == true) {
// val ack = "{\"_type\":\"AckSuccess\"}"
// sendDataToClient(ack)
@ -472,7 +470,7 @@ class TaskSocketServer(private val context: Context, port: Int) {
// sendDataToClient(ack)
// }
// client?.close()
}
// }
} catch (e: Exception) {
Log.i(TAG, "error:" + e.message)
e.printStackTrace()
@ -554,7 +552,11 @@ class TaskSocketServer(private val context: Context, port: Int) {
var _id: String? = null
var idList: List<String>? = null
var task: Task? = null
var bindModel: BindModel? = null
// var bindModel: cn.trans88.taxiappkotlin.ledok.BindModel? = null
var accountIdToken: String? = null
var server: String? = null
var tlsServer: String? = null
}
var idList1: List<String>? = null

View File

@ -15,7 +15,6 @@ import cn.trans88.taxiappkotlin.ext.loge
//import cn.trans88.taxiappkotlin.ledok.WsManagerLedOk
import cn.trans88.taxiappkotlin.logic.Repository
import cn.trans88.taxiappkotlin.logic.Repository.taxiApp
import cn.trans88.taxiappkotlin.logic.dao.AdvertiseDao
import cn.trans88.taxiappkotlin.logic.dao.DaoUtil
import cn.trans88.taxiappkotlin.logic.dao.DaoUtil.getAdvertise
import cn.trans88.taxiappkotlin.logic.dao.DaoUtil.getFirstPlayDao
@ -26,7 +25,6 @@ import cn.trans88.taxiappkotlin.net.OkHttpTool
import cn.trans88.taxiappkotlin.play.RefreshPlayer
import cn.trans88.taxiappkotlin.ui.play.PlayActivity
import cn.trans88.taxiappkotlin.ui.play.PlayViewModel
import cn.trans88.taxiappkotlin.util.DateUtil.daysBetweenTimestamps
import cn.trans88.taxiappkotlin.util.KuroTimer
import cn.trans88.taxiappkotlin.util.YoungUtil
import cn.trans88.taxiappkotlin.util.checkAppToken
@ -46,7 +44,6 @@ import okhttp3.*
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import java.io.File
import java.io.IOException
import java.lang.ref.WeakReference
import java.util.*
import java.util.concurrent.TimeUnit
import kotlin.concurrent.thread