public DataTunnel(Kernel kernel,Logger logger,Ip listenIp,int listenPort,Ip connectIp,int connectPort,Tunnel tunnel) : base(logger) { _sock[CS.Client] = null; _sock[CS.Server] = null; _buf[CS.Client] = new byte[0]; _buf[CS.Server] = new byte[0]; _kernel = kernel; _listenIp = listenIp; _listenPort = listenPort; _connectIp = connectIp; _connectPort = connectPort; _tunnel = tunnel; }
//接続単位の処理 protected override 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(); }
//�ڑ��P�ʂ̏��� protected override 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(); }
// public void CallBack(IAsyncResult ar) { // //接続完了(Listenソケットを取得) // var sockObj = (SockObj)(ar.AsyncState); // // if (_sock[CS.Server] != null) { //サーバ側がListen状態の場合 // //接続ソケットを保持して // _sock[CS.Server] = (SockTcp)sockObj.CreateChildObj(ar); // //Listenソケットをクローズする // sockObj.Close(); // //クライアントと接続する // Ssl ssl = null; // _sock[CS.Client] = Inet.Connect(ref _life, _kernel, _logger, _ip, _port, ssl); // if (_sock[CS.Client] == null) // return; // } else { //クライアント側がListen状態の場合 // //接続ソケットを保持して // _sock[CS.Client] = (SockTcp)sockObj.CreateChildObj(ar); // //Listenソケットをクローズする // sockObj.Close(); // //サーバと接続する // // Ssl ssl = null; // _sock[CS.Server] = Inet.Connect(ref _life, _kernel, _logger, _ip, _port, ssl); // if (_sock[CS.Server] == null) // return; // } // //パイプスレッドの生成 // _t = new Thread(Pipe){ // IsBackground = true // }; // _t.Start(); // } void Pipe() { const int idleTime = 0; var tunnel = new Tunnel(_logger, idleTime, _timeout); tunnel.Pipe(_sock[CS.Server], _sock[CS.Client],this); _sock[CS.Client].Close(); _sock[CS.Server].Close(); }
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(); }