public static void Initialize(string keyPass) { VerificationKeyProvider.Initialize(KeyFile, keyPass); Initialization.Run(); foreach (var t in ConnectionTargets) { try { new ErebusLink(TCPIPConnectionUtils.Connect(t.Item1, t.Item2), Instance, false); } catch (Exception e) { Log.RecordEvent(typeof(App), $"Failed to connect to {t.Item1}:{t.Item2}: {e.Message}", LogEntrySeverity.Error); } } btConnListener = new BluetoothConnectionListener(Instance); btReqListener = new BluetoothContactRequestListener(Instance.Address, (addr, name) => { if (Current.Dispatcher.Invoke(() => MessageBox.Show(Current.MainWindow, "Would you like to accept this request?", $"Received contact request via Bluetooth from {name}", MessageBoxButton.YesNo)) == MessageBoxResult.Yes) { Current.Dispatcher.Invoke(() => (Current.MainWindow as MainWindow).Navigate(new Uri("/Views/AddContact.xaml", UriKind.Relative), new { Address = addr, Name = $"Bluetooth device '{name}'" })); return(true); } return(false); }); Task.Run(usersUpdateProcess); Task.Run(onlineUpdateProcess); }
/// <summary> /// Sends a contact request to the specified Bluetooth device. /// </summary> /// <param name="device">The device to send the request to.</param> /// <param name="myAddress">The local address to send to the device.</param> /// <returns>The Erebus address of the device if the request was accepted; otherwise null.</returns> public static ErebusAddress?RequestAddContact(BluetoothDevice device, ErebusAddress myAddress) { Log.RecordEvent(typeof(BluetoothUtils), $"Attempting to add Bluetooth device '{device.Name}' as a contact.", LogEntrySeverity.Info); try { var s = device.CreateRfcommSocketToServiceRecord(RfcommAddContactService); s.Connect(); using (var r = new BinaryReader(s.InputStream)) using (var w = new BinaryWriter(s.OutputStream)) { w.Write(true); r.ReadBoolean(); w.Write(myAddress); var addr = r.ReadErebusAddress(); var vkp = new VerificationKeyProvider(); var local = vkp.CreatePrivateKey(); w.Write(local.Item2.Length); w.Write(local.Item2); vkp.AddKeyPair(addr, local.Item1, r.ReadBytes(r.ReadInt32())); Log.RecordEvent(typeof(BluetoothUtils), $"Successfully negotiated contact with {addr} via Bluetooth RFCOMM.", LogEntrySeverity.Info); return(addr); } } catch (Exception e) { Log.RecordEvent(typeof(BluetoothUtils), $"Exception adding contact: {e.Message}; assuming other user rejected request.", LogEntrySeverity.Error); return(null); } }
private void done(object sender, RoutedEventArgs e) { using (var k = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\472\Erebus\Client")) { k.SetValue("logfile", Path.Combine(path.Text, "Erebus.log")); k.SetValue("keyfile", Path.Combine(path.Text, "keyinfo.dat")); k.SetValue("address", addr.ToString()); using (var tk = k.CreateSubKey("Targets")) tk.SetValue("mertin1", 1234); } Directory.CreateDirectory(path.Text); VerificationKeyProvider.Initialize(Path.Combine(path.Text, "keyinfo.dat"), password.Password); Close(); }
Task listenProcess() => Task.Run(() => { while (process) { var s = l.Accept(); using (var r = new BinaryReader(s.InputStream)) using (var w = new BinaryWriter(s.OutputStream)) { var _addr = r.ReadErebusAddress(); if (h(_addr, s.RemoteDevice.Name)) { w.Write(addr); var vkp = new VerificationKeyProvider(); var local = vkp.CreatePrivateKey(); w.Write(local.Item2.Length); w.Write(local.Item2); vkp.AddKeyPair(addr, local.Item1, r.ReadBytes(r.ReadInt32())); Log.RecordEvent(this, $"Successfully negotiated contact with {_addr} via Bluetooth RFCOMM.", LogEntrySeverity.Info); } } } });
static void Main(string[] args) { Log.EntryRecorded += (sender, e) => Console.WriteLine($"{sender} ({e.Severity}): {e.Message}"); VerificationKeyProvider.Initialize("keyinfo.dat", "hello world"); var vkp = new VerificationKeyProvider(); Initialization.Run(); SSMP ssmp = null; SSMP.InvitationReceived += (sender, e) => { if (ssmp == null) { ssmp = e.Accept(); } ssmp.MessageReceived += Ssmp_MessageReceived; ssmp.ConnectionClosed += (_sender, _e) => ssmp = null; }; var ei = new ErebusInstance(new ErebusAddress(Console.ReadLine())); ei.Services = new[] { HDPService.SSMP }; var l = new TCPIPConnectionListener(int.Parse(Console.ReadLine()), ei); while (true) { try { var cmd = Console.ReadLine().Split(' '); switch (cmd[0]) { case "addlink": new ErebusLink(TCPIPConnectionUtils.Connect(cmd[1], int.Parse(cmd[2])), ei, false); break; case "listlink": foreach (var link in ei.Links) { Console.WriteLine(link.RemoteAddress); } break; case "hdp": var hdp = new HDP(ei, HDPService.SSMP); hdp.SendRequest(ErebusAddress.Broadcast); hdp.InitClose(); break; case "ssmp": ssmp = new SSMP(ei, new ErebusAddress(cmd[1])); ssmp.MessageReceived += Ssmp_MessageReceived; break; case "msg": ssmp?.SendMessage(cmd.Skip(1).Aggregate((x, y) => x + " " + y)); break; case "addkey": var addr = new ErebusAddress(cmd[1]); vkp.RemoveKeyPair(addr); var key = vkp.CreatePrivateKey(); Console.WriteLine("Key: " + key.Item2.Select(x => x.ToString()).Aggregate((x, y) => x + "," + y)); Console.Write("Please copy other key to clipboard, then press <enter> . . ."); Console.ReadLine(); vkp.AddKeyPair(addr, key.Item1, Clipboard.GetText().Split(',').Select(x => byte.Parse(x)).ToArray()); break; case "removekey": vkp.RemoveKeyPair(new ErebusAddress(cmd[1])); break; case "close": ssmp?.Dispose(); ssmp = null; break; case "exit": return; } } catch (Exception e) { Console.WriteLine(e); } } }