자바 소켓에서 Socket 클래스를 이용할 때, SOCKS 프록시를 이용할 수 있습니다.
문제는 디폴트 ProxySelector 가 아닌 커스터마이징된 ProxySelector 를 이용할 때,
Fail-over 가 되지 않는 버그를 발견해서 리포팅 했더니 아래와 같이 메일이 왔네요. ㅋ
-------- 아래 메일 ---------
Subject: Your Report (Bug ID: 7141231) - SOCKS proxy failover using ProxySelector does not work.
Dear Java Developer,
Thank you for reporting this issue.
We have determined that this report is a new bug and entered the bug into our internal bug tracking system under Bug Id: 7141231.
You can monitor this bug on the Java Bug Database at http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7141231.
It may take a day or two before your bug shows up in this external database. If you are a member of the Sun Developer Network (SDN), there are two additional options once the bug is visible.
1. Voting for the bug
Click http://bugs.sun.com/bugdatabase/addVote.do?bug_id=7141231.
2. Adding the report to your Bug Watch list.
You will receive an email notification when this bug is updated.
Click http://bugs.sun.com/bugdatabase/addBugWatch.do?bug_id=7141231.
The Sun Developer Network (http://developers.sun.com) is a free service that Sun offers. To join, visit https://softwarereg.sun.com/registration/developer/en_US/new_user.
Regards,
Java Developer Support
----------------- 아래는 버그 테스트 코드 -----------------
package dmlim.net;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URI;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
public class SocksProxyBugTest
{
static InetSocketAddress DEAD__HTTP__PROXY_ADDR = new InetSocketAddress("10.13.1.218", 8080);
static InetSocketAddress ALIVE_HTTP__PROXY_ADDR = new InetSocketAddress("10.13.1.218", 8081);
static InetSocketAddress DEAD__SOCKS_PROXY_ADDR = new InetSocketAddress("10.13.1.218", 1080);
static InetSocketAddress ALIVE_SOCKS_PROXY_ADDR = new InetSocketAddress("10.13.1.218", 1081);
static LinkedList<Proxy> HTTP__PROXY_LIST = new LinkedList<Proxy>();
static LinkedList<Proxy> SOCKS_PROXY_LIST = new LinkedList<Proxy>();
static
{
HTTP__PROXY_LIST.add(new Proxy(Proxy.Type.HTTP, DEAD__HTTP__PROXY_ADDR));
HTTP__PROXY_LIST.add(new Proxy(Proxy.Type.HTTP, ALIVE_HTTP__PROXY_ADDR));
SOCKS_PROXY_LIST.add(new Proxy(Proxy.Type.SOCKS, DEAD__SOCKS_PROXY_ADDR));
SOCKS_PROXY_LIST.add(new Proxy(Proxy.Type.SOCKS, ALIVE_SOCKS_PROXY_ADDR));
}
static class MyProxySelector extends ProxySelector
{
@Override
public void connectFailed(URI uri, SocketAddress addr, IOException e)
{
System.err.println(uri + ", " + addr + " " + e);
}
@Override
public List<Proxy> select(URI uri)
{
if ( uri.getScheme().equals("socket") )
return SOCKS_PROXY_LIST;
else
return HTTP__PROXY_LIST;
}
}
public static void testProxyAlive()
{
InetSocketAddress[] addrs = {
DEAD__HTTP__PROXY_ADDR, ALIVE_HTTP__PROXY_ADDR, DEAD__SOCKS_PROXY_ADDR, ALIVE_SOCKS_PROXY_ADDR
};
for ( InetSocketAddress addr : addrs )
{
System.err.println("Connecting to " + addr + "...");
Socket sock = null;
try
{
sock = new Socket(Proxy.NO_PROXY);
sock.connect(addr);
System.err.println("\t proxy is alive.");
}
catch (Exception e)
{
System.err.println("\t proxy is dead. (" + e + ")");
}
finally
{
if ( sock != null ) try { sock.close(); } catch (Exception e) {}
}
}
}
public static void testHttp() throws IOException
{
URL url = new URL("http://www.oracle.com/index.html");
ByteArrayOutputStream os = new ByteArrayOutputStream();
InputStream is = null;
try
{
is = (InputStream)url.getContent();
byte[] buf = new byte[1024];
for ( int nRead = 0 ; (nRead = is.read(buf)) != -1 ; )
{
os.write(buf, 0, nRead);
}
}
finally
{
if ( is != null )
is.close();
}
FileOutputStream fos = new FileOutputStream("index.http.html");
fos.write(os.toByteArray());
fos.close();
}
public static void testSocks() throws IOException
{
Socket sock = null;
InputStream is = null;
ByteArrayOutputStream os = new ByteArrayOutputStream();
try
{
sock = new Socket();
sock.connect(new InetSocketAddress("www.oracle.com", 80));
sock.getOutputStream().write("GET /index.html HTTP/1.0\r\nHost: www.oracle.com\r\nUser-Agent: Mozilla/1.0\r\n\r\n".getBytes());
sock.getOutputStream().flush();
is = sock.getInputStream();
byte[] buf = new byte[1024];
for ( int nRead = 0 ; (nRead = is.read(buf)) != -1 ; )
{
os.write(buf, 0, nRead);
}
}
finally
{
if ( is != null )
is.close();
if ( sock != null )
sock.close();
}
FileOutputStream fos = new FileOutputStream("index.socks.html");
fos.write(os.toByteArray());
fos.close();
}
public static void main(String[] args)
{
System.err.println("* Test for Proxy Alive...");
testProxyAlive();
System.err.println();
ProxySelector.setDefault(new MyProxySelector());
try
{
System.err.println("* Test for HTTP Proxy...");
testHttp();
System.err.println("Success");
}
catch (Exception e)
{
e.printStackTrace();
}
System.err.println();
try
{
System.err.println("* Test for SOCKS Proxy...");
testSocks();
System.err.println("Success");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}


최근 덧글