jdk/test/sun/security/x509/URICertStore/SocksProxy.java
author ihse
Tue, 09 May 2017 12:57:30 +0200
changeset 45028 b0ea3c0bfb81
parent 39489 ae846c9286b0
permissions -rw-r--r--
8179889: Fix typographic errors in copyright headers Reviewed-by: erikj, dholmes
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;
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    25
import java.net.ServerSocket;
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    26
import java.net.Socket;
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    27
import java.util.Objects;
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    28
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
    29
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    30
import javax.net.ServerSocketFactory;
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    31
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
 * A simple socks4 proxy for traveling socket.
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    34
 */
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    35
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
    36
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    37
    private ServerSocket server;
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    38
    private Consumer<Socket> socketConsumer;
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    39
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    40
    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
    41
        this.server = server;
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    42
        this.socketConsumer = socketConsumer;
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    43
    }
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
    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
    46
            throws IOException {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    47
        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
    48
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    49
        ServerSocket server
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    50
                = ServerSocketFactory.getDefault().createServerSocket(0);
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    51
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    52
        System.setProperty("socksProxyHost", "127.0.0.1");
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    53
        System.setProperty("socksProxyPort",
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    54
                String.valueOf(server.getLocalPort()));
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    55
        System.setProperty("socksProxyVersion", "4");
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    56
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    57
        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
    58
        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
    59
        proxyThread.setDaemon(true);
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    60
        proxyThread.start();
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    61
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    62
        return proxy;
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
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    65
    @Override
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    66
    public void run() {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    67
        while (!server.isClosed()) {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    68
            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
    69
                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
    70
                if (socketConsumer != null) {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    71
                    socketConsumer.accept(socket);
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    72
                }
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    73
            } catch (IOException e) {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    74
                if (!server.isClosed()) {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    75
                    throw new RuntimeException(
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    76
                            "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
    77
                } else {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    78
                    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
    79
                }
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    80
            }
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
    @Override
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    85
    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
    86
        if (!server.isClosed()) {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    87
            server.close();
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    88
        }
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    89
    }
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
    int getPort() {
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    92
        return server.getLocalPort();
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    93
    }
ae846c9286b0 8157667: sun/security/x509/URICertStore/ExtensionsWithLDAP.java has to be updated due to JDK-8134577
mli
parents:
diff changeset
    94
}