jdk/test/java/net/Socks/SocksServer.java
author coleenp
Wed, 17 Jun 2015 21:44:48 +0000
changeset 31362 8957ccbb5821
parent 22276 7fc4c8b08e49
permissions -rw-r--r--
8098821: Crash in system dictionary initialization with shared strings Summary: map string regions after the compressed class base is known Reviewed-by: iklam, dcubed Contributed-by: coleen.phillimore@oracle.com, mikhailo.seledtsov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12183
diff changeset
     2
 * Copyright (c) 2002, 2012, 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.*;
12183
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
    25
import java.util.HashMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
public class SocksServer extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
    // Some useful SOCKS constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
12183
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
    30
    static final int PROTO_VERS4        = 4;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
    static final int PROTO_VERS         = 5;
12183
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
    32
    static final int DEFAULT_PORT       = 1080;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    static final int NO_AUTH            = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    static final int GSSAPI             = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    static final int USER_PASSW         = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    static final int NO_METHODS         = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static final int CONNECT            = 1;
12183
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
    40
    static final int BIND               = 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static final int UDP_ASSOC          = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
12183
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
    43
    static final int IPV4               = 1;
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
    44
    static final int DOMAIN_NAME        = 3;
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
    45
    static final int IPV6               = 4;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static final int REQUEST_OK         = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static final int GENERAL_FAILURE    = 1;
12183
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
    49
    static final int NOT_ALLOWED        = 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static final int NET_UNREACHABLE    = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static final int HOST_UNREACHABLE   = 4;
12183
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
    52
    static final int CONN_REFUSED       = 5;
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
    53
    static final int TTL_EXPIRED        = 6;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static final int CMD_NOT_SUPPORTED  = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static final int ADDR_TYPE_NOT_SUP  = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private ServerSocket server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private boolean useV4 = false;
12183
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
    60
    private HashMap<String,String> users = new HashMap<>();
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
    61
    private volatile boolean done = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // Inner class to handle protocol with client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // This is the bulk of the work (protocol handler)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    class ClientHandler extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        private InputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        private OutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        private Socket client;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        private Socket dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // Simple tunneling class, moving bits from one stream to another
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        class Tunnel extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            private InputStream tin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            private OutputStream tout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            Tunnel(InputStream in, OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                tin = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                tout = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                int b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        b = tin.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                        if (b == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                            tin.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                            tout.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                        tout.write(b);
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 14342
diff changeset
    92
                        tout.flush();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                        // actually exit from the thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                        return;
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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        ClientHandler(Socket s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            client = s;
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 14342
diff changeset
   103
            in = new BufferedInputStream(client.getInputStream());
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 14342
diff changeset
   104
            out = new BufferedOutputStream(client.getOutputStream());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        private void readBuf(InputStream is, byte[] buf) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            int l = buf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                i = is.read(buf, count, l - count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                if (i == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    throw new IOException("unexpected EOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                count += i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            } while (count < l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        private boolean userPassAuth() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            int ver = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            int ulen = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (ulen <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                throw new SocketException("SOCKS protocol error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            byte[] buf = new byte[ulen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            readBuf(in, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            String uname = new String(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            String password = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            ulen = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            if (ulen < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                throw new SocketException("SOCKS protocol error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (ulen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                buf = new byte[ulen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                readBuf(in, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                password = new String(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            // Check username/password validity here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            System.err.println("User: '" + uname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            System.err.println("PSWD: '" + password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (users.containsKey(uname)) {
12183
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
   141
                String p1 = users.get(uname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                System.err.println("p1 = " + p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                if (p1.equals(password)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    out.write(REQUEST_OK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            out.write(NOT_ALLOWED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        private void purge() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            client.setSoTimeout(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            while(!done && i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    i = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                } catch(IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    done = true;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // Handle the SOCKS version 4 protocl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        private void getRequestV4() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            int ver = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            int cmd = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            if (ver == -1 || cmd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                // EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (ver != 0 && ver != 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                out.write(PROTO_VERS4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                out.write(91); // Bad Request
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.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                purge();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            if (cmd == CONNECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                int port = ((in.read() & 0xff) << 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                port += (in.read() & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                byte[] buf = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                readBuf(in, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                InetAddress addr = InetAddress.getByAddress(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                // We don't use the username...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    c = (in.read() & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                } while (c!=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                boolean ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    dest = new Socket(addr, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                if (!ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    out.write(PROTO_VERS4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    out.write(91);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    out.write(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    purge();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                out.write(PROTO_VERS4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                out.write(90); // Success
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                out.write((port >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                out.write(port & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                out.write(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                out.flush();
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 14342
diff changeset
   234
                InputStream in2 = new BufferedInputStream(dest.getInputStream());
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 14342
diff changeset
   235
                OutputStream out2 = new BufferedOutputStream(dest.getOutputStream());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                Tunnel tunnel = new Tunnel(in2, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                tunnel.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                int b = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        b = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        if (b == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                            out2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        out2.write(b);
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 14342
diff changeset
   250
                        out2.flush();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                } while (!client.isClosed());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        // Negociate the authentication scheme with the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        private void negociate() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            int ver = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            int n = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            byte[] buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                buf = new byte[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                readBuf(in, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            int scheme = NO_AUTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            for (int i = 0; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                if (buf[i] == USER_PASSW)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    scheme = USER_PASSW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            out.write(scheme);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (scheme == USER_PASSW)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                userPassAuth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        // Send error message then close the streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        private void sendError(int code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                out.write(code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                out.write(IPV4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                for (int i=0; i<6; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        // Actually connect the proxy to the destination then initiate tunneling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        private void doConnect(InetSocketAddress addr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            dest = new Socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                dest.connect(addr, 10000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            } catch (SocketTimeoutException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                sendError(HOST_UNREACHABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            } catch (ConnectException cex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                sendError(CONN_REFUSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            // Success
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            InetAddress iadd = addr.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            if (iadd instanceof Inet4Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                out.write(REQUEST_OK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                out.write(IPV4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                out.write(iadd.getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            } else if (iadd instanceof Inet6Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                out.write(REQUEST_OK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                out.write(IPV6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                out.write(iadd.getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                sendError(GENERAL_FAILURE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            out.write((addr.getPort() >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            out.write((addr.getPort() >> 0) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 14342
diff changeset
   328
            InputStream in2 = new BufferedInputStream(dest.getInputStream());
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 14342
diff changeset
   329
            OutputStream out2 = new BufferedOutputStream(dest.getOutputStream());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            Tunnel tunnel = new Tunnel(in2, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            tunnel.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            int b = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                // Note that the socket might be closed from another thread (the tunnel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    b = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    if (b == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        out2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    out2.write(b);
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 14342
diff changeset
   345
                    out2.flush();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                } catch(IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            } while (!client.isClosed());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        private void doBind(InetSocketAddress addr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            ServerSocket svr = new ServerSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            svr.bind(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            InetSocketAddress bad = (InetSocketAddress) svr.getLocalSocketAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            out.write(REQUEST_OK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            out.write(IPV4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            out.write(bad.getAddress().getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            out.write((bad.getPort() >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            out.write((bad.getPort() & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            dest = svr.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            bad = (InetSocketAddress) dest.getRemoteSocketAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            out.write(REQUEST_OK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            out.write(IPV4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            out.write(bad.getAddress().getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            out.write((bad.getPort() >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            out.write((bad.getPort() & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            InputStream in2 = dest.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            OutputStream out2 = dest.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            Tunnel tunnel = new Tunnel(in2, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            tunnel.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            int b = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                // Note that the socket might be close from another thread (the tunnel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    b = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    if (b == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                        out2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    out2.write(b);
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 14342
diff changeset
   390
                    out2.flush();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                } catch(IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            } while (!client.isClosed());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        // Handle the SOCKS v5 requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        private void getRequest() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            int ver = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            int cmd = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            if (ver == -1 || cmd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            int rsv = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            int atyp = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            String addr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            int port = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            switch(atyp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            case IPV4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                byte[] buf = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                readBuf(in, buf);
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 14342
diff changeset
   417
                addr = InetAddress.getByAddress(buf).getHostAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            case DOMAIN_NAME:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                int i = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                byte[] buf = new byte[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                readBuf(in, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                addr = new String(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            case IPV6:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                byte[] buf = new byte[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                readBuf(in, buf);
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 14342
diff changeset
   432
                addr = InetAddress.getByAddress(buf).getHostAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            port = ((in.read()&0xff) << 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            port += (in.read()&0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            InetSocketAddress socAddr = new InetSocketAddress(addr, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            switch(cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            case CONNECT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                doConnect(socAddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            case BIND:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                doBind(socAddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            case UDP_ASSOC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                // doUDP(socAddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            String line = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                if (useV4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    getRequestV4();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    negociate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    getRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    sendError(GENERAL_FAILURE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    client.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                } catch (IOException e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    public SocksServer(int port, boolean v4) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        this(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        this.useV4 = v4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    public SocksServer(int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        server = new ServerSocket();
12183
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
   486
        if (port == 0) {
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
   487
            server.bind(null);
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
   488
            this.port = server.getLocalPort();
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
   489
        } else {
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
   490
            server.bind(new InetSocketAddress(port));
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
   491
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    public SocksServer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        this (DEFAULT_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 void addUser(String user, String passwd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        users.put(user, passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
12183
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
   502
    public int getPort() {
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
   503
        return port;
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
   504
    }
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
   505
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
   506
    public void terminate() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        done = true;
12183
06672edfb593 7152796: TEST_BUG: java/net/Socks/SocksV4Test.java does not terminate
chegar
parents: 5506
diff changeset
   508
        try { server.close(); } catch (IOException unused) {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        ClientHandler cl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                Socket s = server.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                cl = new ClientHandler(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                cl.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                if (cl != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    cl.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
}