Skip to content

Commit 4b31312

Browse files
blackspherefollowerqdot
authored andcommitted
Adding a loopback only option to the server GUI
This also comes with a more advanced buttplug url display. Also, I really dislike Windows GUI logic... Fixes #213
1 parent b38d7bf commit 4b31312

5 files changed

Lines changed: 112 additions & 22 deletions

File tree

Buttplug.Apps.KiirooEmulatorGUI/MainWindow.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:controls="clr-namespace:Buttplug.Components.Controls;assembly=Buttplug.Components.Controls"
77
mc:Ignorable="d"
8-
Title="Buttplug" Height="350" Width="525">
8+
Title="Buttplug" Height="400" Width="600">
99
<Grid>
1010
<controls:ButtplugTabControl Name="ButtplugTab"/>
1111
</Grid>
12-
</Window>
12+
</Window>

Buttplug.Apps.WebsocketServerGUI/WebsocketServerControl.xaml

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
66
mc:Ignorable="d"
7-
d:DesignHeight="300" d:DesignWidth="525">
8-
<Grid Background="#FFE5E5E5" Margin="0,-1,0,1" >
7+
d:DesignHeight="400" d:DesignWidth="600">
8+
<Grid Background="#FFE5E5E5" >
99
<Grid.RowDefinitions>
1010
<RowDefinition Height="45"/>
1111
<RowDefinition Height="25"/>
@@ -14,24 +14,36 @@
1414
<Label Content="Port:" Grid.Row="0" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
1515
<TextBox Name="PortTextBox" Grid.Row="0" HorizontalAlignment="Left" Height="23" Margin="60,14,0,0" TextWrapping="Wrap" Text="12345" VerticalAlignment="Top" Width="120" TextChanged="PortTextBox_TextChanged"/>
1616
<CheckBox Name="SecureCheckBox" Grid.Row="0" Content="SSL/TLS" HorizontalAlignment="Right" Margin="0,17,10,0" VerticalAlignment="Top" Unchecked="SecureCheckBox_Unchecked" Checked="SecureCheckBox_Checked"/>
17-
17+
<CheckBox Name="LoopbackCheckBox" Grid.Row="0" Content="Localhost Only" HorizontalAlignment="Right" Margin="0,17,90,0" VerticalAlignment="Top" Unchecked="LoopbackCheckBox_Unchecked" Checked="LoopbackCheckBox_Checked"/>
1818
<Button Name="ConnToggleButton" Grid.Row="1" Content="Start" HorizontalAlignment="Right" Margin="0,0,10,0" VerticalAlignment="Top" Width="75" Click="ConnToggleButton_Click"/>
19-
<GroupBox Header="Connection Details" Grid.Row="2" Margin="10,0" VerticalAlignment="Top" Name="ConnInfo">
19+
<GroupBox Header="Connection Details" Grid.Row="2" Margin="10,0" Padding="10,10,10,10" VerticalAlignment="Top" x:Name="ConnInfo">
2020
<Grid Background="#FFE5E5E5" >
2121
<Grid.RowDefinitions>
2222
<RowDefinition Height="auto"/>
2323
<RowDefinition Height="auto"/>
2424
<RowDefinition Height="auto"/>
2525
</Grid.RowDefinitions>
26-
<Label Content="Port:" Grid.Row="0" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
27-
<TextBox Name="ConnectionUrl" Grid.Row="0" Height="23" Margin="104,14,0,-18" TextWrapping="Wrap" VerticalAlignment="Top" Text="ws://localhost:12345/buttplug" IsReadOnly="True"/>
26+
<Label Content="Port:" Grid.Row="0" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top"/>
27+
<ListView Name="ConnectionUrl" Grid.Row="0" Height="auto" Margin="60,0,0,0" VerticalAlignment="Top" HorizontalContentAlignment="Stretch">
28+
<ListView.ItemTemplate>
29+
<DataTemplate>
30+
<Grid>
31+
<Grid.ColumnDefinitions>
32+
<ColumnDefinition Width="*" />
33+
<ColumnDefinition Width="auto" />
34+
<ColumnDefinition Width="auto" />
35+
</Grid.ColumnDefinitions>
36+
<TextBlock Name="WsUrl" Text="{Binding}" VerticalAlignment="Center" Grid.Column="0"/>
37+
<Button Name="ConnItemCopy" Content="Copy" VerticalAlignment="Center" Grid.Column="1" Width="40" Click="ConnItemCopy_Click" Margin="0,0,5,0"/>
38+
<Button Name="ConnItemTest" Content="Test" VerticalAlignment="Center" Grid.Column="2" Width="40" Click="ConnItemTest_Click"/>
39+
</Grid>
40+
</DataTemplate>
41+
</ListView.ItemTemplate>
42+
</ListView>
2843

29-
<Label Content="Test URL:" Grid.Row="1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
30-
<TextBlock Grid.Row="1" Height="23" Margin="104,10,0,-18" VerticalAlignment="Top" ><Hyperlink Name="TestUrl" NavigateUri="http://localhost:12345" RequestNavigate="TestUrl_RequestNavigate">http://localhost:12345</Hyperlink></TextBlock>
31-
32-
<Label Content="Status:" Grid.Row="2" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top"/>
33-
<Label Name="ConnStatus" Content="(Not Connected)" Grid.Row="2" HorizontalAlignment="Left" Margin="100,00,0,0" VerticalAlignment="Top"/>
34-
<Button Name="DisconnectButton" Height="25" Grid.Row="2" Content="Disconnect" IsEnabled="False" HorizontalAlignment="Right" Margin="0,0,10,0" VerticalAlignment="Top" Width="75" Click="DisconnectButton_Click"/>
44+
<Label Content="Status:" Grid.Row="2" HorizontalAlignment="Left" Margin="0,5,0,0" VerticalAlignment="Top"/>
45+
<Label Name="ConnStatus" Content="(Not Connected)" Grid.Row="2" HorizontalAlignment="Left" Margin="60,5,0,0" VerticalAlignment="Top"/>
46+
<Button Name="DisconnectButton" Height="25" Grid.Row="2" Content="Disconnect" IsEnabled="False" HorizontalAlignment="Right" Margin="0,5,0,0" VerticalAlignment="Top" Width="75" Click="DisconnectButton_Click"/>
3547
</Grid>
3648
</GroupBox>
3749

Buttplug.Apps.WebsocketServerGUI/WebsocketServerControl.xaml.cs

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using System;
2+
using System.Collections.ObjectModel;
3+
using System.Net;
4+
using System.Net.NetworkInformation;
25
using System.Net.Sockets;
36
using System.Windows;
47
using System.Windows.Controls;
@@ -19,14 +22,17 @@ public partial class WebsocketServerControl
1922
private readonly ButtplugConfig _config;
2023
private uint _port;
2124
private bool _secure;
25+
private bool _loopback;
2226
private string _hostname;
27+
private ConnUrlList _connUrls;
2328

2429
public WebsocketServerControl(IButtplugServerFactory bpFactory)
2530
{
2631
InitializeComponent();
2732
_ws = new ButtplugWebsocketServer();
2833
_bpFactory = bpFactory;
2934
_config = new ButtplugConfig("Buttplug");
35+
_connUrls = new ConnUrlList();
3036
_port = 12345;
3137
if (uint.TryParse(_config.GetValue("buttplug.server.port", "12345"), out uint pres))
3238
{
@@ -39,10 +45,19 @@ public WebsocketServerControl(IButtplugServerFactory bpFactory)
3945
_secure = sres;
4046
}
4147

48+
_loopback = true;
49+
if (bool.TryParse(_config.GetValue("buttplug.server.loopbackOnly", "true"), out bool lres))
50+
{
51+
_loopback = lres;
52+
}
53+
4254
_hostname = _config.GetValue("buttplug.server.hostname", "localhost");
4355

4456
PortTextBox.Text = _port.ToString();
4557
SecureCheckBox.IsChecked = _secure;
58+
LoopbackCheckBox.IsChecked = _loopback;
59+
60+
ConnectionUrl.ItemsSource = _connUrls;
4661

4762
_ws.OnException += WebSocketExceptionHandler;
4863
_ws.ConnectionAccepted += WebSocketConnectionAccepted;
@@ -79,13 +94,25 @@ public void StartServer()
7994
{
8095
try
8196
{
82-
_ws.StartServer(_bpFactory, (int)_port, _secure, _hostname);
97+
_ws.StartServer(_bpFactory, (int)_port, _loopback, _secure, _hostname);
8398
ConnToggleButton.Content = "Stop";
8499
SecureCheckBox.IsEnabled = false;
85100
PortTextBox.IsEnabled = false;
86-
ConnectionUrl.Text = (_secure ? "wss" : "ws") + "://localhost:" + _port.ToString() + "/buttplug";
87-
TestUrl.Inlines.Clear();
88-
TestUrl.Inlines.Add((_secure ? "https" : "http") + "://localhost:" + _port.ToString());
101+
LoopbackCheckBox.IsEnabled = false;
102+
_connUrls.Clear();
103+
_connUrls.Add(new ConnUrlData(_secure, "localhost", _port));
104+
105+
foreach (var network in NetworkInterface.GetAllNetworkInterfaces())
106+
{
107+
foreach (IPAddressInformation address in network.GetIPProperties().UnicastAddresses)
108+
{
109+
if (address.Address.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(address.Address))
110+
{
111+
_connUrls.Add(new ConnUrlData(_secure, address.Address.ToString(), _port));
112+
}
113+
}
114+
}
115+
89116
ConnStatus.Content = "(Not Connected)";
90117
DisconnectButton.IsEnabled = false;
91118
ConnInfo.Visibility = Visibility.Visible;
@@ -102,6 +129,7 @@ public void StopServer()
102129
ConnToggleButton.Content = "Start";
103130
SecureCheckBox.IsEnabled = true;
104131
PortTextBox.IsEnabled = true;
132+
LoopbackCheckBox.IsEnabled = true;
105133
ConnInfo.Visibility = Visibility.Collapsed;
106134
}
107135

@@ -112,6 +140,7 @@ private void ConnToggleButton_Click(object aObj, RoutedEventArgs aEvent)
112140
StartServer();
113141
_config.SetValue("buttplug.server.port", _port.ToString());
114142
_config.SetValue("buttplug.server.secure", _secure.ToString());
143+
_config.SetValue("buttplug.server.loopbackOnly", _loopback.ToString());
115144
}
116145
else
117146
{
@@ -149,5 +178,54 @@ private void DisconnectButton_Click(object sender, RoutedEventArgs e)
149178
{
150179
_ws.Disconnect();
151180
}
181+
182+
private void LoopbackCheckBox_Unchecked(object sender, RoutedEventArgs e)
183+
{
184+
_loopback = false;
185+
}
186+
187+
private void LoopbackCheckBox_Checked(object sender, RoutedEventArgs e)
188+
{
189+
_loopback = true;
190+
}
191+
192+
private void ConnItemCopy_Click(object sender, RoutedEventArgs e)
193+
{
194+
if (sender is Button)
195+
{
196+
var data = (sender as Button).DataContext as ConnUrlData;
197+
Clipboard.SetText(data.WsUrl);
198+
}
199+
}
200+
201+
private void ConnItemTest_Click(object sender, RoutedEventArgs e)
202+
{
203+
if (sender is Button)
204+
{
205+
var data = (sender as Button).DataContext as ConnUrlData;
206+
System.Diagnostics.Process.Start(new Uri(data.TestUrl).AbsoluteUri);
207+
}
208+
}
209+
}
210+
211+
public class ConnUrlList : ObservableCollection<ConnUrlData>
212+
{
213+
}
214+
215+
public class ConnUrlData
216+
{
217+
public string WsUrl;
218+
public string TestUrl;
219+
220+
public ConnUrlData(bool aSecure, string aHost, uint aPort)
221+
{
222+
WsUrl = (aSecure ? "wss" : "ws") + "://" + aHost + ":" + aPort.ToString() + "/buttplug";
223+
TestUrl = (aSecure ? "https" : "http") + "://" + aHost + ":" + aPort.ToString() + "/";
224+
}
225+
226+
public override string ToString()
227+
{
228+
return WsUrl;
229+
}
152230
}
153231
}

Buttplug.Components.Controls/ButtplugLogControl.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
using System;
22
using System.Collections.ObjectModel;
33
using System.Linq;
4+
using System.Text;
45
using System.Threading;
56
using System.Threading.Tasks;
67
using System.Windows;
78
using System.Windows.Controls;
9+
using System.Windows.Input;
810
using System.Windows.Threading;
911
using Microsoft.Win32;
1012
using NLog;
1113
using NLog.Config;
1214
using NLog.Targets;
13-
using System.Windows.Input;
14-
using System.Text;
1515

1616
namespace Buttplug.Components.Controls
1717
{

Buttplug.Components.WebsocketServer/ButtplugWebsocketServer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ public class ButtplugWebsocketServer
4141
[NotNull]
4242
private ConcurrentDictionary<string, WebSocket> _connections = new ConcurrentDictionary<string, WebSocket>();
4343

44-
public void StartServer([NotNull] IButtplugServerFactory aFactory, int aPort = 12345, bool aSecure = false, string aHostname = "localhost")
44+
public void StartServer([NotNull] IButtplugServerFactory aFactory, int aPort = 12345, bool aLoopBack = true, bool aSecure = false, string aHostname = "localhost")
4545
{
4646
CancellationTokenSource cancellation = new CancellationTokenSource();
4747
_factory = aFactory;
4848

4949
_logManager = new ButtplugLogManager();
5050
_logger = _logManager.GetLogger(this.GetType());
5151

52-
var endpoint = new IPEndPoint(IPAddress.Any, aPort);
52+
var endpoint = new IPEndPoint(aLoopBack ? IPAddress.Loopback : IPAddress.Any, aPort);
5353
_server = new WebSocketListener(endpoint);
5454
var rfc6455 = new vtortola.WebSockets.Rfc6455.WebSocketFactoryRfc6455(_server);
5555
_server.Standards.RegisterStandard(rfc6455);

0 commit comments

Comments
 (0)