|
21 | 21 |
|
22 | 22 | import java.net.InetSocketAddress;
|
23 | 23 | import java.net.SocketAddress;
|
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | +import java.util.HashMap; |
| 27 | + |
24 | 28 |
|
25 | 29 | import org.apache.mina.core.service.IoAcceptor;
|
26 | 30 | import org.apache.mina.core.service.IoConnector;
|
| 31 | +import org.apache.mina.transport.socket.SocketConnector; |
27 | 32 | import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
|
28 | 33 | import org.apache.mina.transport.socket.nio.NioSocketConnector;
|
29 | 34 | import org.apache.mina.transport.vmpipe.VmPipeAcceptor;
|
30 | 35 | import org.apache.mina.transport.vmpipe.VmPipeAddress;
|
31 | 36 | import org.apache.mina.transport.vmpipe.VmPipeConnector;
|
| 37 | +import org.apache.mina.proxy.ProxyConnector; |
| 38 | +import org.apache.mina.proxy.handlers.ProxyRequest; |
| 39 | +import org.apache.mina.proxy.handlers.http.HttpAuthenticationMethods; |
| 40 | +import org.apache.mina.proxy.handlers.http.HttpProxyConstants; |
| 41 | +import org.apache.mina.proxy.handlers.http.HttpProxyRequest; |
| 42 | +import org.apache.mina.proxy.handlers.socks.SocksProxyConstants; |
| 43 | +import org.apache.mina.proxy.handlers.socks.SocksProxyRequest; |
| 44 | +import org.apache.mina.proxy.session.ProxyIoSession; |
| 45 | + |
32 | 46 |
|
33 | 47 | import quickfix.ConfigError;
|
34 | 48 | import quickfix.RuntimeError;
|
@@ -58,7 +72,7 @@ public static String getTypeString(int type) {
|
58 | 72 |
|
59 | 73 | public static SocketAddress createSocketAddress(int transportType, String host,
|
60 | 74 | int port) throws ConfigError {
|
61 |
| - if (transportType == SOCKET) { |
| 75 | + if (transportType == SOCKET || transportType == PROXY) { |
62 | 76 | return host != null ? new InetSocketAddress(host, port) : new InetSocketAddress(port);
|
63 | 77 | } else if (transportType == VM_PIPE) {
|
64 | 78 | return new VmPipeAddress(port);
|
@@ -102,6 +116,109 @@ public static IoAcceptor createIoAcceptor(int transportType) {
|
102 | 116 | }
|
103 | 117 | }
|
104 | 118 |
|
| 119 | + public static ProxyConnector createIoProxyConnector(SocketConnector socketConnector, |
| 120 | + InetSocketAddress address, |
| 121 | + InetSocketAddress proxyAddress, |
| 122 | + String proxyType, |
| 123 | + String proxyVersion, |
| 124 | + String proxyUser, |
| 125 | + String proxyPassword, |
| 126 | + String proxyDomain, |
| 127 | + String proxyWorkstation ) throws ConfigError { |
| 128 | + |
| 129 | + // Create proxy connector. |
| 130 | + ProxyRequest req; |
| 131 | + |
| 132 | + ProxyConnector connector = new ProxyConnector(socketConnector); |
| 133 | + connector.setConnectTimeoutMillis(5000); |
| 134 | + |
| 135 | + if (proxyType.equalsIgnoreCase("http")) { |
| 136 | + req = createHttpProxyRequest(address, proxyVersion, proxyUser, proxyPassword, proxyDomain, proxyWorkstation); |
| 137 | + } else if (proxyType.equalsIgnoreCase("socks")) { |
| 138 | + req = createSocksProxyRequest(address, proxyVersion, proxyUser, proxyPassword); |
| 139 | + } else { |
| 140 | + throw new ConfigError("Proxy type must be http or socks"); |
| 141 | + } |
| 142 | + |
| 143 | + ProxyIoSession proxyIoSession = new ProxyIoSession(proxyAddress, req); |
| 144 | + |
| 145 | + List<HttpAuthenticationMethods> l = new ArrayList<>(); |
| 146 | + l.add(HttpAuthenticationMethods.NO_AUTH); |
| 147 | + l.add(HttpAuthenticationMethods.DIGEST); |
| 148 | + l.add(HttpAuthenticationMethods.BASIC); |
| 149 | + |
| 150 | + proxyIoSession.setPreferedOrder(l); |
| 151 | + connector.setProxyIoSession(proxyIoSession); |
| 152 | + |
| 153 | + return connector; |
| 154 | + } |
| 155 | + |
| 156 | + |
| 157 | + private static ProxyRequest createHttpProxyRequest(InetSocketAddress address, |
| 158 | + String proxyVersion, |
| 159 | + String proxyUser, |
| 160 | + String proxyPassword, |
| 161 | + String proxyDomain, |
| 162 | + String proxyWorkstation) { |
| 163 | + String uri = "http://" + address.getAddress().getHostAddress() + ":" + address.getPort(); |
| 164 | + HashMap<String, String> props = new HashMap<>(); |
| 165 | + props.put(HttpProxyConstants.USER_PROPERTY, proxyUser); |
| 166 | + props.put(HttpProxyConstants.PWD_PROPERTY, proxyPassword); |
| 167 | + if (proxyDomain != null && proxyWorkstation != null) { |
| 168 | + props.put(HttpProxyConstants.DOMAIN_PROPERTY, proxyDomain); |
| 169 | + props.put(HttpProxyConstants.WORKSTATION_PROPERTY, proxyWorkstation); |
| 170 | + } |
| 171 | + |
| 172 | + HttpProxyRequest req = new HttpProxyRequest(uri); |
| 173 | + req.setProperties(props); |
| 174 | + if (proxyVersion != null && proxyVersion.equalsIgnoreCase("1.1")) { |
| 175 | + req.setHttpVersion(HttpProxyConstants.HTTP_1_1); |
| 176 | + } else { |
| 177 | + req.setHttpVersion(HttpProxyConstants.HTTP_1_0); |
| 178 | + } |
| 179 | + |
| 180 | + return req; |
| 181 | + } |
| 182 | + |
| 183 | + |
| 184 | + private static ProxyRequest createSocksProxyRequest(InetSocketAddress address, |
| 185 | + String proxyVersion, |
| 186 | + String proxyUser, |
| 187 | + String proxyPassword) throws ConfigError { |
| 188 | + SocksProxyRequest req; |
| 189 | + if (proxyVersion.equalsIgnoreCase("4")) { |
| 190 | + req = new SocksProxyRequest( |
| 191 | + SocksProxyConstants.SOCKS_VERSION_4, |
| 192 | + SocksProxyConstants.ESTABLISH_TCPIP_STREAM, |
| 193 | + address, |
| 194 | + proxyUser); |
| 195 | + |
| 196 | + } else if (proxyVersion.equalsIgnoreCase("4a")) { |
| 197 | + req = new SocksProxyRequest( |
| 198 | + SocksProxyConstants.ESTABLISH_TCPIP_STREAM, |
| 199 | + address.getAddress().getHostAddress(), |
| 200 | + address.getPort(), |
| 201 | + proxyUser); |
| 202 | + |
| 203 | + } else if (proxyVersion.equalsIgnoreCase("5")) { |
| 204 | + req = new SocksProxyRequest( |
| 205 | + SocksProxyConstants.SOCKS_VERSION_5, |
| 206 | + SocksProxyConstants.ESTABLISH_TCPIP_STREAM, |
| 207 | + address, |
| 208 | + proxyUser); |
| 209 | + |
| 210 | + } else { |
| 211 | + throw new ConfigError("SOCKS ProxyType must be 4,4a or 5"); |
| 212 | + } |
| 213 | + |
| 214 | + if (proxyPassword != null) { |
| 215 | + req.setPassword(proxyPassword); |
| 216 | + } |
| 217 | + |
| 218 | + return req; |
| 219 | + } |
| 220 | + |
| 221 | + |
105 | 222 | public static IoConnector createIoConnector(SocketAddress address) throws ConfigError {
|
106 | 223 | if (address instanceof InetSocketAddress) {
|
107 | 224 | return new NioSocketConnector();
|
|
0 commit comments