private void btn_click(object sender, RoutedEventArgs e) { Task.Factory.StartNew(() => { for (int i = 0; i < 8; i++) { Task.Factory.StartNew(() => { IClient client = TcpProxy.GetClient(); client.Connect(6000, "127.0.0.1"); client.SetConnectedHandler((ce) => { if (ce.StatusCode != 0) { MessageBox.Show(ce.Msg); return; } ce.Conn.Send(Encoding.UTF8.GetBytes("hello world909000000000000000000000000000000009090909091111666666666666666666666666" + "7777777777778888888888888888888888888811111111111111111111111111111111111111111111111100000000000000000000" + "6666666666666666666666666pppppppppppppppppppppppppppppppppppppppppppppppppppp10110")); }); }); // Thread.Sleep(1); } }); }
public void ShouldConnectThroughTcpProxy() { using (var tcpProxy = new TcpProxy(12424)) using (var context = new TestDatabaseContextProxy(tcpProxy.Forwarding)) using (var database = new ODatabase(TestConnectionProxy.GlobalTestDatabaseAlias)) { SimpleTest(database); } }
static void Main(string[] args) { var definition = new ProxyDefinition() { ServerAddress = IPAddress.Any }; // Use localhost if the debugger is attached if (Debugger.IsAttached) { definition.ServerAddress = IPAddress.Any; definition.ServerPort = 4501; } else { Console.WriteLine("Enter server port:"); var port = Console.ReadLine(); if (!String.IsNullOrEmpty(port)) { definition.ServerPort = Convert.ToInt16(port); } Console.WriteLine("Enter client/destination address:"); var address = Console.ReadLine(); if (!String.IsNullOrEmpty(port)) { definition.ClientAddress = IPAddress.Parse(address); } Console.WriteLine("Enter client/destination port:"); var clientPort = Console.ReadLine(); if (!String.IsNullOrEmpty(port)) { definition.ClientPort = Convert.ToInt16(clientPort); } } Proxy = new TcpProxy(definition); Proxy.Start(); Console.WriteLine("Proxy started between {0}:{1} and {2}:{3}", definition.ServerAddress, definition.ServerPort, definition.ClientAddress, definition.ClientPort); Proxy.BytesTransfered += Proxy_BytesTransfered; Proxy.ServerDataSentToClient += Proxy_ServerDataSentToClient; Proxy.ClientDataSentToServer += Proxy_ClientDataSentToServer; Console.WriteLine("Press any key to stop proxy"); Console.ReadLine(); Proxy.Stop(); Console.WriteLine("Press any key to exit"); Console.ReadLine(); }
private void Button_Click_1(object sender, RoutedEventArgs e) { IServer srv = TcpProxy.GetServer(); srv.SetConnectedHandler((ce) => { Interlocked.Increment(ref countSyn); this.Dispatcher.Invoke(new Action(() => { this.txtSyn.Text = countSyn.ToString(); })); }); srv.Start(6000, out _); }
static void Main(string[] args) { // Packets from 127.0.0.1 are forwarded to 127.0.0.1:1007 var newRule = new ProxyRule(IPAddress.Parse("127.0.0.1"), new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1007)); configuration.AddRule(newRule); var proxy = new TcpProxy(configuration); Console.ReadKey(); }
public static void OnTcpConnect(IAsyncResult ret) { LocalMsg result = new LocalMsg(); try { sProxy.EndConnect(ret); if (tConn != null) { tConn.Change(Timeout.Infinite, Timeout.Infinite); } } catch (Exception exp) { Log.LogInfo(exp.Message); result.Message = (int)LocalMsgType.Connect; result.Result = 0; ProtoHandler.PostMessage(result); if (tConn != null) { tConn.Change(5000, 5000); } return; } result.Message = (int)LocalMsgType.Connect; result.Result = 1; ProtoHandler.PostMessage(result); if (proxy == null) { proxy = new TcpProxy(); } try { sProxy.BeginReceive(proxy.GetBuffer(), 0, TcpProxy.PacketSize, SocketFlags.None, OnReceivedData, sProxy); } catch { result.Message = (int)LocalMsgType.DisConnect; result.Result = 0; ProtoHandler.PostMessage(result); sProxy.Close(); sProxy = null; proxy = null; if (tConn != null) { tConn.Change(5000, 5000); } } }
private static async Task <IEnumerable <Task> > GetTcpProxyTasks(AppConfiguration config, CancellationToken token) { await Task.Yield(); var tasks = new List <Task>(); foreach (var proxy in config.Proxies) { var tcpProxy = new TcpProxy(proxy.Host, proxy.ProxyHost, proxy.ControlPort, token); tasks.Add(tcpProxy.Run()); } return(tasks); }
public void ShouldReconnectIfTcpProxyIsRestored() { using (var tcpProxy = new TcpProxy(12424)) using (var context = new TestDatabaseContextProxy(tcpProxy.Forwarding)) using (var database = new ODatabase(TestConnectionProxy.GlobalTestDatabaseAlias)) { tcpProxy.End(); tcpProxy.Start(); try { SimpleTest(database); Assert.True(false, "SimpleTest should of failed without a socket connection"); } catch { SimpleTest(database); } } }
public void ShouldReconnectIfTcpProxyIsRestored() { using (var tcpProxy = new TcpProxy(12424)) using (var context = new TestDatabaseContextProxy(tcpProxy.Forwarding)) using (var database = new ODatabase(TestConnectionProxy.GlobalTestDatabaseAlias)) { tcpProxy.End(); tcpProxy.Start(); try { SimpleTest(database); Assert.Fail("SimpleTest should of failed without a socket connection"); } catch { SimpleTest(database); } } }
static void Main(string[] args) { IServer server = TcpProxy.GetServerAsync(); int count = 0; server.SetConnectedHandler((e) => { Console.WriteLine($"Connection Count is:{Interlocked.Increment(ref count)},Port is:{e.Conn.GetSourcePort()}"); //e.Conn.Receive(4,(buf)=> //{ // int len = BitConverter.ToInt32(buf, 0); // e.Conn.Receive(len, (cbuf) => // { // Console.WriteLine(Encoding.UTF8.GetString(cbuf)); // }); //}); }); server.Start(7000, out _); Console.WriteLine("start..."); Console.Read(); }
public async Task Proxy_streams_in_both_directions() { using (var clientStream = new LoopbackTcpStream(1, 2, 3, 4, 5)) using (var remoteStream = new LoopbackTcpStream(10, 9, 8, 7, 6)) { var connector = remoteStream.GetConnector(); clientStream.Write(200, 199, 198, 197, 196); remoteStream.Write(100, 99, 98, 97, 96); clientStream.CloseStagedBytesSender(); remoteStream.CloseStagedBytesSender(); using (var proxy = new TcpProxy( clientStream, FakeEndPoints.CreateRemote(), connector, this._bufferPool)) { await proxy.RunAsync(CancellationToken.None).ConfigureAwait(true); } clientStream.GetAllWrittenBytes().Should().Equal(200, 199, 198, 197, 196, 10, 9, 8, 7, 6); remoteStream.GetAllWrittenBytes().Should().Equal(100, 99, 98, 97, 96, 1, 2, 3, 4, 5); } }
public DeviceClient(int _clientId, TcpProxy _proxy) { id = _clientId; proxy = _proxy; tcp = new TCP(id, this); }
static void OnReceivedData(IAsyncResult ar) { int len = 0; try { len = sProxy.EndReceive(ar); } catch { } if (len <= 0) { if (!quit) { LocalMsg msg = new LocalMsg(); msg.Message = (int)LocalMsgType.DisConnect; msg.Result = 1; ProtoHandler.PostMessage(msg); sProxy.Close(); sProxy = null; proxy = null; if (tConn != null) { tConn.Change(5000, 5000); } } return; } lock (Packet) { if (!proxy.Analysis(len, Packet)) { sProxy.Close(); sProxy = null; return; } } logicEvent.Set(); if (!quit) { try { sProxy.BeginReceive(proxy.GetBuffer(), 0, TcpProxy.PacketSize, SocketFlags.None, OnReceivedData, sProxy); } catch { LocalMsg msg = new LocalMsg(); msg.Message = (int)LocalMsgType.DisConnect; msg.Result = 1; ProtoHandler.PostMessage(msg); sProxy.Close(); sProxy = null; proxy = null; if (tConn != null) { tConn.Change(5000, 5000); } } } }