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
46 public class ClientSessionHandler extends AbstractProxyIoHandler {
47 private final static Logger logger = LoggerFactory
48 .getLogger(ClientSessionHandler.class);
49
50
51
52
53 private File file;
54
55
56
57
58 private FileChannel wChannel;
59
60
61
62
63 private String cmd;
64
65 public ClientSessionHandler(String cmd) {
66 this.cmd = cmd;
67 }
68
69
70
71
72 @Override
73 public void sessionCreated(IoSession session) throws Exception {
74 logger.debug("CLIENT - Session created: " + session);
75 }
76
77
78
79
80
81 @Override
82 public void proxySessionOpened(IoSession session) throws Exception {
83 logger.debug("CLIENT - Session opened: " + session);
84 ProxyIoSession proxyIoSession = (ProxyIoSession) session
85 .getAttribute(ProxyIoSession.PROXY_SESSION);
86 if (proxyIoSession != null) {
87 ProxyRequest req = proxyIoSession.getRequest();
88
89 byte[] c = null;
90 if (req instanceof SocksProxyRequest && cmd != null) {
91 logger.debug("Sending request to a SOCKS SESSION ...");
92 c = cmd.getBytes(proxyIoSession.getCharsetName());
93 } else if (req instanceof HttpProxyRequest
94 && ((HttpProxyRequest) req).getHttpVerb() == HttpProxyConstants.CONNECT) {
95 logger.debug("Sending request to a HTTP CONNECTED SESSION ...");
96 c = (((HttpProxyRequest) req).toHttpString())
97 .getBytes(proxyIoSession.getCharsetName());
98 }
99
100 if (c != null) {
101 IoBuffer buf = IoBuffer.allocate(c.length);
102 buf.put(c);
103 buf.flip();
104 session.write(buf);
105 }
106 }
107 }
108
109
110
111
112 @Override
113 public void messageReceived(IoSession session, Object message) {
114 logger.debug("CLIENT - Message received: " + session);
115 IoBuffer buf = (IoBuffer) message;
116
117 try {
118 if (file == null) {
119 file = File.createTempFile("http", ".html");
120 logger.info("Writing request result to "
121 + file.getAbsolutePath());
122 wChannel = new FileOutputStream(file, false).getChannel();
123 }
124
125
126
127 wChannel.write(buf.buf());
128 } catch (IOException e) {
129
130 }
131 }
132
133
134
135
136 @Override
137 public void sessionClosed(IoSession session) throws Exception {
138 logger.debug("CLIENT - Session closed - closing result file if open.");
139
140 if (wChannel != null) {
141 wChannel.close();
142 }
143 }
144
145
146
147
148 @Override
149 public void sessionIdle(IoSession session, IdleStatus status)
150 throws Exception {
151 if (session.isClosing()) {
152 return;
153 }
154
155 logger.debug("CLIENT - Session idle");
156 }
157
158
159
160
161 @Override
162 public void exceptionCaught(IoSession session, Throwable cause) {
163 logger.debug("CLIENT - Exception caught");
164
165 session.close(true);
166 }
167 }