1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.example.proxy;
21
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.nio.channels.FileChannel;
26
27 import org.apache.mina.core.buffer.IoBuffer;
28 import org.apache.mina.core.session.IdleStatus;
29 import org.apache.mina.core.session.IoSession;
30 import org.apache.mina.proxy.AbstractProxyIoHandler;
31 import org.apache.mina.proxy.handlers.ProxyRequest;
32 import org.apache.mina.proxy.handlers.http.HttpProxyConstants;
33 import org.apache.mina.proxy.handlers.http.HttpProxyRequest;
34 import org.apache.mina.proxy.handlers.socks.SocksProxyRequest;
35 import org.apache.mina.proxy.session.ProxyIoSession;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
40
41
42
43
44
45 public class ClientSessionHandler extends AbstractProxyIoHandler {
46 private final static Logger logger = LoggerFactory
47 .getLogger(ClientSessionHandler.class);
48
49
50
51
52 private File file;
53
54
55
56
57 private FileChannel wChannel;
58
59
60
61
62 private String cmd;
63
64 public ClientSessionHandler(String cmd) {
65 this.cmd = cmd;
66 }
67
68
69
70
71 @Override
72 public void sessionCreated(IoSession session) throws Exception {
73 logger.debug("CLIENT - Session created: " + session);
74 }
75
76
77
78
79
80 @Override
81 public void proxySessionOpened(IoSession session) throws Exception {
82 logger.debug("CLIENT - Session opened: " + session);
83 ProxyIoSession proxyIoSession = (ProxyIoSession) session
84 .getAttribute(ProxyIoSession.PROXY_SESSION);
85 if (proxyIoSession != null) {
86 ProxyRequest req = proxyIoSession.getRequest();
87
88 byte[] c = null;
89 if (req instanceof SocksProxyRequest && cmd != null) {
90 logger.debug("Sending request to a SOCKS SESSION ...");
91 c = cmd.getBytes(proxyIoSession.getCharsetName());
92 } else if (req instanceof HttpProxyRequest
93 && ((HttpProxyRequest) req).getHttpVerb() == HttpProxyConstants.CONNECT) {
94 logger.debug("Sending request to a HTTP CONNECTED SESSION ...");
95 c = (((HttpProxyRequest) req).toHttpString())
96 .getBytes(proxyIoSession.getCharsetName());
97 }
98
99 if (c != null) {
100 IoBuffer buf = IoBuffer.allocate(c.length);
101 buf.put(c);
102 buf.flip();
103 session.write(buf);
104 }
105 }
106 }
107
108
109
110
111 @Override
112 public void messageReceived(IoSession session, Object message) {
113 logger.debug("CLIENT - Message received: " + session);
114 IoBuffer buf = (IoBuffer) message;
115
116 try {
117 if (file == null) {
118 file = File.createTempFile("http", ".html");
119 logger.info("Writing request result to "
120 + file.getAbsolutePath());
121 wChannel = new FileOutputStream(file, false).getChannel();
122 }
123
124
125
126 wChannel.write(buf.buf());
127 } catch (IOException e) {
128
129 }
130 }
131
132
133
134
135 @Override
136 public void sessionClosed(IoSession session) throws Exception {
137 logger.debug("CLIENT - Session closed - closing result file if open.");
138
139 if (wChannel != null) {
140 wChannel.close();
141 }
142 }
143
144
145
146
147 @Override
148 public void sessionIdle(IoSession session, IdleStatus status)
149 throws Exception {
150 if (session.isClosing()) {
151 return;
152 }
153
154 logger.debug("CLIENT - Session idle");
155 }
156
157
158
159
160 @Override
161 public void exceptionCaught(IoSession session, Throwable cause) {
162 logger.debug("CLIENT - Exception caught");
163
164 session.closeNow();
165 }
166 }