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:
Adam
2021-12-23 12:36:05 -05:00
parent 37d538f0db
commit 0a501429e6
14 changed files with 162 additions and 179 deletions

View File

@@ -38,6 +38,8 @@
<properties>
<jarsigner.skip>true</jarsigner.skip>
<pmd.skip>true</pmd.skip>
<git.commit.id.abbrev>nogit</git.commit.id.abbrev>
<git.dirty>false</git.dirty>
</properties>
<dependencies>
@@ -474,6 +476,30 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.6</version>
<executions>
<execution>
<id>query-git-info</id>
<goals>
<goal>revision</goal>
</goals>
<configuration>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
<failOnUnableToExtractRepoInfo>false</failOnUnableToExtractRepoInfo>
<gitDescribe>
<skip>true</skip>
</gitDescribe>
<includeOnlyProperties>
<includeOnlyProperty>git.commit.id.abbrev</includeOnlyProperty>
<includeOnlyProperty>git.dirty</includeOnlyProperty>
</includeOnlyProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -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)

View File

@@ -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);

View File

@@ -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);

View File

@@ -1,5 +1,7 @@
runelite.title=RuneLite
runelite.version=${project.version}
runelite.commit=${git.commit.id.abbrev}
runelite.dirty=${git.dirty}
runelite.discord.appid=409416265891971072
runelite.discord.invite=https://discord.gg/ArdAhnN
runelite.github.link=https://github.com/runelite

View File

@@ -30,7 +30,6 @@ import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import net.runelite.http.api.RuneLiteAPI;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
@@ -38,11 +37,12 @@ import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.Assert;
import static org.junit.Assert.assertTrue;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
public class OkHttpCacheSanityTest
public class OkHttpTest
{
@Rule
public TemporaryFolder cacheFolder = new TemporaryFolder();
@@ -71,9 +71,7 @@ public class OkHttpCacheSanityTest
@Test
public void testCacheSanity() throws IOException, InterruptedException
{
OkHttpClient.Builder builder = RuneLiteAPI.CLIENT.newBuilder();
RuneLite.setupCache(builder, cacheFolder.getRoot());
OkHttpClient client = builder.build();
OkHttpClient client = RuneLite.buildHttpClient(false);
Instant lastModified = Instant.now().minusSeconds(20);
@@ -122,4 +120,19 @@ public class OkHttpCacheSanityTest
Assert.assertNotNull("cache did not make a conditional request", req);
Assert.assertNotNull(req.getHeader("If-Modified-Since"));
}
@Test
public void testUserAgent() throws IOException, InterruptedException
{
server.enqueue(new MockResponse().setBody("OK"));
Request request = new Request.Builder()
.url(server.url("/"))
.build();
RuneLite.buildHttpClient(false)
.newCall(request).execute().close();
// rest of UA depends on if git is found
assertTrue(server.takeRequest().getHeader("User-Agent").startsWith("RuneLite/" + RuneLiteProperties.getVersion()));
}
}