1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
/*
* This file is part of spark.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package me.lucko.spark.common.ws;
import com.google.protobuf.ByteString;
import me.lucko.bytesocks.client.BytesocksClient;
import me.lucko.spark.common.SparkPlatform;
import me.lucko.spark.proto.SparkWebSocketProtos.PacketWrapper;
import me.lucko.spark.proto.SparkWebSocketProtos.RawPacket;
import java.io.IOException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.util.Base64;
import java.util.function.Consumer;
import java.util.logging.Level;
/**
* Controls a websocket connection between a spark server (the plugin/mod) and a spark client (the web viewer).
*/
public class ViewerSocketConnection implements BytesocksClient.Listener, AutoCloseable {
/** The protocol version */
public static final int VERSION_1 = 1;
/** The crypto algorithm used to sign/verify messages sent between the server and client */
public static final CryptoAlgorithm CRYPTO = CryptoAlgorithm.RSA2048;
/** The platform */
private final SparkPlatform platform;
/** The underlying listener */
private final Listener listener;
/** The private key used to sign messages sent from this connection */
private final PrivateKey privateKey;
/** The bytesocks socket */
private final BytesocksClient.Socket socket;
public ViewerSocketConnection(SparkPlatform platform, BytesocksClient client, Listener listener) throws Exception {
this.platform = platform;
this.listener = listener;
this.privateKey = platform.getTrustedKeyStore().getLocalPrivateKey();
this.socket = client.createAndConnect(this);
}
public interface Listener {
/**
* Checks if the given public key is trusted
*
* @param publicKey the public key
* @return true if trusted
*/
boolean isKeyTrusted(PublicKey publicKey);
/**
* Handles a packet sent to the socket
*
* @param packet the packet that was sent
* @param verified if the packet was signed by a trusted key
* @param publicKey the public key the packet was signed with
*/
void onPacket(PacketWrapper packet, boolean verified, PublicKey publicKey) throws Exception;
}
/**
* Gets the bytesocks channel id
*
* @return the channel id
*/
public String getChannelId() {
return this.socket.getChannelId();
}
/**
* Gets if the underlying socket is open
*
* @return true if the socket is open
*/
public boolean isOpen() {
return this.socket.isOpen();
}
@Override
public void onText(CharSequence data) {
try {
RawPacket packet = decodeRawPacket(data);
handleRawPacket(packet);
} catch (Exception e) {
this.platform.getPlugin().log(Level.WARNING, "Exception occurred while reading data from the socket");
e.printStackTrace();
}
}
@Override
public void onError(Throwable error) {
this.platform.getPlugin().log(Level.INFO, "Socket error: " + error.getClass().getName() + " " + error.getMessage());
error.printStackTrace();
}
@Override
public void onClose(int statusCode, String reason) {
//this.platform.getPlugin().log(Level.INFO, "Socket closed with status " + statusCode + " and reason " + reason);
}
/**
* Sends a packet to the socket.
*
* @param packetBuilder the builder to construct the wrapper packet
*/
public void sendPacket(Consumer<PacketWrapper.Builder> packetBuilder) {
PacketWrapper.Builder builder = PacketWrapper.newBuilder();
packetBuilder.accept(builder);
PacketWrapper wrapper = builder.build();
try {
sendPacket(wrapper);
} catch (Exception e) {
this.platform.getPlugin().log(Level.WARNING, "Exception occurred while sending data to the socket");
e.printStackTrace();
}
}
/**
* Sends a packet to the socket.
*
* @param packet the packet to send
*/
private void sendPacket(PacketWrapper packet) throws Exception {
ByteString msg = packet.toByteString();
// sign the message using the server private key
Signature sign = CRYPTO.createSignature();
sign.initSign(this.privateKey);
sign.update(msg.asReadOnlyByteBuffer());
byte[] signature = sign.sign();
sendRawPacket(RawPacket.newBuilder()
.setVersion(VERSION_1)
.setSignature(ByteString.copyFrom(signature))
.setMessage(msg)
.build()
);
}
/**
* Sends a raw packet to the socket.
*
* @param packet the packet to send
*/
private void sendRawPacket(RawPacket packet) throws IOException {
byte[] buf = packet.toByteArray();
String encoded = Base64.getEncoder().encodeToString(buf);
this.socket.send(encoded);
}
/**
* Decodes a raw packet sent to the socket.
*
* @param data the encoded data
* @return the decoded packet
*/
private RawPacket decodeRawPacket(CharSequence data) throws IOException {
byte[] buf = Base64.getDecoder().decode(data.toString());
return RawPacket.parseFrom(buf);
}
/**
* Handles a raw packet sent to the socket
*
* @param packet the packet
*/
private void handleRawPacket(RawPacket packet) throws Exception {
int version = packet.getVersion();
if (version != VERSION_1) {
throw new IllegalArgumentException("Unsupported packet version " + version);
}
ByteString message = packet.getMessage();
PublicKey publicKey = CRYPTO.decodePublicKey(packet.getPublicKey());
ByteString signature = packet.getSignature();
boolean verified = false;
if (signature != null && publicKey != null && this.listener.isKeyTrusted(publicKey)) {
Signature sign = CRYPTO.createSignature();
sign.initVerify(publicKey);
sign.update(message.asReadOnlyByteBuffer());
verified = sign.verify(signature.toByteArray());
}
PacketWrapper wrapper = PacketWrapper.parseFrom(message);
this.listener.onPacket(wrapper, verified, publicKey);
}
@Override
public void close() {
this.socket.close(1001 /* going away */, "spark plugin disconnected");
}
}
|