Example #1
0
        public byte[] ToBytes()
        {
            byte[] result;
            using (var bs = new VgcApis.Libs.Streams.BitStream())
            {
                bs.Clear();

                var writeString = Utils.GenWriteStringHelper(bs, strTable);

                bs.Write(alias);
                writeString(description);

                bs.Write(tlsType);
                bs.Write(isSecTls);
                bs.Write(tlsServName);

                bs.Write(port);
                bs.Write(encryption);
                bs.Write(uuid);
                bs.Write(flow);
                bs.WriteAddress(address);
                writeString(streamType);
                writeString(streamParam1);
                writeString(streamParam2);
                writeString(streamParam3);

                result = bs.ToBytes(version);
            }

            return(result);
        }
Example #2
0
        public byte[] ToBytes()
        {
            byte[] result;
            using (var bs = new VgcApis.Libs.Streams.BitStream())
            {
                var writeString = Utils.GenWriteStringHelper(bs, strTable);

                bs.Clear();

                bs.Write(alias);
                writeString(description);

                bs.WriteAddress(address);
                bs.Write(port);
                bs.Write(password);
                writeString(method);
                bs.WriteTinyInt(networkType, networkTypeLenInBits);
                bs.Write(isUseOta);

                bs.Write(isUseTls);
                writeString(streamType);
                writeString(streamParam1);
                writeString(streamParam2);
                writeString(streamParam3);

                result = bs.ToBytes(version);
            }
            return(result);
        }
Example #3
0
        public Ss1a(byte[] bytes) :
            this()
        {
            var ver = VgcApis.Libs.Streams.BitStream.ReadVersion(bytes);

            if (ver != version)
            {
                throw new NotSupportedException(
                          $"Not supported version ${ver}");
            }

            using (var bs = new VgcApis.Libs.Streams.BitStream(bytes))
            {
                var readString = Utils.GenReadStringHelper(bs, strTable);

                alias       = bs.Read <string>();
                description = readString();

                address     = bs.ReadAddress();
                port        = bs.Read <int>();
                password    = bs.Read <string>();
                method      = readString();
                networkType = bs.ReadTinyInt(networkTypeLenInBits);
                isUseOta    = bs.Read <bool>();

                isUseTls     = bs.Read <bool>();
                streamType   = readString();
                streamParam1 = readString();
                streamParam2 = readString();
                streamParam3 = readString();
            }
        }
Example #4
0
        public void BsNormalTest()
        {
            var bs1  = new VgcApis.Libs.Streams.BitStream();
            var uuid = Guid.NewGuid();

            bs1.Write(true);
            bs1.Write(12345);
            bs1.Write(uuid);
            bs1.WriteAddress("abc.com");
            bs1.WriteAddress("::1");
            bs1.WriteAddress("1.2.3.4");
            bs1.Write("123");
            bs1.Write("1中23文");
            var b1 = bs1.ToBytes("1a");

            bs1.Dispose();
            var bs2 = new VgcApis.Libs.Streams.BitStream(b1);
            var b2  = bs2.ToBytes("1a");

            bs2.Dispose();

            for (int i = 0; i < b1.Length; i++)
            {
                Assert.AreEqual(b2[i], b1[i]);
            }
        }
Example #5
0
        public Socks2a(byte[] bytes) :
            this()
        {
            var ver = VgcApis.Libs.Streams.BitStream.ReadVersion(bytes);

            if (ver != version)
            {
                throw new NotSupportedException($"Not supported version ${ver}");
            }

            using (var bs = new VgcApis.Libs.Streams.BitStream(bytes))
            {
                var readString = Utils.GenReadStringHelper(bs, strTable);

                alias        = bs.Read <string>();
                description  = readString();
                isUseTls     = bs.Read <bool>();
                isSecTls     = bs.Read <bool>();
                port         = bs.Read <int>();
                address      = bs.ReadAddress();
                userName     = readString();
                userPassword = readString();
                streamType   = readString();
                streamParam1 = readString();
                streamParam2 = readString();
                streamParam3 = readString();
            }
        }
Example #6
0
        public Vless4a(byte[] bytes) :
            this()
        {
            var ver = VgcApis.Libs.Streams.BitStream.ReadVersion(bytes);

            if (ver != version)
            {
                throw new NotSupportedException($"Not supported version ${ver}");
            }

            using (var bs = new VgcApis.Libs.Streams.BitStream(bytes))
            {
                var readString = Utils.GenReadStringHelper(bs, strTable);

                alias        = bs.Read <string>();
                description  = readString();
                isUseTls     = bs.Read <bool>();
                isSecTls     = bs.Read <bool>();
                port         = bs.Read <int>();
                encryption   = bs.Read <string>();
                uuid         = bs.Read <Guid>();
                address      = bs.ReadAddress();
                streamType   = readString();
                streamParam1 = readString();
                streamParam2 = readString();
                streamParam3 = readString();
            }

            if (string.IsNullOrEmpty(encryption))
            {
                encryption = "none";
            }
        }
Example #7
0
        public static Func <string> GenReadStringHelper(
            VgcApis.Libs.Streams.BitStream bitStream,
            List <string> strTable)
        {
            var bs    = bitStream;
            var table = strTable;

            return(() => ReadString(bs, table));
        }
Example #8
0
        public static Action <string> GenWriteStringHelper(
            VgcApis.Libs.Streams.BitStream bitStream,
            List <string> strTable)
        {
            var bs    = bitStream;
            var table = strTable;

            return(str => WriteString(bs, table, str));
        }
Example #9
0
        public static string ReadString(
            VgcApis.Libs.Streams.BitStream bitStream,
            List <string> strTable)
        {
            var lenInBits = VgcApis.Misc.Utils.GetLenInBitsOfInt(strTable.Count);

            if (bitStream.Read <bool>())
            {
                // using string table
                var index = bitStream.ReadTinyInt(lenInBits);
                return(strTable[index]);
            }
            else
            {
                return(bitStream.Read <string>());
            }
        }
Example #10
0
        public void Crc8ChecksumFailTest()
        {
            var bs1 = new VgcApis.Libs.Streams.BitStream();

            bs1.Write(12345);
            bs1.WriteAddress("abc.com");

            var b1 = bs1.ToBytes("2b");

            b1[0] = (byte)(b1[0] + 1);
            try
            {
                var b2 = new VgcApis.Libs.Streams.BitStream(b1);
            }
            catch (ArgumentException)
            {
                return;
            }
            Assert.Fail();
        }
Example #11
0
        public static void WriteString(
            VgcApis.Libs.Streams.BitStream bitStream,
            List <string> strTable,
            string str)
        {
            var lenInBits = VgcApis.Misc.Utils.GetLenInBitsOfInt(strTable.Count);

            var index = strTable.IndexOf(str);

            if (index == -1)
            {
                bitStream.Write(false);
                bitStream.Write(str);
            }
            else
            {
                bitStream.Write(true);
                bitStream.WriteTinyInt(index, lenInBits);
            }
        }
Example #12
0
 public BitStreamTests()
 {
     bs = new VgcApis.Libs.Streams.BitStream();
 }