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
|
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;
private String pubkey;
private String id;
public I2PSamControl(boolean zeroHop) {
this.zeroHop = zeroHop;
}
@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();
// FIXME "Friend Code Type: World" vs "Friend Code Type: Session"
// generate our keys
this.sendCommand(new I2PSamCommand(
"DEST", "GENERATE",
ImmutableMap.of("SIGNATURE_TYPE", "EdDSA_SHA512_Ed25519")
));
var dest = this.getCommand("DEST", "REPLY");
this.pubkey = dest.parameters().get("PUB");
var privkey = 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", privkey
).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")));
return "OK".equals(status);
} 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 pubkey.
*/
public String pubkey() {
return this.pubkey;
}
/**
* 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
this.sendCommand(new I2PSamCommand(
"AUTH", "ADD",
ImmutableMap.of("USER", "minecraft_friendcode", "PASSWORD", "friendcode")
));
// if the user already exists, don't enable auth
if ("OK".equals(this.getCommand("AUTH", "STATUS").parameters().get("RESULT"))) {
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;
}
}
|