package com.gh.gamecenter.mock.mockinizer

import com.gh.gamecenter.mock.mockwebserver3.MockWebServer
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import okhttp3.tls.HandshakeCertificates
import okhttp3.tls.HeldCertificate


//原库是固定了一个port = 34567端口 但是固定一个端口的话 会产生崩溃 项目有多个apiService的创建 这边也会调用多次 导致端口被占用
internal fun MockWebServer.configure(port: Int = ((0..9999).random() + 30000)): MockWebServer {

    GlobalScope.launch {
        start(port)
    }

    val localhostCertificate = HeldCertificate.Builder()
        .addSubjectAlternativeName("127.0.0.1")
        .build()

    val serverCertificates = HandshakeCertificates.Builder()
        .heldCertificate(localhostCertificate)
        .build()

    useHttps(serverCertificates.sslSocketFactory(), false)
    return this
}