11package top.fumiama.copymanga.api
22
3+ import android.util.Log
34import com.bumptech.glide.load.model.LazyHeaders
5+ import com.google.gson.Gson
6+ import kotlinx.coroutines.runBlocking
7+ import kotlinx.coroutines.sync.Mutex
8+ import kotlinx.coroutines.sync.withLock
49import top.fumiama.copymanga.MainActivity
10+ import top.fumiama.copymanga.json.NetworkStructure
11+ import top.fumiama.copymanga.net.DownloadTools
12+ import top.fumiama.copymanga.net.Proxy
13+ import top.fumiama.copymanga.net.Resolution
514import top.fumiama.copymanga.storage.PreferenceBoolean
615import top.fumiama.copymanga.storage.PreferenceInt
716import top.fumiama.copymanga.storage.PreferenceString
817import top.fumiama.copymanga.storage.UserPreferenceInt
918import top.fumiama.copymanga.storage.UserPreferenceString
10- import top.fumiama.copymanga.net.Proxy
11- import top.fumiama.copymanga.net.Resolution
1219import top.fumiama.dmzj.copymanga.R
1320import java.io.File
1421
@@ -53,9 +60,52 @@ object Config {
5360 return field
5461 }
5562
56- val myHostApiUrl = PreferenceString (" settings_cat_net_et_api_url" , R .string.hostUrl)
63+ val proxyUrl = MainActivity .mainWeakReference?.get()?.getString(R .string.proxyUrl)!!
64+ private val reverseProxyUrl = PreferenceString (R .string.reverseProxyKeyID)
65+ private val networkApiUrl = PreferenceString (" settings_cat_net_et_api_url" , R .string.hostUrl)
66+ private var mHostApiUrls: Array <String > = arrayOf()
67+ private var mHostApiUrlsMutex = Mutex ()
68+ val myHostApiUrl: Array <String >
69+ get() {
70+ if (mHostApiUrls.isNotEmpty()) return mHostApiUrls
71+ if (reverseProxyUrl.value.isNotEmpty() && reverseProxyUrl.value != proxyUrl) {
72+ mHostApiUrls = arrayOf(reverseProxyUrl.value)
73+ Log .d(" MyC" , " myHostApiUrl set reverse proxy to ${mHostApiUrls[0 ]} " )
74+ return mHostApiUrls
75+ }
76+ MainActivity .mainWeakReference?.get()?.apply {
77+ runBlocking {
78+ mHostApiUrlsMutex.withLock {
79+ if (mHostApiUrls.isNotEmpty()) return @runBlocking
80+ try {
81+ val u = getString(R .string.networkApiUrl).format(networkApiUrl.value)
82+ val r = Gson ().fromJson((apiProxy?.comancry(u) {
83+ DownloadTools .getHttpContent(it, referer, pc_ua)
84+ }? : DownloadTools .getHttpContent(u, referer, pc_ua)).decodeToString(), NetworkStructure ::class .java)
85+ if (r != null ) {
86+ Log .d(" MyC" , " myHostApiUrl get code ${r.code} msg ${r.message} " )
87+ if (r.code == 200 && r.results != null ) {
88+ // need header umstring
89+ // r.results.api.forEach { it.forEach { api -> if (!api.isNullOrEmpty() && api !in field) field += api } }
90+ r.results.share.forEach { api -> if (! api.isNullOrEmpty() && api !in mHostApiUrls) mHostApiUrls + = api }
91+ }
92+ }
93+ } catch (e: Exception ) {
94+ e.printStackTrace()
95+ mHostApiUrls = arrayOf(networkApiUrl.value)
96+ }
97+ if (mHostApiUrls.isEmpty()) {
98+ mHostApiUrls = arrayOf(networkApiUrl.value)
99+ Log .d(" MyC" , " myHostApiUrl set default ${mHostApiUrls[0 ]} " )
100+ }
101+ }
102+ }
103+ }
104+ Log .d(" MyC" , " myHostApiUrl get hosts ${mHostApiUrls.joinToString(" , " )} " )
105+ return mHostApiUrls
106+ }
57107 val navTextInfo = UserPreferenceString (" navTextInfo" , R .string.navTextInfo)
58- val proxy_key = PreferenceString (R .string.imgProxyKeyID )
108+ val proxy_key = PreferenceString (R .string.imgProxyCodeKeyID )
59109 val app_ver = PreferenceString (" settings_cat_general_et_app_version" , R .string.app_ver)
60110 val token = UserPreferenceString (" token" , " " , null )
61111 val pc_ua get() = MainActivity .mainWeakReference?.get()?.getString(R .string.pc_ua)?.format(app_ver.value)? : " "
@@ -80,6 +130,7 @@ object Config {
80130 val net_use_api_proxy = PreferenceBoolean (" settings_cat_net_sw_use_api_proxy" , false )
81131 val net_img_resolution = PreferenceString (R .string.imgResolutionKeyID)
82132
133+ val view_manga_inverse_chapters = PreferenceBoolean (" settings_cat_vm_sw_inverse_chapters" , false )
83134 val view_manga_always_dark_bg = PreferenceBoolean (" settings_cat_vm_sw_always_dark_bg" , false )
84135 val view_manga_vertical_max = PreferenceInt (" settings_cat_vm_sb_vertical_max" , 20 )
85136 val view_manga_quality = PreferenceInt (" settings_cat_vm_sb_quality" , 100 )
@@ -92,5 +143,5 @@ object Config {
92143
93144 fun getChapterInfoApiUrl (path : String? , uuid : String? , version : Int ) =
94145 MainActivity .mainWeakReference?.get()?.getString(R .string.chapterInfoApiUrl)
95- ?.format(myHostApiUrl.value , path, if (version >= 2 ) " $version " else " " , uuid)
146+ ?.format(myHostApiUrl.random() , path, if (version >= 2 ) " $version " else " " , uuid)
96147}
0 commit comments