jdk/test/java/net/Socks/SocksServer.java
author chegar
Wed, 21 Jul 2010 13:52:38 +0100 (2010-07-21)
changeset 6116 3880a05ff32e
parent 5506 202f599c92aa
child 12183 06672edfb593
permissions -rw-r--r--
6970262: TEST_BUG: test/java/net/NetworkInterface/IPv4Only.java has wrong test name in @run tag Reviewed-by: alanb, dsamersoff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
public class SocksServer extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
    // Some useful SOCKS constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
    static final int PROTO_VERS4                = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
    static final int PROTO_VERS         = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
    static final int DEFAULT_PORT               = 1080;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    static final int NO_AUTH            = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    static final int GSSAPI             = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    static final int USER_PASSW         = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    static final int NO_METHODS         = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static final int CONNECT            = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static final int BIND                       = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static final int UDP_ASSOC          = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static final int IPV4                       = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static final int DOMAIN_NAME                = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static final int IPV6                       = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static final int REQUEST_OK         = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static final int GENERAL_FAILURE    = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static final int NOT_ALLOWED                = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static final int NET_UNREACHABLE    = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static final int HOST_UNREACHABLE   = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static final int CONN_REFUSED               = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static final int TTL_EXPIRED                = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static final int CMD_NOT_SUPPORTED  = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static final int ADDR_TYPE_NOT_SUP  = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private ServerSocket server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private boolean useV4 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private java.util.Hashtable users = new java.util.Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // Inner class to handle protocol with client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // This is the bulk of the work (protocol handler)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    class ClientHandler extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        private InputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        private OutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        private Socket client;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        private Socket dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // Simple tunneling class, moving bits from one stream to another
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        class Tunnel extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            private InputStream tin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            private OutputStream tout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            Tunnel(InputStream in, OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                tin = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                tout = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                int b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        b = tin.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        if (b == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                            tin.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                            tout.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                        tout.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        // actually exit from the thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        ClientHandler(Socket s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            client = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            in = client.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            out = client.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        private void readBuf(InputStream is, byte[] buf) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            int l = buf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                i = is.read(buf, count, l - count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                if (i == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    throw new IOException("unexpected EOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                count += i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            } while (count < l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        private boolean userPassAuth() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            int ver = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            int ulen = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if (ulen <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                throw new SocketException("SOCKS protocol error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            byte[] buf = new byte[ulen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            readBuf(in, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            String uname = new String(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            String password = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            ulen = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (ulen < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                throw new SocketException("SOCKS protocol error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            if (ulen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                buf = new byte[ulen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                readBuf(in, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                password = new String(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            // Check username/password validity here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            System.err.println("User: '" + uname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            System.err.println("PSWD: '" + password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (users.containsKey(uname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                String p1 = (String) users.get(uname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                System.err.println("p1 = " + p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                if (p1.equals(password)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    out.write(REQUEST_OK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            out.write(NOT_ALLOWED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        private void purge() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            client.setSoTimeout(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            while(!done && i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    i = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                } catch(IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        // Handle the SOCKS version 4 protocl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        private void getRequestV4() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            int ver = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            int cmd = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            if (ver == -1 || cmd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                // EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (ver != 0 && ver != 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                out.write(PROTO_VERS4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                out.write(91); // Bad Request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                purge();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (cmd == CONNECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                int port = ((in.read() & 0xff) << 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                port += (in.read() & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                byte[] buf = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                readBuf(in, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                InetAddress addr = InetAddress.getByAddress(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                // We don't use the username...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    c = (in.read() & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                } while (c!=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                boolean ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    dest = new Socket(addr, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                if (!ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    out.write(PROTO_VERS4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    out.write(91);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    out.write(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    purge();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                out.write(PROTO_VERS4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                out.write(90); // Success
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                out.write((port >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                out.write(port & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                out.write(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                InputStream in2 = dest.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                OutputStream out2 = dest.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                Tunnel tunnel = new Tunnel(in2, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                tunnel.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                int b = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        b = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        if (b == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                            out2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        out2.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                } while (!client.isClosed());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        // Negociate the authentication scheme with the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        private void negociate() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            int ver = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            int n = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            byte[] buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                buf = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                readBuf(in, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            int scheme = NO_AUTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            for (int i = 0; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                if (buf[i] == USER_PASSW)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    scheme = USER_PASSW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            out.write(scheme);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            if (scheme == USER_PASSW)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                userPassAuth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        // Send error message then close the streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        private void sendError(int code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                out.write(code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                out.write(IPV4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                for (int i=0; i<6; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        // Actually connect the proxy to the destination then initiate tunneling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        private void doConnect(InetSocketAddress addr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            dest = new Socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                dest.connect(addr, 10000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            } catch (SocketTimeoutException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                sendError(HOST_UNREACHABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            } catch (ConnectException cex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                sendError(CONN_REFUSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            // Success
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            InetAddress iadd = addr.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            if (iadd instanceof Inet4Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                out.write(REQUEST_OK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                out.write(IPV4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                out.write(iadd.getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            } else if (iadd instanceof Inet6Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                out.write(REQUEST_OK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                out.write(IPV6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                out.write(iadd.getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                sendError(GENERAL_FAILURE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            out.write((addr.getPort() >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            out.write((addr.getPort() >> 0) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            InputStream in2 = dest.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            OutputStream out2 = dest.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            Tunnel tunnel = new Tunnel(in2, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            tunnel.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            int b = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                // Note that the socket might be closed from another thread (the tunnel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    b = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    if (b == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        out2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    out2.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                } catch(IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            } while (!client.isClosed());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        private void doBind(InetSocketAddress addr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            ServerSocket svr = new ServerSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            svr.bind(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            InetSocketAddress bad = (InetSocketAddress) svr.getLocalSocketAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            out.write(REQUEST_OK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            out.write(IPV4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            out.write(bad.getAddress().getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            out.write((bad.getPort() >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            out.write((bad.getPort() & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            dest = svr.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            bad = (InetSocketAddress) dest.getRemoteSocketAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            out.write(REQUEST_OK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            out.write(IPV4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            out.write(bad.getAddress().getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            out.write((bad.getPort() >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            out.write((bad.getPort() & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            InputStream in2 = dest.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            OutputStream out2 = dest.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            Tunnel tunnel = new Tunnel(in2, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            tunnel.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            int b = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                // Note that the socket might be close from another thread (the tunnel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    b = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    if (b == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        out2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    out2.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                } catch(IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            } while (!client.isClosed());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        // Handle the SOCKS v5 requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        private void getRequest() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            int ver = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            int cmd = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            if (ver == -1 || cmd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            int rsv = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            int atyp = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            String addr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            int port = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            switch(atyp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            case IPV4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                byte[] buf = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                readBuf(in, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                for (i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    sb.append(buf[i]&0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    if (i < 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                        sb.append('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                addr = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            case DOMAIN_NAME:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                int i = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                byte[] buf = new byte[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                readBuf(in, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                addr = new String(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            case IPV6:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                byte[] buf = new byte[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                readBuf(in, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                for (i = 0; i<16; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    sb.append(Integer.toHexString(buf[i]&0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    if (i < 15)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                        sb.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                addr = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            port = ((in.read()&0xff) << 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            port += (in.read()&0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            InetSocketAddress socAddr = new InetSocketAddress(addr, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            switch(cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            case CONNECT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                doConnect(socAddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            case BIND:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                doBind(socAddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            case UDP_ASSOC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                // doUDP(socAddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            String line = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                if (useV4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    getRequestV4();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    negociate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    getRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    sendError(GENERAL_FAILURE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    client.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                } catch (IOException e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    public SocksServer(int port, boolean v4) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        this(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        this.useV4 = v4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public SocksServer(int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        server = new ServerSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        server.bind(new InetSocketAddress(port));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    public SocksServer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        this (DEFAULT_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    public void addUser(String user, String passwd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        users.put(user, passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    public synchronized void terminate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        ClientHandler cl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                Socket s = server.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                cl = new ClientHandler(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                cl.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                if (cl != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    cl.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
}