/// <summary> /// Delete VPN Config /// </summary> /// <param name="hostname"></param> /// <param name="username"></param> public void RemoveVPN(string hostname, string username) { // count exist record int nCount = 0; for (int i = 0; i < this.VPNs.Length; i++) { if (this.VPNs[i].Hostname == hostname && this.VPNs[i].Username == username) { nCount++; } } // remove exist record if (nCount != 0) { VPN[] VPNs = new VPN[this.VPNs.Length - nCount]; int n = 0; for (int i = 0; i < this.VPNs.Length; i++) { if (this.VPNs[i].Hostname != hostname || this.VPNs[i].Username != username) { VPNs[n] = this.VPNs[i]; n++; } } this.VPNs = VPNs; } // save change WriteConfig(); }
/// <summary> /// Change VPN Index /// </summary> /// <param name="hostname"></param> /// <param name="username"></param> /// <param name="newindex"></param> public void ChangeVPNIndex(string hostname, string username, int newindex) { // search vpn int oldindex = -1; for (int i = 0; i < this.VPNs.Length; i++) { if (this.VPNs[i].Hostname == hostname && this.VPNs[i].Username == username) { oldindex = i; } } // change index if (oldindex >= 0) { VPN vpn = this.VPNs[oldindex]; newindex = Math.Min(Math.Max(newindex, 0), this.VPNs.Length - 1); if (oldindex < newindex) { for (int i = oldindex; i < newindex; i++) this.VPNs[i] = this.VPNs[i + 1]; this.VPNs[newindex] = vpn; } else if (oldindex > newindex) { for (int i = oldindex; i > newindex; i--) this.VPNs[i] = this.VPNs[i - 1]; this.VPNs[newindex] = vpn; } } // save change WriteConfig(); }
/// <summary> /// load config from config file /// </summary> private void ReadConfig() { // read data file.Seek(0, SeekOrigin.Begin); byte[] byteAll = new byte[file.Length]; file.Read(byteAll, 0, (int)file.Length); // it's .. not safe ... mark here // check signature byteAll = DesignArray(byteAll); // decrypt data byteAll = DecryptArray(byteAll); // parse data if (byteAll.Length >= ADAPTER_MAX_LENGTH) { // entry name byte[] byteAdapter = new byte[ADAPTER_MAX_LENGTH]; for (int i = 0; i < byteAdapter.Length; i++) { byteAdapter[i] = byteAll[i]; } this._EntryName = System.Text.Encoding.Default.GetString(byteAdapter).Trim(new char[] { '\r', '\n', ' ', '\t', '\0' }); // accounts VPN[] VPNs = new VPN[(byteAll.Length - ADAPTER_MAX_LENGTH) / (HOSTNAME_MAX_LENGTH + USERNAME_MAX_LENGTH + PASSWORD_MAX_LENGTH)]; for (int i = 0; i < VPNs.Length; i++) { int offset = ADAPTER_MAX_LENGTH + i * (HOSTNAME_MAX_LENGTH + USERNAME_MAX_LENGTH + PASSWORD_MAX_LENGTH); byte[] byteHostname = new byte[HOSTNAME_MAX_LENGTH]; byte[] byteUsername = new byte[USERNAME_MAX_LENGTH]; byte[] bytePassword = new byte[PASSWORD_MAX_LENGTH]; for (int j = 0; j < HOSTNAME_MAX_LENGTH; j++) byteHostname[j] = byteAll[offset + j]; for (int j = 0; j < USERNAME_MAX_LENGTH; j++) byteUsername[j] = byteAll[offset + HOSTNAME_MAX_LENGTH + j]; for (int j = 0; j < PASSWORD_MAX_LENGTH; j++) bytePassword[j] = byteAll[offset + HOSTNAME_MAX_LENGTH + PASSWORD_MAX_LENGTH + j]; VPNs[i].Hostname = System.Text.Encoding.Default.GetString(byteHostname).Trim(new char[] { '\r', '\n', ' ', '\t', '\0' }); VPNs[i].Username = System.Text.Encoding.Default.GetString(byteUsername).Trim(new char[] { '\r', '\n', ' ', '\t', '\0' }); VPNs[i].Password = System.Text.Encoding.Default.GetString(bytePassword).Trim(new char[] { '\r', '\n', ' ', '\t', '\0' }); } this.VPNs = VPNs; } }
/// <summary> /// Add or update one vpn config. /// </summary> /// <param name="hostname"></param> /// <param name="username"></param> /// <param name="password"></param> public void SetupVPN(string hostname, string username, string password) { // if exist for (int i = 0; i < this.VPNs.Length; i++) { if (this.VPNs[i].Hostname == hostname && this.VPNs[i].Username == username) { this.VPNs[i].Password = password; return; } } // else append record VPN[] VPNs = new VPN[this.VPNs.Length + 1]; this.VPNs.CopyTo(VPNs, 0); VPNs[VPNs.Length - 1].Hostname = hostname; VPNs[VPNs.Length - 1].Username = username; VPNs[VPNs.Length - 1].Password = password; this.VPNs = VPNs; // save change WriteConfig(); }