Add account plugin, and support for acquiring a session with the account webservice

This commit is contained in:
Adam
2017-05-14 19:23:44 -04:00
parent aebeba06ad
commit cf4e9fab1d
19 changed files with 1144 additions and 30 deletions

View File

@@ -26,18 +26,25 @@ package net.runelite.client;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.SubscriberExceptionContext;
import com.google.gson.Gson;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import net.runelite.api.Client;
import net.runelite.client.account.AccountSession;
import net.runelite.client.events.SessionOpen;
import net.runelite.client.menus.MenuManager;
import net.runelite.client.plugins.PluginManager;
import net.runelite.client.ui.ClientUI;
@@ -50,7 +57,7 @@ public class RuneLite
private static final Logger logger = LoggerFactory.getLogger(RuneLite.class);
public static final File RUNELITE_DIR = new File(System.getProperty("user.home"), ".runelite");
public static final File REPO_DIR = new File(RUNELITE_DIR, "repository");
public static final File SESSION_FILE = new File(RUNELITE_DIR, "session");
public static Image ICON;
@@ -65,7 +72,9 @@ public class RuneLite
private OverlayRenderer renderer;
private EventBus eventBus = new EventBus(this::eventExceptionHandler);
private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(4);
private final WSClient wsclient = new WSClient();
private WSClient wsclient;
private AccountSession accountSession;
static
{
@@ -106,6 +115,8 @@ public class RuneLite
pluginManager.loadAll();
renderer = new OverlayRenderer();
loadSession();
}
private void setupTrayIcon()
@@ -130,6 +141,79 @@ public class RuneLite
}
}
private void loadSession()
{
if (!SESSION_FILE.exists())
{
logger.info("No session file exists");
return;
}
try (FileInputStream in = new FileInputStream(SESSION_FILE))
{
accountSession = new Gson().fromJson(new InputStreamReader(in), AccountSession.class);
logger.debug("Loaded session for {}", accountSession.getUsername());
}
catch (Exception ex)
{
logger.warn("Unable to load session file", ex);
return;
}
openSession(accountSession);
}
public void saveSession()
{
if (accountSession == null)
{
return;
}
try (FileWriter fw = new FileWriter(SESSION_FILE))
{
new Gson().toJson(accountSession, fw);
logger.debug("Saved session to {}", SESSION_FILE);
}
catch (IOException ex)
{
logger.warn("Unable to save session file", ex);
}
}
/**
* Set the given session as the active session and open a socket to the
* server with the given session
* @param session
*/
public void openSession(AccountSession session)
{
boolean needExecutor = false;
if (wsclient != null)
{
wsclient.close();
}
else
{
needExecutor = true;
}
wsclient = new WSClient(session);
wsclient.connect();
if (needExecutor)
{
executor.scheduleWithFixedDelay(wsclient::ping, WSClient.PING_TIME.getSeconds(), WSClient.PING_TIME.getSeconds(), TimeUnit.SECONDS);
}
accountSession = session;
eventBus.post(new SessionOpen());
}
private void eventExceptionHandler(Throwable exception, SubscriberExceptionContext context)
{
logger.warn("uncaught exception in event subscriber", exception);
@@ -189,4 +273,9 @@ public class RuneLite
{
return trayIcon;
}
public AccountSession getAccountSession()
{
return accountSession;
}
}

View File

@@ -24,7 +24,16 @@
*/
package net.runelite.client;
import com.google.common.eventbus.EventBus;
import com.google.gson.Gson;
import java.time.Duration;
import java.time.Instant;
import net.runelite.client.account.AccountSession;
import net.runelite.http.api.RuneliteAPI;
import net.runelite.http.api.ws.messages.Handshake;
import net.runelite.http.api.ws.messages.Ping;
import net.runelite.http.api.ws.WebsocketGsonFactory;
import net.runelite.http.api.ws.WebsocketMessage;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
@@ -37,22 +46,47 @@ public class WSClient extends WebSocketListener implements AutoCloseable
{
private static final Logger logger = LoggerFactory.getLogger(WSClient.class);
public static final Duration PING_TIME = Duration.ofSeconds(30);
private static final Gson gson = WebsocketGsonFactory.build();
private static final EventBus eventBus = RuneLite.getRunelite().getEventBus();
private final OkHttpClient client = new OkHttpClient();
private final AccountSession session;
private WebSocket webSocket;
public WSClient()
public WSClient(AccountSession session)
{
connect();
this.session = session;
}
private void connect()
public void connect()
{
Request request = new Request.Builder()
.url(RuneliteAPI.getWsEndpoint())
.build();
webSocket = client.newWebSocket(request, this);
Handshake handshake = new Handshake();
handshake.setSession(session.getUuid());
send(handshake);
}
public void ping()
{
Ping ping = new Ping();
ping.setTime(Instant.now());
send(ping);
}
public void send(WebsocketMessage message)
{
String json = gson.toJson(message, WebsocketMessage.class);
webSocket.send(json);
logger.debug("Sent: {}", json);
}
@Override
@@ -65,14 +99,15 @@ public class WSClient extends WebSocketListener implements AutoCloseable
public void onOpen(WebSocket webSocket, Response response)
{
logger.info("Websocket {} opened", webSocket);
webSocket.send("Hello");
}
@Override
public void onMessage(WebSocket webSocket, String text)
{
logger.debug("Got message: {}", text);
WebsocketMessage message = gson.fromJson(text, WebsocketMessage.class);
logger.debug("Got message: {}", message);
eventBus.post(message);
}
@Override

View File

@@ -0,0 +1,65 @@
/*
* Copyright (c) 2017, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.account;
import java.time.Instant;
import java.util.UUID;
public class AccountSession
{
private UUID uuid;
private String username;
private Instant created;
public UUID getUuid()
{
return uuid;
}
public void setUuid(UUID uuid)
{
this.uuid = uuid;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public Instant getCreated()
{
return created;
}
public void setCreated(Instant created)
{
this.created = created;
}
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright (c) 2017, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.events;
/**
* Called when a session has been opened with the server
* @author Adam
*/
public class SessionOpen
{
}

View File

@@ -32,6 +32,7 @@ import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import net.runelite.client.RuneLite;
import net.runelite.client.plugins.account.AccountPlugin;
import net.runelite.client.plugins.boosts.Boosts;
import net.runelite.client.plugins.bosstimer.BossTimers;
import net.runelite.client.plugins.clanchat.ClanChat;
@@ -75,6 +76,7 @@ public class PluginManager
plugins.add(new PestControl());
plugins.add(new ClanChat());
plugins.add(new Zulrah());
plugins.add(new AccountPlugin());
if (RuneLite.getOptions().has("developer-mode"))
{

View File

@@ -0,0 +1,156 @@
/*
* Copyright (c) 2017, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.plugins.account;
import com.google.common.eventbus.Subscribe;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Instant;
import java.util.concurrent.ScheduledExecutorService;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import net.runelite.client.RuneLite;
import net.runelite.client.account.AccountSession;
import net.runelite.client.events.SessionOpen;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.ui.ClientUI;
import net.runelite.client.ui.NavigationButton;
import net.runelite.client.util.RunnableExceptionLogger;
import net.runelite.http.api.account.LoginClient;
import net.runelite.http.api.account.OAuthResponse;
import net.runelite.http.api.ws.messages.LoginResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AccountPlugin extends Plugin
{
private static final Logger logger = LoggerFactory.getLogger(AccountPlugin.class);
private final RuneLite runelite = RuneLite.getRunelite();
private final ClientUI ui = runelite.getGui();
private final NavigationButton loginButton = new NavigationButton("Login");
private final LoginClient loginClient = new LoginClient();
@Override
protected void startUp() throws Exception
{
loginButton.getButton().addActionListener(this::loginClick);
ImageIcon icon = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("login_icon.png")));
loginButton.getButton().setIcon(icon);
ui.getNavigationPanel().addNavigation(loginButton);
}
@Override
protected void shutDown() throws Exception
{
}
private void loginClick(ActionEvent ae)
{
ScheduledExecutorService executor = runelite.getExecutor();
executor.execute(RunnableExceptionLogger.wrap(this::openLoginPage));
}
private void openLoginPage()
{
OAuthResponse login;
try
{
login = loginClient.login();
}
catch (IOException ex)
{
logger.warn("Unable to get oauth url", ex);
return;
}
// Create new session
AccountSession session = new AccountSession();
session.setUuid(login.getUid());
session.setCreated(Instant.now());
runelite.openSession(session);
if (!Desktop.isDesktopSupported())
{
logger.info("Desktop is not supported. Visit {}", login.getOauthUrl());
return;
}
Desktop desktop = Desktop.getDesktop();
if (!desktop.isSupported(Desktop.Action.BROWSE))
{
logger.info("Desktop browser is not supported. Visit {}", login.getOauthUrl());
return;
}
try
{
desktop.browse(new URI(login.getOauthUrl()));
logger.debug("Opened browser to {}", login.getOauthUrl());
}
catch (IOException | URISyntaxException ex)
{
logger.warn("Unable to open login page", ex);
}
}
@Subscribe
public void onLogin(LoginResponse loginResponse)
{
logger.debug("Now logged in as {}", loginResponse.getUsername());
runelite.getGui().setTitle("RuneLite (" + loginResponse.getUsername() + ")");
AccountSession session = runelite.getAccountSession();
session.setUsername(loginResponse.getUsername());
runelite.saveSession();
}
@Subscribe
public void onSessionOpen(SessionOpen sessionOpen)
{
AccountSession session = runelite.getAccountSession();
if (session.getUsername() == null)
{
return; // No username yet
}
logger.debug("Session opened as {}", session.getUsername());
runelite.getGui().setTitle("RuneLite (" + session.getUsername() + ")");
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 463 B