Skip to content

Commit 3f0fca0

Browse files
committed
[fix] HTTP2 can't work on SSL_FORWARDER connection
1 parent 6f529d0 commit 3f0fca0

File tree

2 files changed

+100
-7
lines changed

2 files changed

+100
-7
lines changed

src/main/java/core/packetproxy/ProxyFactory.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,9 @@ public static Proxy create(ListenPort listen_info) throws Exception {
3232
proxy = new ProxyHttp(listen_socket, listen_info);
3333

3434
} else if (listen_info.getType() == ListenPort.TYPE.SSL_FORWARDER) {
35-
String commonName = listen_info.getServer().getIp();
36-
if (listen_info.getCA().isPresent()) {
37-
CA ca = listen_info.getCA().get();
38-
ServerSocket listen_socket = Https.createServerSSLSocket(listen_info.getPort(), commonName, ca);
39-
proxy = new ProxyForward(listen_socket, listen_info);
40-
}
35+
PacketProxyUtility.getInstance().packetProxyLog("type is SSL_FORWARDER");
36+
ServerSocket listen_socket = new ServerSocket(listen_info.getPort());
37+
proxy = new ProxySSLForward(listen_socket, listen_info);
4138

4239
} else if (listen_info.getType() == ListenPort.TYPE.HTTP_TRANSPARENT_PROXY) {
4340
PacketProxyUtility.getInstance().packetProxyLog("type is HTTP_TRANSPARENT_PROXY");
@@ -52,7 +49,7 @@ public static Proxy create(ListenPort listen_info) throws Exception {
5249
} else if (listen_info.getType() == ListenPort.TYPE.UDP_FORWARDER) {
5350
proxy = new ProxyUDPForward(listen_info);
5451

55-
} else {
52+
} else { /* FORWARDER */
5653
ServerSocket listen_socket = new ServerSocket(listen_info.getPort());
5754
listen_socket.setReuseAddress(true);
5855
proxy = new ProxyForward(listen_socket, listen_info);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)