diff options
-rw-r--r-- | src/main/java/com/juick/http/www/UserThread.java | 3 | ||||
-rw-r--r-- | src/main/java/com/juick/xmpp/s2s/CacheEntry.java | 19 | ||||
-rw-r--r-- | src/main/java/com/juick/xmpp/s2s/CleaningUp.java | 102 | ||||
-rw-r--r-- | src/main/java/com/juick/xmpp/s2s/Connection.java | 99 | ||||
-rw-r--r-- | src/main/java/com/juick/xmpp/s2s/ConnectionIn.java | 181 | ||||
-rw-r--r-- | src/main/java/com/juick/xmpp/s2s/ConnectionListener.java | 28 | ||||
-rw-r--r-- | src/main/java/com/juick/xmpp/s2s/ConnectionOut.java | 122 | ||||
-rw-r--r-- | src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java | 256 | ||||
-rw-r--r-- | src/main/java/com/juick/xmpp/s2s/DNSQueries.java | 46 | ||||
-rw-r--r-- | src/main/java/com/juick/xmpp/s2s/HostnamePort.java | 16 | ||||
-rw-r--r-- | src/main/java/com/juick/xmpp/s2s/JuickBot.java | 403 | ||||
-rw-r--r-- | src/main/java/com/juick/xmpp/s2s/Shutdown.java | 45 | ||||
-rw-r--r-- | src/main/java/com/juick/xmpp/s2s/XMPPComponent.java | 211 | ||||
-rw-r--r-- | src/main/webapp/WEB-INF/web.xml | 9 |
14 files changed, 1536 insertions, 4 deletions
diff --git a/src/main/java/com/juick/http/www/UserThread.java b/src/main/java/com/juick/http/www/UserThread.java index 8b6707eb3..fe5ca32f5 100644 --- a/src/main/java/com/juick/http/www/UserThread.java +++ b/src/main/java/com/juick/http/www/UserThread.java @@ -88,9 +88,6 @@ public class UserThread { PageTemplates.pageFooter(request, out, visitor, false); out.println("<script type='text/javascript'>"); - if (visitor != null && visitor.getUID() == 1 && msg.getUser().getUID() == 1) { - out.println("var juickDebug=1;"); - } out.println("var pageMID=" + msg.getMID() + ";"); out.println("initWS();"); out.println("</script>"); diff --git a/src/main/java/com/juick/xmpp/s2s/CacheEntry.java b/src/main/java/com/juick/xmpp/s2s/CacheEntry.java new file mode 100644 index 000000000..7cdb18ab3 --- /dev/null +++ b/src/main/java/com/juick/xmpp/s2s/CacheEntry.java @@ -0,0 +1,19 @@ +package com.juick.xmpp.s2s; + +/** + * + * @author ugnich + */ +public class CacheEntry { + + public String hostname; + public long tsCreated; + public long tsUpdated; + public String xml; + + public CacheEntry(String hostname, String xml) { + this.hostname = hostname; + this.tsCreated = this.tsUpdated = System.currentTimeMillis(); + this.xml = xml; + } +} diff --git a/src/main/java/com/juick/xmpp/s2s/CleaningUp.java b/src/main/java/com/juick/xmpp/s2s/CleaningUp.java new file mode 100644 index 000000000..48771580e --- /dev/null +++ b/src/main/java/com/juick/xmpp/s2s/CleaningUp.java @@ -0,0 +1,102 @@ +package com.juick.xmpp.s2s; + +import java.io.FileNotFoundException; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.util.Iterator; + +/** + * + * @author ugnich + */ +public class CleaningUp implements Runnable { + + @Override + public void run() { + while (true) { + try { + PrintWriter statsFile = new PrintWriter(XMPPComponent.STATSFILE, "UTF-8"); + statsFile.write("<html><body><h2>Threads: " + Thread.activeCount() + "</h2>"); + statsFile.write("<h2>Out (" + XMPPComponent.outConnections.size() + ")</h2><table border=1><tr><th>to</th><th>sid</th><th>inactive</th><th>out packets</th><th>out bytes</th></tr>"); + + long now = System.currentTimeMillis(); + + synchronized (XMPPComponent.outConnections) { + for (Iterator<ConnectionOut> i = XMPPComponent.outConnections.iterator(); i.hasNext();) { + ConnectionOut c = i.next(); + int inactive = (int) ((double) (now - c.tsLocalData) / 1000.0); + if (inactive > 900) { + c.closeConnection(); + i.remove(); + } else { + statsFile.write(" <tr>"); + statsFile.write(" <td>" + c.to + "</td>\n"); + statsFile.write(" <td>" + c.streamID + "</td>\n"); + statsFile.write(" <td>" + inactive + "</td>\n"); + statsFile.write(" <td>" + c.packetsLocal + "</td>\n"); + statsFile.write(" <td>" + c.bytesLocal + "</td>\n"); + statsFile.write(" <tr>"); + } + } + } + + statsFile.write("</table><h2>In (" + XMPPComponent.inConnections.size() + ")</h2><table border=1><tr><th>from</th><th>sid</th><th>inactive</th><th>in packets</th></tr>"); + + synchronized (XMPPComponent.inConnections) { + for (Iterator<ConnectionIn> i = XMPPComponent.inConnections.iterator(); i.hasNext();) { + ConnectionIn c = i.next(); + int inactive = (int) ((double) (now - c.tsRemoteData) / 1000.0); + if (inactive > 900) { + c.closeConnection(); + i.remove(); + } else { + statsFile.write(" <tr>"); + if (c.from.isEmpty()) { + statsFile.write(" <td> </td>\n"); + } else if (c.from.size() == 1) { + statsFile.write(" <td>" + c.from.get(0) + "</td>\n"); + } else { + String out = " <td>"; + for (int n = 0; n < c.from.size(); n++) { + if (n > 0) { + out += "<br/>"; + } + out += c.from.get(n); + } + statsFile.write(out + "</td>\n"); + } + statsFile.write(" <td>" + c.streamID + "</td>\n"); + statsFile.write(" <td>" + inactive + "</td>\n"); + statsFile.write(" <td>" + c.packetsRemote + "</td>\n"); + statsFile.write(" <tr>"); + } + } + } + + statsFile.write("</table><h2>Cache (" + XMPPComponent.outCache.size() + ")</h2><table border=1><tr><th>host</th><th>live</th><th>size</th></tr>"); + + synchronized (XMPPComponent.outCache) { + for (Iterator<CacheEntry> i = XMPPComponent.outCache.iterator(); i.hasNext();) { + CacheEntry c = i.next(); + int inactive = (int) ((double) (now - c.tsCreated) / 1000.0); + if (inactive > 600) { + i.remove(); + } else { + statsFile.write("<tr><td>" + c.hostname + "</td><td>" + inactive + "</td><td>" + c.xml.length() + "</td></tr>"); + } + } + } + + statsFile.write("</table></body></html>"); + statsFile.close(); + + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + } + } catch (FileNotFoundException e) { + } catch (UnsupportedEncodingException e) { + } + } + } +} diff --git a/src/main/java/com/juick/xmpp/s2s/Connection.java b/src/main/java/com/juick/xmpp/s2s/Connection.java new file mode 100644 index 000000000..699e52bfc --- /dev/null +++ b/src/main/java/com/juick/xmpp/s2s/Connection.java @@ -0,0 +1,99 @@ +package com.juick.xmpp.s2s; + +import java.io.FileWriter; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.net.Socket; +import java.util.Date; +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import org.xmlpull.mxp1.MXParser; +import org.xmlpull.v1.XmlPullParser; + +/** + * + * @author ugnich + */ +public class Connection { + + public String streamID; + public long tsCreated = 0; + public long tsLocalData = 0; + public long bytesLocal = 0; + public long packetsLocal = 0; + Socket socket; + final XmlPullParser parser = new MXParser(); + OutputStreamWriter writer; + + public Connection() { + tsCreated = System.currentTimeMillis(); + } + + public void logXml(String xml) { + try { + FileWriter logFile = new FileWriter(XMPPComponent.LOGFILE, true); + logFile.write(new Date().toString() + "\t" + streamID + "\t" + xml); + logFile.close(); + } catch (IOException e) { + } + } + + public void logParser() { + if (streamID == null) { + return; + } + String tag = "IN: <" + parser.getName(); + for (int i = 0; i < parser.getAttributeCount(); i++) { + tag += " " + parser.getAttributeName(i) + "=\"" + parser.getAttributeValue(i) + "\""; + } + tag += ">...</" + parser.getName() + ">\n"; + logXml(tag); + } + + public void sendStanza(String xml) throws IOException { + if (XMPPComponent.LOGFILE != null && streamID != null) { + logXml("OUT: " + xml + "\n"); + } + writer.write(xml); + writer.flush(); + tsLocalData = System.currentTimeMillis(); + bytesLocal += xml.length(); + packetsLocal++; + } + + void closeConnection() { + if (XMPPComponent.LOGFILE != null && streamID != null) { + logXml("CLOSING STREAM\n"); + } + + try { + writer.write("</stream:stream>"); + } catch (Exception e) { + } + + try { + writer.close(); + } catch (Exception e) { + } + + try { + socket.close(); + } catch (Exception e) { + } + } + + static String generateDialbackKey(String to, String from, String id) throws Exception { + Mac hmacSha256 = Mac.getInstance("hmacSHA256"); + + SecretKeySpec secret_key = new SecretKeySpec("$UppPerSeCCret4".getBytes(), "SHA-256"); + hmacSha256.init(secret_key); + byte key[] = hmacSha256.doFinal((to + " " + from + " " + id).getBytes()); + + StringBuilder hexkey = new StringBuilder(); + for (int i = 0; i < key.length; i++) { + hexkey.append(Integer.toHexString(0xFF & key[i])); + } + + return hexkey.toString(); + } +} diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java b/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java new file mode 100644 index 000000000..8150cc27b --- /dev/null +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java @@ -0,0 +1,181 @@ +package com.juick.xmpp.s2s; + +import com.juick.xmpp.Iq; +import com.juick.xmpp.JID; +import com.juick.xmpp.Message; +import com.juick.xmpp.Presence; +import com.juick.xmpp.utils.XmlUtils; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.Socket; +import java.util.ArrayList; +import java.util.UUID; +import org.xmlpull.v1.XmlPullParser; + +/** + * + * @author ugnich + */ +public class ConnectionIn extends Connection implements Runnable { + + final public ArrayList<String> from = new ArrayList<String>(); + public long tsRemoteData = 0; + public long packetsRemote = 0; + + public ConnectionIn(Socket socket) { + super(); + this.socket = socket; + streamID = UUID.randomUUID().toString(); + } + + @Override + public void run() { + System.out.println("STREAM FROM ? " + streamID + " START"); + try { + parser.setInput(new InputStreamReader(socket.getInputStream())); + writer = new OutputStreamWriter(socket.getOutputStream()); + + parser.next(); // stream:stream + updateTsRemoteData(); + if (!parser.getName().equals("stream:stream") + || !parser.getAttributeValue(null, "xmlns").equals("jabber:server") + || !parser.getAttributeValue(null, "xmlns:stream").equals("http://etherx.jabber.org/streams") + || !parser.getAttributeValue(null, "xmlns:db").equals("jabber:server:dialback")) { +// || !parser.getAttributeValue(null, "version").equals("1.0") +// || !parser.getAttributeValue(null, "to").equals(Main.HOSTNAME)) { + throw new Exception("STREAM FROM ? " + streamID + " INVALID FIRST PACKET"); + } + boolean xmppversionnew = parser.getAttributeValue(null, "version") != null; + + String openStream = "<?xml version='1.0'?><stream:stream xmlns='jabber:server' " + + "xmlns:stream='http://etherx.jabber.org/streams' xmlns:db='jabber:server:dialback' from='" + + XMPPComponent.HOSTNAME + "' id='" + streamID + "' version='1.0'>"; + if (xmppversionnew) { + openStream += "<stream:features></stream:features>"; + } + sendStanza(openStream); + + while (parser.next() != XmlPullParser.END_DOCUMENT) { + updateTsRemoteData(); + if (parser.getEventType() != XmlPullParser.START_TAG) { + continue; + } + + if (XMPPComponent.LOGFILE != null) { + logParser(); + } + + packetsRemote++; + + String tag = parser.getName(); + if (tag.equals("db:result")) { + String dfrom = parser.getAttributeValue(null, "from"); + String to = parser.getAttributeValue(null, "to"); + System.out.println("STREAM FROM " + dfrom + " TO " + to + " " + streamID + " ASKING FOR DIALBACK"); + if (dfrom.endsWith(XMPPComponent.HOSTNAME) && (dfrom.equals(XMPPComponent.HOSTNAME) || dfrom.endsWith("." + XMPPComponent.HOSTNAME))) { + break; + } + if (dfrom != null && to != null && to.equals(XMPPComponent.HOSTNAME)) { + String dbKey = XmlUtils.getTagText(parser); + updateTsRemoteData(); + + ConnectionOut c = XMPPComponent.getConnectionOut(dfrom, false); + if (c != null) { + c.sendDialbackVerify(streamID, dbKey); + } else { + c = new ConnectionOut(dfrom, streamID, dbKey); + new Thread(c).start(); + } + } else { + throw new Exception("STREAM FROM " + dfrom + " " + streamID + " DIALBACK RESULT FAIL"); + } + } else if (tag.equals("db:verify")) { + String vfrom = parser.getAttributeValue(null, "from"); + String vto = parser.getAttributeValue(null, "to"); + String vid = parser.getAttributeValue(null, "id"); + String vkey = XmlUtils.getTagText(parser); + updateTsRemoteData(); + boolean valid = false; + if (vfrom != null && vto != null && vid != null && vkey != null) { + String vkey2 = generateDialbackKey(vfrom, vto, vid); + valid = vkey.equals(vkey2); + } + if (valid) { + sendStanza("<db:verify from='" + vto + "' to='" + vfrom + "' id='" + vid + "' type='valid'/>"); + System.out.println("STREAM FROM " + vfrom + " " + streamID + " DIALBACK VERIFY VALID"); + } else { + sendStanza("<db:verify from='" + vto + "' to='" + vfrom + "' id='" + vid + "' type='invalid'/>"); + System.err.println("STREAM FROM " + vfrom + " " + streamID + " DIALBACK VERIFY INVALID"); + } + } else if (tag.equals("presence") && checkFromTo(parser)) { + Presence p = Presence.parse(parser, null); + if (p != null && (p.type == null || !p.type.equals(Presence.Type.error))) { + JuickBot.incomingPresence(p); + } + } else if (tag.equals("message") && checkFromTo(parser)) { + updateTsRemoteData(); + Message msg = Message.parse(parser, XMPPComponent.childParsers); + if (msg != null && (msg.type == null || !msg.type.equals(Message.Type.error))) { + System.out.println("STREAM " + streamID + ": " + msg.toString()); + if (!JuickBot.incomingMessage(msg)) { + XMPPComponent.connRouter.sendStanza(msg.toString()); + } + } + } else if (tag.equals("iq") && checkFromTo(parser)) { + updateTsRemoteData(); + String type = parser.getAttributeValue(null, "type"); + String xml = XmlUtils.parseToString(parser, true); + if (type == null || !type.equals(Iq.Type.error)) { + System.out.println("STREAM " + streamID + ": " + xml); + XMPPComponent.connRouter.sendStanza(xml); + } + } else { + System.out.println("STREAM " + streamID + ": " + XmlUtils.parseToString(parser, true)); + } + } + System.err.println("STREAM " + streamID + " FINISHED"); + XMPPComponent.removeConnectionIn(this); + closeConnection(); + } catch (Exception e) { + System.err.println("STREAM " + streamID + " ERROR:" + e.toString()); + e.printStackTrace(); + XMPPComponent.removeConnectionIn(this); + closeConnection(); + } + } + + void updateTsRemoteData() { + tsRemoteData = System.currentTimeMillis(); + } + + public void sendDialbackResult(String sfrom, String type) { + try { + sendStanza("<db:result from='" + XMPPComponent.HOSTNAME + "' to='" + sfrom + "' type='" + type + "'/>"); + if (type.equals("valid")) { + from.add(sfrom); + System.out.println("STREAM FROM " + sfrom + " " + streamID + " READY"); + } + } catch (IOException e) { + System.err.println("STREAM FROM " + sfrom + " " + streamID + " ERROR: " + e.toString()); + } + } + + boolean checkFromTo(XmlPullParser parser) throws Exception { + String cfrom = parser.getAttributeValue(null, "from"); + String cto = parser.getAttributeValue(null, "to"); + if (cfrom != null && cto != null && !cfrom.isEmpty() && !cto.isEmpty()) { + JID jidto = new JID(cto); + if (jidto.Host != null && jidto.Username != null && jidto.Host.equals(XMPPComponent.HOSTNAME) && jidto.Username.matches("^[a-zA-Z0-9\\-]{2,16}$")) { + JID jidfrom = new JID(cfrom); + int size = from.size(); + for (int i = 0; i < size; i++) { + if (from.get(i).equals(jidfrom.Host)) { + return true; + } + } + } + } + return false; + } +} diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionListener.java b/src/main/java/com/juick/xmpp/s2s/ConnectionListener.java new file mode 100644 index 000000000..f29b8d095 --- /dev/null +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionListener.java @@ -0,0 +1,28 @@ +package com.juick.xmpp.s2s; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; + +/** + * + * @author ugnich + */ +public class ConnectionListener implements Runnable { + + @Override + public void run() { + try { + ServerSocket listener = new ServerSocket(5269); + while (true) { + Socket sock = listener.accept(); + ConnectionIn conn = new ConnectionIn(sock); + XMPPComponent.addConnectionIn(conn); + Thread t = new Thread(conn); + t.start(); + } + } catch (IOException e) { + System.out.println("IOException on socket listen: " + e.toString()); + } + } +} diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java b/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java new file mode 100644 index 000000000..2626b926d --- /dev/null +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java @@ -0,0 +1,122 @@ +package com.juick.xmpp.s2s; + +import com.juick.xmpp.utils.XmlUtils; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.Socket; +import org.xmlpull.v1.XmlPullParser; + +/** + * + * @author ugnich + */ +public class ConnectionOut extends Connection implements Runnable { + + public boolean streamReady = false; + public String to; + String checkSID = null; + String dbKey = null; + + public ConnectionOut(String hostname) { + super(); + to = hostname; + } + + public ConnectionOut(String hostname, String checkSID, String dbKey) { + super(); + to = hostname; + this.checkSID = checkSID; + this.dbKey = dbKey; + } + + @Override + public void run() { + System.out.println("STREAM TO " + to + " START"); + + try { + HostnamePort addr = DNSQueries.getServerAddress(to); + socket = new Socket(addr.hostname, addr.port); + + parser.setInput(new InputStreamReader(socket.getInputStream())); + writer = new OutputStreamWriter(socket.getOutputStream()); + + sendStanza("<?xml version='1.0'?><stream:stream xmlns='jabber:server' " + + "xmlns:stream='http://etherx.jabber.org/streams' xmlns:db='jabber:server:dialback' from='" + + XMPPComponent.HOSTNAME + "' to='" + to + "' version='1.0'>"); + + parser.next(); // stream:stream + streamID = parser.getAttributeValue(null, "id"); + if (streamID == null || streamID.isEmpty()) { + throw new Exception("STREAM TO " + to + " INVALID FIRST PACKET"); + } + + System.out.println("STREAM TO " + to + " " + streamID + " OPEN"); + XMPPComponent.addConnectionOut(this); + + if (checkSID != null) { + sendDialbackVerify(checkSID, dbKey); + } + + sendStanza("<db:result from='" + XMPPComponent.HOSTNAME + "' to='" + to + "'>" + generateDialbackKey(to, XMPPComponent.HOSTNAME, streamID) + "</db:result>"); + + while (parser.next() != XmlPullParser.END_DOCUMENT) { + if (parser.getEventType() != XmlPullParser.START_TAG) { + continue; + } + + if (XMPPComponent.LOGFILE != null) { + logParser(); + } + + String tag = parser.getName(); + if (tag.equals("db:result")) { + String type = parser.getAttributeValue(null, "type"); + if (type != null && type.equals("valid")) { + streamReady = true; + System.out.println("STREAM TO " + to + " " + streamID + " READY"); + + String cache = XMPPComponent.getFromCache(to); + if (cache != null) { + System.out.println("STREAM TO " + to + " " + streamID + " SENDING CACHE"); + sendStanza(cache); + } + + } else { + System.out.println("STREAM TO " + to + " " + streamID + " DIALBACK FAIL"); + } + XmlUtils.skip(parser); + } else if (tag.equals("db:verify")) { + String from = parser.getAttributeValue(null, "from"); + String type = parser.getAttributeValue(null, "type"); + String sid = parser.getAttributeValue(null, "id"); + if (from != null && from.equals(to) && sid != null && !sid.isEmpty() && type != null) { + ConnectionIn c = XMPPComponent.getConnectionIn(sid); + if (c != null) { + c.sendDialbackResult(from, type); + } + } + XmlUtils.skip(parser); + } else { + System.out.println("STREAM TO " + to + " " + streamID + ": " + XmlUtils.parseToString(parser, true)); + } + } + + System.err.println("STREAM TO " + to + " " + streamID + " FINISHED"); + XMPPComponent.removeConnectionOut(this); + closeConnection(); + } catch (Exception e) { + System.err.println(e.toString()); + XMPPComponent.removeConnectionOut(this); + closeConnection(); + } + } + + public void sendDialbackVerify(String sid, String key) { + try { + sendStanza("<db:verify from='" + XMPPComponent.HOSTNAME + "' to='" + to + "' id='" + sid + "'>" + key + "</db:verify>"); + } catch (IOException e) { + System.err.println("STREAM TO " + to + " " + streamID + " ERROR: " + e.toString()); + } + } +} diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java new file mode 100644 index 000000000..d8ce0daff --- /dev/null +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java @@ -0,0 +1,256 @@ +package com.juick.xmpp.s2s; + +import com.juick.server.MessagesQueries; +import com.juick.server.SubscriptionsQueries; +import com.juick.xmpp.JID; +import com.juick.xmpp.Message; +import com.juick.xmpp.extensions.JuickMessage; +import com.juick.xmpp.extensions.Nickname; +import com.juick.xmpp.extensions.XOOB; +import com.juick.xmpp.utils.SHA1; +import com.juick.xmpp.utils.XmlUtils; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.Socket; +import java.util.ArrayList; +import java.util.List; + +import org.xmlpull.v1.XmlPullParser; + +/** + * + * @author ugnich + */ +public class ConnectionRouter extends Connection implements Runnable { + + @Override + public void run() { + System.out.println("STREAM ROUTER START"); + + try { + socket = new Socket("localhost", 5347); + parser.setInput(new InputStreamReader(socket.getInputStream())); + parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); + writer = new OutputStreamWriter(socket.getOutputStream()); + + String msg = "<stream:stream xmlns='jabber:component:accept' xmlns:stream='http://etherx.jabber.org/streams' to='s2s'>"; + writer.write(msg); + writer.flush(); + + parser.next(); // stream:stream + streamID = parser.getAttributeValue(null, "id"); + if (streamID == null || streamID.isEmpty()) { + throw new Exception("FAIL ON FIRST PACKET"); + } + + msg = "<handshake>" + SHA1.encode(streamID + "secret") + "</handshake>"; + writer.write(msg); + writer.flush(); + + parser.next(); + if (!parser.getName().equals("handshake")) { + throw new Exception("NO HANDSHAKE"); + } + XmlUtils.skip(parser); + System.out.println("STREAM ROUTER OPEN"); + + while (parser.next() != XmlPullParser.END_DOCUMENT) { + if (parser.getEventType() != XmlPullParser.START_TAG) { + continue; + } + + String tag = parser.getName(); + String to = parser.getAttributeValue(null, "to"); + if (to != null && (tag.equals("message") || tag.equals("presence") || tag.equals("iq"))) { + JID jid = new JID(to); + if (jid.Host != null) { + if (jid.Host.equals(XMPPComponent.COMPONENTNAME)) { + if (tag.equals("message")) { + Message xmsg = Message.parse(parser, XMPPComponent.childParsers); + System.out.println("STREAM ROUTER (PROCESS): " + xmsg.toString()); + JuickMessage jmsg = (JuickMessage) xmsg.getChild(JuickMessage.XMLNS); + if (jmsg != null) { + if (jid.Username != null && jid.Username.equals("recomm")) { + sendJuickRecommendation(jmsg); + } else { + if (jmsg.getRID() > 0) { + sendJuickComment(jmsg); + } else if (jmsg.getMID() > 0) { + sendJuickMessage(jmsg); + } + } + } + } + } else if (jid.Host.endsWith(XMPPComponent.HOSTNAME) && (jid.Host.equals(XMPPComponent.HOSTNAME) || jid.Host.endsWith("." + XMPPComponent.HOSTNAME))) { + String xml = XmlUtils.parseToString(parser, true); + System.out.println("STREAM ROUTER: " + xml); + } else { + String xml = XmlUtils.parseToString(parser, true); + System.out.println("STREAM ROUTER (OUT): " + xml); + XMPPComponent.sendOut(jid.Host, xml); + } + } else { + System.out.println("STREAM ROUTER (NO TO): " + XmlUtils.parseToString(parser, true)); + } + } else { + System.out.println("STREAM ROUTER: " + XmlUtils.parseToString(parser, true)); + } + } + + System.err.println("STREAM ROUTER FINISHED"); + } catch (Exception e) { + System.err.println("STREAM ROUTER PARSE ERROR: " + e.toString()); + } + System.exit(0); + } + + @Override + synchronized public void sendStanza(String xml) { + try { + writer.write(xml); + writer.flush(); + } catch (IOException e) { + System.err.println("STREAM ROUTER ERROR: " + xml); + System.err.println("STREAM ROUTER ERROR: " + e.toString()); + System.exit(0); + } + } + + public void sendJuickMessage(JuickMessage jmsg) { + List<String> jids; + + synchronized (XMPPComponent.sqlSync) { + if (jmsg.FriendsOnly) { + jids = SubscriptionsQueries.getJIDSubscribedToUser(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.FriendsOnly); + } else { + jids = SubscriptionsQueries.getJIDSubscribedToUserAndTags(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.getMID()); + } + } + + String txt = "@" + jmsg.getUser().getUName() + ":" + jmsg.getTagsString() + "\n"; + String attachment = jmsg.getAttachmentURL(); + if (attachment != null) { + txt += attachment + "\n"; + } + txt += jmsg.getText() + "\n\n"; + txt += "#" + jmsg.getMID() + " http://juick.com/" + jmsg.getMID(); + + Nickname nick = new Nickname(); + nick.Nickname = "@" + jmsg.getUser().getUName(); + + com.juick.xmpp.Message msg = new com.juick.xmpp.Message(); + msg.from = JuickBot.JuickJID; + msg.body = txt; + msg.type = Message.Type.chat; + msg.thread = "juick-" + jmsg.getMID(); + msg.addChild(jmsg); + msg.addChild(nick); + if (attachment != null) { + XOOB oob = new XOOB(); + oob.URL = attachment; + msg.addChild(oob); + } + + for (int i = 0; i < jids.size(); i++) { + msg.to = new JID(jids.get(i)); + XMPPComponent.sendOut(msg); + } + } + + public void sendJuickComment(JuickMessage jmsg) { + List<String> jids; + String replyQuote; + + synchronized (XMPPComponent.sqlSync) { + jids = SubscriptionsQueries.getJIDSubscribedToComments(XMPPComponent.sql, jmsg.getMID(), jmsg.getUser().getUID()); + replyQuote = getReplyQuote(XMPPComponent.sql, jmsg.getMID(), jmsg.ReplyTo); + } + + String txt = "Reply by @" + jmsg.getUser().getUName() + ":\n" + replyQuote + "\n"; + String attachment = jmsg.getAttachmentURL(); + if (attachment != null) { + txt += attachment + "\n"; + } + txt += jmsg.getText() + "\n\n" + "#" + jmsg.getMID() + "/" + jmsg.getRID() + " http://juick.com/" + jmsg.getMID() + "#" + jmsg.getRID(); + + com.juick.xmpp.Message msg = new com.juick.xmpp.Message(); + msg.from = JuickBot.JuickJID; + msg.body = txt; + msg.type = Message.Type.chat; + msg.addChild(jmsg); + for (int i = 0; i < jids.size(); i++) { + msg.to = new JID(jids.get(i)); + XMPPComponent.sendOut(msg); + } + } + + private String getReplyQuote(java.sql.Connection sql, int MID, int ReplyTo) { + String quote = ""; + if (ReplyTo > 0) { + com.juick.Message q = MessagesQueries.getReply(sql, MID, ReplyTo); + if (q != null) { + quote = q.getText(); + } + } else { + com.juick.Message q = MessagesQueries.getMessage(sql, MID); + if (q != null) { + quote = q.getText(); + } + } + if (quote.length() > 50) { + quote = ">" + quote.substring(0, 47).replace('\n', ' ') + "...\n"; + } else if (quote.length() > 0) { + quote = ">" + quote.replace('\n', ' ') + "\n"; + } + return quote; + } + + public void sendJuickRecommendation(JuickMessage recomm) { + List<String> jids; + JuickMessage jmsg; + synchronized (XMPPComponent.sqlSync) { + jmsg = new JuickMessage(MessagesQueries.getMessage(XMPPComponent.sql, recomm.getMID())); + jids = SubscriptionsQueries.getJIDSubscribedToUserRecommendations(XMPPComponent.sql, + recomm.getUser().getUID(), recomm.getMID(), jmsg.getUser().getUID()); + } + + String txt = "Recommended by @" + recomm.getUser().getUName() + ":\n"; + txt += "@" + jmsg.getUser().getUName() + ":" + jmsg.getTagsString() + "\n"; + String attachment = jmsg.getAttachmentURL(); + if (attachment != null) { + txt += attachment + "\n"; + } + txt += jmsg.getText() + "\n\n"; + txt += "#" + jmsg.getMID(); + if (jmsg.Replies > 0) { + if (jmsg.Replies % 10 == 1 && jmsg.Replies % 100 != 11) { + txt += " (" + jmsg.Replies + " reply)"; + } else { + txt += " (" + jmsg.Replies + " replies)"; + } + } + txt += " http://juick.com/" + jmsg.getMID(); + + Nickname nick = new Nickname(); + nick.Nickname = "@" + jmsg.getUser().getUName(); + + com.juick.xmpp.Message msg = new com.juick.xmpp.Message(); + msg.from = JuickBot.JuickJID; + msg.body = txt; + msg.type = Message.Type.chat; + msg.thread = "juick-" + jmsg.getMID(); + msg.addChild(jmsg); + msg.addChild(nick); + if (attachment != null) { + XOOB oob = new XOOB(); + oob.URL = attachment; + msg.addChild(oob); + } + + for (int i = 0; i < jids.size(); i++) { + msg.to = new JID(jids.get(i)); + XMPPComponent.sendOut(msg); + } + } +} diff --git a/src/main/java/com/juick/xmpp/s2s/DNSQueries.java b/src/main/java/com/juick/xmpp/s2s/DNSQueries.java new file mode 100644 index 000000000..2b2d60e0f --- /dev/null +++ b/src/main/java/com/juick/xmpp/s2s/DNSQueries.java @@ -0,0 +1,46 @@ +package com.juick.xmpp.s2s; + +import java.net.UnknownHostException; +import java.util.Hashtable; +import java.util.Random; +import javax.naming.NamingException; +import javax.naming.directory.Attribute; +import javax.naming.directory.DirContext; +import javax.naming.directory.InitialDirContext; + +/** + * + * @author ugnich + */ +public class DNSQueries { + + private static Random rand = new Random(); + + public static HostnamePort getServerAddress(String hostname) throws UnknownHostException { + + String host = hostname; + int port = 5269; + + try { + Hashtable<String, String> env = new Hashtable<String, String>(5); + env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); + DirContext ctx = new InitialDirContext(env); + Attribute att = ctx.getAttributes("_xmpp-server._tcp." + hostname, new String[]{"SRV"}).get("SRV"); + + if (att != null && att.size() > 0) { + int i = rand.nextInt(att.size()); + try { + String srv[] = att.get(i).toString().split(" "); + port = Integer.parseInt(srv[2]); + host = srv[3]; + } catch (Exception e) { + } + } + + ctx.close(); + } catch (NamingException e) { + } + + return new HostnamePort(host, port); + } +} diff --git a/src/main/java/com/juick/xmpp/s2s/HostnamePort.java b/src/main/java/com/juick/xmpp/s2s/HostnamePort.java new file mode 100644 index 000000000..ce020f8d3 --- /dev/null +++ b/src/main/java/com/juick/xmpp/s2s/HostnamePort.java @@ -0,0 +1,16 @@ +package com.juick.xmpp.s2s; + +/** + * + * @author ugnich + */ +public class HostnamePort { + + public String hostname; + public int port; + + public HostnamePort(String hostname, int port) { + this.hostname = hostname; + this.port = port; + } +} diff --git a/src/main/java/com/juick/xmpp/s2s/JuickBot.java b/src/main/java/com/juick/xmpp/s2s/JuickBot.java new file mode 100644 index 000000000..182de10d0 --- /dev/null +++ b/src/main/java/com/juick/xmpp/s2s/JuickBot.java @@ -0,0 +1,403 @@ +package com.juick.xmpp.s2s; + +import com.juick.User; +import com.juick.server.PMQueries; +import com.juick.server.TagQueries; +import com.juick.server.UserQueries; +import com.juick.xmpp.JID; +import com.juick.xmpp.Message; +import com.juick.xmpp.Presence; +import com.juick.xmpp.extensions.Error; +import com.juick.xmpp.extensions.JuickMessage; + +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * + * @author ugnich + */ +public class JuickBot { + + public static final JID JuickJID = new JID("juick", "juick.com", "Juick"); + private static final String HELPTEXT = + "@username text - Send private message\n" + + "*tagname Blah-blah-blah - Post a message with tag 'tagname'\n" + + "#1234 Blah-blah-blah - Answer to message #1234\n" + + "#1234/5 Blah - Answer to reply #1234/5\n" + + "! #1234 - Recommend post\n" + + "\n" + + "# - Show last messages from your feed (## - second page, ...)\n" + + "@ - Show recomendations and popular personal blogs\n" + + "* - Show your tags\n" + + "#1234 - Show message\n" + + "#1234+ - Show message with replies\n" + + "@username - Show user's info\n" + + "@username+ - Show user's info and last 10 messages\n" + + "@username *tag - User's messages with this tag\n" + + "*tag - Show last 10 messages with this tag\n" + + "? blah - Search posts for 'blah'\n" + + "? @username blah - Searching among user\'s posts for 'blah'\n" + + "D #123 - Delete message\n" + + "D #123/45 - Delete reply\n" + + "DL - Delete last message/reply\n" + + "S - Show your subscriptions\n" + + "S #123 - Subscribe to message replies\n" + + "S @username - Subscribe to user's blog\n" + + "U #123 - Unsubscribe from comments\n" + + "U @username - Unsubscribe from user's blog\n" + + "BL - Show your blacklist\n" + + "BL @username - Add/delete user to/from your blacklist\n" + + "BL *tag - Add/delete tag to/from your blacklist\n" + + "ON / OFF - Enable/disable subscriptions delivery\n" + + "PING - Pong\n" + + "\n" + + "Read more: http://juick.com/help/"; + + public static boolean incomingPresence(Presence p) { + final String username = p.to.Username.toLowerCase(); + final boolean toJuick = username.equals("juick"); + + if (p.type == null) { + Presence reply = new Presence(); + reply.from = new JID(p.to.Username, p.to.Host, null); + reply.to = new JID(p.from.Username, p.from.Host, null); + reply.type = Presence.Type.unsubscribe; + XMPPComponent.sendOut(reply); + return true; + } else if (p.type.equals(Presence.Type.probe)) { + int uid_to = 0; + if (!toJuick) { + synchronized (XMPPComponent.sqlSync) { + uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); + } + } + + if (toJuick || uid_to > 0) { + Presence reply = new Presence(); + reply.from = p.to; + reply.from.Resource = "Juick"; + reply.to = p.from; + reply.priority = 10; + XMPPComponent.sendOut(reply); + } else { + Presence reply = new Presence(p.to, p.from, Presence.Type.error); + reply.id = p.id; + reply.addChild(new Error(Error.Type.cancel, "item-not-found")); + XMPPComponent.sendOut(reply); + return true; + } + return true; + } else if (p.type.equals(Presence.Type.subscribe)) { + boolean canSubscribe = false; + if (toJuick) { + canSubscribe = true; + } else { + synchronized (XMPPComponent.sqlSync) { + int uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); + if (uid_to > 0) { + PMQueries.addPMinRoster(XMPPComponent.sql, uid_to, p.from.Bare()); + canSubscribe = true; + } + } + } + + if (canSubscribe) { + Presence reply = new Presence(p.to, p.from, Presence.Type.subscribed); + XMPPComponent.sendOut(reply); + + reply.from.Resource = "Juick"; + reply.priority = 10; + reply.type = null; + XMPPComponent.sendOut(reply); + + return true; + } else { + Presence reply = new Presence(p.to, p.from, Presence.Type.error); + reply.id = p.id; + reply.addChild(new Error(Error.Type.cancel, "item-not-found")); + XMPPComponent.sendOut(reply); + return true; + } + } else if (p.type.equals(Presence.Type.unsubscribe)) { + if (!toJuick) { + synchronized (XMPPComponent.sqlSync) { + int uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); + if (uid_to > 0) { + PMQueries.removePMinRoster(XMPPComponent.sql, uid_to, p.from.Bare()); + } + } + } + + Presence reply = new Presence(p.to, p.from, Presence.Type.unsubscribed); + XMPPComponent.sendOut(reply); + } + + return false; + } + + public static boolean incomingMessage(Message msg) { + if (msg.body == null || msg.body.isEmpty()) { + return true; + } + + String username = msg.to.Username.toLowerCase(); + + User user_from = null; + String signuphash = ""; + synchronized (XMPPComponent.sqlSync) { + user_from = UserQueries.getUserByJID(XMPPComponent.sql, msg.from.Bare()); + if (user_from == null) { + signuphash = UserQueries.getSignUpHashByJID(XMPPComponent.sql, msg.from.Bare()); + } + } + + if (user_from == null) { + Message reply = new Message(msg.to, msg.from, Message.Type.chat); + if (username.equals("juick")) { + reply.body = "Для того, чтобы начать пользоваться сервисом, пожалуйста пройдите быструю регистрацию: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nЕсли у вас уже есть учетная запись на Juick, вы сможете присоединить этот JabberID к ней.\n\nTo start using Juick, please sign up: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nIf you already have an account on Juick, you will be proposed to attach this JabberID to your existing account."; + } else { + reply.body = "Внимание, системное сообщение!\nВаш JabberID не обнаружен в списке доверенных. Для того, чтобы отправить сообщение пользователю " + username + "@juick.com, пожалуйста зарегистрируйте свой JabberID в системе: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nЕсли у вас уже есть учетная запись на Juick, вы сможете присоединить этот JabberID к ней.\n\nWarning, system message!\nYour JabberID is not found in our server's white list. To send a message to " + username + "@juick.com, please sign up: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nIf you already have an account on Juick, you will be proposed to attach this JabberID to your existing account."; + } + XMPPComponent.sendOut(reply); + return true; + } + + if (username.equals("juick")) { + return incomingMessageJuick(user_from, msg); + } + + int uid_to = 0; + synchronized (XMPPComponent.sqlSync) { + uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); + } + + if (uid_to == 0) { + Message reply = new Message(msg.to, msg.from, Message.Type.error); + reply.id = msg.id; + reply.addChild(new Error(Error.Type.cancel, "item-not-found")); + XMPPComponent.sendOut(reply); + return true; + } + + boolean success = false; + synchronized (XMPPComponent.sqlSync) { + if (!UserQueries.isInBLAny(XMPPComponent.sql, uid_to, user_from.getUID())) { + success = PMQueries.createPM(XMPPComponent.sql, user_from.getUID(), uid_to, msg.body); + } + } + + if (success) { + Message m = new Message(); + m.from = new JID("juick", "juick.com", null); + m.to = new JID(Integer.toString(uid_to), "push.juick.com", null); + JuickMessage jmsg = new JuickMessage(); + synchronized (XMPPComponent.sqlSync) { + jmsg.setUser(UserQueries.getUserByUID(XMPPComponent.sql, user_from.getUID())); + } + jmsg.setText(msg.body); + m.childs.add(jmsg); + XMPPComponent.connRouter.sendStanza(m.toString()); + + m.to.Host = "ws.juick.com"; + XMPPComponent.connRouter.sendStanza(m.toString()); + + String jid; + boolean inroster = false; + synchronized (XMPPComponent.sqlSync) { + jid = UserQueries.getJIDbyUID(XMPPComponent.sql, uid_to); + if (jid != null) { + inroster = PMQueries.havePMinRoster(XMPPComponent.sql, user_from.getUID(), jid); + } + } + + if (jid != null) { + Message mm = new Message(); + mm.to = new JID(jid); + mm.type = Message.Type.chat; + if (inroster) { + mm.from = new JID(jmsg.getUser().getUName(), "juick.com", "Juick"); + mm.body = msg.body; + } else { + mm.from = new JID("juick", "juick.com", "Juick"); + mm.body = "Private message from @" + jmsg.getUser().getUName() + ":\n" + msg.body; + } + XMPPComponent.sendOut(mm); + } + + } else { + Message reply = new Message(msg.to, msg.from, Message.Type.error); + reply.id = msg.id; + reply.addChild(new Error(Error.Type.cancel, "not-allowed")); + XMPPComponent.sendOut(reply); + } + + return true; + } + private static Pattern regexPM = Pattern.compile("^\\@(\\S+)\\s+([\\s\\S]+)$"); + + public static boolean incomingMessageJuick(User user_from, Message msg) { + String command = msg.body.trim(); + int commandlen = command.length(); + + // COMPATIBILITY + if (commandlen > 7 && command.substring(0, 3).equalsIgnoreCase("PM ")) { + command = command.substring(3).trim(); + commandlen = command.length(); + } + + if (commandlen == 4) { + if (command.equalsIgnoreCase("PING")) { + commandPing(msg); + return true; + } else if (command.equalsIgnoreCase("HELP")) { + commandHelp(msg); + return true; + } + } else if (commandlen == 5 && command.equalsIgnoreCase("LOGIN")) { + commandLogin(msg, user_from); + return true; + } else if (command.charAt(0) == '@') { + Matcher matchPM = regexPM.matcher(command); + if (matchPM.find()) { + String user_to = matchPM.group(1); + String msgtxt = matchPM.group(2); + commandPM(msg, user_from, user_to, msgtxt); + return true; + } + } else if (commandlen == 2 && command.equalsIgnoreCase("BL")) { + commandBLShow(msg, user_from); + return true; + } + + return false; + } + + private static void commandPing(Message m) { + Presence p = new Presence(JuickJID, m.from); + p.priority = 10; + XMPPComponent.sendOut(p); + + Message reply = new Message(JuickJID, m.from, Message.Type.chat); + reply.body = "PONG"; + XMPPComponent.sendOut(reply); + } + + private static void commandHelp(Message m) { + Message reply = new Message(JuickJID, m.from, Message.Type.chat); + reply.body = HELPTEXT; + XMPPComponent.sendOut(reply); + } + + private static void commandLogin(Message m, User user_from) { + Message reply = new Message(JuickJID, m.from, Message.Type.chat); + reply.body = "http://juick.com/login?" + UserQueries.getHashByUID(XMPPComponent.sql, user_from.getUID()); + XMPPComponent.sendOut(reply); + } + + private static void commandPM(Message m, User user_from, String user_to, String body) { + int ret = 0; + + int uid_to = 0; + String jid_to = null; + boolean haveInRoster = false; + + synchronized (XMPPComponent.sqlSync) { + if (user_to.indexOf('@') > 0) { + uid_to = UserQueries.getUIDbyJID(XMPPComponent.sql, user_to); + } else { + uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, user_to); + } + + if (uid_to > 0) { + if (!UserQueries.isInBLAny(XMPPComponent.sql, uid_to, user_from.getUID())) { + if (PMQueries.createPM(XMPPComponent.sql, user_from.getUID(), uid_to, body)) { + jid_to = UserQueries.getJIDbyUID(XMPPComponent.sql, uid_to); + if (jid_to != null) { + haveInRoster = PMQueries.havePMinRoster(XMPPComponent.sql, user_from.getUID(), jid_to); + } + ret = 200; + } else { + ret = 500; + } + } else { + ret = 403; + } + } else { + ret = 404; + } + } + + if (ret == 200) { + Message msg = new Message(); + msg.from = new JID("juick", "juick.com", null); + msg.to = new JID(Integer.toString(uid_to), "push.juick.com", null); + JuickMessage jmsg = new JuickMessage(); + jmsg.setUser(user_from); + jmsg.setText(body); + msg.childs.add(jmsg); + XMPPComponent.connRouter.sendStanza(msg.toString()); + + msg.to.Host = "ws.juick.com"; + XMPPComponent.connRouter.sendStanza(msg.toString()); + + if (jid_to != null) { + Message mm = new Message(); + mm.to = new JID(jid_to); + mm.type = Message.Type.chat; + if (haveInRoster) { + mm.from = new JID(user_from.getUName(), "juick.com", "Juick"); + mm.body = body; + } else { + mm.from = new JID("juick", "juick.com", "Juick"); + mm.body = "Private message from @" + user_from.getUName() + ":\n" + body; + } + XMPPComponent.sendOut(mm); + } + } + + Message reply = new Message(m.to, m.from); + if (ret == 200) { + reply.type = m.type; + reply.body = "Private message sent"; + } else { + reply.type = Message.Type.error; + reply.body = "Error " + ret; + } + XMPPComponent.sendOut(reply); + } + + private static void commandBLShow(Message m, User user_from) { + List<User> blusers; + List<String> bltags; + + synchronized (XMPPComponent.sqlSync) { + blusers = UserQueries.getUserBLUsers(XMPPComponent.sql, user_from.getUID()); + bltags = TagQueries.getUserBLTags(XMPPComponent.sql, user_from.getUID()); + } + + String txt = ""; + if (bltags.size() > 0) { + for (int i = 0; i < bltags.size(); i++) { + txt += "*" + bltags.get(i) + "\n"; + } + + if (blusers.size() > 0) { + txt += "\n"; + } + } + if (blusers.size() > 0) { + for (int i = 0; i < blusers.size(); i++) { + txt += "@" + blusers.get(i).getUName() + "\n"; + } + } + if (txt.isEmpty()) { + txt = "You don't have any users or tags in your blacklist."; + } + + Message reply = new Message(JuickJID, m.from, Message.Type.chat); + reply.body = txt; + XMPPComponent.sendOut(reply); + } +} diff --git a/src/main/java/com/juick/xmpp/s2s/Shutdown.java b/src/main/java/com/juick/xmpp/s2s/Shutdown.java new file mode 100644 index 000000000..fb3543f81 --- /dev/null +++ b/src/main/java/com/juick/xmpp/s2s/Shutdown.java @@ -0,0 +1,45 @@ +package com.juick.xmpp.s2s; + +import java.sql.SQLException; +import java.util.Iterator; + +/** + * + * @author ugnich + */ +public class Shutdown extends Thread { + + @Override + public void run() { + System.out.println("SHUTTING DOWN"); + + synchronized (XMPPComponent.outConnections) { + for (Iterator<ConnectionOut> i = XMPPComponent.outConnections.iterator(); i.hasNext();) { + ConnectionOut c = i.next(); + c.closeConnection(); + i.remove(); + } + } + + synchronized (XMPPComponent.inConnections) { + for (Iterator<ConnectionIn> i = XMPPComponent.inConnections.iterator(); i.hasNext();) { + ConnectionIn c = i.next(); + c.closeConnection(); + i.remove(); + } + } + + XMPPComponent.connRouter.closeConnection(); + + synchronized (XMPPComponent.sqlSync) { + if (XMPPComponent.sql != null) { + try { + XMPPComponent.sql.close(); + XMPPComponent.sql = null; + } catch (SQLException e) { + System.err.println("SQL ERROR: " + e); + } + } + } + } +} diff --git a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java new file mode 100644 index 000000000..ff4ec3e68 --- /dev/null +++ b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java @@ -0,0 +1,211 @@ +package com.juick.xmpp.s2s; + +import com.juick.xmpp.Stanza; +import com.juick.xmpp.StanzaChild; +import com.juick.xmpp.extensions.JuickMessage; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; +import java.io.FileInputStream; +import java.io.IOException; +import java.sql.Driver; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.*; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author ugnich + */ +public class XMPPComponent implements ServletContextListener { + + private static final Logger LOGGER = Logger.getLogger(XMPPComponent.class.getName()); + + ExecutorService executorService; + + public static String HOSTNAME = null; + public static String COMPONENTNAME = null; + public static String LOGFILE = null; + public static String STATSFILE = null; + public static ConnectionRouter connRouter; + static final List<ConnectionIn> inConnections = Collections.synchronizedList(new ArrayList<>()); + static final List<ConnectionOut> outConnections = Collections.synchronizedList(new ArrayList<>()); + static final List<CacheEntry> outCache = Collections.synchronizedList(new ArrayList<>()); + static final Integer sqlSync = 0; + static java.sql.Connection sql; + final public static HashMap<String, StanzaChild> childParsers = new HashMap<>(); + + public static void addConnectionIn(ConnectionIn c) { + synchronized (inConnections) { + inConnections.add(c); + } + } + + public static void addConnectionOut(ConnectionOut c) { + synchronized (outConnections) { + outConnections.add(c); + } + } + + public static void removeConnectionIn(ConnectionIn c) { + synchronized (inConnections) { + inConnections.remove(c); + } + } + + public static void removeConnectionOut(ConnectionOut c) { + synchronized (outConnections) { + outConnections.remove(c); + } + } + + public static String getFromCache(String hostname) { + CacheEntry ret = null; + synchronized (outCache) { + for (Iterator<CacheEntry> i = outCache.iterator(); i.hasNext();) { + CacheEntry c = i.next(); + if (c.hostname != null && c.hostname.equals(hostname)) { + ret = c; + i.remove(); + break; + } + } + } + return (ret != null) ? ret.xml : null; + } + + public static ConnectionOut getConnectionOut(String hostname, boolean needReady) { + synchronized (outConnections) { + for (ConnectionOut c : outConnections) { + if (c.to != null && c.to.equals(hostname) && (!needReady || c.streamReady)) { + return c; + } + } + } + return null; + } + + public static ConnectionIn getConnectionIn(String streamID) { + synchronized (inConnections) { + for (ConnectionIn c : inConnections) { + if (c.streamID != null && c.streamID.equals(streamID)) { + return c; + } + } + } + return null; + } + + public static void sendOut(Stanza s) { + sendOut(s.to.Host, s.toString()); + } + + public static void sendOut(String hostname, String xml) { + boolean haveAnyConn = false; + + ConnectionOut connOut = null; + synchronized (outConnections) { + for (ConnectionOut c : outConnections) { + if (c.to != null && c.to.equals(hostname)) { + if (c.streamReady) { + connOut = c; + break; + } else { + haveAnyConn = true; + break; + } + } + } + } + if (connOut != null) { + try { + connOut.sendStanza(xml); + } catch (IOException e) { + System.err.println("STREAM TO " + connOut.to + " " + connOut.streamID + " ERROR: " + e.toString()); + } + return; + } + + boolean haveCache = false; + synchronized (outCache) { + for (CacheEntry c : outCache) { + if (c.hostname != null && c.hostname.equals(hostname)) { + c.xml += xml; + c.tsUpdated = System.currentTimeMillis(); + haveCache = true; + break; + } + } + if (!haveCache) { + outCache.add(new CacheEntry(hostname, xml)); + } + } + + if (!haveAnyConn) { + new Thread(new ConnectionOut(hostname)).start(); + } + } + + @Override + public void contextInitialized(ServletContextEvent sce) { + + LOGGER.info("component initialized"); + executorService = Executors.newSingleThreadExecutor(); + executorService.submit(() -> { + Properties conf = new Properties(); + try { + conf.load(new FileInputStream("/etc/juick/s2s.conf")); + HOSTNAME = conf.getProperty("hostname"); + COMPONENTNAME = conf.getProperty("componentname"); + LOGFILE = conf.getProperty("logfile"); + STATSFILE = conf.getProperty("statsfile"); + + Class.forName("com.mysql.jdbc.Driver"); + sql = DriverManager.getConnection("jdbc:mysql://localhost/juick?autoReconnect=true&user=" + conf.getProperty("mysql_username", "") + "&password=" + conf.getProperty("mysql_password", "")); + + Runtime.getRuntime().addShutdownHook(new Shutdown()); + + childParsers.put(JuickMessage.XMLNS, new JuickMessage()); + + connRouter = new ConnectionRouter(); + new Thread(connRouter).start(); + new Thread(new ConnectionListener()).start(); + new Thread(new CleaningUp()).start(); + } catch (IOException | ClassNotFoundException | SQLException e) { + LOGGER.log(Level.SEVERE, "XMPPComponent error", e); + } + }); + } + + + + @Override + public void contextDestroyed(ServletContextEvent sce) { + // Now deregister JDBC drivers in this context's ClassLoader: + // Get the webapp's ClassLoader + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + // Loop through all drivers + Enumeration<Driver> drivers = DriverManager.getDrivers(); + while (drivers.hasMoreElements()) { + Driver driver = drivers.nextElement(); + if (driver.getClass().getClassLoader() == cl) { + // This driver was registered by the webapp's ClassLoader, so deregister it: + try { + LOGGER.info(String.format("Deregistering JDBC driver %s", driver.toString())); + DriverManager.deregisterDriver(driver); + } catch (SQLException ex) { + LOGGER.log(Level.SEVERE, String.format("Error deregistering JDBC driver %s", driver), ex); + } + } else { + // driver was not registered by the webapp's ClassLoader and may be in use elsewhere + LOGGER.log(Level.SEVERE, String.format("Not deregistering JDBC driver %s as it does not belong to this webapp's ClassLoader", driver)); + } + } + executorService.shutdown(); + LOGGER.info("component destroyed"); + } +} diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 40c484d7e..b4fbc03df 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -1,5 +1,7 @@ <?xml version="1.0" encoding="UTF-8"?> -<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> +<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <servlet-name>Main</servlet-name> <servlet-class>com.juick.http.www.Main</servlet-class> @@ -40,4 +42,9 @@ <display-name>CrosspostComponent</display-name> <listener-class>com.juick.CrosspostComponent</listener-class> </listener> + <listener> + <description>XMPP module</description> + <display-name>XMPPComponent</display-name> + <listener-class>com.juick.xmpp.s2s.XMPPComponent</listener-class> + </listener> </web-app> |