コード例 #1
0
    // 商用サービス開発者向け: サーバー情報の取得、アクティベーション、アクティベーション解除処理
    public async Task <ThinControllerRpcServerObjectInfo?> Paid_GetOrSetServerObjectInfoAsync(string hostKey, bool?newState = null, string tag = "", CancellationToken cancel = default)
    {
        hostKey = hostKey._NonNullTrim()._NormalizeHexString();

        // データベースエラー時は処理禁止
        if (IsDatabaseConnected == false)
        {
            throw new VpnException(VpnError.ERR_TEMP_ERROR);
        }

        await using var db = await OpenDatabaseForWriteAsync(cancel);

        // この関数は同時に 1 ユーザーからしか実行されないようにする (高負荷防止のため)
        using var asyncLock = await RenamePcidAsyncLock.LockWithAwait(cancel);

        ThinControllerRpcServerObjectInfo?ret = null;

        await db.TranAsync(async() =>
        {
            var machine = await db.EasySelectSingleAsync <ThinDbMachine>("select * from MACHINE where CERT_HASH = @CERT_HASH",
                                                                         new
            {
                CERT_HASH = hostKey,
            }, false, true, cancel);

            if (machine == null)
            {
                return(false);
            }

            var oldStatus = this.Controller.Paid_CalcServerLicenseStatus(machine.PCID, machine.JSON_ATTRIBUTES, machine.FIRST_CLIENT_DATE._AsDateTimeOffset(true, true));

            if (newState.HasValue == false || newState.Value == oldStatus.IsCurrentActivated)
            {
                // 状態の変更なし
                ret = oldStatus;
                return(false);
            }

            bool activate = newState.Value;

            EasyJsonStrAttributes json = new EasyJsonStrAttributes(machine.JSON_ATTRIBUTES);

            json["Paid_IsCurrentActivated"] = activate._ToBoolStrLower();
            if (activate)
            {
                json["Paid_ActivatedOnceInPast"] = true._ToBoolStrLower();
                json["Paid_ActivatedDateTime"]   = DtOffsetNow._ToDtStr(true);
            }
            else
            {
                json["Paid_DeactivatedDateTime"] = DtOffsetNow._ToDtStr(true);
            }
            json["Paid_Tag"] = tag;

            string newJsonStr = json.ToString();

            var newStatus = this.Controller.Paid_CalcServerLicenseStatus(machine.PCID, newJsonStr, machine.FIRST_CLIENT_DATE._AsDateTimeOffset(true, true));

            int i = await db.EasyExecuteAsync("update MACHINE set JSON_ATTRIBUTES = @JSON_ATTRIBUTES where CERT_HASH = @CERT_HASH",
                                              new
            {
                JSON_ATTRIBUTES = newJsonStr,
                CERT_HASH       = machine.CERT_HASH,
            });

            if (i != 1)
            {
                throw new CoresLibException("Database update error. i != 1.");
            }

            await this.AddApiLogAsync(db, activate ? "Activate" : "Deactivate",
                                      new
            {
                UniqueId  = machine.CERT_HASH,
                Pcid      = machine.PCID,
                OldStatus = oldStatus,
                NewStatus = newStatus,
            });

            ret = newStatus;

            return(true);
        });

        return(ret);
    }
コード例 #2
0
 public static JObject _NormalizeEasyJsonStrAttributes(this JObject?src, IComparer <string>?comparer = null)
 => EasyJsonStrAttributes.NormalizeJsonObject(src, comparer);