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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
package ganarchy.friendcode.sam;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetSocketAddress;
import java.net.Socket;
public class I2PSamControl extends I2PSamStateMachine {
private final boolean zeroHop;
/**
* The session's private key.
* <p>
* The threat model assumes the local machine (including RAM) to be trusted.
*/
private String privateKey;
/**
* The session's public key.
*/
private String publicKey;
private String id;
/**
* Creates a new SAM control socket.
*
* @param zeroHop Whether to use zero-hop tunnels.
* @param privateKey The (optional) identity private key, for persistent friend codes.
*/
public I2PSamControl(boolean zeroHop, String privateKey) {
this.zeroHop = zeroHop;
this.privateKey = privateKey;
}
@Override
public boolean connect() {
try {
// try IPv6 first
// there's no Inet6Address.getLoopbackAddress() which is unfortunate.
final Socket samSocket = new Socket();
samSocket.connect(new InetSocketAddress(Inet6Address.getByAddress("localhost", new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 0), 7656), 3000);
return this.connect(samSocket);
} catch (IOException e) {
try {
final Socket samSocket = new Socket();
samSocket.connect(new InetSocketAddress(Inet4Address.getByAddress("localhost", new byte[] {127, 0, 0, 1}), 7656), 3000);
return this.connect(samSocket);
} catch (IOException ex) {
return false;
}
}
}
@Override
public boolean start() {
if (!super.start()) {
return false;
}
try {
// try to enable auth
this.enableAuth();
if (this.privateKey == null) {
// generate our keys
this.sendCommand(new I2PSamCommand(
"DEST", "GENERATE",
ImmutableMap.of("SIGNATURE_TYPE", "EdDSA_SHA512_Ed25519")
));
var dest = this.getCommand("DEST", "REPLY");
this.publicKey = dest.parameters().get("PUB");
this.privateKey = dest.parameters().get("PRIV");
}
// setup our session
int i = 0;
String status;
do {
i++;
// we want a session with the given privkey and zero hops for maximum performance
// this isn't anonymous but then it's not meant to be - we're using I2P as a free STUN/TURN service
// the connecting clients handle the need for TURN by setting their hop count to 1 as needed
this.id = "minecraft_friendcode_" + i;
final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
this.sendCommand(new I2PSamCommand(
"SESSION", "CREATE",
builder.put(
"STYLE", "STREAM"
).put(
"ID", this.id
).put(
"DESTINATION", this.privateKey
).put(
"inbound.length", this.zeroHop ? "0" : "1"
).put(
"outbound.length", this.zeroHop ? "0" : "1"
).put(
"inbound.allowZeroHop", "true"
).put(
"outbound.allowZeroHop", "true"
).put(
"shouldBundleReplyInfo", "true"
).put(
"i2cp.dontPublishLeaseSet", "false"
).put(
"streaming.maxWindowSize", "1024"
).build()
));
} while ("DUPLICATED_ID".equals(status = this.getCommand("SESSION", "STATUS").parameters().get("RESULT")));
if ("OK".equals(status)) {
// find our public key (if we're using persistent keys)
if (this.publicKey == null) {
this.sendCommand(new I2PSamCommand("NAMING", "LOOKUP", ImmutableMap.of("NAME", "ME")));
final var lookup = this.getCommand("NAMING", "REPLY");
if (!"OK".equals(lookup.parameters().get("RESULT"))) {
return false;
}
this.publicKey = lookup.parameters().get("VALUE");
}
return true;
}
return false;
} catch (IOException e) {
return false;
}
}
/**
* Creates a stream forwarder.
*
* @return A stream forwarder.
*/
public I2PSamStreamForwarder forwardStream(String port) {
return new I2PSamStreamForwarder(this.getSamBridgeAddress(), id, port);
}
/**
* Creates a stream forwarder.
*
* @return A stream connector.
*/
public I2PSamStreamConnector connectStream(String b32) {
return new I2PSamStreamConnector(this.getSamBridgeAddress(), id, b32);
}
/**
* Returns the session public key.
*/
public String publicKey() {
return this.publicKey;
}
/**
* Returs the session private key.
*/
public String privateKey() {
return this.privateKey;
}
/**
* Set up and enable auth.
*
* @throws IOException If an I/O error occurs.
*/
private void enableAuth() throws IOException {
// enable auth (if it hasn't been explicitly disabled by the user), for
// the user's sake
// (ugh i2p you really fucked up by not making this the default)
// btw tor does this with a filesystem path to an auth cookie (which is
// much more secure) but we digress.
// check if we're already using strong auth.
if (I2PSamAuthUtil.isStrongAuth()) {
return;
}
// attempt to delete old user - versions 1.0.0 and 1.0.1 of the mod are
// deprecated, unsupported, and should never be used.
this.sendCommand(new I2PSamCommand(
"AUTH", "REMOVE",
ImmutableMap.of("USER", "minecraft_friendcode")
));
// check if removed.
var removed = "OK".equals(
this.getCommand("AUTH", "STATUS").parameters().get("RESULT")
);
// generate secure auth pair
var auth = I2PSamAuthUtil.upgradeAuthPair();
// add new user
this.sendCommand(new I2PSamCommand(
"AUTH", "ADD",
ImmutableMap.of("USER", auth.user(), "PASSWORD", auth.password())
));
if ("OK".equals(
this.getCommand("AUTH", "STATUS").parameters().get("RESULT")
)) {
// old user existed - don't mess with auth enable.
if (removed) {
return;
}
this.sendCommand(new I2PSamCommand("AUTH", "ENABLE"));
// ignore the response on this one, just get it off the queue
this.getCommand("AUTH", "STATUS");
}
}
public NameStatus checkName(String target) {
try {
this.sendCommand(new I2PSamCommand("NAMING", "LOOKUP", ImmutableMap.of("NAME", target)));
I2PSamCommand result = this.getCommand("NAMING", "REPLY");
if (result.parameters().get("RESULT") == null) {
return NameStatus.FAILED;
}
switch (result.parameters().get("RESULT")) {
case "OK" -> {
return NameStatus.OK;
}
case "INVALID_KEY" -> {
return NameStatus.INVALID;
}
case "KEY_NOT_FOUND" -> {
return NameStatus.UNKNOWN;
}
default -> {
return NameStatus.FAILED;
}
}
} catch (IOException e) {
return NameStatus.FAILED;
}
}
public enum NameStatus {
OK,
INVALID,
UNKNOWN,
FAILED;
}
}
|