Skip to content

Commit

Permalink
Support optional binding only to the loopback address
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyrat committed Mar 30, 2014
1 parent 4756dbb commit 507038f
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/WebSocketServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ public class WebSocketServer : IWebSocketServer
private Action<IWebSocketConnection> _config;

public WebSocketServer(string location)
: this(8181, location)
: this(8181, location, false)
{

}

public WebSocketServer(int port, string location)
: this(8181, location, false)
{
}

public WebSocketServer(int port, string location)
public WebSocketServer(int port, string location, bool bindOnlyToLoopback)
{
var uri = new Uri(location);
Port = uri.Port > 0 ? uri.Port : port;
Port = uri.Port > 0 ? uri.Port : port;
BindOnlyToLoopback = bindOnlyToLoopback;
Location = location;
_scheme = uri.Scheme;
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
Expand All @@ -30,7 +36,8 @@ public WebSocketServer(int port, string location)

public ISocket ListenerSocket { get; set; }
public string Location { get; private set; }
public int Port { get; private set; }
public int Port { get; private set; }
public bool BindOnlyToLoopback { get; private set; }
public X509Certificate2 Certificate { get; set; }

public bool IsSecure
Expand All @@ -44,8 +51,8 @@ public void Dispose()
}

public void Start(Action<IWebSocketConnection> config)
{
var ipLocal = new IPEndPoint(IPAddress.Any, Port);
{
var ipLocal = new IPEndPoint(BindOnlyToLoopback ? IPAddress.Loopback : IPAddress.Any, Port);
ListenerSocket.Bind(ipLocal);
ListenerSocket.Listen(100);
FleckLog.Info("Server started at " + Location,null);
Expand Down

0 comments on commit 507038f

Please sign in to comment.