Move okhttp client from http-api
The client has been recreated with a new builder off of the http-api client for awhile anyway since runelite-client has multiple other requirements (caching, tls, etc). This fully moves creation of the okhttp client into both http-service and runelite-client separately. I've kept the CLIENT field in http-api for now since a few external plugins depend on it currently.
This commit is contained in:
@@ -44,6 +44,7 @@ import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Stream;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Provider;
|
||||
@@ -80,6 +81,7 @@ import net.runelite.client.ui.overlay.worldmap.WorldMapOverlay;
|
||||
import net.runelite.http.api.RuneLiteAPI;
|
||||
import okhttp3.Cache;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -97,6 +99,7 @@ public class RuneLite
|
||||
public static final File DEFAULT_CONFIG_FILE = new File(RUNELITE_DIR, "settings.properties");
|
||||
|
||||
private static final int MAX_OKHTTP_CACHE_SIZE = 20 * 1024 * 1024; // 20mb
|
||||
public static String USER_AGENT = "RuneLite/" + RuneLiteProperties.getVersion() + "-" + RuneLiteProperties.getCommit() + (RuneLiteProperties.isDirty() ? "+" : "");
|
||||
|
||||
@Getter
|
||||
private static Injector injector;
|
||||
@@ -203,16 +206,8 @@ public class RuneLite
|
||||
}
|
||||
});
|
||||
|
||||
OkHttpClient.Builder okHttpClientBuilder = RuneLiteAPI.CLIENT.newBuilder();
|
||||
setupCache(okHttpClientBuilder, new File(CACHE_DIR, "okhttp"));
|
||||
|
||||
final boolean insecureSkipTlsVerification = options.has("insecure-skip-tls-verification");
|
||||
if (insecureSkipTlsVerification || RuneLiteProperties.isInsecureSkipTlsVerification())
|
||||
{
|
||||
setupInsecureTrustManager(okHttpClientBuilder);
|
||||
}
|
||||
|
||||
final OkHttpClient okHttpClient = okHttpClientBuilder.build();
|
||||
final OkHttpClient okHttpClient = buildHttpClient(options.has("insecure-skip-tls-verification"));
|
||||
RuneLiteAPI.CLIENT = okHttpClient;
|
||||
|
||||
SplashScreen.init();
|
||||
SplashScreen.stage(0, "Retrieving client", "");
|
||||
@@ -416,9 +411,20 @@ public class RuneLite
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
static void setupCache(OkHttpClient.Builder builder, File cacheDir)
|
||||
static OkHttpClient buildHttpClient(boolean insecureSkipTlsVerification)
|
||||
{
|
||||
builder.cache(new Cache(cacheDir, MAX_OKHTTP_CACHE_SIZE))
|
||||
OkHttpClient.Builder builder = new OkHttpClient.Builder()
|
||||
.pingInterval(30, TimeUnit.SECONDS)
|
||||
.addNetworkInterceptor(chain ->
|
||||
{
|
||||
Request userAgentRequest = chain.request()
|
||||
.newBuilder()
|
||||
.header("User-Agent", USER_AGENT)
|
||||
.build();
|
||||
return chain.proceed(userAgentRequest);
|
||||
})
|
||||
// Setup cache
|
||||
.cache(new Cache(new File(CACHE_DIR, "okhttp"), MAX_OKHTTP_CACHE_SIZE))
|
||||
.addNetworkInterceptor(chain ->
|
||||
{
|
||||
// This has to be a network interceptor so it gets hit before the cache tries to store stuff
|
||||
@@ -432,6 +438,13 @@ public class RuneLite
|
||||
}
|
||||
return res;
|
||||
});
|
||||
|
||||
if (insecureSkipTlsVerification || RuneLiteProperties.isInsecureSkipTlsVerification())
|
||||
{
|
||||
setupInsecureTrustManager(builder);
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private static void setupInsecureTrustManager(OkHttpClient.Builder okHttpClientBuilder)
|
||||
|
||||
@@ -35,6 +35,8 @@ import okhttp3.HttpUrl;
|
||||
public class RuneLiteProperties
|
||||
{
|
||||
private static final String RUNELITE_VERSION = "runelite.version";
|
||||
private static final String RUNELITE_COMMIT = "runelite.commit";
|
||||
private static final String RUNELITE_DIRTY = "runelite.dirty";
|
||||
private static final String DISCORD_INVITE = "runelite.discord.invite";
|
||||
private static final String LAUNCHER_VERSION_PROPERTY = "runelite.launcher.version";
|
||||
private static final String INSECURE_SKIP_TLS_VERIFICATION_PROPERTY = "runelite.insecure-skip-tls-verification";
|
||||
@@ -67,6 +69,16 @@ public class RuneLiteProperties
|
||||
return properties.getProperty(RUNELITE_VERSION);
|
||||
}
|
||||
|
||||
public static String getCommit()
|
||||
{
|
||||
return properties.getProperty(RUNELITE_COMMIT);
|
||||
}
|
||||
|
||||
public static boolean isDirty()
|
||||
{
|
||||
return Boolean.parseBoolean(properties.getProperty(RUNELITE_DIRTY));
|
||||
}
|
||||
|
||||
public static String getDiscordInvite()
|
||||
{
|
||||
return properties.getProperty(DISCORD_INVITE);
|
||||
|
||||
@@ -35,8 +35,8 @@ import javax.inject.Named;
|
||||
import javax.inject.Singleton;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.runelite.client.RuneLite;
|
||||
import net.runelite.client.eventbus.EventBus;
|
||||
import net.runelite.http.api.RuneLiteAPI;
|
||||
import net.runelite.http.api.ws.WebsocketGsonFactory;
|
||||
import net.runelite.http.api.ws.WebsocketMessage;
|
||||
import net.runelite.http.api.ws.messages.Handshake;
|
||||
@@ -106,7 +106,7 @@ public class WSClient extends WebSocketListener implements AutoCloseable
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(runeliteWs)
|
||||
.header("User-Agent", RuneLiteAPI.userAgent)
|
||||
.header("User-Agent", RuneLite.USER_AGENT)
|
||||
.build();
|
||||
|
||||
webSocket = okHttpClient.newWebSocket(request, this);
|
||||
|
||||
Reference in New Issue
Block a user