aboutsummaryrefslogtreecommitdiff
path: root/src/com/juick/jabber/ws/WSData.java
blob: 9719351004ae56cb99fd64c9e9a05a77b6109a62 (plain) (blame)
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
package com.juick.jabber.ws;

import java.io.IOException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.sql.Connection;
import java.util.Iterator;

/**
 *
 * @author ugnich
 */
public class WSData implements Runnable {

    Connection sql;
    public static Selector sel;

    public WSData(Connection sql) {
        this.sql = sql;
    }

    @Override
    public void run() {
        try {
            sel = Selector.open();
            while (true) {
                sel.select();
                Iterator<SelectionKey> it = sel.selectedKeys().iterator();
                while (it.hasNext()) {
                    SelectionKey selKey = it.next();
                    it.remove();

                    SocketChannel sChannel = (SocketChannel) selKey.channel();
                    ByteBuffer buf = ByteBuffer.allocate(10240);
                    try {
                        if (sChannel.read(buf) > 0) {
                            buf.flip();
                            CharBuffer charbuf = Charset.forName("ISO-8859-1").decode(buf);
                            if (charbuf.charAt(0) == 0 && charbuf.charAt(charbuf.length() - 1) == 0xFF) {
                                wsTextFrame(sChannel, charbuf.subSequence(1, charbuf.length() - 2));
                            } else if (charbuf.charAt(0) == 'G' && charbuf.charAt(1) == 'E' && charbuf.charAt(2) == 'T' && charbuf.charAt(3) == ' ') {
                                wsHandshake(sChannel, buf);
                            } else {
                                throw new IOException(sChannel.socket().getRemoteSocketAddress().toString() + " INVALID FRAME");
                            }
                        } else {
                            throw new IOException(sChannel.socket().getRemoteSocketAddress().toString()+ " NO DATA");
                        }
                    } catch (IOException e) {
                        System.err.println("WSData: " + e);
                        sChannel.socket().close();
                        sChannel.close();
                        selKey.cancel();
                    }
                }
            }
        } catch (Exception e) {
            System.err.println("WSData: " + e);
        }
    }

    public void wsHandshake(SocketChannel sock, ByteBuffer buf) throws Exception {
        String hOrigin = null;
        String hHost = null;
        String hLocation = null;
        String hSecWebSocketKey1 = null;
        String hSecWebSocketKey2 = null;
        String hCookie = null;

        buf.rewind();
        CharBuffer charbuf = Charset.forName("ISO-8859-1").decode(buf);
        String headers[] = charbuf.toString().split("\r\n");
        for (int i = 0; i < headers.length; i++) {
            String h[] = headers[i].split(" ", 2);
            if (h.length == 2) {
                if (h[0].equals("GET")) {
                    hLocation = headers[i].split(" ", 3)[1];
                } else if (h[0].equals("Origin:")) {
                    hOrigin = h[1];
                } else if (h[0].equals("Host:")) {
                    hHost = h[1];
                } else if (h[0].equals("Sec-WebSocket-Key1:")) {
                    hSecWebSocketKey1 = h[1];
                } else if (h[0].equals("Sec-WebSocket-Key2:")) {
                    hSecWebSocketKey2 = h[1];
                } else if (h[0].equals("Cookie:")) {
                    hCookie = h[1];
                }
            }
        }

        if (hOrigin == null || hHost == null || hLocation == null || hSecWebSocketKey1 == null || hSecWebSocketKey2 == null) {
            throw new IOException(sock.socket().getRemoteSocketAddress().toString() + " Invalid headers");
        }

        // Cookies
        int UID = 0;
        String hash = null;
        if (hCookie != null) {
            String cookies[] = hCookie.split("; ");
            for (int i = 0; i < cookies.length; i++) {
                String cookie[] = cookies[i].split("=", 2);
                if (cookie[0].equals("hash")) {
                    hash = cookie[1];
                    break;
                }
            }
            if (hash != null) {
                UID = com.juick.server.UserQueries.getUIDbyHash(sql, hash);
            }
        }

        // URL
        String loc[] = hLocation.split("/");
        int MID = 0;
        if (hLocation.equals("/my") && UID > 0) {
            Main.sockMessages.add(new SocketSubscribed(sock, UID, 0));
        } else if (hLocation.equals("/all")) {
            Main.sockAll.add(new SocketSubscribed(sock, UID, 0));
        } else if ((loc.length == 2 || loc.length == 3) && loc[1].equals("replies")) {
            if (loc.length == 2) {
                Main.sockReplies.add(new SocketSubscribed(sock, UID, 0));
            } else {
                try {
                    MID = Integer.parseInt(loc[2]);
                } catch (Exception e) {
                }
                if (MID > 0) {
                    Main.sockReplies.add(new SocketSubscribed(sock, UID, MID));
                } else {
                    throw new IOException(sock.socket().getRemoteSocketAddress().toString() + " Invalid MID");
                }
            }
        } else {
            throw new IOException(sock.socket().getRemoteSocketAddress().toString() + " Invalid location");
        }

        System.out.println(sock.socket().getRemoteSocketAddress().toString() + " HANDSHAKE (Hash=" + hash + "; UID = " + UID + "; MID = " + MID + ")");

        Long lSecNum1 = calcSecKeyNum(hSecWebSocketKey1);
        Long lSecNum2 = calcSecKeyNum(hSecWebSocketKey2);

        BigInteger sec1 = new BigInteger(lSecNum1.toString());
        BigInteger sec2 = new BigInteger(lSecNum2.toString());

        // concatenate 3 parts secNum1 + secNum2 + secKey (16 Bytes)
        byte[] l128Bit = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        byte[] lTmp;

        lTmp = sec1.toByteArray();
        int lIdx = lTmp.length;
        int lCnt = 0;
        while (lIdx > 0 && lCnt < 4) {
            lIdx--;
            lCnt++;
            l128Bit[4 - lCnt] = lTmp[lIdx];
        }

        lTmp = sec2.toByteArray();
        lIdx = lTmp.length;
        lCnt = 0;
        while (lIdx > 0 && lCnt < 4) {
            lIdx--;
            lCnt++;
            l128Bit[8 - lCnt] = lTmp[lIdx];
        }

        buf.rewind();
        for (int i = 0; i < 8; i++) {
            l128Bit[8 + i] = buf.get(buf.limit() - 8 + i);
        }

        String outstr = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"
                + "Upgrade: WebSocket\r\n"
                + "Connection: Upgrade\r\n"
                + "Sec-WebSocket-Origin: " + hOrigin + "\r\n"
                + "Sec-WebSocket-Location: ws://" + hHost + hLocation + "\r\n"
                + "Sec-WebSocket-Protocol: sample\r\n"
                + "\r\n";
        ByteBuffer out = ByteBuffer.allocate(4096);
        out.put(Charset.forName("ISO-8859-1").encode(outstr));
        out.put(MessageDigest.getInstance("MD5").digest(l128Bit));
        out.flip();

        sock.write(out);
    }

    private static long calcSecKeyNum(String aKey) {
        StringBuilder lSB = new StringBuilder();
        int lSpaces = 0;
        for (int i = 0; i < aKey.length(); i++) {
            char lC = aKey.charAt(i);
            if (lC == ' ') {
                lSpaces++;
            } else if (lC >= '0' && lC <= '9') {
                lSB.append(lC);
            }
        }
        long lRes = -1;
        if (lSpaces > 0) {
            try {
                lRes = Long.parseLong(lSB.toString()) / lSpaces;
            } catch (NumberFormatException ex) {
                // use default result
            }
        }
        return lRes;
    }

    public void wsTextFrame(SocketChannel sock, CharSequence csbuf) {
        String buf = csbuf.toString();
        if (buf.equals(" ")) {
            ByteBuffer out = ByteBuffer.allocate(4);
            out.put((byte) 0x00);
            out.put((byte) 0x20);
            out.put((byte) 0xFF);
            out.flip();
            out.rewind();
            try {
                sock.write(out);
            } catch (IOException e) {
            }
        } else {
            System.out.println(sock.socket().getRemoteSocketAddress().toString() + " DATA '" + buf + "'");
        }
    }
}