public void ConnectionFilterTest() { byte[] badRequest = Encoding.UTF8.GetBytes( "Bad request" ); byte[] request = Encoding.UTF8.GetBytes( "GET / HTTP/1.1\r\nHost: www.yahoo.com\r\n\r\n" ); Mock<INetworkFacade> client = new Mock<INetworkFacade>(); client.Setup( m => m.BeginReceive( It.IsAny<Action<bool, byte[], INetworkFacade>>() ) ) .Callback( ( Action<bool, byte[], INetworkFacade> callback ) => callback( true, request, client.Object ) ); Mock<ICoreSettings> settings = new Mock<ICoreSettings>(); settings.SetupGet( m => m.FilteringEnabled ).Returns( true ); // If I place Encoding.UTF8.GetBytes("Bad request") in the Setup method, this never gets called, yet // the Assert verfies that is the value. Not sure what I'm doing wrong with MOQ. client.Setup( m => m.BeginSend( It.IsAny<byte[]>(), It.IsAny<Action<bool, INetworkFacade>>() ) ) .Callback<byte[], Action<bool, INetworkFacade>>( ( b, d ) => { Assert.That( b, Is.EqualTo( badRequest ) ); d( true, client.Object ); } ); Mock<INetworkFacadeFactory> mockFactory = new Mock<INetworkFacadeFactory>(); Mock<IProxyFilter> outboundFilter = new Mock<IProxyFilter>(); outboundFilter.Setup( m => m.EvaluateConnectionFilters( It.IsAny<HttpRequest>(), It.IsAny<string>() ) ).Returns( "Bad request" ); ProxySession session = new ProxySession( mockFactory.Object, outboundFilter.Object, settings.Object ); session.Start( client.Object ); client.Verify( m => m.BeginSend( badRequest, It.IsAny<Action<bool, INetworkFacade>>() ), Times.Once() ); client.Verify( m => m.BeginClose( It.IsAny<Action<bool, INetworkFacade>>() ), Times.Once() ); mockFactory.Verify( m => m.BeginConnect( It.IsAny<string>(), It.IsAny<int>(), It.IsAny<Action<bool, INetworkFacade>>() ), Times.Never() ); outboundFilter.Verify( m => m.EvaluateConnectionFilters( It.IsAny<HttpRequest>(), It.IsAny<string>() ), Times.Once() ); }
private void RemoveSession(ProxySession session) { lock (sessions) { session.Close(); sessions.Remove(session); } }
public void Start() { Stop(); string ip = Program.Settings.ServerInfo.IP1; if (string.IsNullOrEmpty(ip)) { return; } thread = new Thread(delegate() { listener = new TcpListener(IPAddress.Any, port); listener.Start(); while (listener != null) { TcpClient clientToProxy = null; TcpClient proxyToServer = null; try { clientToProxy = listener.AcceptTcpClient(); proxyToServer = new TcpClient(); proxyToServer.Connect(ip, port); ProxySession session = new ProxySession(this, clientToProxy, proxyToServer); AddSession(session); session.BeginRecieve(); } catch { if (clientToProxy != null) { try { clientToProxy.Close(); clientToProxy = null; } catch { } } if (proxyToServer != null) { try { proxyToServer.Close(); proxyToServer = null; } catch { } } } } }); thread.Start(); }
private void AddSession(ProxySession session) { lock (sessions) { if (!sessions.Contains(session)) { sessions.Add(session); } } }
public void Initialize() { var MsFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Bugreport 165 From Jake.ms"); var CharacterFile = Path.Combine(FurcPath.CharacterPath, "dbugger.ini"); options = new ProxyOptions() { Standalone = true, CharacterIniFile = CharacterFile, ResetSettingTime = 10 }; Proxy = new ProxySession(options); // Set Dream info and Furre List PopulateFurreList(); Proxy.Error += (e, o) => Logger.Error($"{e} {o}"); }
public void Initialize() { #pragma warning disable CS0618 // Obsolete, Place holder till Accounts are ready var CharacterFile = Path.Combine(FurcPaths.CharacterPath, #pragma warning restore CS0618 // Obsolete, Place holder till Accounts are ready "silvermonkey.ini"); Options = new ProxyOptions() { Standalone = true, CharacterIniFile = CharacterFile, ResetSettingTime = 10 }; Proxy = new ProxySession(Options); Proxy.ServerData2 += data => Proxy.SendToClient(data); Proxy.ClientData2 += data => Proxy.SendToServer(data); Proxy.Error += (e, o) => Logger.Error($"{e} {o}"); BotHasConnected(Proxy.StandAlone); }
private ProxySession ProxySessionInitialize() { var options = new ProxyOptions() { CharacterIniFile = "" }; var proxy = new ProxySession(options); proxy.Error += (e, o) => Logger.Error($"{e} {o}"); Furres.Add(new Furre(1, "John")); Furres.Add(new Furre(2, "Bill Nye")); Furres.Add(new Furre(3, "John More")); Furres.Add(new Furre(4, "Silver|Monkey")); Furres.Add(new Furre(5, "Gerolkae")); proxy.ConnectedFurreId = 4; proxy.ConnectedFurreName = "Silver|Monkey"; // proxy.ConnectedFurre = new Furre(4, "Silver Monkey"); return(proxy); }
public void Stop() { if (listener != null) { try { listener.Stop(); } catch { } listener = null; } if (thread != null) { try { thread.Abort(); } catch { thread = null; } } lock (sessions) { while (sessions.Count > 0) { ProxySession session = sessions[0]; session.Close(); sessions.Remove(session); } sessions.Clear(); } }
public void ReceiveMessage(PiranhaMessage message) { int messageType = message.GetMessageType(); int messageServiceNodeType = message.GetServiceNodeType(); ClientConnectionState state = this.m_connection.State; if (state != ClientConnectionState.LOGGED) { if (state == ClientConnectionState.DISCONNECTED) { return; } if (messageType != ClientHelloMessage.MESSAGE_TYPE && messageType != LoginMessage.MESSAGE_TYPE && messageType != KeepAliveMessage.MESSAGE_TYPE && messageType != UnlockAccountMessage.MESSAGE_TYPE) { return; } } Logging.Print("MessageManager.receiveMessage: " + message.GetType().Name); if (messageServiceNodeType == 1) { switch (messageType) { case ClientHelloMessage.MESSAGE_TYPE: this.ClientHelloMessageReceived((ClientHelloMessage)message); break; case LoginMessage.MESSAGE_TYPE: this.LoginMessageReceived((LoginMessage)message); break; case KeepAliveMessage.MESSAGE_TYPE: this.KeepAliveMessageReceived((KeepAliveMessage)message); break; case UnlockAccountMessage.MESSAGE_TYPE: this.UnlockAccountMessageReceived((UnlockAccountMessage)message); break; case BindFacebookAccountMessage.MESSAGE_TYPE: this.OnBindFacebookAccountMessageReceived((BindFacebookAccountMessage)message); break; } } else { if (state != ClientConnectionState.LOGGED) { return; } ProxySession session = this.m_connection.Session; if (session == null) { return; } if (this.IsRequestPiranhaMessage(message)) { switch (messageType) { case AskForAvatarProfileMessage.MESSAGE_TYPE: this.SendForwardLogicMessageRequestMessage(message, ServerManager.GetDocumentSocket(9, ((AskForAvatarProfileMessage)message).RemoveAvatarId())); break; case AskForAllianceDataMessage.MESSAGE_TYPE: this.SendForwardLogicMessageRequestMessage(message, ServerManager.GetDocumentSocket(11, ((AskForAllianceDataMessage)message).RemoveAllianceId())); break; } } else if (messageServiceNodeType == 28 || messageServiceNodeType == 29) { this.SendForwardLogicMessageRequestMessage(message, ServerManager.GetNextSocket(messageServiceNodeType)); } else { session.SendPiranhaMessage(message, messageServiceNodeType); } } }
private async void LoginMessageReceived(LoginMessage message) { if (this.m_connection.State == ClientConnectionState.DEFAULT && this.CheckClientVersion(message.GetClientMajorVersion(), message.GetClientBuildVersion(), message.GetAppVersion(), message.GetResourceSha(), message.IsAndroidClient()) && this.CheckServerCapabilities()) { this.m_connection.Messaging.SetScramblerSeed(message.GetScramblerSeed()); this.m_connection.SetState(ClientConnectionState.LOGIN); AccountDocument accountDocument; if (message.GetAccountId().IsZero() && message.GetPassToken() == null) { IOperationResult <ulong> incrementSeedResult = await ServerProxy.AccountDatabase.IncrementSeed(); if (!incrementSeedResult.Success) { LoginFailedMessage loginFailedMessage = new LoginFailedMessage(); loginFailedMessage.SetErrorCode(LoginFailedMessage.ErrorCode.SERVER_MAINTENANCE); loginFailedMessage.SetReason("Internal server error"); this.SendMessage(loginFailedMessage); return; } accountDocument = new AccountDocument((long)incrementSeedResult.Value); accountDocument.Init(); accountDocument.Country = this.m_connection.Location; IOperationResult <string> createAccountResult = await ServerProxy.AccountDatabase.Insert((long)incrementSeedResult.Value, CouchbaseDocument.Save(accountDocument)); if (!createAccountResult.Success) { LoginFailedMessage loginFailedMessage = new LoginFailedMessage(); loginFailedMessage.SetErrorCode(LoginFailedMessage.ErrorCode.SERVER_MAINTENANCE); loginFailedMessage.SetReason("Internal server error"); this.SendMessage(loginFailedMessage); return; } } else { IOperationResult <string> getResult = await ServerProxy.AccountDatabase.Get(message.GetAccountId()); if (!getResult.Success) { if (getResult.Status == ResponseStatus.KeyNotFound) { LoginFailedMessage loginFailedMessage = new LoginFailedMessage(); loginFailedMessage.SetErrorCode(LoginFailedMessage.ErrorCode.ACCOUNT_NOT_EXISTS); this.SendMessage(loginFailedMessage); } else { LoginFailedMessage loginFailedMessage = new LoginFailedMessage(); loginFailedMessage.SetErrorCode(LoginFailedMessage.ErrorCode.SERVER_MAINTENANCE); loginFailedMessage.SetReason("Internal server error"); this.SendMessage(loginFailedMessage); } return; } accountDocument = CouchbaseDocument.Load <AccountDocument>(getResult.Value); if (accountDocument.PassToken != message.GetPassToken()) { LoginFailedMessage loginFailedMessage = new LoginFailedMessage(); loginFailedMessage.SetErrorCode(LoginFailedMessage.ErrorCode.ACCOUNT_NOT_EXISTS); this.SendMessage(loginFailedMessage); return; } } if (accountDocument.State != AccountState.NORMAL) { switch (accountDocument.State) { case AccountState.BANNED: { LoginFailedMessage loginFailedMessage = new LoginFailedMessage(); loginFailedMessage.SetErrorCode(LoginFailedMessage.ErrorCode.BANNED); loginFailedMessage.SetReason(accountDocument.StateArg); this.SendMessage(loginFailedMessage); return; } case AccountState.LOCKED: { LoginFailedMessage loginFailedMessage = new LoginFailedMessage(); loginFailedMessage.SetErrorCode(LoginFailedMessage.ErrorCode.ACCOUNT_LOCKED); this.SendMessage(loginFailedMessage); return; } } } ProxySession session = ProxySessionManager.Create(this.m_connection, accountDocument); RedisValue prevSession = await ServerProxy.SessionDatabase.GetSet(accountDocument.Id, session.Id.ToString()); if (!prevSession.IsNull) { long prevSessionId = long.Parse(prevSession); ServerMessageManager.SendMessage(new StopSessionMessage { Reason = 1, SessionId = prevSessionId }, ServerManager.GetProxySocket(prevSessionId)); } session.SetSocket(ServerCore.Socket); // Proxy LoginOkMessage loginOkMessage = new LoginOkMessage(); loginOkMessage.SetAccountId(accountDocument.Id); loginOkMessage.SetHomeId(accountDocument.Id); loginOkMessage.SetPassToken(accountDocument.PassToken); loginOkMessage.SetFacebookId(accountDocument.FacebookId); loginOkMessage.SetGamecenterId(accountDocument.GamecenterId); loginOkMessage.SetServerMajorVersion(LogicVersion.MAJOR_VERSION); loginOkMessage.SetServerBuildVersion(LogicVersion.BUILD_VERSION); loginOkMessage.SetContentVersion(ResourceManager.GetContentVersion()); loginOkMessage.SetServerEnvironment(EnvironmentSettings.Environment); loginOkMessage.SetSessionCount(accountDocument.SessionCount); loginOkMessage.SetPlayTimeSeconds(accountDocument.PlayTimeSeconds); loginOkMessage.SetAccountCreatedDate(accountDocument.CreateTime.ToString()); loginOkMessage.SetStartupCooldownSeconds(ServerProxy.GetStartupCooldownSeconds()); loginOkMessage.SetRegion(this.m_connection.Location); loginOkMessage.SetFacebookAppId(ResourceSettings.FacebookAppId); loginOkMessage.SetGoogleServiceId(ResourceSettings.GoogleServiceId); loginOkMessage.SetContentUrlList(ResourceSettings.ContentUrlList); loginOkMessage.SetChronosContentUrlList(ResourceSettings.ChronosContentUrlList); this.SendMessage(loginOkMessage); this.m_connection.SetSession(session); this.m_connection.SetState(ClientConnectionState.LOGGED); if (this.m_connection.State == ClientConnectionState.LOGGED) { accountDocument.SessionCount += 1; accountDocument.LastSessionTime = TimeUtil.GetTimestamp(); ServerRequestManager.Create(new BindServerSocketRequestMessage { ServerType = 9, ServerId = ServerManager.GetDocumentSocket(9, accountDocument.Id).ServerId, SessionId = session.Id }, ServerCore.Socket, 5).OnComplete = this.OnGameServerBound; await ServerProxy.AccountDatabase.Update(accountDocument.Id, CouchbaseDocument.Save(accountDocument)); } else { this.m_connection.DestructSession(); } } }
public void ResponseFilterTestWithBody() { ManualResetEvent waitForClientSends = new ManualResetEvent(false); // If this test ever hangs, change the client.BeginSend mock to use It.IsAny and debug from there byte[] request = Encoding.UTF8.GetBytes( "GET / HTTP/1.1\r\nHost: www.yahoo.com\r\n\r\n" ); byte[] response = Encoding.UTF8.GetBytes( "HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nContent-Length: 2\r\n\r\nhi" ); byte[] responseHeader = Encoding.UTF8.GetBytes("HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nContent-Length: 2\r\n\r\n"); byte[] originalBody = Encoding.UTF8.GetBytes("hi"); byte[] body = Encoding.UTF8.GetBytes("by"); string filterResponse = null; Mock<INetworkFacade> server = new Mock<INetworkFacade>(); // Request from client Mock<INetworkFacade> client = new Mock<INetworkFacade>(); int numClientReceives = 0; client.Setup(m => m.BeginReceive(It.IsAny<Action<bool, byte[], INetworkFacade>>())) .Callback((Action<bool, byte[], INetworkFacade> callback) => { // Only respond with data on the first request. Ignore the others. if (++numClientReceives == 1) { callback(true, request, client.Object); } }); int numClientSends = 0; client.Setup(m => m.BeginSend(It.IsAny<byte[]>(), It.IsAny<Action<bool, INetworkFacade>>())) .Callback<byte[], Action<bool, INetworkFacade>>( (b, c) => { c(true, client.Object); if (++numClientSends == 2) { // After receiving all data (header and body) release the main thread to verify waitForClientSends.Set(); } }); // Response from server int numServerReceives = 0; server.Setup(m => m.BeginReceive(It.IsAny<Action<bool, byte[], INetworkFacade>>())) .Callback((Action<bool, byte[], INetworkFacade> callback) => { // Only respond with data on the first request. The second time, mock that the connection // closed; thereby forcing the modified body to be sent to the client. if (++numServerReceives == 1) { callback(true, response, server.Object); } }); // Mock the connection to the remote host. Return the mock network facade. Mock<INetworkFacadeFactory> mockFactory = new Mock<INetworkFacadeFactory>(); mockFactory.Setup(m => m.BeginConnect("www.yahoo.com", 80, It.IsAny<Action<bool, INetworkFacade>>())) .Callback<string, int, Action<bool, INetworkFacade>>((h, p, c) => c(true, server.Object)); // Set up the filters. We want a response filter that changes the body content from "hi" to "by" Mock<IProxyFilter> filter = new Mock<IProxyFilter>(); filter.Setup(m => m.EvaluateConnectionFilters(It.IsAny<HttpRequest>(), It.IsAny<string>())).Returns(null as string); filter.Setup(m => m.TryEvaluateResponseFilters(It.IsAny<IHttpResponse>(), It.IsAny<string>(), out filterResponse)) .Returns(false); // Notice that we are going to take the original body and map it to the new body filter.Setup(m => m.EvaluateResponseFiltersWithBody(It.IsAny<IHttpResponse>(), It.IsAny<string>(), originalBody)) .Returns(body); Mock<ICoreSettings> settings = new Mock<ICoreSettings>(); // // Mocks are set up. Start the test. // ProxySession session = new ProxySession(mockFactory.Object, filter.Object, settings.Object); session.Start(client.Object); // Wait until all data has been sent to the client Assert.That(waitForClientSends.WaitOne(500000)); client.Verify(m => m.BeginClose(It.IsAny<Action<bool, INetworkFacade>>()), Times.Once()); mockFactory.Verify(m => m.BeginConnect(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<Action<bool, INetworkFacade>>()), Times.Once()); filter.Verify(m => m.EvaluateConnectionFilters(It.IsAny<HttpRequest>(), It.IsAny<string>()), Times.Once()); filter.Verify(m => m.TryEvaluateResponseFilters(It.IsAny<IHttpResponse>(), It.IsAny<string>(), out filterResponse), Times.Once()); filter.Verify( m => m.EvaluateResponseFiltersWithBody(It.IsAny<IHttpResponse>(), It.IsAny<string>(), originalBody), Times.Once()); client.Verify(m => m.BeginSend(responseHeader, It.IsAny<Action<bool, INetworkFacade>>()), Times.Once()); client.Verify(m => m.BeginSend(body, It.IsAny<Action<bool, INetworkFacade>>()), Times.Once()); }
public void PersistentConnectionTest() { int i = 0; byte[] request = Encoding.UTF8.GetBytes( "GET / HTTP/1.1\r\nHost: www.yahoo.com\r\n\r\n" ); byte[] response = Encoding.UTF8.GetBytes( "HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nContent-Length: 2\r\n\r\nhi" ); byte[] request2 = Encoding.UTF8.GetBytes( "GET /foo.jpg HTTP/1.1\r\nHost: www.yahoo.com\r\n\r\n" ); byte[] response2 = Encoding.UTF8.GetBytes( "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 2\r\n\r\nhi" ); Mock<INetworkFacade> client = new Mock<INetworkFacade>(); client.Setup( m => m.BeginReceive( It.IsAny<Action<bool, byte[], INetworkFacade>>() ) ) .Callback( ( Action<bool, byte[], INetworkFacade> callback ) => callback( true, i++ == 0 ? request : request2, client.Object ) ); Mock<INetworkFacade> server = new Mock<INetworkFacade>(); server.Setup( m => m.BeginReceive( It.IsAny<Action<bool, byte[], INetworkFacade>>() ) ) .Callback( ( Action<bool, byte[], INetworkFacade> callback ) => callback( true, i++ == 1 ? response : response2, server.Object ) ); Mock<INetworkFacadeFactory> mockFactory = new Mock<INetworkFacadeFactory>(); mockFactory.Setup( m => m.BeginConnect( "www.yahoo.com", 80, It.IsAny<Action<bool, INetworkFacade>>() ) ) .Callback<string, int, Action<bool, INetworkFacade>>( ( h, p, c ) => c( true, server.Object ) ); Mock<IProxyFilter> outboundFilter = new Mock<IProxyFilter>(); outboundFilter.Setup( m => m.EvaluateConnectionFilters( It.IsAny<HttpRequest>(), It.IsAny<string>() ) ).Returns( null as string ); Mock<ICoreSettings> settings = new Mock<ICoreSettings>(); ProxySession session = new ProxySession( mockFactory.Object, outboundFilter.Object, settings.Object ); session.Start( client.Object ); client.Verify( m => m.BeginClose( It.IsAny<Action<bool, INetworkFacade>>() ), Times.Once() ); server.Verify( m => m.BeginClose( It.IsAny<Action<bool, INetworkFacade>>() ), Times.Once() ); mockFactory.Verify( m => m.BeginConnect( It.IsAny<string>(), It.IsAny<int>(), It.IsAny<Action<bool, INetworkFacade>>() ), Times.Once() ); outboundFilter.Verify( m => m.EvaluateConnectionFilters( It.IsAny<HttpRequest>(), It.IsAny<string>() ), Times.Exactly( 1 ) ); }
public void UseSslTest() { bool callbackInvoked = false; byte[] request = Encoding.UTF8.GetBytes("CONNECT www.yahoo.com:443 HTTP/1.1\r\nHost: www.yahoo.com\r\n\r\n"); // Client sends SSL request Mock<INetworkFacade> client = new Mock<INetworkFacade>(); client.Setup(m => m.BeginReceive(It.IsAny<Action<bool, byte[], INetworkFacade>>())) .Callback((Action<bool, byte[], INetworkFacade> callback) => callback(true, request, client.Object)); // The server should be contacted by the proxy server Mock<INetworkFacade> server = new Mock<INetworkFacade>(); // That the proxy server should attempt to connect on port 443 Mock<INetworkFacadeFactory> mockFactory = new Mock<INetworkFacadeFactory>(); mockFactory.Setup(m => m.BeginConnect("www.yahoo.com", 443, It.IsAny<Action<bool, INetworkFacade>>())) .Callback<string, int, Action<bool, INetworkFacade>>((h, p, c) => c(true, server.Object)); Mock<IProxyFilter> outboundFilter = new Mock<IProxyFilter>(); outboundFilter.Setup(m => m.EvaluateConnectionFilters(It.IsAny<HttpRequest>(), It.IsAny<string>())).Returns(null as string); // Setup the mock SSL tunnel. Yes, this is an integration test and not a pure unit test. Mock<ISslTunnel> mockSslTunnel = new Mock<ISslTunnel>(); mockSslTunnel.Setup( m => m.EstablishTunnel( client.Object, server.Object, "1.1" ) ) .Callback<INetworkFacade, INetworkFacade, string>( ( c, s, v ) => { callbackInvoked = true; mockSslTunnel.Raise( m => m.TunnelClosed += null, new EventArgs() ); } ); CoreFactory.Register( () => mockSslTunnel.Object ); Mock<ICoreSettings> settings = new Mock<ICoreSettings>(); ProxySession session = new ProxySession(mockFactory.Object, outboundFilter.Object, settings.Object); session.Start(client.Object); client.Verify(m => m.BeginClose(It.IsAny<Action<bool, INetworkFacade>>()), Times.Once()); server.Verify(m => m.BeginClose(It.IsAny<Action<bool, INetworkFacade>>()), Times.Once()); Assert.IsTrue(callbackInvoked); }
public void ResponseFilterTestWithoutBody() { byte[] request = Encoding.UTF8.GetBytes( "GET / HTTP/1.1\r\nHost: www.yahoo.com\r\n\r\n" ); byte[] response = Encoding.UTF8.GetBytes( "HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nContent-Length: 2\r\n\r\nhi" ); string filterResponse = "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 20\r\n\r\nbad things live here"; byte[] encodedFilterResponse = Encoding.UTF8.GetBytes( filterResponse ); // Request from client Mock<INetworkFacade> client = new Mock<INetworkFacade>(); client.Setup( m => m.BeginReceive( It.IsAny<Action<bool, byte[], INetworkFacade>>() ) ) .Callback( ( Action<bool, byte[], INetworkFacade> callback ) => callback( true, request, client.Object ) ); client.Setup( m => m.BeginSend( encodedFilterResponse, It.IsAny<Action<bool, INetworkFacade>>() ) ) .Callback<byte[], Action<bool, INetworkFacade>>( ( b, c ) => c( true, client.Object ) ); // Response from server Mock<INetworkFacade> server = new Mock<INetworkFacade>(); server.Setup( m => m.BeginReceive( It.IsAny<Action<bool, byte[], INetworkFacade>>() ) ) .Callback( ( Action<bool, byte[], INetworkFacade> callback ) => callback( true, response, server.Object ) ); // Mock the connection to the remote host Mock<INetworkFacadeFactory> mockFactory = new Mock<INetworkFacadeFactory>(); mockFactory.Setup( m => m.BeginConnect( "www.yahoo.com", 80, It.IsAny<Action<bool, INetworkFacade>>() ) ) .Callback<string, int, Action<bool, INetworkFacade>>( ( h, p, c ) => c( true, server.Object ) ); Mock<IProxyFilter> filter = new Mock<IProxyFilter>(); filter.Setup( m => m.EvaluateConnectionFilters( It.IsAny<HttpRequest>(), It.IsAny<string>() ) ).Returns( null as string ); filter.Setup( m => m.TryEvaluateResponseFilters( It.IsAny<IHttpResponse>(), It.IsAny<string>(), out filterResponse ) ).Returns( true ); Mock<ICoreSettings> settings = new Mock<ICoreSettings>(); ProxySession session = new ProxySession( mockFactory.Object, filter.Object, settings.Object ); session.Start( client.Object ); client.Verify( m => m.BeginClose( It.IsAny<Action<bool, INetworkFacade>>() ), Times.Once() ); mockFactory.Verify( m => m.BeginConnect( It.IsAny<string>(), It.IsAny<int>(), It.IsAny<Action<bool, INetworkFacade>>() ), Times.Once() ); filter.Verify( m => m.EvaluateConnectionFilters( It.IsAny<HttpRequest>(), It.IsAny<string>() ), Times.Once() ); filter.Verify( m => m.TryEvaluateResponseFilters( It.IsAny<IHttpResponse>(), It.IsAny<string>(), out filterResponse ), Times.Once() ); filter.Verify( m => m.EvaluateResponseFiltersWithBody( It.IsAny<IHttpResponse>(), It.IsAny<string>(), It.IsAny<byte[]>() ), Times.Never() ); }
public void SetSession(ProxySession session) { this.Session = session; }
protected override void OnNewSessionConnected(ProxySession session) { session.Type = ProxyType.Http; base.OnNewSessionConnected(session); }