How to implement a proxy server? - java

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

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

How to setup proxy host and port to IBM MQConnectionFactory

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);

Wrong host sending SMTP

I'm trying to send a mail using javax.mail. This is my code:
Properties props = new Properties();
props.setProperty("mail.smtp.host", host);
props.setProperty("mail.smtp.port", port);
props.setProperty("mail.user", user);
props.setProperty("mail.password", password);
Session session = Session.getDefaultInstance(props);
But I get this error:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.wrong.server.com, port: 25;
The funny thing is that "smtp.wrong.server.com" isn't the value that I'm passing as host.
It is like Session.getDefaultInstance(props) is returning an already created session with the wrong host name.
There isn't any other place inside my EAR where javax.mail is used (at least not in my code, maybe inside a third party dependecy?).
This behaviour only happens, of course, in PRO environment. The same EAR deployed in DEV and TEST env works fine.
Any help would be appreciated
The problem was with Session.getDefaultInstance. I should use Session.getInstance
From javadoc:
getDefaultInstance
(...)the default session is potentially available to all code executing in the same Java virtual machine(...)Subsequent calls return the Session object that was created by the first call, and ignore the passed Properties object. Use the getInstance method to get a new Session object every time the method is called.
It seams your are not using the correct key for your proerties. see Javadoc for javax.mail.Session
It is expected that the client supplies values for the properties
listed in Appendix A of the JavaMail spec (particularly
mail.store.protocol, mail.transport.protocol, mail.host, mail.user,
and mail.from) as the defaults are unlikely to work in all cases.

How to use Http,Socksproxies in java?

my java app needs to connect to the internet , I am behind a proxy server. i use the fallow
functions to set proxy but it doesn't work?
System.getProperties().put("http.proxySet", "true");
System.getProperties().put("http.socksProxyHost", value);
System.getProperties().put("http.socksProxyPort", value);
thanks
Check out this question, especially the answer which uses Proxy class.
Check the system parameter names. There are two ways you can do this:
Set the system parameter from the command line itself
java -Dhttp.proxyHost=proxyhostURL
-Dhttp.proxyPort=proxyPortNumber
-Dhttp.proxyUser=userName
-Dhttp.proxyPassword=password YourProgram
OR Set these parameter in the code as follows
System.getProperties().put("http.proxyHost", "proxyURL");
System.getProperties().put("http.proxyPort", "proxyPort");
System.getProperties().put("http.proxyUser", "userName");
System.getProperties().put("http.proxyPassword", "password");

Open browser with proxy setting set from Java

I need to open a browser from Java code. I understand this can be done as follows :
java.awt.Desktop.getDesktop().browse(java.net.URI.create("http://google.com"));
But i need the browser to use certain proxy settings as well. (i.e. when the browser opens, its proxy settings must be set to certain values.) I tried using the follwoing code but it doesnt work :
public static void main(String asf[]){
System.setProperty("java.net.useSystemProxies", "true");
System.setProperty("http.proxyHost", "127.0.0.1");
System.setProperty("http.proxyPort", "8080");
try {
java.awt.Desktop.getDesktop().browse(java.net.URI.create("http://google.com"));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("done");
}
Setting the proxy from command line using
java -Dhttp.proxyHost=webcache.example.com -Dhttp.proxyPort=8080
is not an option for me. How do i accomplish this?
Your code is largely correct which deals with setting the proxy, but in case it is not working there is another way to set the proxy via Java code and that is via the proxy class.
SocketAddress addr = new InetSocketAddress("socks.example.com", 1080);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr);
Socket socket = new Socket(proxy);
InetSocketAddress dest = new InetSocketAddress("server.example.org", 1234);
socket.connect(dest);
Here the socket will try to connect to its destination address (server.example.org:1234) through the specified SOCKS proxy.
For more detail you can go through the Standard Java Documentation for Proxies
Your solution for opening a browser can be improved by adding a check
if(Desktop.isDesktopSupported())
{
Desktop.getDesktop().browse(new URI("http://www.google.com"));
}
this is in addition to your solution .... maybe you can call it an alternate way
try {
Process p=Runtime.getRuntime().exec("cmd /c start http://www.google.com");
}
catch(IOException e1) {
System.out.println(e1);
}
The Google Chromes proxy switches can be useful here. We can just make a shortcut for the chrome browser whose target contains the switch --proxy-server=127.0.0.1:8080 . Now this shortcut can be opened from java code using the Runtime class' exec method. The arguments to exec will be "cmd /c start /d \"d:\" chrome.lnk" where d: is the path of my shortcut. A detailed description of this technique can be found here http://sleepingthreads.blogspot.in/2013/07/open-browser-with-proxy-settings-set.html
Note that Google states that the use of switches is not recommended. So use this as a temporary solution only.

Categories

Resources