Пример #1
0
        public void PackAuthData(byte[] data, int datalength, byte[] outdata, out int outlength)
        {
            const int authhead_len = 4 + 8 + 4 + 16 + 4;
            var       encrypt      = new byte[24];

            if (Server.data is AuthDataAesChain authData)
            {
                lock (authData)
                {
                    if (authData.connectionID > 0xFF000000)
                    {
                        authData.clientID = null;
                    }

                    if (authData.clientID == null)
                    {
                        authData.clientID = new byte[4];
                        g_random.GetBytes(authData.clientID);
                        authData.connectionID = (uint)BitConverter.ToInt32(authData.clientID, 0) % 0xFFFFFD;
                    }

                    authData.connectionID += 1;
                    Array.Copy(authData.clientID, 0, encrypt, 4, 4);
                    Array.Copy(BitConverter.GetBytes(authData.connectionID), 0, encrypt, 8, 4);
                }
            }

            outlength = authhead_len;
            var encrypt_data = new byte[32];
            var key          = new byte[Server.Iv.Length + Server.key.Length];

            Server.Iv.CopyTo(key, 0);
            Server.key.CopyTo(key, Server.Iv.Length);

            var utc_time_second = (ulong)Math.Floor(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds);
            var utc_time        = (uint)utc_time_second;

            Array.Copy(BitConverter.GetBytes(utc_time), 0, encrypt, 0, 4);

            encrypt[12]  = (byte)Server.overhead;
            encrypt[13]  = (byte)(Server.overhead >> 8);
            send_tcp_mss = 1024; //random.Next(1024) + 400;
            recv_tcp_mss = send_tcp_mss;
            encrypt[14]  = (byte)send_tcp_mss;
            encrypt[15]  = (byte)(send_tcp_mss >> 8);

            // first 12 bytes
            {
                var rnd = new byte[4];
                random.NextBytes(rnd);
                rnd.CopyTo(outdata, 0);
                var md5     = CreateHMAC(key);
                var md5data = md5.ComputeHash(rnd, 0, rnd.Length);
                last_client_hash = md5data;
                Array.Copy(md5data, 0, outdata, rnd.Length, 8);
            }
            // uid & 16 bytes auth data
            {
                var uid            = new byte[4];
                var index_of_split = Server.param.IndexOf(':');
                if (index_of_split > 0)
                {
                    try
                    {
                        var user = uint.Parse(Server.param.Substring(0, index_of_split));
                        user_key = System.Text.Encoding.UTF8.GetBytes(Server.param.Substring(index_of_split + 1));
                        BitConverter.GetBytes(user).CopyTo(uid, 0);
                    }
                    catch (Exception ex)
                    {
                        Logging.Log(LogLevel.Warn, $"Faild to parse auth param, fallback to basic mode. {ex}");
                    }
                }
                if (user_key == null)
                {
                    random.NextBytes(uid);
                    user_key = Server.key;
                }
                for (var i = 0; i < 4; ++i)
                {
                    uid[i] ^= last_client_hash[8 + i];
                }

                var encrypt_key = user_key;

                var streamEncryptor = (StreamEncryptor)EncryptorFactory.GetEncryptor("aes-128-cbc", Convert.ToBase64String(encrypt_key) + SALT);

                streamEncryptor.SetIV(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
                streamEncryptor.Encrypt(encrypt, 16, encrypt_data, out _);
                streamEncryptor.Dispose();
                Array.Copy(encrypt_data, 0, encrypt, 4, 16);
                uid.CopyTo(encrypt, 0);
            }
            // final HMAC
            {
                var md5     = CreateHMAC(user_key);
                var md5data = md5.ComputeHash(encrypt, 0, 20);
                last_server_hash = md5data;
                Array.Copy(md5data, 0, encrypt, 20, 4);
            }
            encrypt.CopyTo(outdata, 12);
            encryptor = (StreamEncryptor)EncryptorFactory.GetEncryptor("chacha20", Convert.ToBase64String(user_key) + Convert.ToBase64String(last_client_hash, 0, 16));
            {
                var iv = new byte[8];
                Array.Copy(last_client_hash, iv, 8);
                encryptor.SetIV(iv);
            }
            {
                encryptor.Decrypt(last_server_hash, 8, outdata, out _);
            }

            // combine first chunk
            {
                var pack_outdata = new byte[outdata.Length];
                PackData(data, datalength, pack_outdata, out var pack_outlength);
                Array.Copy(pack_outdata, 0, outdata, outlength, pack_outlength);
                outlength += pack_outlength;
            }
        }
Пример #2
0
        public override byte[] ClientUdpPreEncrypt(byte[] plaindata, int datalength, out int outlength)
        {
            var outdata = new byte[datalength + 1024];

            if (user_key == null)
            {
                user_id = new byte[4];
                var index_of_split = Server.param.IndexOf(':');
                if (index_of_split > 0)
                {
                    try
                    {
                        var user = uint.Parse(Server.param.Substring(0, index_of_split));
                        user_key = System.Text.Encoding.UTF8.GetBytes(Server.param.Substring(index_of_split + 1));
                        BitConverter.GetBytes(user).CopyTo(user_id, 0);
                    }
                    catch (Exception ex)
                    {
                        Logging.Log(LogLevel.Warn, $"Faild to parse auth param, fallback to basic mode. {ex}");
                    }
                }
                if (user_key == null)
                {
                    random.NextBytes(user_id);
                    user_key = Server.key;
                }
            }
            var auth_data = new byte[3];

            random.NextBytes(auth_data);

            var md5       = CreateHMAC(Server.key);
            var md5data   = md5.ComputeHash(auth_data, 0, auth_data.Length);
            var rand_len  = UdpGetRandLen(random_client, md5data);
            var rand_data = new byte[rand_len];

            random.NextBytes(rand_data);
            outlength = datalength + rand_len + 8;
            encryptor = (StreamEncryptor)EncryptorFactory.GetEncryptor("chacha20", Convert.ToBase64String(user_key) + Convert.ToBase64String(md5data, 0, 16));
            {
                var iv = new byte[8];
                Array.Copy(Server.key, iv, 8);
                encryptor.SetIV(iv);
            }
            encryptor.Encrypt(plaindata, datalength, outdata, out datalength);
            rand_data.CopyTo(outdata, datalength);
            auth_data.CopyTo(outdata, outlength - 8);
            var uid = new byte[4];

            for (var i = 0; i < 4; ++i)
            {
                uid[i] = (byte)(user_id[i] ^ md5data[i]);
            }
            uid.CopyTo(outdata, outlength - 5);
            {
                md5     = CreateHMAC(user_key);
                md5data = md5.ComputeHash(outdata, 0, outlength - 1);
                Array.Copy(md5data, 0, outdata, outlength - 1, 1);
            }
            return(outdata);
        }
Пример #3
0
        public void PackAuthData(byte[] data, int datalength, byte[] outdata, out int outlength)
        {
            const int authhead_len = 4 + 8 + 4 + 16 + 4;

            byte[]           encrypt  = new byte[24];
            AuthDataAesChain authData = this.Server.data as AuthDataAesChain;

            lock (authData)
            {
                if (authData.connectionID > 0xFF000000)
                {
                    authData.clientID = null;
                }
                if (authData.clientID == null)
                {
                    authData.clientID = new byte[4];
                    g_random.GetBytes(authData.clientID);
                    authData.connectionID = (UInt32)BitConverter.ToInt32(authData.clientID, 0) % 0xFFFFFD;
                }
                authData.connectionID += 1;
                Array.Copy(authData.clientID, 0, encrypt, 4, 4);
                Array.Copy(BitConverter.GetBytes(authData.connectionID), 0, encrypt, 8, 4);
            }

            outlength = authhead_len;
            byte[] encrypt_data = new byte[32];
            byte[] key          = new byte[Server.iv.Length + Server.key.Length];
            Server.iv.CopyTo(key, 0);
            Server.key.CopyTo(key, Server.iv.Length);

            UInt64 utc_time_second = (UInt64)Math.Floor(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds);
            UInt32 utc_time        = (UInt32)(utc_time_second);

            Array.Copy(BitConverter.GetBytes(utc_time), 0, encrypt, 0, 4);

            encrypt[12] = (byte)(Server.overhead);
            encrypt[13] = (byte)(Server.overhead >> 8);

            // first 12 bytes
            {
                byte[] rnd = new byte[4];
                random.NextBytes(rnd);
                rnd.CopyTo(outdata, 0);
                MbedTLS.HMAC md5     = CreateHMAC(key);
                byte[]       md5data = md5.ComputeHash(rnd, 0, rnd.Length);
                last_client_hash = md5data;
                Array.Copy(md5data, 0, outdata, rnd.Length, 8);
            }
            // uid & 16 bytes auth data
            {
                byte[] uid            = new byte[4];
                int    index_of_split = Server.param.IndexOf(':');
                if (index_of_split > 0)
                {
                    try
                    {
                        uint user = uint.Parse(Server.param.Substring(0, index_of_split));
                        user_key = System.Text.Encoding.UTF8.GetBytes(Server.param.Substring(index_of_split + 1));
                        BitConverter.GetBytes(user).CopyTo(uid, 0);
                    }
                    catch (Exception ex)
                    {
                        Logging.Log(LogLevel.Warn, $"Faild to parse auth param, fallback to basic mode. {ex}");
                    }
                }
                if (user_key == null)
                {
                    random.NextBytes(uid);
                    user_key = Server.key;
                }
                for (int i = 0; i < 4; ++i)
                {
                    uid[i] ^= last_client_hash[8 + i];
                }

                byte[] encrypt_key = user_key;

                Encryption.IEncryptor encryptor = Encryption.EncryptorFactory.GetEncryptor("aes-128-cbc", System.Convert.ToBase64String(encrypt_key) + SALT);
                int enc_outlen;

                encryptor.SetIV(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
                encryptor.Encrypt(encrypt, 16, encrypt_data, out enc_outlen);
                encryptor.Dispose();
                Array.Copy(encrypt_data, 16, encrypt, 4, 16);
                uid.CopyTo(encrypt, 0);
            }
            // final HMAC
            {
                MbedTLS.HMAC md5     = CreateHMAC(user_key);
                byte[]       md5data = md5.ComputeHash(encrypt, 0, 20);
                last_server_hash = md5data;
                Array.Copy(md5data, 0, encrypt, 20, 4);
            }
            encrypt.CopyTo(outdata, 12);
            encryptor = EncryptorFactory.GetEncryptor("rc4", System.Convert.ToBase64String(user_key) + System.Convert.ToBase64String(last_client_hash, 0, 16));

            // combine first chunk
            {
                byte[] pack_outdata = new byte[outdata.Length];
                int    pack_outlength;
                PackData(data, datalength, pack_outdata, out pack_outlength);
                Array.Copy(pack_outdata, 0, outdata, outlength, pack_outlength);
                outlength += pack_outlength;
            }
        }
Пример #4
0
 public void CreateEncryptor(string method, string password)
 {
     _encryptor = EncryptorFactory.GetEncryptor(method, password);
     _method    = method;
     _password  = password;
 }
Пример #5
0
        public ConfigForm(ShadowsocksController controller, UpdateChecker updateChecker, int focusIndex)
        {
            this.Font = System.Drawing.SystemFonts.MessageBoxFont;
            InitializeComponent();
            ServersListBox.Font = CreateFont();

            NumServerPort.Minimum = IPEndPoint.MinPort;
            NumServerPort.Maximum = IPEndPoint.MaxPort;
            NumUDPPort.Minimum    = IPEndPoint.MinPort;
            NumUDPPort.Maximum    = IPEndPoint.MaxPort;

            this.Icon          = Icon.FromHandle(Resources.ssw128.GetHicon());
            this.controller    = controller;
            this.updateChecker = updateChecker;
            if (updateChecker.LatestVersionURL == null)
            {
                LinkUpdate.Visible = false;
            }

            foreach (string name in EncryptorFactory.GetEncryptor())
            {
                EncryptorInfo info = EncryptorFactory.GetEncryptorInfo(name);
                if (info.display)
                {
                    EncryptionSelect.Items.Add(name);
                }
            }
            UpdateTexts();
            controller.ConfigChanged += controller_ConfigChanged;

            LoadCurrentConfiguration();
            if (_modifiedConfiguration.index >= 0 && _modifiedConfiguration.index < _modifiedConfiguration.configs.Count)
            {
                _oldSelectedID = _modifiedConfiguration.configs[_modifiedConfiguration.index].id;
            }
            if (focusIndex == -1)
            {
                int index = _modifiedConfiguration.index + 1;
                if (index < 0 || index > _modifiedConfiguration.configs.Count)
                {
                    index = _modifiedConfiguration.configs.Count;
                }

                focusIndex = index;
            }

            if (_modifiedConfiguration.isHideTips)
            {
                PictureQRcode.Visible = false;
            }

            int dpi_mul = Util.Utils.GetDpiMul();

            //ServersListBox.Height = ServersListBox.Height * 4 / dpi_mul;
            ServersListBox.Width = ServersListBox.Width * dpi_mul / 4;
            //ServersListBox.Height = ServersListBox.Height * dpi_mul / 4;
            ServersListBox.Height = checkAdvSetting.Top + checkAdvSetting.Height;
            AddButton.Width       = AddButton.Width * dpi_mul / 4;
            AddButton.Height      = AddButton.Height * dpi_mul / 4;
            DeleteButton.Width    = DeleteButton.Width * dpi_mul / 4;
            DeleteButton.Height   = DeleteButton.Height * dpi_mul / 4;
            UpButton.Width        = UpButton.Width * dpi_mul / 4;
            UpButton.Height       = UpButton.Height * dpi_mul / 4;
            DownButton.Width      = DownButton.Width * dpi_mul / 4;
            DownButton.Height     = DownButton.Height * dpi_mul / 4;

            //IPTextBox.Width = IPTextBox.Width * dpi_mul / 4;
            //ServerPortNumericUpDown.Width = ServerPortNumericUpDown.Width * dpi_mul / 4;
            //PasswordTextBox.Width = PasswordTextBox.Width * dpi_mul / 4;
            //EncryptionSelect.Width = EncryptionSelect.Width * dpi_mul / 4;
            //TCPProtocolComboBox.Width = TCPProtocolComboBox.Width * dpi_mul / 4;
            //ObfsCombo.Width = ObfsCombo.Width * dpi_mul / 4;
            //TextObfsParam.Width = TextObfsParam.Width * dpi_mul / 4;
            //RemarksTextBox.Width = RemarksTextBox.Width * dpi_mul / 4;
            //TextGroup.Width = TextGroup.Width * dpi_mul / 4;
            //TextLink.Width = TextLink.Width * dpi_mul / 4;
            //TextUDPPort.Width = TextUDPPort.Width * dpi_mul / 4;

            //int font_height = 9;
            //EncryptionSelect.Height = EncryptionSelect.Height - font_height + font_height * dpi_mul / 4;
            //TCPProtocolComboBox.Height = TCPProtocolComboBox.Height - font_height + font_height * dpi_mul / 4;
            //ObfsCombo.Height = ObfsCombo.Height - font_height + font_height * dpi_mul / 4;

            //OKButton.Width = OKButton.Width * dpi_mul / 4;
            OKButton.Height = OKButton.Height * dpi_mul / 4;
            //MyCancelButton.Width = MyCancelButton.Width * dpi_mul / 4;
            MyCancelButton.Height = MyCancelButton.Height * dpi_mul / 4;

            DrawLogo(350 * dpi_mul / 4);
            //DrawLogo(350);

            ShowWindow();

            if (focusIndex >= 0 && focusIndex < _modifiedConfiguration.configs.Count)
            {
                SetServerListSelectedIndex(focusIndex);
                LoadSelectedServer();
            }
        }
Пример #6
0
            public async Task Start(byte[] data, int length)
            {
                Interlocked.Exchange(ref _state, _running);
                SaeaAwaitable udpSaea = null;

                try
                {
                    while (IsRunning)
                    {
                        IEncryptor encryptor = EncryptorFactory.GetEncryptor(_server.method, _server.password);
                        byte[]     dataIn    = new byte[length - 3];
                        Array.Copy(data, 3, dataIn, 0, length - 3);
                        udpSaea = _argsPool.Rent();

                        int outlen;
                        encryptor.EncryptUDP(dataIn, length - 3, udpSaea.Saea.Buffer, out outlen);
                        udpSaea.Saea.SetBuffer(0, outlen);
                        udpSaea.Saea.RemoteEndPoint = _remoteEndPoint;

                        Logging.Debug(_localEndPoint, _remoteEndPoint, outlen, "UDP Relay");
                        var ret = await _remote.SendToAsync(udpSaea);

                        if (ret != SocketError.Success)
                        {
                            Close();
                            return;
                        }
                        udpSaea = _argsPool.Rent();
                        udpSaea.Saea.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
                        ret = await _remote.ReceiveFromAsync(udpSaea);

                        if (ret != SocketError.Success)
                        {
                            Close();
                            return;
                        }
                        var bytesReceived = udpSaea.Saea.BytesTransferred;
                        Logging.Debug($"++++++Receive Server Port, size:" + bytesReceived);


                        byte[] dataOut = new byte[bytesReceived];
                        encryptor = EncryptorFactory.GetEncryptor(_server.method, _server.password);
                        encryptor.DecryptUDP(udpSaea.Saea.Buffer, bytesReceived, dataOut, out outlen);
                        _argsPool.Return(udpSaea);
                        udpSaea = null;

                        udpSaea = _argsPool.Rent();
                        byte[] buf = udpSaea.Saea.Buffer;
                        buf[0] = buf[1] = buf[2] = 0;
                        Array.Copy(dataOut, 0, buf, 3, outlen);
                        udpSaea.Saea.RemoteEndPoint = _localEndPoint;
                        udpSaea.Saea.SetBuffer(0, outlen + 3);
                        Logging.Debug(_localEndPoint, _remoteEndPoint, outlen, "UDP Relay");
                        ret = await _local.SendToAsync(udpSaea);

                        if (ret != SocketError.Success)
                        {
                            Close();
                            return;
                        }
                        _argsPool.Return(udpSaea);
                        udpSaea = null;
                    }
                }
                catch (Exception e)
                {
                    Logging.LogUsefulException(e);
                    Close();
                }
                finally
                {
                    _argsPool.Return(udpSaea);
                    udpSaea = null;
                }
            }
Пример #7
0
        public void PackAuthData(byte[] data, int datalength, byte[] outdata, out int outlength)
        {
            const int authhead_len = 7 + 4 + 16 + 4;
            const int overhead     = authhead_len + 4;

            byte[]         encrypt  = new byte[24];
            AuthDataAes128 authData = Server.data as AuthDataAes128;

            lock (authData)
            {
                if (authData.connectionID > 0xFF000000)
                {
                    authData.clientID = null;
                }
                if (authData.clientID == null)
                {
                    authData.clientID = new byte[4];
                    g_random.GetBytes(authData.clientID);
                    authData.connectionID = (uint)BitConverter.ToInt32(authData.clientID, 0) % 0xFFFFFD;
                }
                authData.connectionID += 1;
                Array.Copy(authData.clientID, 0, encrypt, 4, 4);
                Array.Copy(BitConverter.GetBytes(authData.connectionID), 0, encrypt, 8, 4);

                StatisticsInit(authData);
            }

#if !PROTOCOL_STATISTICS
            int rand_len = TrapezoidRandomInt(Server.tcp_mss - datalength - overhead + 1, -0.3); //(datalength > 400 ? LinearRandomInt(512) : LinearRandomInt(1024));
#else
            int rand_len = GenRandLenFull(datalength + overhead, datalength, false) - datalength - overhead;
#endif
            int data_offset = rand_len + authhead_len;
            outlength = data_offset + datalength + 4;
            byte[] encrypt_data = new byte[32];
            byte[] key          = new byte[Server.iv.Length + Server.key.Length];
            Server.iv.CopyTo(key, 0);
            Server.key.CopyTo(key, Server.iv.Length);

            {
                byte[] rnd_data = new byte[rand_len];
                random.NextBytes(rnd_data);
                rnd_data.CopyTo(outdata, data_offset - rand_len);
            }

            ulong utc_time_second = (ulong)Math.Floor(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds);
            uint  utc_time        = (uint)(utc_time_second);
            Array.Copy(BitConverter.GetBytes(utc_time), 0, encrypt, 0, 4);
            encrypt[12] = (byte)(outlength);
            encrypt[13] = (byte)(outlength >> 8);
            encrypt[14] = (byte)(rand_len);
            encrypt[15] = (byte)(rand_len >> 8);

            {
                byte[] uid            = new byte[4];
                int    index_of_split = Server.param.IndexOf(':');
                if (index_of_split > 0)
                {
                    try
                    {
                        uint user = uint.Parse(Server.param.Substring(0, index_of_split));
                        user_key = hash(System.Text.Encoding.UTF8.GetBytes(Server.param.Substring(index_of_split + 1)));
                        BitConverter.GetBytes(user).CopyTo(uid, 0);
                    }
                    catch (Exception ex)
                    {
                        Logging.Log(LogLevel.Warn, $"Faild to parse auth param, fallback to basic mode. {ex}");
                    }
                }
                if (user_key == null)
                {
                    random.NextBytes(uid);
                    user_key = Server.key;
                }

                byte[] encrypt_key = user_key;

                var encryptor = (StreamEncryptor)EncryptorFactory.GetEncryptor("aes-128-cbc", Convert.ToBase64String(encrypt_key) + SALT);
                int enc_outlen;

                encryptor.SetIV(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
                encryptor.Encrypt(encrypt, 16, encrypt_data, out enc_outlen);
                encryptor.Dispose();
                Array.Copy(encrypt_data, 0, encrypt, 4, 16);
                uid.CopyTo(encrypt, 0);
            }
            {
                MbedTLS.HMAC sha1     = CreateHMAC(key);
                byte[]       sha1data = sha1.ComputeHash(encrypt, 0, 20);
                Array.Copy(sha1data, 0, encrypt, 20, 4);
            }
            {
                byte[] rnd = new byte[1];
                random.NextBytes(rnd);
                rnd.CopyTo(outdata, 0);
                MbedTLS.HMAC sha1     = CreateHMAC(key);
                byte[]       sha1data = sha1.ComputeHash(rnd, 0, rnd.Length);
                Array.Copy(sha1data, 0, outdata, rnd.Length, 7 - rnd.Length);
            }
            encrypt.CopyTo(outdata, 7);
            Array.Copy(data, 0, outdata, data_offset, datalength);

            {
                MbedTLS.HMAC sha1     = CreateHMAC(user_key);
                byte[]       sha1data = sha1.ComputeHash(outdata, 0, outlength - 4);
                Array.Copy(sha1data, 0, outdata, outlength - 4, 4);
            }
        }
        public static void Save(Configuration config)
        {
            if (config.index < 0 || config.index >= config.Servers.Count)
            {
                int newindex = config.Servers.FindIndex(t => t.enable);
                if (newindex != -1)
                {
                    config.index = newindex;
                }
                else
                {
                    config.index = 0;
                }
            }
            try
            {
                string jsonString = SimpleJson.SimpleJson.SerializeObject(config);
                if (GlobalConfiguration.config_password.Length > 0)
                {
                    IEncryptor encryptor   = EncryptorFactory.GetEncryptor("aes-256-cfb", GlobalConfiguration.config_password, false);
                    byte[]     cfg_data    = UTF8Encoding.UTF8.GetBytes(jsonString);
                    byte[]     cfg_encrypt = new byte[cfg_data.Length + 128];
                    int        data_len    = 0;
                    const int  buffer_size = 32768;
                    byte[]     input       = new byte[buffer_size];
                    byte[]     ouput       = new byte[buffer_size + 128];
                    for (int start_pos = 0; start_pos < cfg_data.Length; start_pos += buffer_size)
                    {
                        int len = Math.Min(cfg_data.Length - start_pos, buffer_size);
                        int out_len;
                        Buffer.BlockCopy(cfg_data, start_pos, input, 0, len);
                        encryptor.Encrypt(input, len, ouput, out out_len);
                        Buffer.BlockCopy(ouput, 0, cfg_encrypt, data_len, out_len);
                        data_len += out_len;
                    }
                    jsonString = System.Convert.ToBase64String(cfg_encrypt, 0, data_len);
                }
                using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create)))
                {
                    sw.Write(jsonString);
                    sw.Flush();
                }

                if (File.Exists(CONFIG_FILE_BACKUP))
                {
                    DateTime dt  = File.GetLastWriteTimeUtc(CONFIG_FILE_BACKUP);
                    DateTime now = DateTime.Now;
                    if ((now - dt).TotalHours > 4)
                    {
                        File.Copy(CONFIG_FILE, CONFIG_FILE_BACKUP, true);
                    }
                }
                else
                {
                    File.Copy(CONFIG_FILE, CONFIG_FILE_BACKUP, true);
                }
            }
            catch (IOException e)
            {
                Console.Error.WriteLine(e);
            }
        }
Пример #9
0
        public ConfigForm(ShadowsocksController controller, UpdateChecker updateChecker, int focusIndex)
        {
            this.Font = System.Drawing.SystemFonts.MessageBoxFont;
            InitializeComponent();

            this.Icon          = Icon.FromHandle(Resources.ssw128.GetHicon());
            this.controller    = controller;
            this.updateChecker = updateChecker;
            if (updateChecker.LatestVersionURL == null)
            {
                LinkUpdate.Visible = false;
            }

            foreach (string name in EncryptorFactory.GetEncryptor())
            {
                EncryptorInfo info = EncryptorFactory.GetEncryptorInfo(name);
                if (info.display)
                {
                    EncryptionSelect.Items.Add(name);
                }
            }
            UpdateTexts();
            controller.ConfigChanged += controller_ConfigChanged;

            LoadCurrentConfiguration();
            if (_modifiedConfiguration.index >= 0 && _modifiedConfiguration.index < _modifiedConfiguration.configs.Count)
            {
                _oldSelectedID = _modifiedConfiguration.configs[_modifiedConfiguration.index].id;
            }
            if (focusIndex == -1)
            {
                focusIndex = _modifiedConfiguration.configs.Count - 1;
            }
            if (focusIndex >= 0 && focusIndex < _modifiedConfiguration.configs.Count)
            {
                SetServerListSelectedIndex(focusIndex);
            }

            if (_modifiedConfiguration.isHideTips)
            {
                PictureQRcode.Visible = false;
            }

            int dpi_mul = Util.Utils.GetDpiMul();

            ServersListBox.Width  = ServersListBox.Width * dpi_mul / 4;
            ServersListBox.Height = ServersListBox.Height * dpi_mul / 4;
            AddButton.Width       = AddButton.Width * dpi_mul / 4;
            AddButton.Height      = AddButton.Height * dpi_mul / 4;
            DeleteButton.Width    = DeleteButton.Width * dpi_mul / 4;
            DeleteButton.Height   = DeleteButton.Height * dpi_mul / 4;
            UpButton.Width        = UpButton.Width * dpi_mul / 4;
            UpButton.Height       = UpButton.Height * dpi_mul / 4;
            DownButton.Width      = DownButton.Width * dpi_mul / 4;
            DownButton.Height     = DownButton.Height * dpi_mul / 4;

            //IPTextBox.Width = IPTextBox.Width * dpi_mul / 4;
            //ServerPortTextBox.Width = ServerPortTextBox.Width * dpi_mul / 4;
            //PasswordTextBox.Width = PasswordTextBox.Width * dpi_mul / 4;
            //EncryptionSelect.Width = EncryptionSelect.Width * dpi_mul / 4;
            //TCPProtocolComboBox.Width = TCPProtocolComboBox.Width * dpi_mul / 4;
            //ObfsCombo.Width = ObfsCombo.Width * dpi_mul / 4;
            //TextObfsParam.Width = TextObfsParam.Width * dpi_mul / 4;
            //RemarksTextBox.Width = RemarksTextBox.Width * dpi_mul / 4;
            //TextGroup.Width = TextGroup.Width * dpi_mul / 4;
            //TextLink.Width = TextLink.Width * dpi_mul / 4;
            //TextUDPPort.Width = TextUDPPort.Width * dpi_mul / 4;
            //Font new_font = new Font("Arial", (float)(9.0 * dpi_mul / 4));
            //this.Font = new_font;
            //IPTextBox.Font = new_font;
            //ServerPortTextBox.Font = new_font;
            //PasswordTextBox.Font = new_font;
            //EncryptionSelect.Font = new_font;
            //TCPProtocolComboBox.Font = new_font;
            //ObfsCombo.Font = new_font;
            //TextObfsParam.Font = new_font;
            //RemarksTextBox.Font = new_font;
            //TextGroup.Font = new_font;
            //TextLink.Font = new_font;
            //TextUDPPort.Font = new_font;

            int font_height = 9;

            EncryptionSelect.Height    = EncryptionSelect.Height - font_height + font_height * dpi_mul / 4;
            TCPProtocolComboBox.Height = TCPProtocolComboBox.Height - font_height + font_height * dpi_mul / 4;
            ObfsCombo.Height           = ObfsCombo.Height - font_height + font_height * dpi_mul / 4;

            //MyCancelButton.Height = MyCancelButton.Height * dpi_mul / 4;
            MyCancelButton.Width = MyCancelButton.Width * dpi_mul / 4;
            OKButton.Width       = OKButton.Width * dpi_mul / 4;
            //OKButton.Height = OKButton.Height * dpi_mul / 4;

            GenQR(TextLink.Text);
        }