Ejemplo n.º 1
0
        private async Task <WebSocketServer> HandleWebsocket(HttpConnection p, byte[] realKey, string encType)
        {
            var ws = new WebSocketServer(p);

            ws.AddToManaged(timeout / 2, timeout);
            NaiveProtocol.ApplyEncryption(ws, realKey, encType); // check encryption parameters before http response
            if ((await ws.HandleRequestAsync(false)).IsConnected == false)
            {
                throw new Exception("websocket handshake failed.");
            }
            return(ws);
        }
Ejemplo n.º 2
0
        private async Task ws(HttpConnection p)
        {
            var wss = new WebSocketServer(p);

            wss.AddToManaged();
            if (!(await wss.HandleRequestAsync(false).CAF()).IsConnected)
            {
                return;
            }
            if (!no_passwd)
            {
                var realPasswd = passwd;
                var aesEnabled = false;
                if (p.ParseUrlQstr()["encryption"] == "1")
                {
                    wss.ApplyAesStreamFilter(GetMD5FromString(realPasswd));
                    await wss.StartVerify(true).CAF();
                }
                int chances = 3;
                while (true)
                {
                    await wss.SendStringAsync("passwd:\r\n");

                    var passwd = await wss.RecvString();

                    if (passwd == null)
                    {
                        return;
                    }
                    if (!aesEnabled && passwd == "__AesStreamFilter__")
                    {
                        wss.ApplyAesStreamFilter(GetMD5FromString(realPasswd));
                        await wss.StartVerify(false);

                        continue;
                    }
                    if (passwd == realPasswd)
                    {
                        break;
                    }
                    else
                    {
                        Logger.warning($"{(passwd.Length == 0 ? "empty" : "wrong")} passwd from {p.myStream}");
                        if (--chances <= 0)
                        {
                            await wss.SendStringAsync("session end.\r\n");

                            return;
                        }
                    }
                }
                await wss.SendStringAsync("success.\r\n");
            }
            else
            {
                await wss.SendStringAsync("success (no passwd).\r\n");
            }

            var concli = new ConClient(wss);

            new Thread(() => {
                try {
                    consoleHub.SessionSelectLoop(concli);
                } catch (Exception e) { }
            })
            {
                IsBackground = true, Name = "consolewsSession"
            }.Start();

            await wss.RecvLoopAsync().CAF();
        }