Beispiel #1
0
        /// <summary>
        /// IDatabase.Authenticate implementation for MySQL driver
        /// </summary>
        public Task <Plugin.AuthenticationInformation> Authenticate(Plugin.IDatabaseConnection clientImpl, string username, string password)
        {
            VirtualConnection connection = clientImpl as VirtualConnection;

            var tableResult = connection
                              .Command("SELECT acc.id, acc.username, acc.email, acc.balance, subs.subscriptionType, subs.paidDate FROM `accounts` AS acc JOIN `subscriptions` AS subs ON acc.id = subs.accountId WHERE acc.username = @username AND acc.password = MD5(CONCAT(@password, acc.salt)) AND subs.paid = TRUE ORDER BY subs.paidDate DESC LIMIT 0,1;")
                              .SetParameter("@username", username)
                              .SetParameter("@password", password)
                              .ReadTable();

            connection.IsBusy = false;

            // return default authentication information (success = false)
            if (tableResult.Rows.Count == 0)
            {
                return(Task.FromResult(new Plugin.AuthenticationInformation(false)));
            }
            else
            {
                var    row = tableResult.Rows[0];
                string subscriptionTypeName = row["subscriptionType"] as string;
                subscriptionTypeName = subscriptionTypeName[0].ToString().ToUpper() + subscriptionTypeName.Substring(1);
                return(Task.FromResult(
                           new Plugin.AuthenticationInformation(
                               (long)(int)row["id"],
                               row["username"] as string,
                               row["email"] as string,
                               double.Parse(row["balance"].ToString(), System.Globalization.CultureInfo.GetCultureInfo("en-US")),
                               (Plugin.SubscriptionType)Enum.Parse(typeof(Plugin.SubscriptionType), subscriptionTypeName),
                               ((DateTime)row["paidDate"]).AddDays(30)
                               )
                           ));
            }
        }
Beispiel #2
0
        private async void _processCommits()
        {
            while (true)
            {
                this._commitReset.WaitOne();
                this._commitReset.Reset();

                if (this._commitInfo.Count > 0)
                {
                    Program programInstance = Program.GetInstance();

                    Plugin.IDatabaseConnection connection = await programInstance.Database.GetConnection();

                    using (Plugin.IDatabaseTransaction transaction = connection.OpenTransaction())
                    {
                        programInstance.Log.DebugFormat("Processing commit info, {0} commits", this._commitInfo.Count);
                        int totalQueries = 0;

                        for (int i = this._commitInfo.Count - 1; i >= 0; i--)
                        {
                            Commit.Info info = this._commitInfo[i];

                            if (info.Stats.HasValue)
                            {
                                await programInstance.Database.CommitStats(connection, info.AccountId, info.Stats.Value.WiredTx, info.Stats.Value.WiredRx);

                                totalQueries++;
                            }
                            if (info.Command.HasValue)
                            {
                                await programInstance.Database.CommitCommand(connection, info.AccountId, info.Command.Value.SocksCommand, info.Command.Value.ClientEndPoint, info.Command.Value.ProxyEndPoint, info.Command.Value.WiredTx, info.Command.Value.WiredRx, info.Command.Value.CommandTime, info.Command.Value.Success);

                                totalQueries++;
                            }

                            programInstance.Log.DebugFormat("Pci > stats: {0}, command: {1} for {2}", info.Stats.HasValue, info.Command.HasValue, info.AccountId);

                            this._commitInfo.RemoveAt(i);
                        }

                        programInstance.Log.DebugFormat("Processing commit info, commiting {0} queries to database impl", totalQueries);
                        transaction.Commit();
                    }

                    connection.IsBusy = false;
                }

                this._commitReset.Set();
                Thread.Sleep(2500); // process all 2.5 seconds
            }
        }
Beispiel #3
0
        /// <summary>
        /// IDatabase.CommitStats implementation for MySQL driver
        /// </summary>
        public Task <object> CommitStats(Plugin.IDatabaseConnection clientImpl, long accountId, long newTx, long newRx)
        {
            VirtualConnection connection = clientImpl as VirtualConnection;

            connection
            .Command("UPDATE `accounts` AS acc SET acc.totalTx = acc.totalTx + @newTx, acc.totalRx = acc.totalRx + @newRx WHERE acc.id = @accountId;")
            .SetParameter("@newTx", newTx)
            .SetParameter("@newRx", newRx)
            .SetParameter("@accountId", accountId)
            .Execute();

            connection.IsBusy = false;
            return(Task.FromResult <object>(null));
        }
Beispiel #4
0
        /// <summary>
        /// IDatabase.CommitConnection implementation for MySQL driver
        /// </summary>
        public Task <object> CommitCommand(Plugin.IDatabaseConnection clientImpl, long accountId, Socks.Constants.Command command, System.Net.IPEndPoint clientEndPoint, System.Net.IPEndPoint proxyEndPoint, long wiredTx, long wiredRx, DateTime commandTime, bool success)
        {
            VirtualConnection connection = clientImpl as VirtualConnection;

            uint sourceAddr = BitConverter.ToUInt32(clientEndPoint.Address.GetAddressBytes(), 0);
            uint targetAddr = BitConverter.ToUInt32(proxyEndPoint.Address.GetAddressBytes(), 0);

            connection
            .Command("INSERT INTO `commands` (`accountId`, `commandType`, `sourceAddr`, `sourcePort`, `targetAddr`, `targetPort`, `wiredTx`, `wiredRx`, `timestamp`, `success`) VALUES (@accountId, @command, @sourceAddr, @sourcePort, @targetAddr, @targetPort, @wiredTx, @wiredRx, @commandTime, @success);")
            .SetParameter("@accountId", accountId)
            .SetParameter("@command", command.ToString())
            .SetParameter("@sourceAddr", sourceAddr)
            .SetParameter("@sourcePort", clientEndPoint.Port)
            .SetParameter("@targetAddr", targetAddr)
            .SetParameter("@targetPort", proxyEndPoint.Port)
            .SetParameter("@wiredTx", wiredTx)
            .SetParameter("@wiredRx", wiredRx)
            .SetParameter("@commandTime", commandTime)
            .SetParameter("@success", success)
            .Execute();

            connection.IsBusy = false;
            return(Task.FromResult <object>(null));
        }
Beispiel #5
0
        public async Task <object> CommitCommand(long accountId, Socks.Constants.Command command, System.Net.IPEndPoint clientEndPoint, System.Net.IPEndPoint proxyEndPoint, long wiredTx, long wiredRx, DateTime commandTime, bool success)
        {
            Plugin.IDatabaseConnection connection = await this.GetConnection();

            return(await this.CommitCommand(connection, accountId, command, clientEndPoint, proxyEndPoint, wiredTx, wiredRx, commandTime, success));
        }
Beispiel #6
0
        public async Task <object> CommitStats(long accountId, long newTx, long newRx)
        {
            Plugin.IDatabaseConnection connection = await this.GetConnection();

            return(await this.CommitStats(connection, accountId, newTx, newRx));
        }
Beispiel #7
0
        public async Task <Plugin.AuthenticationInformation> Authenticate(string username, string password)
        {
            Plugin.IDatabaseConnection connection = await this.GetConnection();

            return(await this.Authenticate(connection, username, password));
        }