|
| 1 | +/* |
| 2 | + * Copyright 2019 DeNA Co., Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package packetproxy; |
| 17 | + |
| 18 | +import java.net.InetSocketAddress; |
| 19 | +import java.net.ServerSocket; |
| 20 | +import java.net.Socket; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import packetproxy.common.EndpointFactory; |
| 25 | +import packetproxy.common.SSLSocketEndpoint; |
| 26 | +import packetproxy.encode.EncodeHTTPBase; |
| 27 | +import packetproxy.encode.Encoder; |
| 28 | +import packetproxy.model.ListenPort; |
| 29 | +import packetproxy.model.Server; |
| 30 | +import packetproxy.util.PacketProxyUtility; |
| 31 | + |
| 32 | +public class ProxySSLForward extends Proxy |
| 33 | +{ |
| 34 | + private ListenPort listen_info; |
| 35 | + private ServerSocket listen_socket; |
| 36 | + |
| 37 | + public ProxySSLForward(ServerSocket listen_socket, ListenPort listen_info) { |
| 38 | + this.listen_socket = listen_socket; |
| 39 | + this.listen_info = listen_info; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void run() { |
| 44 | + List<Socket> clients = new ArrayList<Socket>(); |
| 45 | + while (!listen_socket.isClosed()) { |
| 46 | + try { |
| 47 | + Socket client = listen_socket.accept(); |
| 48 | + clients.add(client); |
| 49 | + PacketProxyUtility.getInstance().packetProxyLog("[SSLForward] accept"); |
| 50 | + checkSSLForward(client); |
| 51 | + } catch (Exception e) { |
| 52 | + e.printStackTrace(); |
| 53 | + } |
| 54 | + } |
| 55 | + for(Socket sc : clients) { |
| 56 | + try { |
| 57 | + sc.close(); |
| 58 | + } catch (Exception e) { |
| 59 | + e.printStackTrace(); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private void checkSSLForward(Socket client) throws Exception { |
| 65 | + InetSocketAddress serverAddr = listen_info.getServer().getAddress(); |
| 66 | + SSLSocketEndpoint[] eps = EndpointFactory.createBothSideSSLEndpoints(client, null, serverAddr, null, listen_info.getServer().getIp(), listen_info.getCA().get()); |
| 67 | + createConnection(eps[0], eps[1], listen_info.getServer()); |
| 68 | + } |
| 69 | + |
| 70 | + public void createConnection(SSLSocketEndpoint client_e, SSLSocketEndpoint server_e, Server server) throws Exception { |
| 71 | + DuplexAsync duplex = null; |
| 72 | + String alpn = client_e.getApplicationProtocol(); |
| 73 | + if (server == null) { |
| 74 | + if (alpn.equals("h2") || alpn.equals("http/1.1") || alpn.equals("http/1.0")) { |
| 75 | + duplex = DuplexFactory.createDuplexAsync(client_e, server_e, "HTTP", alpn); |
| 76 | + } else { |
| 77 | + duplex = DuplexFactory.createDuplexAsync(client_e, server_e, "Sample", alpn); |
| 78 | + } |
| 79 | + } else { |
| 80 | + if (alpn == null || alpn.length() == 0) { |
| 81 | + Encoder encoder = EncoderManager.getInstance().createInstance(server.getEncoder(), ""); |
| 82 | + if (encoder instanceof EncodeHTTPBase) { |
| 83 | + /* The client does not support ALPN. It seems to be an old HTTP client */ |
| 84 | + alpn = "http/1.1"; |
| 85 | + } |
| 86 | + } |
| 87 | + duplex = DuplexFactory.createDuplexAsync(client_e, server_e, server.getEncoder(), alpn); |
| 88 | + } |
| 89 | + duplex.start(); |
| 90 | + DuplexManager.getInstance().registerDuplex(duplex); |
| 91 | + } |
| 92 | + |
| 93 | + public void close() throws Exception { |
| 94 | + listen_socket.close(); |
| 95 | + } |
| 96 | +} |
0 commit comments