예제 #1
0
    public async Task <string> WideClientGetWoLMacList(string pcid, CancellationToken cancel = default)
    {
        Pack r = new Pack();

        r.AddStr("SvcName", Options.SvcName);
        r.AddStr("Pcid", pcid);
        r.AddSInt("Ver", CoresConfig.WtcConfig.PseudoVer);
        r.AddSInt("Build", CoresConfig.WtcConfig.PseudoBuild);
        r.AddData("ClientId", Options.ClientId);

        var p = await WtWpcCall("ClientGetWoLMacList", r, cancel);

        p.ThrowIfError();

        string list = p["wol_maclist"].StrValueNonNull;

        if (list._IsEmpty())
        {
            throw new VpnException(VpnError.ERR_WOL_TARGET_NOT_ENABLED);
        }

        return(list);
    }
예제 #2
0
    async Task <WtConnectParam> WideClientConnectInnerAsync(string pcid, WideTunnelClientOptions clientOptions, bool noCache, CancellationToken cancel = default)
    {
        if (noCache == false)
        {
            WtConnectParam?cached = ConnectParamCache[pcid];
            if (cached != null)
            {
                var ret = cached._CloneDeep();
                ret.CacheUsed = true;
                return(ret);
            }
        }

        Pack r = new Pack();

        r.AddStr("SvcName", Options.SvcName);
        r.AddStr("Pcid", pcid);
        r.AddSInt("Ver", CoresConfig.WtcConfig.PseudoVer);
        r.AddSInt("Build", CoresConfig.WtcConfig.PseudoBuild);
        r.AddInt("ClientOptions", (uint)clientOptions.Flags);
        r.AddData("ClientId", Options.ClientId);

        var p = await WtWpcCall("ClientConnect", r, cancel);

        p.ThrowIfError();

        WtConnectParam c = new WtConnectParam
        {
            HostName                    = p["Hostname"].StrValueNonNullCheck,
            HostNameForProxy            = p["HostNameForProxy"].StrValueNonNullCheck,
            Port                        = p["Port"].SIntValue,
            SessionId                   = p["SessionId"].DataValueNonNull.ToArray(),
            ServerMask64                = p["ServerMask64"].Int64Value,
            WebSocketWildCardDomainName = p["WebSocketWildCardDomainName"].StrValueNonNull,
            ClientOptions               = clientOptions._CloneDeep(),
            IsStandaloneMode            = p["IsStandaloneMode"].BoolValue,
        };

        if (c.WebSocketWildCardDomainName._IsEmpty())
        {
            throw new CoresLibException("c.WebSocketWildCardDomainName is empty.");
        }

        if (c.HostName._IsSamei("<<!!samehost!!>>"))
        {
            string hostTmp = p["__remote_hostname"].StrValueNonNull;
            int    portTmp = p["__remote_port"].SIntValue;
            if (hostTmp._IsFilled() && portTmp != 0)
            {
                c.HostName = hostTmp;
                c.Port     = portTmp;
            }
        }

        c.CacheUsed = false;

        if (noCache == false)
        {
            WideTunnel.ConnectParamCache[pcid] = c._CloneDeep();
        }

        return(c);
    }
예제 #3
0
    public async Task StartWtcAsync(CancellationToken cancel = default)
    {
        if (Started.IsFirstCall() == false)
        {
            throw new ApplicationException("Already started.");
        }

        LowerStream.ReadTimeout  = CoresConfig.WtcConfig.ConnectingTimeoutMsec;
        LowerStream.WriteTimeout = CoresConfig.WtcConfig.ConnectingTimeoutMsec;

        // シグネチャをアップロードする
        HttpHeader h = new HttpHeader("POST", "/widetunnel/connect.cgi", "HTTP/1.1");

        h.ValueList.Add("Content-Type", "image/jpeg");
        h.ValueList.Add("Connection", "Keep-Alive");

        // 透かしデータのアップロード
        int randSize = Util.RandSInt31() % 2000;
        MemoryBuffer <byte> water = new MemoryBuffer <byte>();

        water.Write(this.WideTunnel.Options.WaterMark);
        water.Write(Util.Rand(randSize));
        await h.PostHttpAsync(LowerStream, water, cancel);

        // Hello パケットのダウンロード
        var hello = await LowerStream._HttpClientRecvPackAsync(cancel);

        if (hello["hello"].BoolValue == false)
        {
            throw new CoresLibException("Hello packet recv error.");
        }

        // 接続パラメータの送信
        var p = new Pack();

        p.AddData("client_id", WideTunnel.Options.ClientId);
        p.AddStr("method", "connect_session");
        p.AddBool("use_compress", false);
        p.AddData("session_id", this.Options.ConnectParam.SessionId);
        p.AddSInt("ver", CoresConfig.WtcConfig.PseudoVer);
        p.AddSInt("build", CoresConfig.WtcConfig.PseudoBuild);
        p.AddStr("name_suite", this.WideTunnel.Options.NameSuite);
        p.AddBool("support_timeout_param", true);

        var clientOptions = Options.ConnectParam.ClientOptions;

        if (clientOptions.RealClientIp._IsFilled())
        {
            p.AddBool("is_trusted", true);
            p.AddStr("trusted_real_client_ip", clientOptions.RealClientIp._NonNullTrim());
            p.AddStr("trusted_real_client_fqdn", clientOptions.RealClientFqdn._NonNullTrim());
            p.AddInt("trusted_real_client_port", (uint)clientOptions.RealClientPort);

            string auth_str = $"{clientOptions.RealClientIp._NonNullTrim()}/{clientOptions.RealClientFqdn._NonNullTrim()}/{(uint)clientOptions.RealClientPort}/{this.WideTunnel.Options.ControllerGateSecretKey}";
            p.AddData("trusted_auth_sha1", auth_str._HashSHA1());
        }

        await LowerStream._HttpClientSendPackAsync(p, cancel);

        // 結果の受信
        p = await LowerStream._HttpClientRecvPackAsync(cancel);

        int      code = p["code"].SIntValue;
        VpnError err  = (VpnError)code;

        err.ThrowIfError(p);

        int  tunnelTimeout              = Consts.WideTunnelConsts.TunnelTimeout;
        int  tunnelKeepalive            = Consts.WideTunnelConsts.TunnelKeepAlive;
        bool tunnelUseAggressiveTimeout = false;

        int  tunnelTimeout2              = p["tunnel_timeout"].SIntValue;
        int  tunnelKeepAlive2            = p["tunnel_keepalive"].SIntValue;
        bool tunnelUseAggressiveTimeout2 = p["tunnel_use_aggressive_timeout"].BoolValue;

        if (tunnelTimeout2 >= 1 && tunnelKeepAlive2 >= 1)
        {
            tunnelTimeout              = tunnelTimeout2;
            tunnelKeepalive            = tunnelKeepAlive2;
            tunnelUseAggressiveTimeout = tunnelUseAggressiveTimeout2;
        }

        this.WebSocketUrl = p["websocket_url"].StrValue !;
        if (this.WebSocketUrl._IsEmpty())
        {
            throw new CoresLibException("This version of the destination ThinGate has no WebSocket capability.");
        }

        LowerStream.ReadTimeout  = tunnelTimeout;
        LowerStream.WriteTimeout = Timeout.Infinite;

        this.SendTask = SendLoopAsync(tunnelKeepalive)._LeakCheck();
        this.RecvTask = RecvLoopAsync()._LeakCheck();
    }