if (!"confirm".equals(type)) {
String addr = req.queryParams("addr");
String host = req.queryParams("host");
String port = req.queryParams("port");
if (addr != null && host != null && port != null) {
LinkRequest lr = new LinkRequest();
try {
FtnAddress ftn = new FtnAddress(addr);
if (ftn.getPoint() != 0) {
code = "POINT";
} else {
FtnNdlAddress ndl = NodelistScanner.getInstance()
.isExists(ftn);
boolean exists = (ndl != null);
if (!exists) {
code = "NODELIST";
} else {
String name = (ndl.getLine() != null) ? ndl
.getLine().split(",")[4].replace('_', ' ')
: addr;
lr.setName(name);
lr.setAddress(addr);
}
}
} catch (NumberFormatException e) {
code = "FTN";
}
if (code == null) { // next step
try {
Integer iport = Integer.valueOf(port);
if ("-".equals(host) || iport == 0) {
host = "-";
iport = 0;
} else {
Socket sock = new Socket();
sock.connect(new InetSocketAddress(host, iport),
10000);
sock.close();
}
lr.setHost(host);
lr.setPort(iport);
} catch (IOException | NumberFormatException e) {
code = "INET";
}
}
if (code == null) { // next step
synchronized (LinkRequest.class) {
LinkRequest lr2 = ORMManager.get(LinkRequest.class)
.getFirstAnd("address", "=", addr);
if (lr2 == null) {
String akey = FtnTools.generate8d();
lr.setAkey(akey);
ORMManager.get(LinkRequest.class).save(lr);
writeKey(req,lr);
} else {
code = "EXISTS";
}
}
}
} else {
code = "ERROR";
}
} else {
String akey = req.queryParams("key");
String id = req.queryParams("id");
try {
LinkRequest lr = ORMManager.get(LinkRequest.class).getById(id);
if (lr != null && lr.getAkey().equals(akey)) { // valid
String password = FtnTools.generate8d();
Link link = new Link();
link.setLinkName(lr.getName());
link.setLinkAddress(lr.getAddress());
link.setProtocolHost(lr.getHost());
link.setProtocolPort(lr.getPort());
link.setPaketPassword(password);
link.setProtocolPassword(password);
// check
synchronized (Link.class) {
Link l2 = ORMManager.get(Link.class).getFirstAnd(