public void TestJsonPingResponseWriteRead() { // Arrange var address = new ProxySocketAddress(); address.Flags = 0xff; address.Port = 65500; address.Host = "rawrawrawraw"; var args = PingResponse.Create(address); args.PhysicalAddress[3] = 3; args.PhysicalAddress[5] = 5; args.TimeMs = 1333333; var response = Message.Create(null, null, null, args); var stream = new MemoryStream(); // Act response.Encode(stream, CodecId.Json); byte[] buf = Interop.MessageDecodeEncode(CodecId.Json, 1, stream.GetBuffer(), stream.Length, buffer, buffer.Length); var returned = Serializable.Decode <Message>(new MemoryStream(buf), CodecId.Json); // Assert Assert.IsTrue(response.Equals(returned)); }
public void TestJsonLinkRequestWriteRead2() { // Arrange var address = new ProxySocketAddress(); address.Port = 443; address.Host = "myhosttolinkto"; var props = new SocketInfo(); props.Address = address; props.Family = AddressFamily.InterNetwork; props.Protocol = ProtocolType.Udp; props.Type = SocketType.Stream; props.Flags = (uint)SocketFlags.Passive; for (Int32 i = 0; i < 10; i++) { props.Options.Add(Property <ulong> .Create((uint)(13 - i), (ulong)i)); } var request = Message.Create(null, null, null, LinkRequest.Create(props)); var stream = new MemoryStream(); // Act request.Encode(stream, CodecId.Json); byte[] buf = Interop.MessageDecodeEncode(CodecId.Json, 1, stream.GetBuffer(), stream.Length, buffer, buffer.Length); var returned = Serializable.Decode <Message>(new MemoryStream(buf), CodecId.Json); // Assert Assert.IsTrue(request.Equals(returned)); }
public void TestJsonLinkRequestWriteRead2() { // Arrange ProxySocketAddress address = new ProxySocketAddress(); address.Port = 443; address.Host = "myhosttolinkto"; SocketInfo props = new SocketInfo(); props.Address = address; props.Family = AddressFamily.InterNetwork; props.Protocol = ProtocolType.Udp; props.Type = SocketType.Stream; props.Flags = (uint)SocketFlags.Passive; for (Int32 i = 0; i < 10; i++) { var ov = new Property <ulong>(); ov.Type = (uint)(13 - i); ov.Value = (ulong)i; props.Options.Add(ov); } Message request = new Message(null, null, null, new LinkRequest(props)); MemoryStream stream = new MemoryStream(); // Act request.Encode(stream, CodecId.Json); byte[] buf = Interop.MessageDecodeEncode(CodecId.Json, 1, stream.GetBuffer(), stream.Length, buffer, buffer.Length); Message returned = Message.Decode(new MemoryStream(buf), CodecId.Json); // Assert Assert.IsTrue(request.Equals(returned)); }
public void TestMpackPingResponseWriteRead() { // Arrange ProxySocketAddress address = new ProxySocketAddress(); address.Flags = 0xff; address.Port = 65500; address.Host = "rawrawrawraw"; PingResponse args = new PingResponse(address); args.PhysicalAddress[3] = 3; args.PhysicalAddress[5] = 5; args.TimeMs = 1333333; Message response = new Message(null, null, null, args); MemoryStream stream = new MemoryStream(); // Act response.Encode(stream, CodecId.Mpack); byte[] buf = Interop.MessageDecodeEncode(CodecId.Mpack, 1, stream.GetBuffer(), stream.Length, buffer, buffer.Length); Message returned = Message.Decode(new MemoryStream(buf), CodecId.Mpack); // Assert Assert.IsTrue(response.Equals(returned)); }
public void TestJsonLinkRequestWriteRead1() { // Arrange ProxySocketAddress address = new ProxySocketAddress(); address.Port = 443; address.Host = "myhosttolinkto"; SocketInfo props = new SocketInfo(); props.Address = address; props.Family = AddressFamily.InterNetwork; props.Protocol = ProtocolType.Udp; props.Type = SocketType.Stream; props.Flags = (UInt32)SocketFlags.Passive; // no options Message request = new Message(null, null, null, new LinkRequest(props)); MemoryStream stream = new MemoryStream(); // Act request.Encode(stream, CodecId.Json); byte[] buf = Interop.MessageDecodeEncode(CodecId.Json, 1, stream.GetBuffer(), stream.Length, buffer, buffer.Length); Message returned = Message.Decode(new MemoryStream(buf), CodecId.Json); // Assert Assert.IsTrue(request.Equals(returned)); }
public void TestJsonResolveResponseWriteRead() { // Arrange ProxySocketAddress address = new ProxySocketAddress(); address.Flow = 0xff; address.Port = 65500; address.Host = "rawrawrawraw"; ResolveResponse args = new ResolveResponse(); for (UInt32 i = 0; i < 10; i++) { var ai = new AddressInfo(); ai.CanonicalName = String.Format("name{0}", i); var sa = new Inet4SocketAddress(i | 0xf0000, (UInt16)i, i + 10); ai.SocketAddress = sa; args.Results.Add(ai); } Message response = new Message(null, null, null, args); MemoryStream stream = new MemoryStream(); // Act response.Encode(stream, CodecId.Json); byte[] buf = Interop.MessageDecodeEncode(CodecId.Json, 1, stream.GetBuffer(), stream.Length, buffer, buffer.Length); Message returned = Message.Decode(new MemoryStream(buf), CodecId.Json); // Assert Assert.IsTrue(response.Equals(returned)); }
public void TestJsonLinkRequestWriteRead3() { // Arrange ProxySocketAddress address = new ProxySocketAddress(); address.Port = 1; address.Host = ""; SocketInfo props = new SocketInfo(); props.Address = address; props.Family = AddressFamily.Proxy; props.Protocol = ProtocolType.Unspecified; props.Type = SocketType.Dgram; props.Flags = (uint)SocketFlags.Internal; for (Int32 i = 0; i < 3; i++) { var ov = new Property <ulong>(); ov.Type = (uint)(13 - i); ov.Value = (ulong)i; props.Options.Add(ov); } props.Options.Add(new Property <MulticastOption>((uint)SocketOption.IpMulticastJoin, new Inet4MulticastOption { InterfaceIndex = 5, Address = BitConverter.GetBytes((int)234) })); var ab = new byte[16]; new Random().NextBytes(ab); props.Options.Add(new Property <MulticastOption>((uint)SocketOption.IpMulticastLeave, new Inet6MulticastOption { InterfaceIndex = 5, Address = ab })); Message request = new Message(null, null, null, new LinkRequest(props)); MemoryStream stream = new MemoryStream(); // Act request.Encode(stream, CodecId.Json); byte[] buf = Interop.MessageDecodeEncode(CodecId.Json, 1, stream.GetBuffer(), stream.Length, buffer, buffer.Length); Message returned = Message.Decode(new MemoryStream(buf), CodecId.Json); // Assert Assert.IsTrue(request.Equals(returned)); }
public void TestMpackLinkRequestWriteRead3() { // Arrange var address = new ProxySocketAddress(); address.Port = 1; address.Host = ""; var props = new SocketInfo(); props.Address = address; props.Family = AddressFamily.Proxy; props.Protocol = ProtocolType.Unspecified; props.Type = SocketType.Dgram; props.Flags = (uint)SocketFlags.Internal; for (Int32 i = 0; i < 3; i++) { props.Options.Add(Property <ulong> .Create((uint)(13 - i), (ulong)i)); } props.Options.Add(Property <IMulticastOption> .Create((uint)SocketOption.IpMulticastJoin, new Inet4MulticastOption { InterfaceIndex = 5, Address = BitConverter.GetBytes((int)234) })); var ab = new byte[16]; new Random().NextBytes(ab); props.Options.Add(Property <IMulticastOption> .Create((uint)SocketOption.IpMulticastLeave, new Inet6MulticastOption { InterfaceIndex = 5, Address = ab })); var request = Message.Create(null, null, null, LinkRequest.Create(props)); var stream = new MemoryStream(); // Act request.Encode(stream, CodecId.Mpack); byte[] buf = Interop.MessageDecodeEncode(CodecId.Mpack, 1, stream.GetBuffer(), stream.Length, buffer, buffer.Length); var returned = Serializable.Decode <Message>(new MemoryStream(buf), CodecId.Mpack); // Assert Assert.IsTrue(request.Equals(returned)); }
public void TestJsonPingRequestWriteRead() { // Arrange var address = new ProxySocketAddress(); address.Flags = 1; address.Port = 777; address.Host = "localhost"; var request = Message.Create(null, null, null, PingRequest.Create(address)); var stream = new MemoryStream(); // Act request.Encode(stream, CodecId.Json); byte[] buf = Interop.MessageDecodeEncode(CodecId.Json, 1, stream.GetBuffer(), stream.Length, buffer, buffer.Length); var returned = Serializable.Decode <Message>(new MemoryStream(buf), CodecId.Json); // Assert Assert.IsTrue(request.Equals(returned)); }
/// <summary> /// Resolve address /// </summary> static async Task <IEnumerable <DnsHostEntry> > ResolveAddressAsync(ProxySocketAddress addr, int period, bool cache) { Console.WriteLine($"Resolving {addr} for {period} ms..."); var entries = new HashSet <DnsHostEntry>(); var cts = new CancellationTokenSource(period); try { using (var resolver = await Dns.ResolveAsync(addr, cache, cts.Token)) { cts.Token.ThrowIfCancellationRequested(); while (await resolver.MoveNextAsync(cts.Token)) { cts.Token.ThrowIfCancellationRequested(); Console.WriteLine($"{DateTime.Now}: {resolver.Current}"); entries.Add(resolver.Current); } } } catch (OperationCanceledException) { } return(entries); }
public void TestMpackPingRequestWriteRead() { // Arrange ProxySocketAddress address = new ProxySocketAddress(); address.Flags = 1; address.Port = 777; address.Host = "localhost"; Message request = new Message(null, null, null, new PingRequest(address)); MemoryStream stream = new MemoryStream(); // Act request.Encode(stream, CodecId.Mpack); byte[] buf = Interop.MessageDecodeEncode(CodecId.Mpack, 1, stream.GetBuffer(), stream.Length, buffer, buffer.Length); Message returned = Message.Decode(new MemoryStream(buf), CodecId.Mpack); // Assert Assert.IsTrue(request.Equals(returned)); }
public async override Task <SocketAddress> ReadAsync(Reader reader, SerializerContext context, CancellationToken ct) { SocketAddress result = null; int members = await reader.ReadObjectHeaderAsync(ct).ConfigureAwait(false); if (members < 1) { throw new FormatException( $"Unexpected number of properties {members}"); } AddressFamily family = (AddressFamily) await reader.ReadInt32Async(ct).ConfigureAwait(false); if (family == AddressFamily.Unspecified) { result = new NullSocketAddress(); } else if (family == AddressFamily.Unix) { if (members < 2) { throw new FormatException( $"Unexpected number of properties {members}"); } string path = await reader.ReadStringAsync(ct).ConfigureAwait(false); result = new UnixSocketAddress(path); } else if ( family != AddressFamily.Proxy && family != AddressFamily.InterNetwork && family != AddressFamily.InterNetworkV6) { throw new FormatException($"Bad address family {family}"); } else { if (members < 4) { throw new FormatException( $"Unexpected number of properties {members}"); } uint flow = await reader.ReadUInt32Async(ct).ConfigureAwait(false); ushort port = await reader.ReadUInt16Async(ct).ConfigureAwait(false); byte[] address; switch (family) { case AddressFamily.InterNetwork: address = await reader.ReadBinAsync(ct).ConfigureAwait(false); if (address.Length != 4) { throw new FormatException( $"Bad v4 address size {address.Length}"); } result = new Inet4SocketAddress(address, port, flow); break; case AddressFamily.InterNetworkV6: address = await reader.ReadBinAsync(ct).ConfigureAwait(false); if (address.Length != 16) { throw new FormatException( $"Bad v6 address size {address.Length}"); } if (members < 5) { throw new FormatException( $"Unexpected number of properties {members}"); } uint scopeId = await reader.ReadUInt32Async(ct).ConfigureAwait(false); result = new Inet6SocketAddress(address, port, flow, scopeId); break; case AddressFamily.Proxy: default: string host = await reader.ReadStringAsync(ct).ConfigureAwait(false); result = new ProxySocketAddress(host, port, flow); break; } } return(result); }
/// <summary> /// Main program entry point /// </summary> static void Main(string[] args) { Op op = Op.None; bool cache = false; int period = 60 * 1000; DnsServiceRecord record = null; ProxySocketAddress address = null; // Parse command line try { for (int i = 0; i < args.Length; i++) { switch (args[i]) { case "-a": case "--all": if (op != Op.None) { throw new ArgumentException("Operations are mutual exclusive"); } op = Op.All; break; case "-s": case "--services": i++; if (op != Op.None) { throw new ArgumentException("Operations are mutual exclusive"); } op = Op.Browse; if (i < args.Length) { record = DnsServiceRecord.Parse(args[i]); } break; case "-t": case "--timeout": i++; if (i >= args.Length || !int.TryParse(args[i], out period)) { throw new ArgumentException($"Bad -t arg"); } break; case "-r": case "--resolve": i++; if (op != Op.None) { throw new ArgumentException("Operations are mutual exclusive"); } op = Op.Resolve; if (i < args.Length) { address = ProxySocketAddress.Parse(args[i]); } break; case "-d": case "--dir": i++; if (op != Op.None) { throw new ArgumentException("Operations are mutual exclusive"); } op = Op.Dir; if (i < args.Length) { address = ProxySocketAddress.Parse(args[i]); } break; case "--fs": if (op != Op.None) { throw new ArgumentException("Operations are mutual exclusive"); } op = Op.Fs; break; case "--use-cache": cache = true; break; case "-R": case "--relay": Socket.Provider = Provider.RelayProvider.CreateAsync().Result; break; case "-?": case "-h": case "--help": throw new ArgumentException("Help"); default: throw new ArgumentException($"Unknown {args[i]}"); } } } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine( @" Browser - Proxy .net browser sample. usage: Browser [options] operation [args] Options: -t --timeout Timeout in ms to use for each browse request. --use-cache Return data from cache only (meaning depends on operation). --relay -R Use relay provider instead of default provider. --help -? -h Prints out this help. Operations (Mutually exclusive): -s --services Without further argument browses all domains. Otherwise browses for service types or service names in a domain. If service name is provided, resolves to host:port and txt. -a --all Browse all services in all domains on all proxies and resolve each one. (default!) -r --resolve Resolve host to address (getaddrbyhost) or address to host (gethostbyaddr) on all proxies. -d --dir Browse a folder on any proxy. --fs Browse entire file system on all proxies recursively. " ); return; } if (op == Op.Browse) { if (record != null) { if (string.IsNullOrEmpty(record.Name)) { var entries = BrowseServicesAsync( record.Type, record.Domain, period, cache).Result; } else { var entries = ResolveServiceAsync( record, period, cache).Result; } } else { var entries = BrowseDomainsAsync(period).Result; Console.WriteLine($"{entries.Count} entries found!!!"); } } else if (op == Op.Resolve) { var entries = ResolveAddressAsync(address, period, cache).Result; } else if (op == Op.Dir) { var files = BrowseFilesAsync(null, address?.Host, period, cache).Result; Console.WriteLine($"{files.Count} files/folders found!!!"); } else if (op == Op.Fs) { BrowseFilesRecursiveAsync(null, null, period, cache).Wait(); } else if (op == Op.All || op == Op.None) { Console.WriteLine("Browse and resolve all services"); var entries = ResolveServiceNamesAsync(period).Result; Console.WriteLine($"{entries.Count} entries resolved!!!"); } Console.WriteLine("Press a key to exit..."); Console.ReadKey(); }