Compare commits
No commits in common. "0c29d56317c2dd4250d4bc19b316369ae09a315d" and "7c4ea13e3cbe6ee5eb4200291695db899c68b3e8" have entirely different histories.
0c29d56317
...
7c4ea13e3c
|
@ -108,17 +108,12 @@ public final class Chords extends ListenerAdapter
|
||||||
|
|
||||||
if (settings.getLocalAddr() != null)
|
if (settings.getLocalAddr() != null)
|
||||||
{
|
{
|
||||||
// make local binding socket factory
|
final WebSocketFactory webSocketFactory = new WebSocketFactory();
|
||||||
final LocalBindSocketFactory localBindSocketFactory = new LocalBindSocketFactory();
|
final LocalBindSocketFactory localBindSocketFactory = new LocalBindSocketFactory();
|
||||||
localBindSocketFactory.setLocalAddress(InetAddress.getByName(settings.getLocalAddr()));
|
|
||||||
|
|
||||||
// install local socket factory for HTTP
|
|
||||||
OkHttpClient.Builder httpBuilder = IOUtil.newHttpClientBuilder();
|
OkHttpClient.Builder httpBuilder = IOUtil.newHttpClientBuilder();
|
||||||
httpBuilder.socketFactory(localBindSocketFactory);
|
httpBuilder.socketFactory(localBindSocketFactory);
|
||||||
builder.setHttpClientBuilder(httpBuilder);
|
|
||||||
|
|
||||||
// install local socket factory for websockets
|
localBindSocketFactory.setLocalAddress(InetAddress.getByName(settings.getLocalAddr()));
|
||||||
final WebSocketFactory webSocketFactory = new WebSocketFactory();
|
|
||||||
webSocketFactory.setSocketFactory(localBindSocketFactory);
|
webSocketFactory.setSocketFactory(localBindSocketFactory);
|
||||||
builder.setWebsocketFactory(webSocketFactory);
|
builder.setWebsocketFactory(webSocketFactory);
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,10 @@
|
||||||
package moe.nekojimi.chords;
|
package moe.nekojimi.chords;
|
||||||
|
|
||||||
import java.io.IOException;
|
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;
|
import javax.net.SocketFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -39,18 +42,6 @@ public class LocalBindSocketFactory extends SocketFactory
|
||||||
this.localAddress = localAddress;
|
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
|
@Override
|
||||||
public Socket createSocket(String remoteAddr, int remotePort) throws IOException, UnknownHostException
|
public Socket createSocket(String remoteAddr, int remotePort) throws IOException, UnknownHostException
|
||||||
{
|
{
|
||||||
|
@ -72,10 +63,7 @@ public class LocalBindSocketFactory extends SocketFactory
|
||||||
@Override
|
@Override
|
||||||
public Socket createSocket(InetAddress remoteAddr, int remotePort, InetAddress x, int localPort) throws IOException
|
public Socket createSocket(InetAddress remoteAddr, int remotePort, InetAddress x, int localPort) throws IOException
|
||||||
{
|
{
|
||||||
System.out.println("Requested socket; " + localAddress + ":" + localPort + " -> " + remoteAddr + ":" + remotePort);
|
return new Socket(remoteAddr, remotePort, localAddress, localPort);
|
||||||
Socket socket = new Socket(remoteAddr, remotePort, localAddress, localPort);
|
|
||||||
System.out.println("Returned socket; " + socket.getLocalSocketAddress() + ":" + socket.getLocalPort() + " -> " + socket.getRemoteSocketAddress() + ":" + socket.getPort());
|
|
||||||
return socket;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int findFreePort(InetAddress localAddr)
|
private static int findFreePort(InetAddress localAddr)
|
||||||
|
@ -97,16 +85,4 @@ public class LocalBindSocketFactory extends SocketFactory
|
||||||
throw new RuntimeException("Could not find a free port");
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue