예제 #1
0
        // Create new connection profile
        public void AddProfile(string name, string host, string user, string password)
        {
            if (ProfileExists(name))
            {
                throw new KanpachiProfileException($"Profile {name} already exists.");
            }
            KanpachiProfile profile = new KanpachiProfile(name, host, user);

            try{
                if (GetActiveProfile(true).Name == name)
                {
                    System.Console.WriteLine($"Profile {name} is already active.");
                    return;
                }
            } catch (KanpachiProfileException) {
                profile.IsActive = true; // no active profile, so set this new one
            }

            if (password.Length > 0)
            {
                profile.PasswordDecrypted = password;
                profile.Password          = SecUtils.EncryptProfile(profile);
            }
            WriteProfile(profile);
            Console.WriteLine($"Added profile {name}.");
        }
예제 #2
0
        // build base download path
        public static string BuildDownloadPath(string downloadPath, KanpachiProfile profile)
        {
            string clientPath = string.Empty;

            // if no argument passed, fallback to download path defined in profile
            if (downloadPath == null || downloadPath.Trim().Length == 0)
            {
                if (profile.DownloadPath == null || profile.DownloadPath.Trim().Length == 0)
                {
                    clientPath = Directory.GetCurrentDirectory();
                }
                else
                {
                    clientPath = profile.DownloadPath;
                }
            }
            else
            {
                clientPath = Path.GetFullPath(downloadPath);
            }

            if (!Directory.Exists(clientPath))
            {
                Directory.CreateDirectory(clientPath);
            }
            return(clientPath);
        }
예제 #3
0
        // set profile value
        public void SetValue(string key, string value)
        {
            KanpachiProfile original = GetActiveProfile();
            KanpachiProfile updated  = ClientUtils.CopyProfile(original);

            // skip Name
            switch (key.ToUpper())
            {
            case "HOST":
                updated.Host = value;
                break;

            case "USER":
                updated.User = value;
                break;

            case "PASSWORD":
                updated.PasswordDecrypted = value;
                break;

            case "PORT":
                int parsePort;
                if (!Int32.TryParse(value, out parsePort))
                {
                    throw new KanpachiConfigException($"Could not parse value {value} to Int32.");
                }
                updated.Port = parsePort;
                break;

            case "TIMEOUT":
                double parseTimeout;
                if (!Double.TryParse(value, out parseTimeout))
                {
                    throw new KanpachiConfigException($"Could not parse value {value} to Double.");
                }
                updated.Timeout = Double.Parse(value);
                break;

            case "CONNECTATTEMPTS":
                int parseConnectAttempts;
                if (!Int32.TryParse(value, out parseConnectAttempts))
                {
                    throw new KanpachiConfigException($"Could not parse value {value} to Int32.");
                }
                updated.Port = parseConnectAttempts;
                break;

            case "IFSUSERPATH":
                updated.IfsUserPath = value;
                break;

            case "ODBCDRIVER":
                updated.OdbcDriver = value;
                break;

            default:
                throw new KanpachiConfigException("Could not find key {key}");
            }
            WriteProfile(updated);
        }
예제 #4
0
        // List profiles present in local cache
        public void ListProfiles()
        {
            Console.WriteLine($"Profile configurations located at {ProfilesPath}\n");

            foreach (var f in Directory.GetFiles(ProfilesPath, "*.json"))
            {
                string[]        splitPath = f.Split(Path.DirectorySeparatorChar);
                KanpachiProfile profile   = ReadProfile(splitPath[splitPath.Length - 1].Split('.')[0]);
                Console.WriteLine((profile.IsActive ? "*": "") + $"{profile.Name}");
            }
        }
예제 #5
0
 // Write profile to json file
 public void WriteProfile(KanpachiProfile profile)
 {
     if (!Directory.Exists(ProfilesPath))
     {
         Directory.CreateDirectory(ProfilesPath);
     }
     using (StreamWriter f = File.CreateText(GetProfilePath(profile.Name))){
         if (profile.PasswordDecrypted != null && profile.PasswordDecrypted.Length > 0)
         {
             profile.Password = SecUtils.EncryptProfile(profile);
         }
         f.Write(JsonConvert.SerializeObject(profile, Formatting.Indented, new JsonSerializerSettings {
             ContractResolver = new CamelCasePropertyNamesContractResolver()
         }));
     }
 }
예제 #6
0
        // Set active profile from profiles in local cache
        public void SetActiveProfile(string nextActive)
        {
            try{
                KanpachiProfile lastProfile = GetActiveProfile(true);
                lastProfile.IsActive = false;
                WriteProfile(lastProfile);
            } catch (KanpachiProfileException) {
                Console.WriteLine("No active profile was found.");
            }
            KanpachiProfile nextProfile = ReadProfile(nextActive);

            nextProfile.IsActive = true;
            WriteProfile(nextProfile);

            Console.WriteLine($"Set active profile to {nextActive}");
        }
예제 #7
0
        public static KanpachiProfile CopyProfile(KanpachiProfile profile)
        {
            var copy = new KanpachiProfile();

            copy.Name              = profile.Name;
            copy.Host              = profile.Host;
            copy.User              = profile.User;
            copy.Password          = profile.Password;
            copy.PasswordDecrypted = profile.PasswordDecrypted;
            copy.Port              = profile.Port;
            copy.Timeout           = profile.Timeout;
            copy.ConnectAttempts   = profile.ConnectAttempts;
            copy.DownloadPath      = profile.DownloadPath;
            copy.IfsUserPath       = profile.IfsUserPath;
            copy.OdbcDriver        = profile.OdbcDriver;
            return(copy);
        }
예제 #8
0
        // get connection profile value
        public void GetValue(string key)
        {
            KanpachiProfile active = GetActiveProfile(true);
            string          v      = string.Empty;

            // Skip password
            switch (key.ToUpper())
            {
            case "NAME":
                v = active.Name;
                break;

            case "HOST":
                v = active.Host;
                break;

            case "USER":
                v = active.User;
                break;

            case "PORT":
                v = active.Port.ToString();
                break;

            case "TIMEOUT":
                v = active.Timeout.ToString();
                break;

            case "CONNECTATTEMPTS":
                v = active.ConnectAttempts.ToString();
                break;

            case "IFSUSERPATH":
                v = active.IfsUserPath;
                break;

            case "ODBCDRIVER":
                v = active.OdbcDriver;
                break;

            default:
                throw new KanpachiConfigException("Could not find key {key}");
            }
            Console.WriteLine(v);
        }
예제 #9
0
        public KanpachiClient(KanpachiProfile profile)
        {
            Profile = profile;

            SshClient = new SshClient(profile.Host, profile.Port, profile.User, profile.PasswordDecrypted);
            SshClient.ConnectionInfo.Timeout = TimeSpan.FromSeconds(profile.Timeout);

            SftpClient = new SftpClient(profile.Host, profile.Port, profile.User, profile.PasswordDecrypted);
            SftpClient.ConnectionInfo.Timeout = TimeSpan.FromSeconds(profile.Timeout);

            string connStr = "Driver={" + profile.OdbcDriver + "};" +
                             $"System={profile.Host};Uid={profile.User};Pwd={profile.PasswordDecrypted};NAM=1;DBQ=,*USRLIBL;";

            Db2Client = new OdbcConnection(connStr);
            Db2Client.ConnectionTimeout = (int)Math.Round(profile.Timeout);

            IfsCache = $"{Profile.IfsUserPath}/.kanpachi";
            Connect();
        }
예제 #10
0
        // Get active profile from local cache
        public KanpachiProfile GetActiveProfile(bool skipDecryption = false)
        {
            foreach (var filename in Directory.GetFiles(ProfilesPath, "*.json"))
            {
                using (StreamReader f = File.OpenText(filename)){
                    JsonSerializer  serializer = new JsonSerializer();
                    KanpachiProfile profile    = (KanpachiProfile)serializer.Deserialize(f, typeof(KanpachiProfile));

                    if (profile.IsActive)
                    {
                        if (!skipDecryption)
                        {
                            profile.PasswordDecrypted = SecUtils.DecryptProfile(profile);
                        }
                        return(profile);
                    }
                }
            }
            throw new KanpachiProfileException("Could not find an active profile.");
        }
예제 #11
0
        public static string EncryptProfile(KanpachiProfile profile)
        {
            var passphrase = BuildPassphrase(profile);

            return(Convert.ToBase64String(EncryptAes(passphrase, profile.PasswordDecrypted)));
        }
예제 #12
0
 public static string DecryptProfile(KanpachiProfile profile)
 {
     return(DecryptAes(BuildPassphrase(profile), profile.Password));
 }
예제 #13
0
 // build simple passphrase for encryption
 private static string BuildPassphrase(KanpachiProfile profile)
 {
     return($"{profile.Name}+{profile.Host}+{profile.User}");
 }
예제 #14
0
 public QsysService(KanpachiProfile profile)
 {
     Profile = profile;
 }
예제 #15
0
 public ExecService(KanpachiProfile profile)
 {
     Profile = profile;
 }