我正嘗試使用java和jetty創建代理服務器。我已經管理連接到http(非安全的),但https有問題。在Java中使用SSL的HTTP代理
我一直在做的是:
private boolean handleConnect(HttpServletRequest req,HttpServletResponse response){
String uri=req.getRequestURI();
System.out.println("--Handle--Connect-");
String port="";
String host="";
int c=uri.indexOf(":");
if (c >= 0){
port = uri.substring(c + 1);
host = uri.substring(0,c);
if (host.indexOf('/') > 0)
host = host.substring(host.indexOf('/') + 1);
}
boolean isBlocked;
isBlocked=false;
SocketChannel sock = null;
InputStream in=null;
OutputStream out=null;
try {
sock = SocketChannel.open();
in=req.getInputStream();
out=response.getOutputStream();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
InetSocketAddress inetAddress = new InetSocketAddress(host,req.getRemotePort());
sock.connect(inetAddress);
InputStreamReader inputstreamreader = new InputStreamReader(req.getInputStream());
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
OutputStreamWriter outputstreamwriter = new OutputStreamWriter(sock.socket().getOutputStream());
BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter);
response.setStatus(200);
response.setHeader("Connection", req.getHeader("Connection"));
String string = null;
while ((string = bufferedreader.readLine()) != null) {
bufferedwriter.write(string);
bufferedwriter.flush();
}
sock.configureBlocking(false);
sock.connect(new InetSocketAddress(host,Integer.parseInt(port)));
while(!sock.finishConnect()){
// Wait for connecting
}
response.setStatus(200);
response.setHeader("Connection", req.getHeader("Connection"));
response.flushBuffer();
ByteBuffer bb=ByteBuffer.allocate(1024*1024);
bb.clear();
in.mark(0);
if(in.available()>0){
byte by[]=new byte[in.available()];
bb.put(by);
if(sock.isConnected()){
sock.write(bb);
}
}
}
catch(Exception ex){
ex.printStackTrace();
isBlocked=true;
try {
// sock.close();
throw new IOException("Hello World");
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
finally{
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
我的問題是:
- 的代碼是不工作
(Error code: ssl_error_rx_record_too_long)
- 當是關閉套接字連接,和什麼類型要使用的套接字。
在此先感謝。
更新的代碼:
private boolean handleConnect(HttpServletRequest req,HttpServletResponse response){
String uri=req.getRequestURI();
System.out.println("--Handle--Connect-");
String port="";
String host="";
int c=uri.indexOf(":");
if (c >= 0){
port = uri.substring(c + 1);
host = uri.substring(0,c);
if (host.indexOf('/') > 0)
host = host.substring(host.indexOf('/') + 1);
}
boolean isBlocked;
isBlocked=false;
Socket sock = null;
IORedirect c2s, s2c;
// Make Asyncronous connection
try{
sock=new Socket(host,Integer.parseInt(port));
OutputStream os = response.getOutputStream();
byte[] msg = new String("HTTP/1.0 200 Connection established\r\nProxy-agent: RPS-Proxy/1.0\r\n\r\n").getBytes();
os.write(msg);
os.flush();
c2s=new IORedirect(req.getInputStream(),sock.getOutputStream());
c2s.setName("Client to Server");
c2s.start();
Thread.sleep(500);
s2c=new IORedirect(sock.getInputStream(), os);
s2c.setName("Server to Client");
s2c.start();
Thread.sleep(500);
System.err.println("A");
}
catch(Exception ex){
ex.printStackTrace();
isBlocked=true;
try {
sock.close();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
while(c2s.isAlive() || s2c.isAlive()){
}
try {
sock.close();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
下面是我們在Jetty 9中發佈的一個:http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/proxy/ProxyServlet.html –