Compare commits

..

No commits in common. "0c29d56317c2dd4250d4bc19b316369ae09a315d" and "7c4ea13e3cbe6ee5eb4200291695db899c68b3e8" have entirely different histories.

2 changed files with 7 additions and 36 deletions
src/main/java/moe/nekojimi/chords

View File

@ -108,17 +108,12 @@ public final class Chords extends ListenerAdapter
if (settings.getLocalAddr() != null)
{
// make local binding socket factory
final WebSocketFactory webSocketFactory = new WebSocketFactory();
final LocalBindSocketFactory localBindSocketFactory = new LocalBindSocketFactory();
localBindSocketFactory.setLocalAddress(InetAddress.getByName(settings.getLocalAddr()));
// install local socket factory for HTTP
OkHttpClient.Builder httpBuilder = IOUtil.newHttpClientBuilder();
httpBuilder.socketFactory(localBindSocketFactory);
builder.setHttpClientBuilder(httpBuilder);
// install local socket factory for websockets
final WebSocketFactory webSocketFactory = new WebSocketFactory();
localBindSocketFactory.setLocalAddress(InetAddress.getByName(settings.getLocalAddr()));
webSocketFactory.setSocketFactory(localBindSocketFactory);
builder.setWebsocketFactory(webSocketFactory);
}

View File

@ -17,7 +17,10 @@
package moe.nekojimi.chords;
import java.io.IOException;
import java.net.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.net.SocketFactory;
/**
@ -39,18 +42,6 @@ public class LocalBindSocketFactory extends SocketFactory
this.localAddress = localAddress;
}
@Override
public Socket createSocket() throws IOException
{
return new LocalBoundSocket();
// throw new RuntimeException("Trying to create an empty socket!");
}
private InetSocketAddress findFreeSocketAddress()
{
return new InetSocketAddress(localAddress, findFreePort(localAddress));
}
@Override
public Socket createSocket(String remoteAddr, int remotePort) throws IOException, UnknownHostException
{
@ -72,10 +63,7 @@ public class LocalBindSocketFactory extends SocketFactory
@Override
public Socket createSocket(InetAddress remoteAddr, int remotePort, InetAddress x, int localPort) throws IOException
{
System.out.println("Requested socket; " + localAddress + ":" + localPort + " -> " + remoteAddr + ":" + remotePort);
Socket socket = new Socket(remoteAddr, remotePort, localAddress, localPort);
System.out.println("Returned socket; " + socket.getLocalSocketAddress() + ":" + socket.getLocalPort() + " -> " + socket.getRemoteSocketAddress() + ":" + socket.getPort());
return socket;
return new Socket(remoteAddr, remotePort, localAddress, localPort);
}
private static int findFreePort(InetAddress localAddr)
@ -97,16 +85,4 @@ public class LocalBindSocketFactory extends SocketFactory
throw new RuntimeException("Could not find a free port");
}
private class LocalBoundSocket extends Socket
{
@Override
public void bind(SocketAddress bindpoint) throws IOException
{
InetSocketAddress localAddress = findFreeSocketAddress();
System.err.println("LocalBoundSocket NOT binding to " + bindpoint + ", using " + localAddress + " instead!");
super.bind(localAddress);
}
}
}