test/jdk/sun/security/x509/URICertStore/SocksProxy.java
author aeubanks
Thu, 16 May 2019 15:58:15 -0700
changeset 55093 b7ac60035a28
parent 47216 71c04702a3d5
permissions -rw-r--r--
8224081: SOCKS v4 doesn't work with IPv6 Reviewed-by: chegar, dfuchs, mullan, vtewari Contributed-by: aeubanks@google.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39489
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
     1
/*
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
     4
 *
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
     8
 *
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
45028
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39489
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
39489
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    13
 * accompanied this code).
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    14
 *
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    18
 *
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    21
 * questions.
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    22
 */
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    23
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    24
import java.io.IOException;
55093
b7ac60035a28 8224081: SOCKS v4 doesn't work with IPv6
aeubanks
parents: 47216
diff changeset
    25
import java.net.InetAddress;
39489
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    26
import java.net.ServerSocket;
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    27
import java.net.Socket;
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    28
import java.util.Objects;
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    29
import java.util.function.Consumer;
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    30
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    31
import javax.net.ServerSocketFactory;
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    32
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    33
/*
55093
b7ac60035a28 8224081: SOCKS v4 doesn't work with IPv6
aeubanks
parents: 47216
diff changeset
    34
 * A simple socks proxy for traveling socket.
39489
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    35
 */
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    36
class SocksProxy implements Runnable, AutoCloseable {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    37
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    38
    private ServerSocket server;
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    39
    private Consumer<Socket> socketConsumer;
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    40
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    41
    private SocksProxy(ServerSocket server, Consumer<Socket> socketConsumer) {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    42
        this.server = server;
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    43
        this.socketConsumer = socketConsumer;
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    44
    }
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    45
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    46
    static SocksProxy startProxy(Consumer<Socket> socketConsumer)
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    47
            throws IOException {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    48
        Objects.requireNonNull(socketConsumer, "socketConsumer cannot be null");
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    49
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    50
        ServerSocket server
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    51
                = ServerSocketFactory.getDefault().createServerSocket(0);
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    52
55093
b7ac60035a28 8224081: SOCKS v4 doesn't work with IPv6
aeubanks
parents: 47216
diff changeset
    53
        System.setProperty("socksProxyHost",
b7ac60035a28 8224081: SOCKS v4 doesn't work with IPv6
aeubanks
parents: 47216
diff changeset
    54
                InetAddress.getLoopbackAddress().getHostAddress());
39489
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    55
        System.setProperty("socksProxyPort",
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    56
                String.valueOf(server.getLocalPort()));
55093
b7ac60035a28 8224081: SOCKS v4 doesn't work with IPv6
aeubanks
parents: 47216
diff changeset
    57
        System.setProperty("socksProxyVersion", "5");
39489
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    58
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    59
        SocksProxy proxy = new SocksProxy(server, socketConsumer);
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    60
        Thread proxyThread = new Thread(proxy, "Proxy");
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    61
        proxyThread.setDaemon(true);
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    62
        proxyThread.start();
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    63
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    64
        return proxy;
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    65
    }
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    66
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    67
    @Override
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    68
    public void run() {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    69
        while (!server.isClosed()) {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    70
            try(Socket socket = server.accept()) {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    71
                System.out.println("Server: accepted connection");
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    72
                if (socketConsumer != null) {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    73
                    socketConsumer.accept(socket);
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    74
                }
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    75
            } catch (IOException e) {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    76
                if (!server.isClosed()) {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    77
                    throw new RuntimeException(
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    78
                            "Server: accept connection failed", e);
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    79
                } else {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    80
                    System.out.println("Server is closed.");
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    81
                }
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    82
            }
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    83
        }
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    84
    }
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    85
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    86
    @Override
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    87
    public void close() throws Exception {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    88
        if (!server.isClosed()) {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    89
            server.close();
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    90
        }
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    91
    }
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    92
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    93
    int getPort() {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    94
        return server.getLocalPort();
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    95
    }
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    96
}