|
Packit |
5e46da |
/*
|
|
Packit |
5e46da |
* This file is part of libbluray
|
|
Packit |
5e46da |
* Copyright (C) 2014 Petri Hintukainen <phintuka@users.sourceforge.net>
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* This library is free software; you can redistribute it and/or
|
|
Packit |
5e46da |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit |
5e46da |
* License as published by the Free Software Foundation; either
|
|
Packit |
5e46da |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
5e46da |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
5e46da |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
5e46da |
* Lesser General Public License for more details.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit |
5e46da |
* License along with this library. If not, see
|
|
Packit |
5e46da |
* <http://www.gnu.org/licenses/>.
|
|
Packit |
5e46da |
*/
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
package org.videolan;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
import java.io.IOException;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
import java.lang.reflect.Constructor;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
import java.net.ServerSocket;
|
|
Packit |
5e46da |
import java.net.Socket;
|
|
Packit |
5e46da |
import java.net.SocketImpl;
|
|
Packit |
5e46da |
import java.net.SocketImplFactory;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
import java.security.AccessController;
|
|
Packit |
5e46da |
import java.security.PrivilegedActionException;
|
|
Packit |
5e46da |
import java.security.PrivilegedExceptionAction;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
class BDJSocketFactory implements SocketImplFactory {
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
protected static void init() {
|
|
Packit |
5e46da |
try {
|
|
Packit |
5e46da |
Socket.setSocketImplFactory(new BDJSocketFactory(false));
|
|
Packit |
5e46da |
ServerSocket.setSocketFactory(new BDJSocketFactory(true));
|
|
Packit |
5e46da |
} catch (IOException e) {
|
|
Packit |
5e46da |
logger.error("Failed to hook SocketFactory: " + e);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
private BDJSocketFactory(boolean server) {
|
|
Packit |
5e46da |
this.server = server;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
private SocketImpl newSocket() {
|
|
Packit |
5e46da |
try {
|
|
Packit |
5e46da |
return (SocketImpl)AccessController.doPrivileged(
|
|
Packit |
5e46da |
new PrivilegedExceptionAction() {
|
|
Packit |
5e46da |
public Object run() throws Exception {
|
|
Packit |
5e46da |
Class defaultSocketImpl = Class.forName("java.net.SocksSocketImpl");
|
|
Packit |
5e46da |
Constructor constructor = defaultSocketImpl.getDeclaredConstructor/*s*/(new Class[0])/*[0]*/;
|
|
Packit |
5e46da |
constructor.setAccessible(true);
|
|
Packit |
5e46da |
return constructor.newInstance(new Object[0]);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
});
|
|
Packit |
5e46da |
} catch (PrivilegedActionException e) {
|
|
Packit |
5e46da |
logger.error("Failed to create socket: " + e.getException() + " at " + Logger.dumpStack());
|
|
Packit |
5e46da |
throw new RuntimeException(e.getException());
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
public SocketImpl createSocketImpl() {
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
if (server) {
|
|
Packit |
5e46da |
logger.error("Xlet tried to create server socket");
|
|
Packit |
5e46da |
throw new RuntimeException("server sockets disabled");
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
SocketImpl socket = newSocket();
|
|
Packit |
5e46da |
BDJXletContext ctx = BDJXletContext.getCurrentContext();
|
|
Packit |
5e46da |
if (ctx != null) {
|
|
Packit |
5e46da |
logger.info("Xlet " + ctx + " created new socket");
|
|
Packit |
5e46da |
ctx.addSocket(socket);
|
|
Packit |
5e46da |
} else {
|
|
Packit |
5e46da |
logger.error("New socket created outside of Xlet context: " + Logger.dumpStack());
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
return socket;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
private boolean server = false;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
private static final Logger logger = Logger.getLogger(BDJSocketFactory.class.getName());
|
|
Packit |
5e46da |
}
|