Skip to content

Instantly share code, notes, and snippets.

@cpinto
Created July 28, 2009 12:04
Show Gist options
  • Save cpinto/157188 to your computer and use it in GitHub Desktop.
Save cpinto/157188 to your computer and use it in GitHub Desktop.
I use this program to wrap putty, which doesn't support NTLM auth, setting the proxy type to telnet and create a SOCKS proxy to a remote SSH server.
package ntlmptunnel;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.NTCredentials;
import org.apache.commons.httpclient.ProxyClient;
import org.apache.commons.httpclient.auth.AuthScope;
/*
I use this program to wrap putty, which doesn't support NTLM auth, setting the proxy type to telnet and create a SOCKS proxy to a remote SSH server.
*/
public class Main
{
private static void pipe(final InputStream is, final OutputStream os)
{
Thread t = new Thread(new Runnable()
{
public void run()
{
try
{
int read = -1;
byte[] bytes = new byte[8192];
while ((read = is.read(bytes)) != -1)
os.write(bytes, 0, read);
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
});
t.setDaemon(true);
t.start();
}
public static void main(String[] args)
{
try
{
ServerSocket ss = new ServerSocket();
ss.bind(new InetSocketAddress("localhost",localport));
System.out.println("listening");
Socket s = ss.accept();
System.out.println("got connection");
ProxyClient pc = new ProxyClient();
HostConfiguration config = pc.getHostConfiguration();
config.setHost("remote address", remote port);
config.setProxy("proxy address", proxy port);
pc.getState().setProxyCredentials(AuthScope.ANY,
new NTCredentials("username", "password", "", ""));
ProxyClient.ConnectResponse r = pc.connect();
System.out.println("connected to proxy...");
OutputStream proxyOutput = r.getSocket().getOutputStream();
InputStream proxyInput = r.getSocket().getInputStream();
OutputStream outputToPutty = s.getOutputStream();
InputStream inputFromPutty = s.getInputStream();
pipe(inputFromPutty,proxyOutput);
pipe(proxyInput,outputToPutty);
System.out.println("Running...");
System.in.read();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment