Exemplo n.º 1
0
        public void EchoサーバにlineSendで1行送信してlineRecvで1行受信する()
        {
            //setUp
            const string addr = "127.0.0.1";
            const int    port = 9993;

            var sv = new EchoServer(addr, port);

            sv.Start();
            var sut = new SockTcp(new Kernel(), new Ip(addr), port, 100, null);

            sut.LineSend(Encoding.UTF8.GetBytes("本日は晴天なり"));
            Thread.Sleep(10);

            var expected = "本日は晴天なり\r\n";

            //exercise
            var bytes  = sut.LineRecv(1, this);
            var actual = Encoding.UTF8.GetString(bytes);

            //verify
            Assert.That(actual, Is.EqualTo(expected));

            //tearDown
            sut.Close();
            sv.Stop();
        }
Exemplo n.º 2
0
 void Pipe()
 {
     _sockTcp = SockServer.CreateConnection(_kernel, _ip, _listenPort, null, this);
     if (_sockTcp != null)
     {
         while (_life)
         {
             var len = _sockTcp.Length();
             if (len > 0)
             {
                 const int tout = 3; //受信バイト数がわかっているので、ここでのタイムアウト値はあまり意味が無い
                 var       b    = _sockTcp.Recv(len, tout, this);
                 if (b != null)
                 {
                     _buffer = Bytes.Create(_buffer, b);
                 }
             }
             if (_sockTcp.Length() == 0 && _sockTcp.SockState != SockState.Connect)
             {
                 break;
             }
         }
         _sockTcp.Close();
     }
     IsRecv = false;
 }
Exemplo n.º 3
0
        public void EchoサーバにstringSendで1行送信してstringRecvで1行受信する()
        {
            //setUp
            const string addr = "127.0.0.1";
            const int    port = 9993;

            var sv = new EchoServer(addr, port);

            sv.Start();
            var sut = new SockTcp(new Kernel(), new Ip(addr), port, 100, null);

            sut.StringSend("本日は晴天なり", "UTF-8");
            Thread.Sleep(10);

            var expected = "本日は晴天なり\r\n";

            //exercise
            var actual = sut.StringRecv("UTF-8", 1, this);

            //verify
            Assert.That(actual, Is.EqualTo(expected));

            //tearDown
            sut.Close();
            sv.Stop();
        }
Exemplo n.º 4
0
        public void Echoサーバに送信して溜まったデータサイズ_lengthを確認する()
        {
            //setUp
            const string addr = "127.0.0.1";
            const int    port = 9982;
            var          sv   = new EchoServer(addr, port);

            sv.Start();

            var       sut = new SockTcp(new Kernel(), new Ip(addr), port, 100, null);
            const int max = 1000;

            for (int i = 0; i < 3; i++)
            {
                sut.Send(new byte[max]);
            }

            Thread.Sleep(200);

            int expected = max * 3;

            //exercise
            var actual = sut.Length();

            //verify
            Assert.That(actual, Is.EqualTo(expected));

            //tearDown
            sut.Close();
            sv.Stop();
        }
Exemplo n.º 5
0
        void TcpTunnel(SockTcp tcpObj)
        {
            var     client = tcpObj;
            SockTcp server = null;

            //***************************************************************
            // �T�[�o�Ƃ̐ڑ�
            //***************************************************************
            {
                var port = _targetPort;

                //var ipList = new List<Ip>{new Ip(_targetServer)};
                //if (ipList[0].ToString() == "0.0.0.0") {
                //    ipList = Kernel.DnsCache.Get(_targetServer);
                //    if(ipList.Count==0){
                //        Logger.Set(LogKind.Normal,null,4,string.Format("{0}:{1}",_targetServer,_targetPort));
                //        goto end;
                //    }
                //}
                var ipList = Kernel.GetIpList(_targetServer);
                if (ipList.Count == 0)
                {
                    Logger.Set(LogKind.Normal, null, 4, string.Format("{0}:{1}", _targetServer, _targetPort));
                    goto end;
                }
                foreach (var ip in ipList)
                {
                    server = Inet.Connect(Kernel, ip, port, Timeout, null);
                    if (server != null)
                    {
                        break;
                    }
                }
                if (server == null)
                {
                    Logger.Set(LogKind.Normal, server, 5, string.Format("{0}:{1}", _targetServer, _targetPort));
                    goto end;
                }
            }
            Logger.Set(LogKind.Normal, server, 6, string.Format("TCP {0}:{1} - {2}:{3}", client.RemoteHostname, client.RemoteAddress.Port, _targetServer, _targetPort));

            //***************************************************************
            // �p�C�v
            //***************************************************************
            var tunnel = new Tunnel(Logger, (int)Conf.Get("idleTime"), Timeout);

            tunnel.Pipe(server, client, this);
end:
            if (client != null)
            {
                client.Close();
            }
            if (server != null)
            {
                server.Close();
            }
        }
Exemplo n.º 6
0
        public void OneServerを継承したEchoServer_TCP版_を使用して接続する()
        {
            const string addr    = "127.0.0.1";
            const int    port    = 9999;
            const int    timeout = 300;
            Ip           ip      = null;

            try{
                ip = new Ip(addr);
            } catch (ValidObjException ex) {
                Assert.Fail(ex.Message);
            }
            var oneBind = new OneBind(ip, ProtocolKind.Tcp);
            var conf    = TestUtil.CreateConf("OptionSample");

            conf.Set("port", port);
            conf.Set("multiple", 10);
            conf.Set("acl", new Dat(new CtrlType[0]));
            conf.Set("enableAcl", 1);
            conf.Set("timeOut", timeout);

            var echoServer = new EchoServer(conf, oneBind);

            echoServer.Start();

            //TCPクライアント

            const int max = 10000;
            var       buf = new byte[max];

            buf[8] = 100; //CheckData
            for (int i = 0; i < 3; i++)
            {
                var sockTcp = new SockTcp(new Kernel(), ip, port, timeout, null);

                sockTcp.Send(buf);

                while (sockTcp.Length() == 0)
                {
                    Thread.Sleep(2);
                }

                var len = sockTcp.Length();
                if (0 < len)
                {
                    var b = sockTcp.Recv(len, timeout, this);
                    Assert.That(b[8], Is.EqualTo(buf[8]));//CheckData
                }
                Assert.That(max, Is.EqualTo(len));

                sockTcp.Close();
            }

            echoServer.Dispose();
        }
Exemplo n.º 7
0
 public void Clear()
 {
     if (sock != null)
     {
         sock.Close();
         sock = null;
     }
     if (switchAfter != null)
     {
         switchAfter.Close();
         switchAfter = null;
     }
 }
Exemplo n.º 8
0
        public void Echoサーバにsendで送信てtcpQueueのlength分ずつRecvする()
        {
            const string addr = "127.0.0.1";
            const int    port = 9981;

            var echoServer = new EchoServer(addr, port);

            echoServer.Start();

            const int timeout = 100;
            Ip        ip      = null;

            try{
                ip = new Ip(addr);
            } catch (ValidObjException ex) {
                Assert.Fail(ex.Message);
            }
            var sockTcp = new SockTcp(new Kernel(), ip, port, timeout, null);

            const int max  = 1000;
            const int loop = 3;
            var       tmp  = new byte[max];

            for (var i = 0; i < max; i++)
            {
                tmp[i] = (byte)i;
            }

            int recvCount = 0;

            for (var i = 0; i < loop; i++)
            {
                var len = sockTcp.Send(tmp);
                Assert.That(len, Is.EqualTo(tmp.Length));

                Thread.Sleep(10);

                var b = sockTcp.Recv(len, timeout, this);
                recvCount += b.Length;
                for (int m = 0; m < max; m += 10)
                {
                    Assert.That(b[m], Is.EqualTo(tmp[m])); //送信したデータと受信したデータが同一かどうかのテスト
                }
            }
            Assert.That(loop * max, Is.EqualTo(recvCount)); //送信したデータ数と受信したデータ数が一致するかどうかのテスト

            sockTcp.Close();
            echoServer.Stop();
        }
Exemplo n.º 9
0
 public bool Quit()
 {
     //切断中の場合はエラー
     if (Status == PopClientStatus.Idle)
     {
         SetLastError("Quit() Status == PIdle");
         return(false);
     }
     //QUIT送信
     if (!SendCmd("QUIT"))
     {
         return(false);
     }
     //+OK受信
     if (!RecvStatus())
     {
         return(false);
     }
     //切断
     _sockTcp.Close();
     _sockTcp = null;
     Status   = PopClientStatus.Idle;
     return(true);
 }
Exemplo n.º 10
0
 public bool Quit()
 {
     //トランザクションでない場合エラー
     if (Status == SmtpClientStatus.Idle)
     {
         SetLastError("Quit() Status == Idle");
         return(false);
     }
     //QUIT送信
     if (!SendCmd("QUIT"))
     {
         return(false);
     }
     //221受信
     if (!RecvStatus(221))
     {
         return(false);
     }
     //切断
     _sockTcp.Close();
     _sockTcp = null;
     Status   = SmtpClientStatus.Idle;
     return(true);
 }
Exemplo n.º 11
0
        public void PORTコマンド()
        {
            var cl = _v4Cl;

            //共通処理(ログイン成功)
            Login("user1", cl);

            int port = 256; //テストの連続のためにPORTコマンドのテストとはポート番号をずらす必要がある

            cl.StringSend("PORT 127,0,0,1,0,256");
            SockTcp dl = SockServer.CreateConnection(new Kernel(), new Ip(IpKind.V4Localhost), port, null, this);

            Assert.That(cl.StringRecv(1, this), Is.EqualTo("200 PORT command successful.\r\n"));

            dl.Close();
        }
Exemplo n.º 12
0
        public void PASVコマンド()
        {
            var cl = _v4Cl;

            //共通処理(ログイン成功)
            Login("user1", cl);

            cl.StringSend("PASV");

            //227 Entering Passive Mode. (127,0,0,1,xx,xx)
            string[] t    = cl.StringRecv(1, this).Split(new[] { '(', ')' });
            string[] tmp  = t[1].Split(',');
            int      n    = Convert.ToInt32(tmp[4]);
            int      m    = Convert.ToInt32(tmp[5]);
            int      port = n * 256 + m;

            Thread.Sleep(10);
            SockTcp dl = Inet.Connect(new Kernel(), new Ip(IpKind.V4Localhost), port, 10, null);

            Assert.That(dl.SockState, Is.EqualTo(SockState.Connect));
            dl.Close();
        }
Exemplo n.º 13
0
        //�ڑ��P�ʂ̏���
        override protected void OnSubThread(SockObj sockObj)
        {
            string hostName;

            var     client = (SockTcp)sockObj;
            SockTcp server = null;

            //***************************************************************
            //�O�����i�ڑ���E���[�U���E�p�X���[�h�̎擾)
            //***************************************************************
            //{
            //    //�ڑ���i�z�X�g�j���擾
            //    client.AsciiSend("open>");
            //    var sb = new StringBuilder();
            //    while (IsLife()) {
            //        var b = client.Recv(1,Timeout,this);//timeout=60sec
            //        if (b == null)
            //            break;

            //        var c = Convert.ToChar(b[0]);
            //        if (c == '\r'){
            //            continue;
            //        }
            //        if (c == '\n')
            //            break;
            //        //Ver6.0.6 TeraTerm�Ή�
            //        if (c == 0 && sb.Length != 0)
            //        {
            //            break;
            //        }

            //        if ((c == '.') || ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || ('0' <= c && c <= '9'))
            //        {
            //            client.Send(b);//�G�R�[
            //            sb.Append(c);
            //        }

            //    }
            //    hostName = sb.ToString();
            //}
            var buf = new List <byte>();

            {
                //�ڑ���i�z�X�g�j���擾
                client.Send(Encoding.ASCII.GetBytes("open>"));

                var iac = false;
                var ego = false;
                var sb  = new StringBuilder();
                while (IsLife())
                {
                    var b = client.Recv(1, Timeout, this); //timeout=60sec
                    if (b == null)
                    {
                        break;
                    }
                    var d = Convert.ToChar(b[0]);
                    if (d == 0)
                    {
                        continue;
                    }
                    if (d == '\xFF')
                    {
                        iac = true;
                        buf.Add(b[0]);
                        continue;
                    }
                    if (iac)
                    {
                        if (d == '\xFA')
                        {
                            ego = true;
                            iac = false;
                            buf.Add(b[0]);
                            continue;
                        }
                        if (d == '\xF0')
                        {
                            ego = false;
                            iac = false;
                            buf.Add(b[0]);
                            continue;
                        }
                        if (d == '\xFB' || d == '\xFC' || d == '\xFD' || d == '\xFE')
                        {
                            buf.Add(b[0]);
                            continue;
                        }
                        iac = false;
                        buf.Add(b[0]);
                        continue;
                    }
                    if (ego)
                    {
                        buf.Add(b[0]);
                        continue;
                    }
                    client.Send(b); //�G�R�[

                    if (d == '\r' || d == '\n')
                    {
                        break;
                    }

                    if (d == '\b')
                    {
                        sb.Remove(sb.Length - 1, 1);
                    }
                    else
                    {
                        sb.Append(d);
                    }
                }
                hostName = sb.ToString();
            }
            // ���݂̔j��
            while (IsLife() && client.Length() > 0)
            {
                var b = client.Recv(1, Timeout, this); //timeout=60sec
            }


            //***************************************************************
            // �T�[�o�Ƃ̐ڑ�
            //***************************************************************
            {
                const int port = 23;
                //var ipList = new List<Ip>{new Ip(hostName)};
                //if (ipList[0].ToString() == "0.0.0.0") {
                //    ipList = Kernel.DnsCache.Get(hostName);
                //    if (ipList.Count == 0) {
                //        Logger.Set(LogKind.Normal,null,2,string.Format("open>{0}",hostName));
                //        goto end;
                //    }
                //}
                var ipList = Kernel.GetIpList(hostName);
                if (ipList.Count == 0)
                {
                    Logger.Set(LogKind.Normal, null, 2, string.Format("open>{0}", hostName));
                    goto end;
                }
                foreach (var ip in ipList)
                {
                    server = Inet.Connect(Kernel, ip, port, Timeout, null);
                    if (server != null)
                    {
                        break;
                    }
                }

                if (server == null)
                {
                    Logger.Set(LogKind.Normal, null, 3, string.Format("open>{0}", hostName));
                    goto end;
                }

                //Ver6.0.6
                if (server.SockState != Bjd.sock.SockState.Connect)
                {
                    Logger.Set(LogKind.Normal, null, 3, string.Format("open>{0}", hostName));
                    goto end;
                }
            }
            Logger.Set(LogKind.Normal, server, 1, string.Format("open>{0}", hostName));

            server.Send(buf.ToArray(), buf.Count);
            //***************************************************************
            // �p�C�v
            //***************************************************************
            var tunnel = new Tunnel(Logger, (int)Conf.Get("idleTime"), Timeout);

            tunnel.Pipe(server, client, this);
end:
            client.Close();
            if (server != null)
            {
                server.Close();
            }
        }
Exemplo n.º 14
0
        //�ڑ��P�ʂ̏���
        override protected void OnSubThread(SockObj sockObj)
        {
            var timeout = (int)Conf.Get("timeOut");

            var     client = (SockTcp)sockObj;
            SockTcp server = null;

            var    user = "";     //���[�U��
            string pass;          //�p�X���[�h
            var    hostName = ""; //�z�X�g��

            //***************************************************************
            //�O�����i�ڑ���E���[�U���E�p�X���[�h�̎擾)
            //***************************************************************
            {
                var str = string.Format("220 {0} {1}", Define.ApplicationName(), Define.Copyright());
                client.AsciiSend(str);

                var cmdStr   = "";
                var paramStr = "";
                //wait USER user@hostName
                if (!WaitLine(client, ref cmdStr, ref paramStr))
                {
                    goto end;
                }
                if (cmdStr.ToUpper() != "USER")
                {
                    goto end;
                }

                //paramStr = "user@hostName"
                if (paramStr != null)
                {
                    //string[] tmp = paramStr.Split('@');
                    //if(tmp.Length == 2) {
                    //    user = tmp[0];//���[�U���擾
                    //    hostName = tmp[1];//�z�X�g���擾
                    //}
                    var i = paramStr.LastIndexOf('@');
                    if (i != -1)
                    {
                        user     = paramStr.Substring(0, i);  //���[�U���擾
                        hostName = paramStr.Substring(i + 1); //�z�X�g���擾
                    }
                }
                if (hostName == "")
                {
                    Logger.Set(LogKind.Error, sockObj, 8, "");
                    goto end;
                }

                client.AsciiSend("331 USER OK enter password");

                //wait PASS password
                if (!WaitLine(client, ref cmdStr, ref paramStr))
                {
                    goto end;
                }
                if (cmdStr.ToUpper() != "PASS")
                {
                    goto end;
                }
                //paramStr = "password"
                pass = paramStr;//�p�X���[�h�擾
            }
            //***************************************************************
            // �T�[�o�Ƃ̐ڑ�
            //***************************************************************
            {
                const int port = 21;

                //var ipList = new List<Ip>{new Ip(hostName)};
                //if (ipList[0].ToString() == "0.0.0.0") {
                //    ipList = Kernel.DnsCache.Get(hostName);
                //    if (ipList.Count == 0) {
                //        goto end;
                //    }
                //}
                var ipList = Kernel.GetIpList(hostName);
                if (ipList.Count == 0)
                {
                    goto end;
                }

                Ssl ssl = null;

                foreach (var ip in ipList)
                {
                    server = Inet.Connect(Kernel, ip, port, Timeout, ssl);
                    if (server != null)
                    {
                        break;
                    }
                }
                if (server == null)
                {
                    goto end;
                }
            }
            //***************************************************************
            //�㏈���i���[�U���E�p�X���[�h�̑��M)
            //***************************************************************
            {
                var cmdStr   = "";
                var paramStr = "";
                //wait 220 welcome
                while (cmdStr != "220")
                {
                    if (!WaitLine(server, ref cmdStr, ref paramStr))
                    {
                        goto end;
                    }
                }
                server.AsciiSend(string.Format("USER {0}", user));
                //wait 331 USER OK enter password
                while (cmdStr != "331")
                {
                    if (!WaitLine(server, ref cmdStr, ref paramStr))
                    {
                        goto end;
                    }
                }
                server.AsciiSend(string.Format("PASS {0}", pass));
                if (!WaitLine(server, ref cmdStr, ref paramStr))
                {
                    goto end;
                }
                client.AsciiSend(string.Format("{0} {1}", cmdStr, paramStr));
            }

            //***************************************************************
            // �p�C�v
            //***************************************************************
            var ftpTunnel = new FtpTunnel(Kernel, Logger, (int)Conf.Get("idleTime"), _dataPort, timeout);

            //Ver5.0.5
            //ftpTunnel.BytePipe(ref life, server,client);
            ftpTunnel.Pipe(server, client, this);
            _dataPort = ftpTunnel.Dispose();
            if (_dataPort > DataPortMax)
            {
                _dataPort = DataPortMin;
            }
end:
            client.Close();
            if (server != null)
            {
                server.Close();
            }
        }
Exemplo n.º 15
0
 public void TearDown()
 {
     //クライアント停止
     _v4Cl.Close();
     _v6Cl.Close();
 }
Exemplo n.º 16
0
        //�㏈��
        override protected void OnRunThread() //�{��
        {
            _kernel.View.SetColor();          //�y�E�C���h�F�z

            //[C#]
            ThreadBaseKind = ThreadBaseKind.Running;


            if (_ip == new Ip(IpKind.V4_0))
            {
                return; //���������s
            }

            while (IsLife())
            {
                //TraceDlg traceDlg = null;
                Ssl ssl     = null;
                var timeout = 3;
                _sockTcp = Inet.Connect(_kernel, _ip, _port, timeout, ssl);

                if (_sockTcp == null)
                {
                    //isRun = false;
                    _logger.Set(LogKind.Error, _sockTcp, 4, string.Format("address={0} port={1}", _ip, _port));
                    //�Đڑ�����݂�̂́A2�b��
                    for (int i = 0; i < 20 && IsLife(); i++)
                    {
                        Thread.Sleep(100);
                    }
                }
                else
                {
                    _logger.Set(LogKind.Normal, _sockTcp, 5, string.Format("address={0} port={1}", _ip, _port));

                    while (IsLife())  //�ڑ���

                    {
                        if (_sockTcp.SockState != SockState.Connect)
                        {
                            //�ڑ����؂ꂽ�ꍇ�́A�����^�C�~���O��u���Ă���A�Đڑ������ɖ߂�
                            //�Đڑ�����݂�̂́A1�b��
                            _sockTcp.Close();
                            for (int i = 0; i < 10 && IsLife(); i++)
                            {
                                Thread.Sleep(100);
                            }
                            break;
                        }

                        var o = RemoteData.Recv(_sockTcp, this);
                        if (o == null)
                        {
                            Thread.Sleep(100);
                            continue;
                        }

                        switch (o.Kind)
                        {
                        case RemoteDataKind.DatAuth:    //�F�؏��i�p�X���[�h�v���j
                            var dlg = new PasswordDlg(_kernel);
                            if (DialogResult.OK == dlg.ShowDialog())
                            {
                                //�n�b�V��������̍쐬�iMD5�j
                                string md5Str = Inet.Md5Str(dlg.PasswordStr + o.Str);
                                //DAT_AUTH�ɑ΂���p�X���[�h(C->S)
                                RemoteData.Send(_sockTcp, RemoteDataKind.CmdAuth, md5Str);
                            }
                            else
                            {
                                StopLife();    //Ver5.8.4 �b�菈�u
                            }
                            break;

                        case RemoteDataKind.DatVer:    //�o�[�W�������
                            if (!_kernel.Ver.VerData(o.Str))
                            {
                                //�T�[�o�ƃN���C�A���g�Ńo�[�W�����ɈႢ���L��ꍇ�A�N���C�A���g�@�\���~����
                                StopLife();    //Ver5.8.4 �b�菈�u
                            }
                            else
                            {
                                IsConected = true;       //�ڑ���
                                _kernel.View.SetColor(); //�y�E�C���h�F�z

                                //���O�C������
                                _logger.Set(LogKind.Normal, _sockTcp, 10, "");
                            }
                            break;

                        case RemoteDataKind.DatLocaladdress:    //���[�J���A�h���X
                            LocalAddress.SetInstance(o.Str);
                            //_kernel.LocalAddress = new LocalAddress(o.Str);
                            break;

                        case RemoteDataKind.DatTool:    //�f�[�^��M
                            if (_toolDlg != null)
                            {
                                var tmp = o.Str.Split(new[] { '\t' }, 2);
                                _toolDlg.CmdRecv(tmp[0], tmp[1]);
                            }
                            break;

                        case RemoteDataKind.DatBrowse:    //�f�B���N�g������M
                            if (_browseDlg != null)
                            {
                                _browseDlg.CmdRecv(o.Str);
                            }
                            break;

                        case RemoteDataKind.DatTrace:    //�g���[�X��M
                            _kernel.TraceDlg.AddTrace(o.Str);
                            break;

                        case RemoteDataKind.DatLog:                    //���O��M
                            _kernel.LogView.Append(new OneLog(o.Str)); //���O�r���[�ւ̒lj�
                            break;

                        case RemoteDataKind.DatOption:    //�I�v�V�����̎�M
                            //Option.ini���M����$remote.ini�ɏo�͂���
                            using (var sw = new StreamWriter(_optionFileName, false, Encoding.GetEncoding("Shift_JIS"))) {
                                sw.Write(o.Str);
                                sw.Close();
                            }
                            _kernel.ListInitialize();

                            break;

                        default:
                            _logger.Set(LogKind.Error, null, 999, string.Format("kind = {0}", o.Kind));
                            break;
                        }
                    }
                    //err:
                    _sockTcp.Close();
                    _sockTcp   = null;
                    IsConected = false;//�ڑ��f
                    _kernel.Menu.InitializeRemote(_kernel.IsJp());
                    _kernel.View.SetColor();
                    _logger.Set(LogKind.Normal, null, 8, "");
                }
            }
            _logger.Set(LogKind.Normal, null, 7, "");//�����[�g�N���C�A���g��~
        }
Exemplo n.º 17
0
        SockTcp _sockTcp;//�����Ő錾����ꍇ�A�}���`�X���b�h�ł͎g�p�ł��Ȃ�
        override protected void OnSubThread(SockObj sockObj)
        {
            _sockTcp = (SockTcp)sockObj;

            //*************************************************************
            // �p�X���[�h�F��
            //*************************************************************
            var password = (string)Conf.Get("password");

            if (password == "")
            {
                Logger.Set(LogKind.Normal, _sockTcp, 5, "");
            }
            else  //�p�X���[�h�F�؂��K�v�ȏꍇ
            {
                var challengeStr = Inet.ChallengeStr(10);//�`�������W������̐���

                RemoteData.Send(_sockTcp, RemoteDataKind.DatAuth, challengeStr);

                //�p�X���[�h�̉����҂�
                var success = false;//Ver5.0.0-b14
                while (IsLife() && _sockTcp.SockState == Bjd.sock.SockState.Connect)
                {
                    var o = RemoteData.Recv(_sockTcp, this);
                    if (o != null)
                    {
                        if (o.Kind == RemoteDataKind.CmdAuth)
                        {
                            //�n�b�V��������̍쐬�iMD5�j
                            var md5Str = Inet.Md5Str(password + challengeStr);
                            if (md5Str != o.Str)
                            {
                                Logger.Set(LogKind.Secure, _sockTcp, 4, "");

                                //DOS�΍� 3�b�Ԃ͎��̐ڑ���󂯕t���Ȃ�
                                //for (int i = 0; i < 30 && life; i++) {
                                //    Thread.Sleep(100);
                                //}
                                //tcpObj.Close();//���̐ڑ��͔j�������
                                //return;
                            }
                            else
                            {
                                success = true;//Ver5.0.0-b14
                            }
                            break;
                        }
                    }
                    else
                    {
                        Thread.Sleep(500);
                    }
                }
                //Ver5.0.0-b14
                if (!success)
                {
                    //�F�؎��s�i�p�X���[�h�L�����Z���E�p�X���[�h�Ⴂ�E�����ؒf�j
                    //DOS�΍� 3�b�Ԃ͎��̐ڑ���󂯕t���Ȃ�
                    for (var i = 0; i < 30 && IsLife(); i++)
                    {
                        Thread.Sleep(100);
                    }
                    _sockTcp.Close();//���̐ڑ��͔j�������
                    return;
                }
            }

            //*************************************************************
            // �F�؊���
            //*************************************************************

            Logger.Set(LogKind.Normal, _sockTcp, 1, string.Format("address={0}", _sockTcp.RemoteAddress.Address));

            //�o�[�W����/���O�C�������̑��M
            RemoteData.Send(_sockTcp, RemoteDataKind.DatVer, Kernel.Ver.VerData());

            //kernel.LocalAddress��Remote����������
            RemoteData.Send(_sockTcp, RemoteDataKind.DatLocaladdress, LocalAddress.GetInstance().RemoteStr());

            //�I�v�V�����̑��M
            var    optionFileName = string.Format("{0}\\Option.ini", Kernel.ProgDir());
            string optionStr;

            using (var sr = new StreamReader(optionFileName, Encoding.GetEncoding("Shift_JIS"))) {
                optionStr = sr.ReadToEnd();
                sr.Close();
            }
            RemoteData.Send(_sockTcp, RemoteDataKind.DatOption, optionStr);
            Kernel.RemoteConnect = new Bjd.remote.RemoteConnect(_sockTcp); //�����[�g�N���C�A���g�ڑ��J�n
            Kernel.View.SetColor();                                        //�E�C���h�F�̏�����

            while (IsLife() && _sockTcp.SockState == Bjd.sock.SockState.Connect)
            {
                var o = RemoteData.Recv(_sockTcp, this);
                if (o == null)
                {
                    continue;
                }
                //�R�}���h�́A���ׂăL���[�Ɋi�[����
                _queue.Enqueue(o);
                if (_queue.Count == 0)
                {
                    GC.Collect();
                    Thread.Sleep(500);
                }
                else
                {
                    Cmd(_queue.Dequeue());
                }
            }

            Kernel.RemoteConnect = null;//�����[�g�N���C�A���g�ڑ��I��

            Logger.Set(LogKind.Normal, _sockTcp, 2, string.Format("address={0}", _sockTcp.RemoteAddress.Address));
            Kernel.View.SetColor();//�E�C���h�F�̏�����

            _sockTcp.Close();
        }
Exemplo n.º 18
0
        //接続単位の処理
        override protected void OnSubThread(SockObj sockObj)
        {
            var     client = (SockTcp)sockObj;
            SockTcp server = null;

            string _targetServer;
            int    _targetPort;
            var    clientBuf = new List <byte[]>();

            _targetServer = (string)Conf.Get("targetServer");
            _targetPort   = (int)Conf.Get("targetPort");
            if (_targetServer == "")
            {
                Logger.Set(LogKind.Error, client, 1, "");
                goto end;
            }
            if (_targetPort == 0)
            {
                Logger.Set(LogKind.Error, client, 2, "");
                goto end;
            }


            //***************************************************************
            //前処理(接続先・ユーザの取得と特別なユーザの置換)
            //***************************************************************
            {
                var keyWord = BeforeJob(client, clientBuf);//接続前の処理
                if (keyWord == null)
                {
                    goto end;
                }


                //特別なユーザにヒットしているかどうかの確認
                OneSpecialUser oneSpecialUser = _specialUser.Search(keyWord);
                if (oneSpecialUser != null) //ヒットした場合
                //置換
                {
                    _targetServer = oneSpecialUser.Server; //サーバ
                    _targetPort   = oneSpecialUser.Port;   //ポート番号

                    for (var i = 0; i < clientBuf.Count; i++)
                    {
                        //string str = Inet.TrimCRLF(Encoding.ASCII.GetString(clientBuf[i]));
                        var str = Encoding.ASCII.GetString(clientBuf[i]);
                        if ((Protocol == MailProxyProtocolKind.Smtp && str.ToUpper().IndexOf("MAIL FROM:") == 0) ||
                            (Protocol == MailProxyProtocolKind.Pop3 && str.ToUpper().IndexOf("USER") == 0))
                        {
                            str          = Util.SwapStr(oneSpecialUser.Before, oneSpecialUser.After, str);
                            clientBuf[i] = Encoding.ASCII.GetBytes(str);
                            break;
                        }
                    }
                    Logger.Set(LogKind.Normal, client, 3, string.Format("{0}->{1} {2}:{3}", oneSpecialUser.Before, oneSpecialUser.After, _targetServer, _targetPort));
                }
            }

            //***************************************************************
            // サーバとの接続
            //***************************************************************
            {
                var port = _targetPort;
                //var ipList = new List<Ip>{new Ip(_targetServer)};
                //if(ipList[0].ToString() == "0.0.0.0") {
                //    ipList = Kernel.DnsCache.Get(_targetServer);
                //    if(ipList.Count == 0) {
                //        Logger.Set(LogKind.Normal,client,4,string.Format("{0}:{1}",_targetServer,_targetPort));
                //        goto end;
                //    }
                //}
                var ipList = Kernel.GetIpList(_targetServer);
                if (ipList.Count == 0)
                {
                    Logger.Set(LogKind.Normal, client, 4, string.Format("{0}:{1}", _targetServer, _targetPort));
                    goto end;
                }

                foreach (var ip in ipList)
                {
                    server = Inet.Connect(Kernel, ip, port, Timeout, null);
                    if (server != null)
                    {
                        break;
                    }
                }
                if (server == null)
                {
                    Logger.Set(LogKind.Normal, client, 5, string.Format("{0}:{1}", _targetServer, _targetPort));
                    goto end;
                }

                if (null == ConnectJob(client, server, clientBuf))//接続後の処理
                {
                    goto end;
                }
            }
            Logger.Set(LogKind.Normal, client, 4, string.Format("connect {0}:{1}", _targetServer, _targetPort));

            //***************************************************************
            //後処理(接続先・ユーザの取得と特別なユーザの置換)
            //***************************************************************

            foreach (byte[] buf in clientBuf)
            {
                //Ver5.2.5
                //byte[] serverBuf = server.LineRecv(timeout,OPERATE_CRLF.NO,ref life);

                //クライアントからの受信分を送信する
                server.LineSend(buf);
            }

            //***************************************************************
            // パイプ
            //***************************************************************
            var tunnel = new Tunnel(Logger, (int)Conf.Get("idleTime"), Timeout);

            tunnel.Pipe(server, client, this);
end:
            if (client != null)
            {
                client.Close();
            }
            if (server != null)
            {
                server.Close();
            }
        }
Exemplo n.º 19
0
        //後処理
        override protected void OnRunThread() //本体
        {
            _kernel.View.SetColor();          //【ウインド色】

            //[C#]
            ThreadBaseKind = ThreadBaseKind.Running;


            if (_ip == new Ip(IpKind.V4_0))
            {
                return; //初期化失敗
            }

            while (IsLife())
            {
                //TraceDlg traceDlg = null;
                Ssl ssl     = null;
                var timeout = 3;
                _sockTcp = Inet.Connect(_kernel, _ip, _port, timeout, ssl);

                if (_sockTcp == null)
                {
                    //isRun = false;
                    _logger.Set(LogKind.Error, _sockTcp, 4, string.Format("address={0} port={1}", _ip, _port));
                    //再接続を試みるのは、2秒後
                    for (int i = 0; i < 20 && IsLife(); i++)
                    {
                        Thread.Sleep(100);
                    }
                }
                else
                {
                    _logger.Set(LogKind.Normal, _sockTcp, 5, string.Format("address={0} port={1}", _ip, _port));

                    while (IsLife())  //接続中

                    {
                        if (_sockTcp.SockState != SockState.Connect)
                        {
                            //接続が切れた場合は、少しタイミングを置いてから、再接続処理に戻る
                            //再接続を試みるのは、1秒後
                            _sockTcp.Close();
                            for (int i = 0; i < 10 && IsLife(); i++)
                            {
                                Thread.Sleep(100);
                            }
                            break;
                        }

                        var o = RemoteData.Recv(_sockTcp, this);
                        if (o == null)
                        {
                            Thread.Sleep(100);
                            continue;
                        }

                        switch (o.Kind)
                        {
                        case RemoteDataKind.DatAuth:    //認証情報(パスワード要求)
                            var dlg = new PasswordDlg(_kernel);
                            if (DialogResult.OK == dlg.ShowDialog())
                            {
                                //ハッシュ文字列の作成(MD5)
                                string md5Str = Inet.Md5Str(dlg.PasswordStr + o.Str);
                                //DAT_AUTHに対するパスワード(C->S)
                                RemoteData.Send(_sockTcp, RemoteDataKind.CmdAuth, md5Str);
                            }
                            else
                            {
                                StopLife();    //Ver5.8.4 暫定処置
                            }
                            break;

                        case RemoteDataKind.DatVer:    //バージョン情報
                            if (!_kernel.Ver.VerData(o.Str))
                            {
                                //サーバとクライアントでバージョンに違いが有る場合、クライアント機能を停止する
                                StopLife();    //Ver5.8.4 暫定処置
                            }
                            else
                            {
                                IsConected = true;       //接続中
                                _kernel.View.SetColor(); //【ウインド色】

                                //ログイン完了
                                _logger.Set(LogKind.Normal, _sockTcp, 10, "");
                            }
                            break;

                        case RemoteDataKind.DatLocaladdress:    //ローカルアドレス
                            LocalAddress.SetInstance(o.Str);
                            //_kernel.LocalAddress = new LocalAddress(o.Str);
                            break;

                        case RemoteDataKind.DatTool:    //データ受信
                            if (_toolDlg != null)
                            {
                                var tmp = o.Str.Split(new[] { '\t' }, 2);
                                _toolDlg.CmdRecv(tmp[0], tmp[1]);
                            }
                            break;

                        case RemoteDataKind.DatBrowse:    //ディレクトリ情報受信
                            if (_browseDlg != null)
                            {
                                _browseDlg.CmdRecv(o.Str);
                            }
                            break;

                        case RemoteDataKind.DatTrace:    //トレース受信
                            _kernel.TraceDlg.AddTrace(o.Str);
                            break;

                        case RemoteDataKind.DatLog:                    //ログ受信
                            _kernel.LogView.Append(new OneLog(o.Str)); //ログビューへの追加
                            break;

                        case RemoteDataKind.DatOption:    //オプションの受信
                            //Option.iniを受信して$remote.iniに出力する
                            using (var sw = new StreamWriter(_optionFileName, false, Encoding.GetEncoding("Shift_JIS"))) {
                                sw.Write(o.Str);
                                sw.Close();
                            }
                            _kernel.ListInitialize();

                            break;

                        default:
                            _logger.Set(LogKind.Error, null, 999, string.Format("kind = {0}", o.Kind));
                            break;
                        }
                    }
                    //err:
                    _sockTcp.Close();
                    _sockTcp   = null;
                    IsConected = false;//接続断
                    _kernel.Menu.InitializeRemote(_kernel.IsJp());
                    _kernel.View.SetColor();
                    _logger.Set(LogKind.Normal, null, 8, "");
                }
            }
            _logger.Set(LogKind.Normal, null, 7, "");//リモートクライアント停止
        }