Add account plugin, and support for acquiring a session with the account webservice
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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.http.api.account;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import net.runelite.http.api.RuneliteAPI;
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class LoginClient
|
||||
{
|
||||
private static final Logger logger = LoggerFactory.getLogger(LoginClient.class);
|
||||
|
||||
private final OkHttpClient client = new OkHttpClient();
|
||||
private final Gson gson = new Gson();
|
||||
|
||||
public OAuthResponse login() throws IOException
|
||||
{
|
||||
HttpUrl.Builder builder = RuneliteAPI.getApiBase().newBuilder()
|
||||
.addPathSegment("account")
|
||||
.addPathSegment("login");
|
||||
|
||||
HttpUrl url = builder.build();
|
||||
|
||||
logger.debug("Built URI: {}", url);
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(url)
|
||||
.build();
|
||||
|
||||
Response response = client.newCall(request).execute();
|
||||
|
||||
try (ResponseBody body = response.body())
|
||||
{
|
||||
InputStream in = body.byteStream();
|
||||
return gson.fromJson(new InputStreamReader(in), OAuthResponse.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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.http.api.account;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class OAuthResponse
|
||||
{
|
||||
private String oauthUrl;
|
||||
private UUID uid;
|
||||
|
||||
public String getOauthUrl()
|
||||
{
|
||||
return oauthUrl;
|
||||
}
|
||||
|
||||
public void setOauthUrl(String oauthUrl)
|
||||
{
|
||||
this.oauthUrl = oauthUrl;
|
||||
}
|
||||
|
||||
public UUID getUid()
|
||||
{
|
||||
return uid;
|
||||
}
|
||||
|
||||
public void setUid(UUID uid)
|
||||
{
|
||||
this.uid = uid;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
/*
|
||||
* Copyright (C) 2011 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.runelite.http.api.ws;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.TypeAdapterFactory;
|
||||
import com.google.gson.internal.Streams;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
/**
|
||||
* Adapts values whose runtime type may differ from their declaration type. This
|
||||
* is necessary when a field's type is not the same type that GSON should create
|
||||
* when deserializing that field. For example, consider these types:
|
||||
* <pre> {@code
|
||||
* abstract class Shape {
|
||||
* int x;
|
||||
* int y;
|
||||
* }
|
||||
* class Circle extends Shape {
|
||||
* int radius;
|
||||
* }
|
||||
* class Rectangle extends Shape {
|
||||
* int width;
|
||||
* int height;
|
||||
* }
|
||||
* class Diamond extends Shape {
|
||||
* int width;
|
||||
* int height;
|
||||
* }
|
||||
* class Drawing {
|
||||
* Shape bottomShape;
|
||||
* Shape topShape;
|
||||
* }
|
||||
* }</pre>
|
||||
* <p>Without additional type information, the serialized JSON is ambiguous. Is
|
||||
* the bottom shape in this drawing a rectangle or a diamond? <pre> {@code
|
||||
* {
|
||||
* "bottomShape": {
|
||||
* "width": 10,
|
||||
* "height": 5,
|
||||
* "x": 0,
|
||||
* "y": 0
|
||||
* },
|
||||
* "topShape": {
|
||||
* "radius": 2,
|
||||
* "x": 4,
|
||||
* "y": 1
|
||||
* }
|
||||
* }}</pre>
|
||||
* This class addresses this problem by adding type information to the
|
||||
* serialized JSON and honoring that type information when the JSON is
|
||||
* deserialized: <pre> {@code
|
||||
* {
|
||||
* "bottomShape": {
|
||||
* "type": "Diamond",
|
||||
* "width": 10,
|
||||
* "height": 5,
|
||||
* "x": 0,
|
||||
* "y": 0
|
||||
* },
|
||||
* "topShape": {
|
||||
* "type": "Circle",
|
||||
* "radius": 2,
|
||||
* "x": 4,
|
||||
* "y": 1
|
||||
* }
|
||||
* }}</pre>
|
||||
* Both the type field name ({@code "type"}) and the type labels ({@code
|
||||
* "Rectangle"}) are configurable.
|
||||
*
|
||||
* <h3>Registering Types</h3>
|
||||
* Create a {@code RuntimeTypeAdapterFactory} by passing the base type and type field
|
||||
* name to the {@link #of} factory method. If you don't supply an explicit type
|
||||
* field name, {@code "type"} will be used. <pre> {@code
|
||||
* RuntimeTypeAdapterFactory<Shape> shapeAdapterFactory
|
||||
* = RuntimeTypeAdapterFactory.of(Shape.class, "type");
|
||||
* }</pre>
|
||||
* Next register all of your subtypes. Every subtype must be explicitly
|
||||
* registered. This protects your application from injection attacks. If you
|
||||
* don't supply an explicit type label, the type's simple name will be used.
|
||||
* <pre> {@code
|
||||
* shapeAdapter.registerSubtype(Rectangle.class, "Rectangle");
|
||||
* shapeAdapter.registerSubtype(Circle.class, "Circle");
|
||||
* shapeAdapter.registerSubtype(Diamond.class, "Diamond");
|
||||
* }</pre>
|
||||
* Finally, register the type adapter factory in your application's GSON builder:
|
||||
* <pre> {@code
|
||||
* Gson gson = new GsonBuilder()
|
||||
* .registerTypeAdapterFactory(shapeAdapterFactory)
|
||||
* .create();
|
||||
* }</pre>
|
||||
* Like {@code GsonBuilder}, this API supports chaining: <pre> {@code
|
||||
* RuntimeTypeAdapterFactory<Shape> shapeAdapterFactory = RuntimeTypeAdapterFactory.of(Shape.class)
|
||||
* .registerSubtype(Rectangle.class)
|
||||
* .registerSubtype(Circle.class)
|
||||
* .registerSubtype(Diamond.class);
|
||||
* }</pre>
|
||||
*/
|
||||
public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
|
||||
private final Class<?> baseType;
|
||||
private final String typeFieldName;
|
||||
private final Map<String, Class<?>> labelToSubtype = new LinkedHashMap<String, Class<?>>();
|
||||
private final Map<Class<?>, String> subtypeToLabel = new LinkedHashMap<Class<?>, String>();
|
||||
|
||||
private RuntimeTypeAdapterFactory(Class<?> baseType, String typeFieldName) {
|
||||
if (typeFieldName == null || baseType == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
this.baseType = baseType;
|
||||
this.typeFieldName = typeFieldName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new runtime type adapter using for {@code baseType} using {@code
|
||||
* typeFieldName} as the type field name. Type field names are case sensitive.
|
||||
*/
|
||||
public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType, String typeFieldName) {
|
||||
return new RuntimeTypeAdapterFactory<T>(baseType, typeFieldName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new runtime type adapter for {@code baseType} using {@code "type"} as
|
||||
* the type field name.
|
||||
*/
|
||||
public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType) {
|
||||
return new RuntimeTypeAdapterFactory<T>(baseType, "type");
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers {@code type} identified by {@code label}. Labels are case
|
||||
* sensitive.
|
||||
*
|
||||
* @throws IllegalArgumentException if either {@code type} or {@code label}
|
||||
* have already been registered on this type adapter.
|
||||
*/
|
||||
public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type, String label) {
|
||||
if (type == null || label == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
if (subtypeToLabel.containsKey(type) || labelToSubtype.containsKey(label)) {
|
||||
throw new IllegalArgumentException("types and labels must be unique");
|
||||
}
|
||||
labelToSubtype.put(label, type);
|
||||
subtypeToLabel.put(type, label);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers {@code type} identified by its {@link Class#getSimpleName simple
|
||||
* name}. Labels are case sensitive.
|
||||
*
|
||||
* @throws IllegalArgumentException if either {@code type} or its simple name
|
||||
* have already been registered on this type adapter.
|
||||
*/
|
||||
public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type) {
|
||||
return registerSubtype(type, type.getSimpleName());
|
||||
}
|
||||
|
||||
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
|
||||
if (type.getRawType() != baseType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final Map<String, TypeAdapter<?>> labelToDelegate
|
||||
= new LinkedHashMap<String, TypeAdapter<?>>();
|
||||
final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate
|
||||
= new LinkedHashMap<Class<?>, TypeAdapter<?>>();
|
||||
for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) {
|
||||
TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue()));
|
||||
labelToDelegate.put(entry.getKey(), delegate);
|
||||
subtypeToDelegate.put(entry.getValue(), delegate);
|
||||
}
|
||||
|
||||
return new TypeAdapter<R>() {
|
||||
@Override public R read(JsonReader in) throws IOException {
|
||||
JsonElement jsonElement = Streams.parse(in);
|
||||
JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName);
|
||||
if (labelJsonElement == null) {
|
||||
throw new JsonParseException("cannot deserialize " + baseType
|
||||
+ " because it does not define a field named " + typeFieldName);
|
||||
}
|
||||
String label = labelJsonElement.getAsString();
|
||||
@SuppressWarnings("unchecked") // registration requires that subtype extends T
|
||||
TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label);
|
||||
if (delegate == null) {
|
||||
throw new JsonParseException("cannot deserialize " + baseType + " subtype named "
|
||||
+ label + "; did you forget to register a subtype?");
|
||||
}
|
||||
return delegate.fromJsonTree(jsonElement);
|
||||
}
|
||||
|
||||
@Override public void write(JsonWriter out, R value) throws IOException {
|
||||
Class<?> srcType = value.getClass();
|
||||
String label = subtypeToLabel.get(srcType);
|
||||
@SuppressWarnings("unchecked") // registration requires that subtype extends T
|
||||
TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType);
|
||||
if (delegate == null) {
|
||||
throw new JsonParseException("cannot serialize " + srcType.getName()
|
||||
+ "; did you forget to register a subtype?");
|
||||
}
|
||||
JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject();
|
||||
if (jsonObject.has(typeFieldName)) {
|
||||
throw new JsonParseException("cannot serialize " + srcType.getName()
|
||||
+ " because it already defines a field named " + typeFieldName);
|
||||
}
|
||||
JsonObject clone = new JsonObject();
|
||||
clone.add(typeFieldName, new JsonPrimitive(label));
|
||||
for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) {
|
||||
clone.add(e.getKey(), e.getValue());
|
||||
}
|
||||
Streams.write(clone, out);
|
||||
}
|
||||
}.nullSafe();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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.http.api.ws;
|
||||
|
||||
import net.runelite.http.api.ws.messages.LoginResponse;
|
||||
import net.runelite.http.api.ws.messages.Handshake;
|
||||
import net.runelite.http.api.ws.messages.Ping;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
public class WebsocketGsonFactory
|
||||
{
|
||||
public static Gson build()
|
||||
{
|
||||
RuntimeTypeAdapterFactory<WebsocketMessage> typeAdapter = RuntimeTypeAdapterFactory.of(WebsocketMessage.class)
|
||||
.registerSubtype(Handshake.class)
|
||||
.registerSubtype(LoginResponse.class)
|
||||
.registerSubtype(Ping.class);
|
||||
|
||||
return new GsonBuilder()
|
||||
.registerTypeAdapterFactory(typeAdapter)
|
||||
.create();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* 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.http.api.ws;
|
||||
|
||||
public class WebsocketMessage
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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.http.api.ws.messages;
|
||||
|
||||
import java.util.UUID;
|
||||
import net.runelite.http.api.ws.WebsocketMessage;
|
||||
|
||||
public class Handshake extends WebsocketMessage
|
||||
{
|
||||
private UUID session;
|
||||
|
||||
public UUID getSession()
|
||||
{
|
||||
return session;
|
||||
}
|
||||
|
||||
public void setSession(UUID session)
|
||||
{
|
||||
this.session = session;
|
||||
}
|
||||
}
|
||||
@@ -22,32 +22,31 @@
|
||||
* (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.http.api.account;
|
||||
package net.runelite.http.api.ws.messages;
|
||||
|
||||
import java.util.UUID;
|
||||
import net.runelite.http.api.ws.WebsocketMessage;
|
||||
|
||||
public class LoginResponse
|
||||
/**
|
||||
* Called after a successful login to the server
|
||||
* @author Adam
|
||||
*/
|
||||
public class LoginResponse extends WebsocketMessage
|
||||
{
|
||||
private String oauthUrl;
|
||||
private UUID uid;
|
||||
private String username;
|
||||
|
||||
public String getOauthUrl()
|
||||
public String getUsername()
|
||||
{
|
||||
return oauthUrl;
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setOauthUrl(String oauthUrl)
|
||||
public void setUsername(String username)
|
||||
{
|
||||
this.oauthUrl = oauthUrl;
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public UUID getUid()
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return uid;
|
||||
}
|
||||
|
||||
public void setUid(UUID uid)
|
||||
{
|
||||
this.uid = uid;
|
||||
return "LoginResponse{" + "username=" + username + '}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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.http.api.ws.messages;
|
||||
|
||||
import java.time.Instant;
|
||||
import net.runelite.http.api.ws.WebsocketMessage;
|
||||
|
||||
public class Ping extends WebsocketMessage
|
||||
{
|
||||
private Instant time;
|
||||
|
||||
public Instant getTime()
|
||||
{
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(Instant time)
|
||||
{
|
||||
this.time = time;
|
||||
}
|
||||
}
|
||||
@@ -39,7 +39,11 @@ import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import net.runelite.http.api.RuneliteAPI;
|
||||
import net.runelite.http.api.account.LoginResponse;
|
||||
import net.runelite.http.api.account.OAuthResponse;
|
||||
import net.runelite.http.api.ws.messages.LoginResponse;
|
||||
import net.runelite.http.service.ws.SessionManager;
|
||||
import net.runelite.http.service.ws.WSService;
|
||||
import net.runelite.http.service.ws.WSSession;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.sql2o.Connection;
|
||||
@@ -117,7 +121,7 @@ public class AccountService
|
||||
}
|
||||
}
|
||||
|
||||
public LoginResponse login(Request request, Response response)
|
||||
public OAuthResponse login(Request request, Response response)
|
||||
{
|
||||
UUID uuid = UUID.randomUUID();
|
||||
|
||||
@@ -135,7 +139,7 @@ public class AccountService
|
||||
|
||||
String authorizationUrl = service.getAuthorizationUrl();
|
||||
|
||||
LoginResponse lr = new LoginResponse();
|
||||
OAuthResponse lr = new OAuthResponse();
|
||||
lr.setOauthUrl(authorizationUrl);
|
||||
lr.setUid(uuid);
|
||||
|
||||
@@ -205,13 +209,33 @@ public class AccountService
|
||||
.addParameter("uuid", state.getUuid().toString())
|
||||
.executeUpdate();
|
||||
|
||||
logger.info("Created session for user {}", user.getUsername());
|
||||
logger.info("Created session for user {}", userInfo.getEmail());
|
||||
}
|
||||
|
||||
response.redirect(RL_REDIR);
|
||||
|
||||
notifySession(state.getUuid(), userInfo.getEmail());
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
private void notifySession(UUID uuid, String username)
|
||||
{
|
||||
WSSession session = SessionManager.findSession(uuid);
|
||||
if (session == null)
|
||||
{
|
||||
logger.info("Session {} logged in - but no websocket session", uuid);
|
||||
return;
|
||||
}
|
||||
|
||||
WSService service = session.getServlet();
|
||||
|
||||
LoginResponse response = new LoginResponse();
|
||||
response.setUsername(username);
|
||||
|
||||
service.send(response);
|
||||
}
|
||||
|
||||
public Object logout(Request request, Response response)
|
||||
{
|
||||
SessionEntry session = request.session().attribute("session");
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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.http.service.ws;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import javax.websocket.Session;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class SessionManager
|
||||
{
|
||||
private static final Logger logger = LoggerFactory.getLogger(SessionManager.class);
|
||||
|
||||
private static final Set<WSSession> sessions = Collections.synchronizedSet(new HashSet<>());
|
||||
|
||||
public static void add(WSService service, Session session)
|
||||
{
|
||||
WSSession wssession = new WSSession(service, session);
|
||||
|
||||
logger.info("Adding service {} session {}", service, session);
|
||||
|
||||
sessions.add(wssession);
|
||||
}
|
||||
|
||||
public static void remove(Session session)
|
||||
{
|
||||
WSSession wssession = new WSSession(null, session);
|
||||
sessions.remove(wssession);
|
||||
}
|
||||
|
||||
public static WSSession findSession(UUID uuid)
|
||||
{
|
||||
synchronized (sessions)
|
||||
{
|
||||
for (WSSession session : sessions)
|
||||
{
|
||||
if (Objects.equal(session.getServlet().getUuid(), uuid))
|
||||
{
|
||||
return session;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -22,8 +22,10 @@
|
||||
* (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.http.service;
|
||||
package net.runelite.http.service.ws;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import java.util.UUID;
|
||||
import javax.websocket.CloseReason;
|
||||
import javax.websocket.EndpointConfig;
|
||||
import javax.websocket.OnClose;
|
||||
@@ -32,6 +34,9 @@ import javax.websocket.OnMessage;
|
||||
import javax.websocket.OnOpen;
|
||||
import javax.websocket.Session;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
import net.runelite.http.api.ws.messages.Handshake;
|
||||
import net.runelite.http.api.ws.WebsocketGsonFactory;
|
||||
import net.runelite.http.api.ws.WebsocketMessage;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -40,9 +45,30 @@ public class WSService
|
||||
{
|
||||
private static final Logger logger = LoggerFactory.getLogger(WSService.class);
|
||||
|
||||
private static final Gson gson = WebsocketGsonFactory.build();
|
||||
|
||||
private Session session;
|
||||
private UUID uuid;
|
||||
|
||||
public UUID getUuid()
|
||||
{
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public void send(WebsocketMessage message)
|
||||
{
|
||||
String json = gson.toJson(message, WebsocketMessage.class);
|
||||
|
||||
logger.debug("Sending {}", json);
|
||||
|
||||
session.getAsyncRemote().sendText(json);
|
||||
}
|
||||
|
||||
@OnOpen
|
||||
public void onOpen(Session session, EndpointConfig config)
|
||||
{
|
||||
this.session = session;
|
||||
SessionManager.add(this, session);
|
||||
logger.info("New session {}", session);
|
||||
}
|
||||
|
||||
@@ -59,8 +85,15 @@ public class WSService
|
||||
}
|
||||
|
||||
@OnMessage
|
||||
public void onMessage(Session session, String message)
|
||||
public void onMessage(Session session, String text)
|
||||
{
|
||||
WebsocketMessage message = gson.fromJson(text, WebsocketMessage.class);
|
||||
logger.info("Got message: {}", message);
|
||||
|
||||
if (message instanceof Handshake)
|
||||
{
|
||||
Handshake hs = (Handshake) message;
|
||||
uuid = hs.getSession();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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.http.service.ws;
|
||||
|
||||
import java.util.Objects;
|
||||
import javax.websocket.Session;
|
||||
|
||||
public class WSSession
|
||||
{
|
||||
private final WSService servlet;
|
||||
private final Session session;
|
||||
|
||||
public WSSession(WSService servlet, Session session)
|
||||
{
|
||||
this.servlet = servlet;
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
public WSService getServlet()
|
||||
{
|
||||
return servlet;
|
||||
}
|
||||
|
||||
public Session getSession()
|
||||
{
|
||||
return session;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
int hash = 3;
|
||||
hash = 29 * hash + Objects.hashCode(this.session);
|
||||
return hash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (this == obj)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (obj == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
final WSSession other = (WSSession) obj;
|
||||
if (!Objects.equals(this.session, other.session))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
|
||||
}
|
||||
@@ -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"))
|
||||
{
|
||||
|
||||
@@ -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 |
Reference in New Issue
Block a user