11using System ;
2+ using System . Collections . ObjectModel ;
3+ using System . Net ;
4+ using System . Net . NetworkInformation ;
25using System . Net . Sockets ;
36using System . Windows ;
47using 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}
0 commit comments