public bool SendServer(ref bool life) { //サーバ側との接続処理 if (!proxy.Connect(ref life, oneObj.Request.HostName, oneObj.Request.Port, oneObj.Request.RequestStr, oneObj.Request.Protocol)) { proxy.Logger.Set(LOG_KIND.DEBUG, null, 999, "□Break http.Connect()==false"); return(false); } //ヘッダ送信 byte[] sendBuf = new byte[0]; if (sideState[CS.SERVER] == HTTP_SIDE_STATE.NON) { if (oneObj.Request.Protocol == PROXY_PROTOCOL.SSL) { if (!proxy.UpperProxy.Use) { //取得したリクエストをバッファに格納する //sendBuf = new byte[0]; //sendBuf[CS.CLIENT] = Bytes.Create("HTTP/1.0 200 Connection established\r\n\r\n");//CONNECTが成功したことをクライアントに返す } else { //上位プロキシを使用する場合(リクエストラインはそのまま使用される) sendBuf = Bytes.Create(oneObj.Request.SendLine(proxy.UpperProxy.Use), oneObj.Header[CS.CLIENT].GetBytes()); } } else if (oneObj.Request.Protocol == PROXY_PROTOCOL.HTTP) //HTTPの場合 //header.Remove("Proxy-Connection");//<=■これ入れていいのか? //取得したリクエストをバッファに格納する //上位プロキシを使用する場合(リクエストラインはそのまま使用される) { sendBuf = Bytes.Create(oneObj.Request.SendLine(proxy.UpperProxy.Use), oneObj.Header[CS.CLIENT].GetBytes()); } if (!Send(proxy.Sock(CS.SERVER), sendBuf))//送信 { return(false); } sideState[CS.SERVER] = HTTP_SIDE_STATE.SERVER_SIDE_SEND_HEADER; } if (sideState[CS.SERVER] == HTTP_SIDE_STATE.SERVER_SIDE_SEND_HEADER) { //バッファに残っているデータの送信 if (!SendBuf(CS.CLIENT)) { return(false); } } //サーバへの送信完了を確認する(ステータス変更) sideState[CS.SERVER] = HTTP_SIDE_STATE.SERVER_SIDE_SEND_BODY; return(true); }
bool SendServer(ILife iLife) { //サーバ側との接続処理 if (!Proxy.Connect(iLife, _oneObj.Request.HostName, _oneObj.Request.Port, _oneObj.Request.RequestStr, _oneObj.Request.Protocol)) { Proxy.Logger.Set(LogKind.Debug, null, 999, "□Break http.Connect()==false"); return(false); } //バッファに残っているデータの送信 if (!SendBuf(CS.Client)) { return(false); } return(true); }
public bool SendServer(ILife iLife) { //処置なし if (_sideState[CS.Server] != HttpSideState.Non && _sideState[CS.Server] != HttpSideState.ServerSideSendHeader) { return(true); } //サーバ側との接続処理 if (!_proxy.Connect(iLife, _oneObj.Request.HostName, _oneObj.Request.Port, _oneObj.Request.RequestStr, _oneObj.Request.Protocol)) { _proxy.Logger.Set(LogKind.Debug, null, 999, "□Break http.Connect()==false"); return(false); } //ヘッダ送信 var sendBuf = new byte[0]; if (_sideState[CS.Server] == HttpSideState.Non) { if (_proxy.UpperProxy.Use) { if (_proxy.UpperProxy.UseAuth) { var s = string.Format("{0}:{1}", _proxy.UpperProxy.AuthUser, _proxy.UpperProxy.AuthPass); s = string.Format("Basic {0}\r\n", Base64.Encode(s)); _oneObj.Header[CS.Client].Append("Proxy-Authorization", Encoding.ASCII.GetBytes(s)); } } if (_oneObj.Request.Protocol == ProxyProtocol.Ssl) { if (!_proxy.UpperProxy.Use) { //取得したリクエストをバッファに格納する //sendBuf = new byte[0]; //sendBuf[CS.CLIENT] = Bytes.Create("HTTP/1.0 200 Connection established\r\n\r\n");//CONNECTが成功したことをクライアントに返す } else { //上位プロキシを使用する場合(リクエストラインはそのまま使用される) sendBuf = Bytes.Create(_oneObj.Request.SendLine(_proxy.UpperProxy.Use), _oneObj.Header[CS.Client].GetBytes()); } } else if (_oneObj.Request.Protocol == ProxyProtocol.Http) //HTTPの場合 //Ver5.4.4 //"Proxy-Connection"ヘッダは,"Connection"ヘッダに変換する { var s = _oneObj.Header[CS.Client].GetVal("Proxy-Connection"); if (s != null) //ヘッダが存在する場合 { _oneObj.Header[CS.Client].Replace("Proxy-Connection", "Connection", s); } //header.Remove("Proxy-Connection");//<=■これ入れていいのか? //取得したリクエストをバッファに格納する //上位プロキシを使用する場合(リクエストラインはそのまま使用される) sendBuf = Bytes.Create(_oneObj.Request.SendLine(_proxy.UpperProxy.Use), _oneObj.Header[CS.Client].GetBytes()); } if (!Send(_proxy.Sock(CS.Server), sendBuf))//送信 { return(false); } _sideState[CS.Server] = HttpSideState.ServerSideSendHeader; } if (_sideState[CS.Server] == HttpSideState.ServerSideSendHeader) { //バッファに残っているデータの送信 if (!SendBuf(CS.Client)) { return(false); } } //サーバへの送信完了を確認する(ステータス変更) _sideState[CS.Server] = HttpSideState.ServerSideSendBody; return(true); }
//プロキシ処理 override public bool Pipe(ILife iLife) { DataThread dataThread = null; var paramStr = ""; //サーバ側との接続処理 if (!Proxy.Connect(iLife, _oneObj.Request.HostName, _oneObj.Request.Port, _oneObj.Request.RequestStr, _oneObj.Request.Protocol)) { Proxy.Logger.Set(LogKind.Debug, null, 999, "□Break proxy.Connect()==false"); return(false); } //wait 220 welcome if (!WaitLine("220", ref paramStr, iLife)) { return(false); } Proxy.Sock(CS.Server).AsciiSend(string.Format("USER {0}", _user)); if (!WaitLine("331", ref paramStr, iLife)) { return(false); } Proxy.Sock(CS.Server).AsciiSend(string.Format("PASS {0}", _pass)); if (!WaitLine("230", ref paramStr, iLife)) { return(false); } //Ver5.6.6 if (_path == "/") { Proxy.Sock(CS.Server).AsciiSend("PWD"); if (!WaitLine("257", ref paramStr, iLife)) { return(false); } var tmp = paramStr.Split(' '); if (tmp.Length >= 1) { _path = tmp[0].Trim(new[] { '"' }); if (_path[_path.Length - 1] != '/') { _path = _path + "/"; } } } //リクエスト if (_path != "") { Proxy.Sock(CS.Server).AsciiSend(string.Format("CWD {0}", _path)); if (!WaitLine("250", ref paramStr, iLife)) { goto end; } } Proxy.Sock(CS.Server).AsciiSend(_file == "" ? "TYPE A" : "TYPE I"); if (!WaitLine("200", ref paramStr, iLife)) { goto end; } //PORTコマンド送信 var bindAddr = Proxy.Sock(CS.Server).LocalIp; // 利用可能なデータポートの選定 while (iLife.IsLife()) { DataPort++; if (DataPort >= 9999) { DataPort = 2000; } if (SockServer.IsAvailable(_kernel, bindAddr, DataPort)) { break; } } int listenPort = DataPort; //データスレッドの生成 dataThread = new DataThread(_kernel, bindAddr, listenPort); // サーバ側に送るPORTコマンドを生成する string str = string.Format("PORT {0},{1},{2},{3},{4},{5}", bindAddr.IpV4[0], bindAddr.IpV4[1], bindAddr.IpV4[2], bindAddr.IpV4[3], listenPort / 256, listenPort % 256); Proxy.Sock(CS.Server).AsciiSend(str); if (!WaitLine("200", ref paramStr, iLife)) { goto end; } if (_file == "") { Proxy.Sock(CS.Server).AsciiSend("LIST"); if (!WaitLine("150", ref paramStr, iLife)) { goto end; } } else { Proxy.Sock(CS.Server).AsciiSend("RETR " + _file); if (!WaitLine("150", ref paramStr, iLife)) { goto end; } } //Ver5.0.2 while (iLife.IsLife()) { if (!dataThread.IsRecv) { break; } } if (!WaitLine("226", ref paramStr, iLife)) { goto end; } byte[] doc; if (_file == "") { //受信結果をデータスレッドから取得する List <string> lines = Inet.GetLines(dataThread.ToString()); //FTPサーバから取得したLISTの情報をHTMLにコンバートする doc = ConvFtpList(lines, _path); } else { doc = dataThread.ToBytes(); } //クライアントへリプライ及びヘッダを送信する var header = new Header(); header.Replace("Server", Util.SwapStr("$v", _kernel.Ver.Version(), (string)_conf.Get("serverHeader"))); header.Replace("MIME-Version", "1.0"); if (_file == "") { header.Replace("Date", Util.UtcTime2Str(DateTime.UtcNow)); header.Replace("Content-Type", "text/html"); } else { header.Replace("Content-Type", "none/none"); } header.Replace("Content-Length", doc.Length.ToString()); Proxy.Sock(CS.Client).AsciiSend("HTTP/1.0 200 OK"); //リプライ送信 Proxy.Sock(CS.Client).SendUseEncode(header.GetBytes()); //ヘッダ送信 Proxy.Sock(CS.Client).SendNoEncode(doc); //ボディ送信 end: if (dataThread != null) { dataThread.Dispose(); } return(false); }