Exemple #1
0
        public EvernoteConnection(AlephLogger log, IWebProxy proxy, EvernoteConfig config, HierarchyEmulationConfig hConfig)
        {
            HConfig = hConfig;

            _config = config;
            _proxy  = proxy;
            _logger = log;
        }
Exemple #2
0
        public SimpleNoteConnection(AlephLogger log, IWebProxy proxy, SimpleNoteConfig config, HierachyEmulationConfig hConfig)
        {
            HConfig = hConfig;

            _config = config;
            _proxy  = proxy;
            _logger = log;
        }
Exemple #3
0
        public static RepoLock Lock(AlephLogger logger, string pathLocalFolder)
        {
            var filename = Path.Combine(pathLocalFolder, ".lock");

            var content =
                $"[[AlephNote::RepoLock::Lockfile]]\n" +
                $"{{\n" +
                $"    ProcessID     := {Process.GetCurrentProcess().Id}\n" +
                $"    ProcessHandle := {Process.GetCurrentProcess().Handle.ToInt64()}\n" +
                $"    StartTime     := {Process.GetCurrentProcess().StartTime:u}\n" +
                $"    FileName      := {Path.GetFileName(Process.GetCurrentProcess().MainModule?.FileName)}\n" +
                $"    FilePath      := {Process.GetCurrentProcess().MainModule?.FileName}\n" +
                $"    ModuleName    := {Process.GetCurrentProcess().MainModule?.ModuleName}\n" +
                $"    ProcessName   := {Process.GetCurrentProcess().ProcessName}\n" +
                $"}}\n";

            try
            {
                if (File.Exists(filename))
                {
                    try
                    {
                        var oldcontent = File.ReadAllText(filename);
                        logger.Warn("RepoLock", "Old Lockfile found", $"File := {filename}\n\nContent:\n{oldcontent}");
                    }
                    catch (Exception)
                    {
                        logger.Warn("RepoLock", "Old Lockfile found (but could not read)", $"File := {filename}");
                    }
                }

                logger.Debug("RepoLock", "Trying to acquire lock", $"File := {filename}\n\nContent:\n{content}");
                var fs    = File.Open(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                var bytes = Encoding.UTF8.GetBytes(content);
                fs.SetLength(0);
                fs.Write(bytes, 0, bytes.Length);
                fs.Flush();
                logger.Debug("RepoLock", "Lock acquired successfully", $"File := {filename}\n\nContent:\n{content}");
                return(new RepoLock(logger, filename, fs));
            }
            catch (Exception e1)
            {
                logger.Error("RepoLock", "Acquiring lock failed", $"File := {filename}\n\nException:\n{e1}");

                try
                {
                    var oldcontent = File.ReadAllText(filename);
                    logger.Info("RepoLock", "Old lock file read", $"File := Content:\n{oldcontent}");
                }
                catch (Exception e2)
                {
                    logger.Error("RepoLock", "Cannot read existing lockfile", e2.ToString());
                }

                throw new RepoLockedException($"Could not open Repository '{pathLocalFolder}'.\nThe repository is currently locked by a different process.", e1);
            }
        }
Exemple #4
0
        public static void Swap(AlephLogger log)
        {
            lock (_lock)
            {
                if (Inst == null)
                {
                    throw new NotSupportedException();
                }

                Inst = log;
            }
        }
        public static void DeleteFileAndFolderIfEmpty(string logsrc, AlephLogger log, string baseFolder, string file)
        {
            var fi = new FileInfo(file);

            if (fi.Exists && fi.IsReadOnly)
            {
                fi.IsReadOnly = false;
            }

            File.Delete(file);

            DeleteFolderIfEmpty(logsrc, log, baseFolder, Path.GetDirectoryName(file));
        }
        public static void DeleteDirectoryWithRetry(AlephLogger logger, string path)
        {
            for (int i = 0; i < 5; i++)
            {
                try
                {
                    Directory.Delete(path);
                    return;
                }
                catch (IOException e)
                {
                    logger.Debug("DeleteDirectoryWithRetry", "Retry Directory delete", "Retry directory delete, exception thrown:\r\n" + e);
                    Thread.Sleep(5);
                }
            }

            Directory.Delete(path);             // Do it again and throw Exception if it fails
        }
        public static void DeleteFolderIfEmpty(string logsrc, AlephLogger log, string baseFolder, string folder)
        {
            var p1 = Path.GetFullPath(baseFolder).TrimEnd(Path.DirectorySeparatorChar).ToLower();
            var p2 = Path.GetFullPath(folder).TrimEnd(Path.DirectorySeparatorChar).ToLower();

            if (p1 == p2)
            {
                return;
            }
            if (p1.Count(c => c == Path.DirectorySeparatorChar) >= p2.Count(c => c == Path.DirectorySeparatorChar))
            {
                return;
            }

            if (Directory.EnumerateFileSystemEntries(folder).Any())
            {
                return;
            }

            log.Debug(logsrc, $"Cleanup empty folder '{p2}' (base = '{p1}')");
            Directory.Delete(folder);
        }
        public static APIResultAuthorize Authenticate(ISimpleJsonRest web, string mail, string password, AlephLogger logger)
        {
            var apiparams = web.Get <APIAuthParams>("auth/params", "email=" + mail);

            if (apiparams.version == "001")
            {
                return(Authenticate001(web, apiparams, mail, password, logger));
            }
            if (apiparams.version == "002")
            {
                return(Authenticate002(web, apiparams, mail, password, logger));
            }
            if (apiparams.version == "003")
            {
                return(Authenticate003(web, apiparams, mail, password, logger));
            }
            if (apiparams.version == "004")
            {
                throw new StandardNoteAPIException("Unsupported encryption scheme 004 in auth-params");
            }
            if (apiparams.version == "005")
            {
                throw new StandardNoteAPIException("Unsupported encryption scheme 005 in auth-params");
            }
            if (apiparams.version == "006")
            {
                throw new StandardNoteAPIException("Unsupported encryption scheme 006 in auth-params");
            }
            if (apiparams.version == "007")
            {
                throw new StandardNoteAPIException("Unsupported encryption scheme 007 in auth-params");
            }
            if (apiparams.version == "008")
            {
                throw new StandardNoteAPIException("Unsupported encryption scheme 008 in auth-params");
            }
            if (apiparams.version == "009")
            {
                throw new StandardNoteAPIException("Unsupported encryption scheme 009 in auth-params");
            }
            if (apiparams.version == "010")
            {
                throw new StandardNoteAPIException("Unsupported encryption scheme 010 in auth-params");
            }

            throw new Exception("Unsupported auth API version: " + apiparams.version);
        }
 public override void Init(AlephLogger logger)
 {
     //
 }
Exemple #10
0
 public FilesystemConnection(AlephLogger log, FilesystemConfig config)
 {
     _config = config;
     _logger = log;
 }
Exemple #11
0
 public override void Init(AlephLogger logger)
 {
     _logger = logger;
 }
Exemple #12
0
        private Tuple <IRemoteStorageSyncPersistance, List <INote>, int, int> DoSync(RemoteStorageAccount acc, AlephLogger log)
        {
            var data = SelectedProvider.CreateEmptyRemoteSyncData();

            var conn = acc.Plugin.CreateRemoteStorageConnection(PluginManagerSingleton.Inst.GetProxyFactory().Build(), acc.Config, new HierarchyEmulationConfig(false, "\\", '\\'));

            var resultNotes  = new ConcurrentQueue <INote>();
            var resultErrors = new ConcurrentQueue <string>();

            int fullCount;

            Application.Current.Dispatcher.Invoke(() => { SyncInfoText = "Connect to remote"; });
            conn.StartSync(data, new List <INote>(), new List <INote>());
            {
                Application.Current.Dispatcher.Invoke(() => { SyncInfoText = "List notes from remote"; });
                var missing = conn.ListMissingNotes(new List <INote>());

                fullCount = missing.Count;

                Application.Current.Dispatcher.Invoke(() =>
                {
                    CanAbort = !acc.Plugin.SupportsNewDownloadMultithreading || missing.Count < AppSettings.DEFAULT_INITIALDOWNLOAD_PARALLELISM_THRESHOLD;
                });

                SynchronizationThread.ExecuteInParallel(
                    log,
                    "InitialDownloadNewNotes",
                    acc.Plugin.SupportsNewDownloadMultithreading,
                    missing,
                    AppSettings.DEFAULT_INITIALDOWNLOAD_PARALLELISM_LEVEL,
                    AppSettings.DEFAULT_INITIALDOWNLOAD_PARALLELISM_THRESHOLD,
                    (e, xnoteid) =>
                {
                    resultErrors.Enqueue(xnoteid);
                    return(true);
                },
                    xnoteid =>
                {
                    var msg = $"Download Note {resultNotes.Count}/{missing.Count}";
                    Application.Current.Dispatcher.Invoke(() => { SyncInfoText = msg; });

                    var note = conn.DownloadNote(xnoteid, out var isnewnote);
                    if (isnewnote)
                    {
                        note.SetLocalDirty("Set Note LocalDirty=true after download in Startupmode");
                        note.ResetRemoteDirty("Set Note RemoteDirty=false after download in Startupmode");
                        resultNotes.Enqueue(note);
                    }
                    else
                    {
                        log.Warn("Sync_FirstStart", $"Download new note {{id:'{xnoteid}'}} returned false");
                    }
                });
            }
            Application.Current.Dispatcher.Invoke(() => { SyncInfoText = "Finish synchronization"; });
            conn.FinishSync(out var _);

            return(Tuple.Create(data, resultNotes.ToList(), resultErrors.Count, fullCount));
        }
 public NextcloudConnection(AlephLogger log, IWebProxy proxy, NextcloudConfig config)
 {
     _config = config;
     _proxy  = proxy;
     _logger = log;
 }
Exemple #14
0
 public override void Init(AlephLogger l)
 {
     logger = l;
 }
 public override void Init(AlephLogger logger)
 {
     _logger = logger;
     SimpleNoteAPI.Logger = logger;
 }
        private static APIResultAuthorize Authenticate003(ISimpleJsonRest web, APIAuthParams apiparams, string mail, string uip, AlephLogger logger)
        {
            try
            {
                logger.Debug(StandardNotePlugin.Name, $"AutParams[version:{apiparams.version}, pw_cost:{apiparams.pw_cost}, pw_nonce:{apiparams.pw_nonce}]");

                if (apiparams.pw_cost < 100000)
                {
                    throw new StandardNoteAPIException($"Account pw_cost is too small ({apiparams.pw_cost})");
                }

                var    salt  = StandardNoteCrypt.SHA256(string.Join(":", mail, "SF", "003", apiparams.pw_cost.ToString(), apiparams.pw_nonce));
                byte[] bytes = PBKDF2.GenerateDerivedKey(768 / 8, Encoding.UTF8.GetBytes(uip), Encoding.UTF8.GetBytes(salt), apiparams.pw_cost, PBKDF2.HMACType.SHA512);

                var pw = bytes.Skip(0 * (bytes.Length / 3)).Take(bytes.Length / 3).ToArray();
                var mk = bytes.Skip(1 * (bytes.Length / 3)).Take(bytes.Length / 3).ToArray();
                var ak = bytes.Skip(2 * (bytes.Length / 3)).Take(bytes.Length / 3).ToArray();

                var reqpw = EncodingConverter.ByteToHexBitFiddleUppercase(pw).ToLower();
                APIResultAuthorize tok;
                try
                {
                    tok = web.PostTwoWay <APIResultAuthorize>(new APIRequestUser {
                        email = mail, password = reqpw
                    }, "auth/sign_in");
                }
                catch (RestStatuscodeException e1)
                {
                    if (e1.StatusCode / 100 == 4 && !string.IsNullOrWhiteSpace(e1.HTTPContent))
                    {
                        var req = web.ParseJsonOrNull <APIBadRequest>(e1.HTTPContent);
                        if (req != null)
                        {
                            throw new StandardNoteAPIException($"Server returned status {e1.StatusCode}.\nMessage: '{req.error.message}'", e1);
                        }
                    }

                    throw;
                }

                tok.masterkey     = mk;
                tok.masterauthkey = ak;
                tok.version       = "003";
                return(tok);
            }
            catch (RestException)
            {
                throw;
            }
            catch (StandardNoteAPIException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new StandardNoteAPIException("Authentification with StandardNoteAPI failed.", e);
            }
        }
        private static APIResultAuthorize Authenticate001(ISimpleJsonRest web, APIAuthParams apiparams, string mail, string uip, AlephLogger logger)
        {
            try
            {
                logger.Debug(StandardNotePlugin.Name, $"AuthParams[version:1, pw_func:{apiparams.pw_func}, pw_alg:{apiparams.pw_alg}, pw_cost:{apiparams.pw_cost}, pw_key_size:{apiparams.pw_key_size}]");

                if (apiparams.pw_func != PasswordFunc.pbkdf2)
                {
                    throw new Exception("Unsupported pw_func: " + apiparams.pw_func);
                }

                byte[] bytes;

                if (apiparams.pw_alg == PasswordAlg.sha512)
                {
                    bytes = PBKDF2.GenerateDerivedKey(apiparams.pw_key_size / 8, Encoding.UTF8.GetBytes(uip), Encoding.UTF8.GetBytes(apiparams.pw_salt), apiparams.pw_cost, PBKDF2.HMACType.SHA512);
                }
                else if (apiparams.pw_alg == PasswordAlg.sha512)
                {
                    bytes = PBKDF2.GenerateDerivedKey(apiparams.pw_key_size / 8, Encoding.UTF8.GetBytes(uip), Encoding.UTF8.GetBytes(apiparams.pw_salt), apiparams.pw_cost, PBKDF2.HMACType.SHA512);
                }
                else
                {
                    throw new Exception("Unknown pw_alg: " + apiparams.pw_alg);
                }

                var pw = bytes.Take(bytes.Length / 2).ToArray();
                var mk = bytes.Skip(bytes.Length / 2).ToArray();

                var reqpw = EncodingConverter.ByteToHexBitFiddleUppercase(pw).ToLower();

                APIResultAuthorize tok;
                try
                {
                    tok = web.PostDownload <APIResultAuthorize>("auth/sign_in", "email=" + mail, "password="******"Server returned status {e1.StatusCode}.\nMessage: '{req.error.message}'", e1);
                        }
                    }

                    throw;
                }

                tok.masterkey = mk;
                tok.version   = "001";
                return(tok);
            }
            catch (StandardNoteAPIException)
            {
                throw;
            }
            catch (RestException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new StandardNoteAPIException("Authentification with StandardNoteAPI failed.", e);
            }
        }
Exemple #18
0
 public abstract void Init(AlephLogger logger);
Exemple #19
0
 public RepoLock(AlephLogger al, string fn, FileStream fl)
 {
     _logger   = al;
     _lockfile = fn;
     _stream   = fl;
 }
        private Tuple <IRemoteStorageSyncPersistance, List <INote> > DoSync(RemoteStorageAccount acc, AlephLogger log)
        {
            var data = SelectedProvider.CreateEmptyRemoteSyncData();

            var conn = acc.Plugin.CreateRemoteStorageConnection(PluginManagerSingleton.Inst.GetProxyFactory().Build(), acc.Config, new HierachyEmulationConfig(false, "\\", '\\'));

            var resultNotes = new List <INote>();

            Application.Current.Dispatcher.Invoke(() => { SyncInfoText = "Connect to remote"; });
            conn.StartSync(data, new List <INote>(), new List <INote>());
            {
                Application.Current.Dispatcher.Invoke(() => { SyncInfoText = "List notes from remote"; });
                var missing = conn.ListMissingNotes(new List <INote>());

                int idx = 0;
                foreach (var xnoteid in missing)
                {
                    var noteid = xnoteid;
                    idx++;

                    try
                    {
                        string msg = $"Download Note {idx}/{missing.Count}";
                        Application.Current.Dispatcher.Invoke(() => { SyncInfoText = msg; });

                        var note = conn.DownloadNote(noteid, out var isnewnote);
                        if (isnewnote)
                        {
                            note.SetLocalDirty("Set Note LocalDirty=true after download in Startupmode");
                            note.ResetRemoteDirty("Set Note RemoteDirty=false after download in Startupmode");
                            resultNotes.Add(note);
                        }
                        else
                        {
                            log.Warn("Sync_FirstStart", string.Format("Download new note {{id:'{0}'}} returned false", noteid));
                        }
                    }
                    catch (ThreadAbortException)
                    {
                        throw;
                    }
                    catch (Exception e)
                    {
                        throw new Exception(string.Format("Could not download new note '{0}' on remote cause of {1}", noteid, e.Message));
                    }
                }
            }
            Application.Current.Dispatcher.Invoke(() => { SyncInfoText = "Finish synchronization"; });
            conn.FinishSync();

            return(Tuple.Create(data, resultNotes));
        }