How to setup proxy host and port to IBM MQConnectionFactory - java

I need to specify a custom proxy host and port for an MQConnectionFactoy and I saw that the set proxy host and port are not supported anymore. I am using exactly this 8.0 version and I don't want to downgrade.
https://www.ibm.com/support/knowledgecenter/en/SSFKSJ_8.0.0/com.ibm.mq.javadoc.doc/WMQJMSClasses/com/ibm/mq/jms/MQConnectionFactory.html#setProxyHostName(java.lang.String)
Does anyone know how I can achieve this? Is there a JmsConnectionFactoryProxy which can wrap the MQConnectionFactory?
Thanks in advance.
Regards,
C

I've been able to achieve what I wanted with ProxySelector.
I've added a custom rule for the case when scheme is "socket", hostname = "MQ_HOSTNAME" and port = "MQ_PORT" to return a custom created proxy.
SocketAddress socketAddress = new InetSocketAddress(proxyHost, proxyPort);
Proxy proxy = new Proxy(Type.SOCKS, socketAddress);

Related

Trying to connect to an SSH server with on-premise proxy type?

I have a new project where I should connect to an SSH server, with a proxy (which is on-premise).
The problem is, that if I don't use proxy, I get an error saying "UnknownHost".
But when I use proxy, it says "JSchException ProxySOCKS5: com.jcraft.jsch.JSchException: fail in SOCKS5 proxy".
I'm pretty new to sockets, proxies and all these kinds of things, so every advice is appreciated.
JSch jsch = new JSch();
jsch.setKnownHosts("known_hosts");
com.jcraft.jsch.Session session = null;
com.jcraft.jsch.ProxySOCKS5 proxy = new ProxySOCKS5("localhost", 20004);
proxy.setUserPasswd(userName, password);
URL url = new URL("http", "<remoteUrl>", 22, filePath, null);
session = jsch.getSession(userName, hostName, 22);
session.setPassword(password);
session.setProxy(proxy );
session.connect(10000);
I did try a different direction, where I don't use jsch, only java.net. That code:
SocketAddress addr = new InetSocketAddress("localhost", 20004);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr);
final String encodedSubaccount = new String(Base64.encodeBase64(subaccount.getBytes()));
final String encodedLocationId = new String(Base64.encodeBase64(locationId.getBytes()));
char[] pwdHelp = [];
Authenticator.setDefault(new Authenticator() {
#Override
protected java.net.PasswordAuthentication getPasswordAuthentication() {
return new java.net.PasswordAuthentication("1." + encodedSubaccount + "." + encodedLocationId , pwdHelp);
}
});
URL url = new URL("http", "<remoteUrl>", 22, filePath, null);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
With this approach, there is no error, but when I try to getResponseMessage() or code, then it returns only -1 or null.
Can somebody help me out?
Thanks in advance
I'm not a java developer so I can help you only the infrastructure part of the problem.
UnknownHost: you cannot connect directly that's why you have to use proxy. UnknownHost means java/your machine cannot resolve DNS name to IP address, maybe that DNS name is an inside/private one.
As I see In your java code You try to connect HTTP protocol instead of SSH protocol.
What is the exact task?
Somebody was provided You an on-premise SocksProxy IP and port, and you have to connect via to an inside SSH server?
OR
You have to connect with SSH protocol to the on-premise server to create a local SocksProxy, and you have to connect to an inside server via local SocksProxy?
In the 2. case you can test the connection with ssh command and a web browser:
SSH to on-premise: ssh -D 1080 on-premise_remote_hosntame_or_IP
Setup socksproxy in a webbrowser: Socks proxy ip: 127.0.0.1, port: 1080
In the web browser try to connect to an inside webserver

Why does Apache Commons VFS consider Http Proxy and Socks5 Proxy but simply ignore Socks4 Proxy?

I am working on a application that connects to an SFTP server and downloads files using Apache Commons VFS, it works just fine, with the exception that the system needs to allow the user to specify a proxy, as needed.
Now, I know Apache Commons VFS is built on top of Jsch and I know Jsch contains the classes: com.jcraft.jsch.ProxyHTTP, com.jcraft.jsch.ProxySOCKS4 and com.jcraft.jsch.ProxySOCKS5.
The code below is an extract of VFS class org.apache.commons.vfs2.provider.sftp.SftpClientFactory:
public static Session createConnection(
...
final SftpFileSystemConfigBuilder.ProxyType proxyType = builder.getProxyType(fileSystemOptions);
...
final String proxyUser = builder.getProxyUser(fileSystemOptions);
final String proxyPassword = builder.getProxyPassword(fileSystemOptions);
Proxy proxy = null;
if (SftpFileSystemConfigBuilder.PROXY_HTTP.equals(proxyType)) {
proxy = createProxyHTTP(proxyHost, proxyPort);
((ProxyHTTP)proxy).setUserPasswd(proxyUser, proxyPassword);
} else if (SftpFileSystemConfigBuilder.PROXY_SOCKS5.equals(proxyType)) {
proxy = createProxySOCKS5(proxyHost, proxyPort);
((ProxySOCKS5)proxy).setUserPasswd(proxyUser, proxyPassword);
} else if (SftpFileSystemConfigBuilder.PROXY_STREAM.equals(proxyType)) {
proxy = createStreamProxy(proxyHost, proxyPort, fileSystemOptions, builder);
}
...
As you can you see, there's no "if" statement to instantiate ProxySOCKS4!
I have duplicated the SftpClientFactory class, set my version to load before the original class on the classpath and changed the code as follow:
public static Session createConnection(
...
final SftpFileSystemConfigBuilder.ProxyType proxyType = builder.getProxyType(fileSystemOptions);
...
final String proxyUser = builder.getProxyUser(fileSystemOptions);
final String proxyPassword = builder.getProxyPassword(fileSystemOptions);
Proxy proxy = null;
if (SftpFileSystemConfigBuilder.PROXY_HTTP.equals(proxyType)) {
proxy = createProxyHTTP(proxyHost, proxyPort);
((ProxyHTTP)proxy).setUserPasswd(proxyUser, proxyPassword);
/// change start (I also created the PROXY_SOCKS4 constant)
} else if (SftpFileSystemConfigBuilder.PROXY_SOCKS4.equals(proxyType)) {
proxy = createProxySOCKS4(proxyHost, proxyPort);
((ProxySOCKS4)proxy).setUserPasswd(proxyUser, proxyPassword);
/// change end
} else if (SftpFileSystemConfigBuilder.PROXY_SOCKS5.equals(proxyType)) {
proxy = createProxySOCKS5(proxyHost, proxyPort);
((ProxySOCKS5)proxy).setUserPasswd(proxyUser, proxyPassword);
} else if (SftpFileSystemConfigBuilder.PROXY_STREAM.equals(proxyType)) {
proxy = createStreamProxy(proxyHost, proxyPort, fileSystemOptions, builder);
}
...
.. and guess what, when I set my application to use a Socks 4 Proxy it works alright with the change above. It is important to say that setting the application to work with Socks 5 does not work if the proxy server is a Socks 4 type, and that's true not only for my application with VFS, but also any other client I tested, like Fillezila or WinSCP.
So, the main question is:
Why does VFS predicts the usage of ProxyHTTP, ProxySOCKS5 but completely ignores the JSch ProxySOCKS4 class? Am I missing some SFTP or Proxy concept here or should I consider VFS bugged? That's the first time I work with VFS.
Please consider the question in bold as the main question not to make it too broad.
I wasn't able to get or find a better answer in time, so what I did to solve my problem was exactly what I described in the question.
I duplicated the classes SftpClientFactory e SftpFileSystemConfigBuilder, made the necessary adjustments and used them instead of the original classes, it's ugly and now I am stuck with a specific VFS version, I know, but the problem was solved.
Lesson for next time: use Jsch instead of VFS.
I'll leave the question open though, in case someone else have a proper solution or answer.

Connect from custom ephemeral RMI port in the create registry action

I'm developing a standalone application that uses Java 6 with RMI in Windows 7.
What I've read is that createRegistry action uses by default the port 1099 and it's perfect. Following some tutorials I have this code as my example:
LocateRegistry.createRegistry(1099, new SocketFactory(), new SocketFactory());
Random r = new Random();
int nextPort = r.nextInt(100 + 1) + 50000;
PowerServiceImpl powerServiceImpl = new PowerServiceImpl(nextPort);
Naming.rebind("rmi://10.1.1.100:1099/PowerService", powerServiceImpl);
When I create the registry, I assign the default and I use it successfuly in the 1099 port as it's seen in this image:
In the image, it is shown the 1099 as the port opened in the "server" and a random port (ephemeral) that establishes connection with it.
What I want to achieve is to set a custom port as the ephemeral port (instead of 53559 in image it could be 50005 for example) to establish connection with the server (1099).
Is there a way to implement this?
Am I missing something?
Thanks and regards

How to implement a proxy server?

I'm looking to have my application connect to the internet through a proxy server (in order to avoid captcha). The code I am currently using is this:
Properties props = System.getProperties();
props.put("http.proxyPort", proxyPort); //proxy port
props.put("http.proxyHost", proxyHost); //proxy host
props.put("http.proxySet", "true");
This code has been unsuccesful, however. Any suggestions?
You can try the following:
SocketAddress sa = new InetSocketAddress(proxy_host_name, proxy_port_address);
Proxy proxy = new Proxy(Proxy.Type.xxx, sa);
URLConnection con = new URL(url).openConnection(proxy);
You are probably using a kind of "User Friendly Website Proxy", like http://newipnow.com or www.proxyultra.com. But you need to use a real SOCKS proxy server.
A free server, that I found, working, in a list of public Proxies:
System.setProperty("http.proxyHost", "187.115.172.82");
System.setProperty("http.proxyPort", "8181");
There is no need to set the http.proxySet property.
Pick a server from the nice list here: Hide My Ass: Proxy List
Put the parameters on the command line or use setProperty.
java -Dhttp.proxyHost=proxy.host -Dhttp.proxyPort=3128 MainClass

Jetty SslConnector's deprecated methods

SslConnector.java interface has been changed in the newest Jetty 7.3.1.v20110307.
Almost all off the methods have been marked as deprecated without mentioning the replacement interface or methods to use.
I've checked the jetty-users and jetty-dev mailing lists for the information with no luck.
Is there anybody out there who knows how should be the code changed for the future?
Thanks in advance!
Okay, digging out from the subversion changelog for the corresponding commits (crazy) it came out that SslContextFactory should be used.
Example:
final SslContextFactory sslContextFactory = new SslContextFactory(sKeyStore);
sslContextFactory.setKeyStorePassword(sPassword);
final SslSocketConnector conn = new SslSocketConnector(sslContextFactory);
conn.setReuseAddress(true);
// ...
Building on your own answer:
Server server = new Server();
// Encrypt the connection using a valid certificate/keystore
SslContextFactory sslContextFactory = new SslContextFactory("path/keystore.jks");
sslContextFactory.setKeyStorePassword("password");
// Create a new SocketConnector at port 443, which is the default port for
// HTTPS web pages (no port number needs to be specified in the browser).
SslSocketConnector sslConnector = new SslSocketConnector(sslContextFactory);
sslConnector.setPort(443);
// Add the SocketConnector to the server
server.setConnectors(new Connector[] {sslConnector});

Categories

Resources