Beispiel #1
0
        public void TestToxEncryptionLoad()
        {
            var tox1 = new Tox(ToxOptions.Default);

            tox1.Name          = "Test";
            tox1.StatusMessage = "Hey";

            string password = "******";
            var    data     = tox1.GetData(password);

            Assert.IsNotNull(data, "Failed to encrypt the Tox data");
            Assert.IsTrue(data.IsEncrypted, "We encrypted the data, but toxencryptsave thinks we didn't");

            var tox2 = new Tox(ToxOptions.Default, ToxData.FromBytes(data.Bytes), password);

            if (tox2.Id != tox1.Id)
            {
                Assert.Fail("Failed to load tox data correctly, tox id's don't match");
            }

            if (tox2.Name != tox1.Name)
            {
                Assert.Fail("Failed to load tox data correctly, names don't match");
            }

            if (tox2.StatusMessage != tox1.StatusMessage)
            {
                Assert.Fail("Failed to load tox data correctly, status messages don't match");
            }

            tox1.Dispose();
            tox2.Dispose();
        }
Beispiel #2
0
        public void Init()
        {
            if (!File.Exists(DataPath))
            {
                Tox = new Tox(ToxOptions.Default);

                Tox.GetData().Save(DataPath);
            }
            else
            {
                Tox = new Tox(ToxOptions.Default, ToxData.FromDisk(DataPath));
            }

            foreach (var node in Nodes)
            {
                Tox.Bootstrap(node);
            }

            BindEvents();
            PopulateList();

            Tox.Start();

            Tox.StatusMessage = "Toxing on Detox";
        }
Beispiel #3
0
        public void TestToxLoadData()
        {
            var tox1 = new Tox(ToxOptions.Default);

            tox1.Name          = "Test";
            tox1.StatusMessage = "Hey";

            var data = tox1.GetData();
            var tox2 = new Tox(ToxOptions.Default, ToxData.FromBytes(data.Bytes));

            if (tox2.Id != tox1.Id)
            {
                Assert.Fail("Failed to load tox data correctly, tox id's don't match");
            }

            if (tox2.Name != tox1.Name)
            {
                Assert.Fail("Failed to load tox data correctly, names don't match");
            }

            if (tox2.StatusMessage != tox1.StatusMessage)
            {
                Assert.Fail("Failed to load tox data correctly, status messages don't match");
            }

            tox1.Dispose();
            tox2.Dispose();
        }
Beispiel #4
0
        public LoginViewModel()
        {
            var profiles = new SourceList <IProfileViewModel>();

            this.WhenAnyValue(x => x.UserProfilesDirectory)
            .Where(x => Directory.Exists(x))
            .Select(x => Directory.EnumerateFiles(x, "*.tox").AsObservableChangeSet())
            .Switch()
            .Transform(x => (IProfileViewModel) new ExistingUserProfileViewModel(x, ToxData.FromDisk(x)))
            .PopulateInto(profiles);

            ReadOnlyObservableCollection <IProfileViewModel> all;

            profiles.Connect()
            .Bind(out all)
            .Subscribe();

            this.Profiles = all;

            this.CreateNew = ReactiveCommand.Create <IProfileViewModel>(() => new NewUserProfileViewModel(this.UserProfilesDirectory),
                                                                        this.WhenAnyValue(x => x.UserProfilesDirectory, (string s) => Directory.Exists(s)));

            this.WhenAnyObservable(x => x.CreateNew)
            .BindTo(this, x => x.SelectedProfile);
        }
Beispiel #5
0
        private ProfileViewModel(IDataService dataService, ToxData toxData, ToxDataInfo toxDataInfo)
        {
            _toxModel      = dataService.ToxModel;
            _avatarManager = dataService.AvatarManager;

            _toxData     = toxData;
            _toxDataInfo = toxDataInfo;
        }
Beispiel #6
0
        public async Task RestoreDataAsync()
        {
            try
            {
                var currentUserPublicKey = ApplicationData.Current.RoamingSettings.Values["currentUserPublicKey"];
                var file = await ApplicationData.Current.RoamingFolder.GetFileAsync(currentUserPublicKey + ".tox");

                var toxData = (await FileIO.ReadBufferAsync(file)).ToArray();
                SetCurrent(new ExtendedTox(new ToxOptions(true, true), ToxData.FromBytes(toxData)));
            }
            catch
            {
                // TODO: Exception handling!
            }
        }
Beispiel #7
0
        public static async Task <ProfileViewModel> GetProfileViewModelFromFile(IDataService dataService,
                                                                                StorageFile file)
        {
            var data    = (await FileIO.ReadBufferAsync(file)).ToArray();
            var toxData = ToxData.FromBytes(data);

            ToxDataInfo toxDataInfo;

            toxData.TryParse(out toxDataInfo);
            if (toxDataInfo == null)
            {
                return(null);
            }

            return(new ProfileViewModel(dataService, toxData, toxDataInfo));
        }
Beispiel #8
0
        public NewUserProfileViewModel(string directory)
        {
            this.WhenAnyValue(x => x.Password)
            .Select(x => !string.IsNullOrEmpty(x))
            .ToPropertyEx(this, x => x.Encrypted);

            this.Login = ReactiveCommand.Create(() =>
            {
                var filePath = Path.Combine(directory, this.Name + ".tox");
                var tox      = new Tox(ToxOptions.Default());
                if (this.Encrypted)
                {
                    var data = tox.GetData().Bytes;
                    ToxEncryption.Encrypt(data, this.Password, out _);
                    var toxdata = ToxData.FromBytes(data);
                    tox.Dispose();
                    toxdata.Save(filePath);

                    tox = new Tox(ToxOptions.Default(), toxdata, this.Password);
                }

                return(new ToxSession(tox, filePath));
            }, this.WhenAnyValue(x => x.Name)
                                                .Select(fileName =>
            {
                if (string.IsNullOrWhiteSpace(fileName))
                {
                    return(false);
                }

                var filePath = Path.Combine(directory, fileName + ".tox");
                if (File.Exists(filePath))
                {
                    return(false);
                }

                if ((from i in Path.GetInvalidFileNameChars()
                     from c in fileName
                     select i == c).Any(x => x))
                {
                    return(false);
                }

                return(true);
            }));
        }
Beispiel #9
0
        public ExistingUserProfileViewModel(string filePath, ToxData data)
        {
            this.FilePath = filePath ?? throw new ArgumentNullException(nameof(filePath));
            this.Data     = data ?? throw new ArgumentNullException(nameof(data));

            this.Login = ReactiveCommand.Create(() =>
            {
                Tox tox = null;
                if (this.Encrypted)
                {
                    tox = new Tox(ToxOptions.Default(), this.Data, this.Password);
                }
                else
                {
                    tox = new Tox(ToxOptions.Default(), this.Data);
                }

                return(new ToxSession(tox, filePath));
            });
        }
Beispiel #10
0
        public Skynet(string filename = "")
        {
            // init tox client
            ToxOptions options = new ToxOptions(true, true);

            if (filename != "")
            {
                tox = new Tox(options, ToxData.FromDisk(filename));
            }
            else
            {
                tox = new Tox(options);
            }

            tox.OnFriendRequestReceived         += tox_OnFriendRequestReceived;
            tox.OnFriendLosslessPacketReceived  += tox_OnFriendLosslessPacketReceived;
            tox.OnFriendConnectionStatusChanged += tox_OnFriendConnectionStatusChanged;

            foreach (ToxNode node in Nodes)
            {
                tox.Bootstrap(node);
            }

            tox.Name          = "Skynet";
            tox.StatusMessage = "Running Skynet";
            tox.Start();

            string id = tox.Id.ToString();

            Console.WriteLine("ID: {0}", id);
            Utils.Utils.Log("ID: " + id, true);

            // Log tox online status
            Task.Factory.StartNew(async() =>
            {
                var offLineCount = 0;
                while (true)
                {
                    Thread.Sleep(2000);
                    if (tox.IsConnected)
                    {
                        Console.WriteLine("From Server " + httpPort + ":" + "tox is connected.");
                        Utils.Utils.Log("From Server " + httpPort + ":" + "tox is connected.", true);
                        Utils.Utils.WriteNodeInfo(tox.Id.ToString(), true);
                        offLineCount = 0;
                        break;
                    }
                    else
                    {
                        Utils.Utils.Log("Event: tox is offline", true);
                        offLineCount++;
                    }
                    if (offLineCount > 10)
                    {
                        // start a new tox node
                        offLineCount = 0;
                        tox.Stop();
                        options = new ToxOptions(true, true);
                        if (filename != "")
                        {
                            tox = new Tox(options, ToxData.FromDisk(filename));
                        }
                        else
                        {
                            tox = new Tox(options);
                        }

                        tox.OnFriendRequestReceived         += tox_OnFriendRequestReceived;
                        tox.OnFriendLosslessPacketReceived  += tox_OnFriendLosslessPacketReceived;
                        tox.OnFriendConnectionStatusChanged += tox_OnFriendConnectionStatusChanged;

                        foreach (ToxNode node in Nodes)
                        {
                            tox.Bootstrap(node);
                        }

                        tox.Name          = "Skynet";
                        tox.StatusMessage = "Running Skynet";
                        tox.Start();

                        id = tox.Id.ToString();
                        Console.WriteLine("ID: {0}", id);
                        Console.WriteLine("Start a new Tox node");
                        Utils.Utils.Log("ID: " + id, true);
                    }
                }

                bool onlineStatus = true;
                while (true)
                {
                    // start queue process
                    while (tox.IsConnected)
                    {
                        if (!onlineStatus)
                        {
                            onlineStatus = true;
                            Utils.Utils.Log("Event: tox is online");
                        }
                        processFriendMessage();
                    }
                    Utils.Utils.Log("Event: tox is offline", true);
                    onlineStatus = false;
                    Thread.Sleep(1000);
                }
            }, TaskCreationOptions.LongRunning).ForgetOrThrow();

            // start http server
            httpPort = Utils.Utils.FreeTcpPort();
            string baseUrl = "http://localhost:" + httpPort + "/";

            //WebApp.Start<StartUp> (url: baseUrl);
            Console.WriteLine("Server listening on " + httpPort);
            Utils.Utils.Log("Server listening on " + httpPort, true);
            allInstance.Add(this);
        }
Beispiel #11
0
        public void SwitchTo(ProfileInfo profile)
        {
            var options = ToxOptions.Default;

            options.Ipv6Enabled = Config.Instance.EnableIpv6;

            if (Config.Instance.ProxyType != ToxProxyType.None)
            {
                options.UdpEnabled = false;
                options.ProxyType  = Config.Instance.ProxyType;
                options.ProxyHost  = Config.Instance.ProxyAddress;
                options.ProxyPort  = Config.Instance.ProxyPort;
            }
            else
            {
                options.UdpEnabled = Config.Instance.EnableUdp;
            }

            Tox newTox;

            if (profile != null)
            {
                var data = ToxData.FromDisk(profile.Path);
                if (data == null)
                {
                    throw new Exception("Could not load profile.");
                }

                if (data.IsEncrypted)
                {
                    throw new Exception("Data is encrypted, Toxy does not support encrypted profiles yet.");
                }

                newTox = new Tox(options, data);
            }
            else
            {
                newTox = new Tox(options);
            }

            var newToxAv = new ToxAv(newTox);

            InitManagers(newTox, newToxAv);

            if (Tox != null)
            {
                Tox.Dispose();
            }

            if (ToxAv != null)
            {
                ToxAv.Dispose();
            }

            Tox   = newTox;
            ToxAv = newToxAv;

            AvatarManager.Rehash();
            ConnectionManager.DoBootstrap();

            //TODO: move this someplace else and make it configurable
            if (string.IsNullOrEmpty(Tox.Name))
            {
                Tox.Name = "Tox User";
            }
            if (string.IsNullOrEmpty(Tox.StatusMessage))
            {
                Tox.StatusMessage = "Toxing on Toxy";
            }

            Tox.Start();
            ToxAv.Start();

            CurrentProfile = profile;
            MainWindow.Instance.Reload();
        }
Beispiel #12
0
        public void SwitchTo(ProfileInfo profile)
        {
            if (profile == null)
            {
                throw new ArgumentNullException("profile");
            }

            ToxOptions options = ToxOptions.Default;

            options.Ipv6Enabled = Config.Instance.EnableIpv6;
            options.UdpEnabled  = Config.Instance.EnableUdp;

            ToxData data = ToxData.FromDisk(profile.Path);

            if (data == null)
            {
                throw new Exception("Could not load profile.");
            }

            Tox   tox   = new Tox(options, data);
            ToxAv toxAv = new ToxAv(tox);

            try
            {
                this.InitManagers(tox, toxAv);
            }
            catch
            {
                toxAv.Dispose();
                tox.Dispose();
                throw;
            }

            if (this.Tox != null)
            {
                this.Tox.Dispose();
                this.Tox = null;
            }

            if (this.ToxAv != null)
            {
                this.ToxAv.Dispose();
                this.ToxAv = null;
            }

            this.Tox   = tox;
            this.ToxAv = toxAv;

            this.Tox.OnFriendRequestReceived         += this.OnToxFriendRequestReceived;
            this.Tox.OnFriendMessageReceived         += this.OnToxFriendMessageReceived;
            this.Tox.OnFriendNameChanged             += this.OnToxFriendNameChanged;
            this.Tox.OnFriendStatusMessageChanged    += this.OnToxFriendStatusMessageChanged;
            this.Tox.OnFriendStatusChanged           += this.OnToxFriendStatusChanged;
            this.Tox.OnFriendTypingChanged           += this.OnToxFriendTypingChanged;
            this.Tox.OnFriendConnectionStatusChanged += this.OnToxFriendConnectionStatusChanged;
            this.Tox.OnReadReceiptReceived           += this.OnToxReadReceiptReceived;

            this.Tox.OnFriendLossyPacketReceived    += this.OnToxFriendLossyPacketReceived;
            this.Tox.OnFriendLosslessPacketReceived += this.OnToxFriendLosslessPacketReceived;

            this.Tox.OnGroupInvite         += this.OnToxGroupInvite;
            this.Tox.OnGroupAction         += this.OnToxGroupAction;
            this.Tox.OnGroupMessage        += this.OnToxGroupMessage;
            this.Tox.OnGroupNamelistChange += this.OnToxGroupNamelistChange;
            this.Tox.OnGroupTitleChanged   += this.OnToxGroupTitleChanged;

            this.Tox.OnFileSendRequestReceived += this.OnToxFileSendRequestReceived;
            this.Tox.OnFileControlReceived     += this.OnToxFileControlReceived;
            this.Tox.OnFileChunkReceived       += this.OnToxFileChunkReceived;
            this.Tox.OnFileChunkRequested      += this.OnToxFileChunkRequested;

            this.CurrentProfile = profile;
        }
Beispiel #13
0
        public Skynet(string filename = "")
        {
            // init tox client
            ToxOptions options = new ToxOptions(true, true);

            if (filename != "")
            {
                tox = new Tox(options, ToxData.FromDisk(filename));
            }
            else
            {
                tox = new Tox(options);
            }



            tox.OnFriendRequestReceived         += tox_OnFriendRequestReceived;
            tox.OnFriendLosslessPacketReceived  += tox_OnFriendLosslessPacketReceived;
            tox.OnFriendConnectionStatusChanged += tox_OnFriendConnectionStatusChanged;

            foreach (ToxNode node in Nodes)
            {
                tox.Bootstrap(node);
            }

            tox.Name          = "Skynet";
            tox.StatusMessage = "Running Skynet";
            tox.Start();

            string id = tox.Id.ToString();

            Console.WriteLine("ID: {0}", id);
            Utils.Utils.LogUtils("ID: " + id);

            // Log tox online status
            Task.Factory.StartNew(async() => {
                var offLineCount = 0;
                while (true)
                {
                    Thread.Sleep(2000);
                    if (tox.IsConnected)
                    {
                        Console.WriteLine("From Server " + httpPort + ":" + "tox is connected.");
                        Utils.Utils.LogUtils("From Server " + httpPort + ":" + "tox is connected.");
                        offLineCount = 0;
                        // send a online message to server
                        using (var client = new HttpClient()){
                            await client.PostAsJsonAsync("http://xiaoqiang.bwbot.org/online", tox.Id.ToString());
                        }
                        break;
                    }
                    else
                    {
                        Utils.Utils.LogUtils("Event: tox is offline");
                        offLineCount++;
                    }
                    if (offLineCount > 10)
                    {
                        // start a new tox node
                        offLineCount = 0;
                        tox.Dispose();
                        options = new ToxOptions(true, true);
                        if (filename != "")
                        {
                            tox = new Tox(options, ToxData.FromDisk(filename));
                        }
                        else
                        {
                            tox = new Tox(options);
                        }

                        tox.OnFriendRequestReceived         += tox_OnFriendRequestReceived;
                        tox.OnFriendLosslessPacketReceived  += tox_OnFriendLosslessPacketReceived;
                        tox.OnFriendConnectionStatusChanged += tox_OnFriendConnectionStatusChanged;

                        foreach (ToxNode node in Nodes)
                        {
                            tox.Bootstrap(node);
                        }

                        tox.Name          = "Skynet";
                        tox.StatusMessage = "Running Skynet";
                        tox.Start();

                        id = tox.Id.ToString();
                        Console.WriteLine("ID: {0}", id);
                        Utils.Utils.LogUtils("ID: " + id);
                    }
                }

                while (true)
                {
                    // start queue process
                    while (tox.IsConnected)
                    {
                        Package processPack = null;
                        lock (reqQueueLock) {
                            if (reqQueue.Count > 0)
                            {
                                processPack = reqQueue.Dequeue();
                            }
                        }
                        if (processPack != null)
                        {
                            newReqReceived(processPack);
                        }
                        else
                        {
                            Thread.Sleep(1);
                        }
                    }
                    Utils.Utils.LogUtils("Event: tox is offline");
                    Thread.Sleep(1000);
                }
            }, TaskCreationOptions.LongRunning).ForgetOrThrow();



            // start http server
            httpPort = Utils.Utils.FreeTcpPort();
            string baseUrl = "http://localhost:" + httpPort + "/";

            WebApp.Start <StartUp> (url: baseUrl);
            Console.WriteLine("Server listening on " + httpPort);
            Utils.Utils.LogUtils("Server listening on " + httpPort);
            allInstance.Add(this);
        }
Beispiel #14
0
 public ExtendedTox(ToxOptions options, ToxData data) :
     base(options, data)
 {
 }