Ejemplo n.º 1
0
 public static void Stop()
 {
     if(echoListener != null) echoListener.Close();
     if(echoResponder != null) echoResponder.Close();
     echoResponder = null;
     echoListener = null;
 }
Ejemplo n.º 2
0
 private async static void requestReceived(TCPSocket sender)
 {
     await uiThreadDispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
     {
         try
         {
             progressIndeterminate();
             sender.ProgressRepport += (x) => { if (ProgressRepport != null) ProgressRepport(x); };
             string requestContents = await sender.Recv();
             progressStop();
             Request receivedRequest = JSONHandling.ParseJSONResponse<Request>(requestContents);
             await requestHandlerMap[receivedRequest.Type](receivedRequest.SenderGuid, receivedRequest.Data, sender);
         }
         catch(OutOfMemoryException)
         {
             DialogBoxes.ShowMessageBox("File too big to fit in memory");
         }
         catch(FileTransferException error)
         {
             DialogBoxes.ShowMessageBox(error.Message);
         }
         catch(Exception error)
         {
             DialogBoxes.ShowMessageBox("Unknown error " + error.Message);
         }
         finally
         {
             progressStop();
             sender.Send(Strings.RESPONSE_REJECT);
         }
     });
 }
Ejemplo n.º 3
0
 public async static Task Reboot()
 {
     if (listenerSocket != null) listenerSocket.Close();
     listenerSocket = null;
     MulticastDiscovery.Stop();
     await Init(myName);
 }
Ejemplo n.º 4
0
        private async static void discoveryRequestReceived(Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args)
        {
            if (args.RemoteAddress.RawName == "127.0.0.1") return;
            try
            {
                var responseSocket = new TCPSocket();
                await responseSocket.Connect(args.RemoteAddress.RawName, DEFAULT_DISCOVERY_PORT);
                await responseSocket.Send(await GuidHandling.GetMyGuid() + ":" + myName);
            }
            catch(Exception)
            {

            }
        }
Ejemplo n.º 5
0
        private static async void deviceDiscovered(TCPSocket sender)
        {
            try
            {
                string ip = sender.GetIP();
                string receivedString = await sender.Recv();
                if (receivedString.IndexOf(":") == -1 || receivedString.IndexOf(":") == receivedString.Length-1) return;
                string guid = receivedString.Substring(0, receivedString.IndexOf(":"));
                if (guid == (await GuidHandling.GetMyGuid())) return;
                string name = receivedString.Substring(receivedString.IndexOf(":") + 1);
                DeviceDiscovered(ip, guid, name);
            }
            catch(Exception)
            {

            }
        }
Ejemplo n.º 6
0
 public async static Task Init(string name)
 {
     if (uiThreadDispatcher == null) uiThreadDispatcher = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;
     myName = name;
     Peers = new ObservableCollection<Peer>();
     preSendRegistrations = new Dictionary<string, PreSendRequest>();
     preSendRegistrationsNoCrypt = new Dictionary<string, PreSendRequestNoCrypt>();
     listenerSocket = new TCPSocket();
     listenerSocket.ConnectionReceived += requestReceived;
     initializeHandlerMap();
     await listenerSocket.StartListening("32102");
     if(!(await DataStorage.GetStoredItemAsync("Peers")==null))
     {
         Peer[] newPeers = JSONHandling.ParseJSONResponse<Peer[]>(await DataStorage.GetStoredItemAsync("Peers"));
         for(int i=0;i<newPeers.Length;i++) Peers.Add(newPeers[i]);
     }
     RefreshPeerMap();
     await MulticastDiscovery.StartMulticastResponder(myName);
     MulticastDiscovery.DeviceDiscovered += newPeerDiscovered;
 }
Ejemplo n.º 7
0
 private async static Task runDiscoveryServer()
 {
     echoListener = new TCPSocket();
     echoListener.ConnectionReceived += deviceDiscovered;
     await echoListener.StartListening(DEFAULT_DISCOVERY_PORT);
 }
Ejemplo n.º 8
0
 private async static Task<TCPSocket> sendRequestToPeer(string peer, string type, string data, bool repportProgress=false, int timeout=5000)
 {
     var newSocket = new TCPSocket();
     await newSocket.Connect(peerMap[peer].LastKnownIP, "32102");
     if (repportProgress) newSocket.ProgressRepport += progressRepport;
     return await sendRequestToSocket(newSocket, type, data, timeout);
 }
Ejemplo n.º 9
0
 private async static Task preSendNoCryptRequestReceived(string peer, string data, TCPSocket socket)
 {
     if (peerMap.ContainsKey(peer) && peerMap[peer].Paired)
     {
         PreSendRequestNoCrypt registration = JSONHandling.ParseJSONResponse<PreSendRequestNoCrypt>(data);
         await DialogBoxes.AskForConfirmation(registration.FileName + " from " + peerMap[peer].Name + " over insecure connection. Accept?", async () =>
         {
             preSendRegistrationsNoCrypt[peer] = registration;
             await socket.Send(Strings.RESPONSE_OK);
         }, async () =>
         {
             await socket.Send(Strings.RESPONSE_REJECT);
         });
     }
     else
     {
         await socket.Send(Strings.RESPONSE_NOT_PAIRED);
     }
 }
Ejemplo n.º 10
0
 private async static Task preSendRequestReceived(string peer, string data, TCPSocket socket)
 {
     if(peerMap.ContainsKey(peer) && peerMap[peer].Paired)
     {
         PreSendRequest registration = JSONHandling.ParseJSONResponse<PreSendRequest>(data);
         if(!registration.key.VerifySignature(peerMap[peer].SharedPassword))
         {
             socket.Send(Strings.RESPONSE_REJECT);
         }
         await DialogBoxes.AskForConfirmation(registration.FileName+" from "+peerMap[peer].Name+". Accept?", async () =>
         {
             preSendRegistrations[peer] = registration;
             await socket.Send(Strings.RESPONSE_OK);
         }, async () =>
         {
             await socket.Send(Strings.RESPONSE_REJECT);
         });
     }
     else
     {
         await socket.Send(Strings.RESPONSE_NOT_PAIRED);
     }
 }
Ejemplo n.º 11
0
 private async static Task fileReceivedNoCrypt(string peer, string data, TCPSocket socket)
 {
     progressIndeterminate();
     if (!(peerMap.ContainsKey(peer) && peerMap[peer].Paired))
     {
         progressStop();
         await socket.Send(Strings.RESPONSE_NOT_PAIRED);
         return;
     }
     if (!(preSendRegistrationsNoCrypt.ContainsKey(peer)))
     {
         progressStop();
         await socket.Send(Strings.RESPONSE_REJECT);
         return;
     }
     PreSendRequestNoCrypt registration = preSendRegistrationsNoCrypt[peer];
     preSendRegistrationsNoCrypt.Remove(peer);
     File receivedFile = JSONHandling.ParseJSONResponse<File>(data);
     if (receivedFile.FileName != registration.FileName)
     {
         progressStop();
         throw new FileTransferException("File name mismatch");
     }
     byte[] fileContents = receivedFile.GetContents();
     progressStop();
     FileReceived(receivedFile.FileName, fileContents);
 }
Ejemplo n.º 12
0
        private async static Task pairRequestReceived(string peer, string data, TCPSocket socket)
        {
            progressIndeterminate();
            var request = JSONHandling.ParseJSONResponse<PairingRequest>(data);
            if(request.Guid!=peer) 
            {
                progressStop();
                await socket.Send(Strings.RESPONSE_REJECT);
                return;
            }
            string signatureSecret = await DialogBoxes.AskForInput("Pairing request", "Please enter the shared password");
            if (signatureSecret == null)
            {
                progressStop();
                await socket.Send(Strings.RESPONSE_REJECT);
                return;
            }

            if(!request.PublicKey.VerifySignature(signatureSecret))
            {
                progressStop();
                await socket.Send(Strings.RESPONSE_BAD_SIGNATURE);
                await DialogBoxes.ShowMessageBox("Shared password incorrect");
                return;
            }
            string myPrivateKey = DiffieHellman.generate_DH_Private();
            string myPublicKey = DiffieHellman.calculate_DH_Public(myPrivateKey);
            PublicKey returnedKey = new PublicKey(myPublicKey, signatureSecret);

            await sendRequestToSocket(socket, Strings.REQYEST_TYPE_PUBLICKEY, JSONHandling.SerializeObject(returnedKey));
            string response = await socket.Recv();
            progressStop();
            if(response!=Strings.RESPONSE_OK)
            {
                await DialogBoxes.ShowMessageBox(response);
                return;
            }

            Peer newPeer = new Peer(request.Guid, request.Name);
            newPeer.LastKnownIP = socket.GetIP();
            newPeer.MyPrivateKey = myPrivateKey;
            newPeer.PublicKey = request.PublicKey.Key;
            newPeer.SharedPassword = signatureSecret;
            addPeer(newPeer);
            await StorePeers();
            RefreshPeerMap();
        }
Ejemplo n.º 13
0
 private async static Task<TCPSocket> sendRequestToSocket(TCPSocket socket, string type, string data, int timeout=5000)
 {
     var request = new Request();
     request.Data = data;
     request.SenderGuid = await GuidHandling.GetMyGuid();
     request.Type = type;
     socket.SetTimeout(timeout);
     await socket.Send(JSONHandling.SerializeObject(request));
     return socket;
 }