Skip to content

Commit

Permalink
fix: 修复gitee page暂停问题
Browse files Browse the repository at this point in the history
  • Loading branch information
goweii committed May 26, 2024
1 parent d248ed8 commit 1a9a089
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package per.goweii.wanandroid.http

import android.net.Uri
import okhttp3.HttpUrl
import okhttp3.Interceptor
import okhttp3.Response
import java.io.IOException

class GoweiiHostInterceptor : Interceptor {
companion object {
private const val GOWEII_HOST = "goweii"

private const val GITEE_PAGE_SERVER_BASE_URL = "https://goweii.gitee.io/wanandroidserver"
private const val GITEE_RAW_SERVER_BASE_URL = "https://gitee.com/goweii/WanAndroidServer/raw/master"
}

override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()

val httpUrl = request.url()
if (httpUrl.host() != GOWEII_HOST) {
return chain.proceed(request)
}

HttpUrl.parse(GITEE_PAGE_SERVER_BASE_URL)
?.let { chain.tryWithBaseUrl(it) }
?.takeIf { it.isSuccessful }
?.let { return it }

HttpUrl.parse(GITEE_RAW_SERVER_BASE_URL)
?.let { chain.tryWithBaseUrl(it) }
?.takeIf { it.isSuccessful }
?.let { return it }

throw IOException("Unknown goweii host.")
}

private fun Interceptor.Chain.tryWithBaseUrl(baseUrl: HttpUrl): Response {
val httpUrl = request().url().replaceWithBaseUrl(baseUrl)
return proceed(request().newBuilder().url(httpUrl).build())
}

private fun HttpUrl.replaceWithBaseUrl(baseUrl: HttpUrl): HttpUrl {
val uriBuilder = Uri.parse(baseUrl.toString()).buildUpon()
encodedPathSegments().forEach { uriBuilder.appendEncodedPath(it) }
uriBuilder.encodedQuery(encodedQuery())
val uri = uriBuilder.build()
return HttpUrl.parse(uri.toString())!!
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Map;

import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import per.goweii.basic.core.common.Config;
import per.goweii.basic.utils.DebugUtils;
Expand Down Expand Up @@ -65,4 +66,12 @@ public void setOkHttpClient(OkHttpClient.Builder builder) {
HttpsCompat.enableTls12ForHttpsURLConnection();
builder.cookieJar(mCookieJar);
}

@Nullable
@Override
public Interceptor[] getInterceptors() {
return new Interceptor[]{
new GoweiiHostInterceptor()
};
}
}
16 changes: 8 additions & 8 deletions app/src/main/java/per/goweii/wanandroid/http/WanApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,35 +72,35 @@ public interface ApiService {
@GET("https://v2.jinrishici.com/sentence")
Observable<WanResponse<JinrishiciBean>> getJinrishici(@retrofit2.http.Header("X-User-Token") String token);

@GET("https://goweii.gitee.io/wanandroidserver/web/article.json")
@GET("https://goweii/web/article.json")
@retrofit2.http.Headers({HEADER_USER_AGENT})
Observable<WanResponse<List<WebArticleUrlRegexBean>>> getWebArticleUrlRegex();

@GET("https://goweii.gitee.io/wanandroidserver/update/update.json")
@GET("https://goweii/update/update.json")
@retrofit2.http.Headers({HEADER_USER_AGENT})
Observable<WanResponse<UpdateBean>> update();

@GET("https://goweii.gitee.io/wanandroidserver/about/about_me.json")
@GET("https://goweii/about/about_me.json")
@retrofit2.http.Headers({HEADER_USER_AGENT})
Observable<WanResponse<AboutMeBean>> getAboutMe();

@GET("https://goweii.gitee.io/wanandroidserver/config/config.json")
@GET("https://goweii/config/config.json")
@retrofit2.http.Headers({HEADER_USER_AGENT})
Observable<WanResponse<ConfigBean>> getConfig();

@GET("https://goweii.gitee.io/wanandroidserver/advert/advert.json")
@GET("https://goweii/advert/advert.json")
@retrofit2.http.Headers({HEADER_USER_AGENT})
Observable<WanResponse<AdvertBean>> getAdvert();

@GET("https://goweii.gitee.io/wanandroidserver/advert/recommend.json")
@GET("https://goweii/advert/recommend.json")
@retrofit2.http.Headers({HEADER_USER_AGENT})
Observable<WanResponse<RecommendBean>> getRecommend();

@GET("https://goweii.gitee.io/wanandroidserver/update/beta/update.json")
@GET("https://goweii/update/beta/update.json")
@retrofit2.http.Headers({HEADER_USER_AGENT})
Observable<WanResponse<UpdateBean>> betaUpdate();

@GET("https://goweii.gitee.io/wanandroidserver/update/beta/users.json")
@GET("https://goweii/update/beta/users.json")
@retrofit2.http.Headers({HEADER_USER_AGENT})
Observable<WanResponse<List<BetaUserBean>>> betaUsers();

Expand Down

0 comments on commit 1a9a089

Please sign in to comment.