-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
DhtShell.java
179 lines (147 loc) · 5.01 KB
/
DhtShell.java
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
package com.frostwire.jlibtorrent.demo;
import com.frostwire.jlibtorrent.*;
import com.frostwire.jlibtorrent.alerts.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
/**
* @author gubatron
* @author aldenml
*/
public final class DhtShell {
public static void main(String[] args) throws Throwable {
System.out.println("Using libtorrent version: " + LibTorrent.fullVersion());
AlertListener mainListener = new AlertListener() {
@Override
public int[] types() {
return null;
}
@Override
public void alert(Alert<?> alert) {
AlertType type = alert.getType();
if (type == AlertType.LISTEN_SUCCEEDED) {
ListenSucceededAlert a = (ListenSucceededAlert) alert;
log(a.getMessage());
}
if (type == AlertType.LISTEN_FAILED) {
ListenFailedAlert a = (ListenFailedAlert) alert;
log(a.getMessage());
}
if (type == AlertType.DHT_PUT) {
DhtPutAlert a = (DhtPutAlert) alert;
log(a.getMessage());
}
}
};
Session s = new Session("0.0.0.0", 33123, 10, false, mainListener);
DHT dht = new DHT(s);
try {
File f = new File("dht_shell.dat");
if (f.exists()) {
byte[] data = Utils.readFileToByteArray(f);
s.loadState(data);
}
} catch (Throwable e) {
log(e.getMessage());
}
Scanner in = new Scanner(System.in);
while (true) {
System.out.print("$ ");
String line = in.nextLine().trim();
if (is_quit(line)) {
quit(s);
} else if (is_put(line)) {
put(dht, line);
} else if (is_get(line)) {
get(dht, line);
} else if (is_get_peers(line)) {
get_peers(dht, line);
} else if (is_announce(line)) {
announce(dht, line);
} else if (is_mkeys(line)) {
mkeys(line);
} else if (is_invalid(line)) {
invalid(line);
}
}
}
private static void print(String s, boolean dollar) {
System.out.println();
System.out.println(s);
if (dollar) {
System.out.print("$ ");
}
}
private static void print(String s) {
print(s, false);
}
private static void log(String s) {
print(s, true);
}
private static boolean is_quit(String s) {
s = s.split(" ")[0];
return s.equals("quit") || s.equals("exit") || s.equals("stop");
}
private static void quit(Session s) {
print("Exiting...");
s.saveState();
byte[] data = s.saveState();
try {
Utils.writeByteArrayToFile(new File("dht_shell.dat"), data, false);
} catch (Throwable e) {
print(e.getMessage());
}
s.abort();
System.exit(0);
}
private static boolean is_put(String s) {
return s.startsWith("put ");
}
private static void put(DHT dht, String s) {
String data = s.split(" ")[1];
String sha1 = dht.put(new Entry(data)).toString();
print("Wait for completion of put for key: " + sha1);
}
private static boolean is_get(String s) {
return s.startsWith("get ");
}
private static void get(DHT dht, String s) {
String sha1 = s.split(" ")[1];
print("Waiting a max of 20 seconds to get data for key: " + sha1);
Entry data = dht.get(new Sha1Hash(sha1), 20000);
print(data.toString());
}
private static boolean is_get_peers(String s) {
return s.startsWith("get_peers ");
}
private static void get_peers(DHT dht, String s) {
String sha1 = s.split(" ")[1];
print("Waiting a max of 20 seconds to get peers for key: " + sha1);
ArrayList<TcpEndpoint> peers = dht.getPeers(new Sha1Hash(sha1), 20000);
print(peers.toString());
}
private static boolean is_announce(String s) {
return s.startsWith("announce ");
}
private static void announce(DHT dht, String s) {
String sha1 = s.split(" ")[1];
dht.announce(new Sha1Hash(sha1), 9000, 0);
print("Wait for completion of announce for key: " + sha1);
}
private static boolean is_mkeys(String s) {
return s.startsWith("mkeys");
}
private static void mkeys(String s) {
byte[][] keys = DHT.createKeypair();
String msg = "Save this key pair\n";
msg += "Public:" + Utils.toHex(keys[0]) + "\n";
msg += "Private:" + Utils.toHex(keys[1]) + "\n";
print(msg);
}
private static boolean is_invalid(String s) {
return !s.isEmpty();
}
private static void invalid(String s) {
print("Invalid command: " + s + "\n" + "Try ? for help");
}
}