Advertisement
FrayxRulez

Untitled

Feb 26th, 2015
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.26 KB | None | 0 0
  1. using Parse;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Runtime.InteropServices.WindowsRuntime;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using Windows.ApplicationModel;
  12. using Windows.Foundation;
  13. using Windows.Foundation.Collections;
  14. using Windows.Networking.Sockets;
  15. using Windows.Storage.Streams;
  16. using Windows.UI.Xaml;
  17. using Windows.UI.Xaml.Controls;
  18. using Windows.UI.Xaml.Controls.Primitives;
  19. using Windows.UI.Xaml.Data;
  20. using Windows.UI.Xaml.Input;
  21. using Windows.UI.Xaml.Media;
  22. using Windows.UI.Xaml.Navigation;
  23. using Windows.Web.Http;
  24. using Windows.Web.Http.Filters;
  25.  
  26. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
  27.  
  28. namespace TestProxy
  29. {
  30.     public struct INTERNET_PROXY_INFO
  31.     {
  32.         public uint dwAccessType;
  33.         public string lpszProxy;
  34.         public string lpszProxyBypass;
  35.     }
  36.  
  37.     public partial class NativeMethods
  38.     {
  39.         [DllImport("urlmon.dll", CharSet = CharSet.Ansi)]
  40.         public static extern uint UrlMkSetSessionOption(uint dwOption, [System.Runtime.InteropServices.InAttribute()] System.IntPtr lpBuffer, uint dwBufferLength, uint dwReserved);
  41.     }
  42.  
  43.     /// <summary>
  44.     /// An empty page that can be used on its own or navigated to within a Frame.
  45.     /// </summary>
  46.     public sealed partial class MainPage : Page
  47.     {
  48.         public MainPage()
  49.         {
  50.             this.InitializeComponent();
  51.         }
  52.  
  53.         public uint INTERNET_OPEN_TYPE_PROXY =                       3;   // via named proxy
  54.         public uint INTERNET_OPTION_PROXY =                          38;
  55.  
  56.         private async void Button_Click(object sender, RoutedEventArgs e)
  57.         {
  58.             this.server = new StreamSocketListener();
  59.             this.server.ConnectionReceived += ConnectionReceivedCallback;
  60.             await this.server.BindServiceNameAsync("33321");
  61.  
  62.             var proxy = new INTERNET_PROXY_INFO();
  63.             proxy.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
  64.             proxy.lpszProxy = "http://127.0.0.1:33321";
  65.             proxy.lpszProxyBypass = "";
  66.  
  67.             var size = Marshal.SizeOf<INTERNET_PROXY_INFO>(proxy);
  68.             var ptr = Marshal.AllocHGlobal(Marshal.SizeOf<INTERNET_PROXY_INFO>(proxy));
  69.             Marshal.StructureToPtr<INTERNET_PROXY_INFO>(proxy, ptr, false);
  70.  
  71.             var result = NativeMethods.UrlMkSetSessionOption(INTERNET_OPTION_PROXY, ptr, (uint)size, 0);
  72.  
  73.             Web.Navigate(new Uri("http://web.whatsapp.com/"));
  74.         }
  75.  
  76.         #region Server
  77.         private StreamSocketListener server;
  78.  
  79.         async void ConnectionReceivedCallback(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
  80.         {
  81.             DataReader reader = new DataReader(args.Socket.InputStream);
  82.             reader.InputStreamOptions = InputStreamOptions.Partial;
  83.             uint numStrBytes = await reader.LoadAsync(1024);
  84.             string request = reader.ReadString(numStrBytes);
  85.  
  86.             using (IOutputStream output = args.Socket.OutputStream)
  87.             {
  88.                 string requestMethod = request.Split('\n')[0];
  89.                 string[] requestParts = requestMethod.Split(' ');
  90.  
  91.                 if (requestParts[0] == "GET")
  92.                     await SendResponse(requestParts[1], output);
  93.             }
  94.         }
  95.  
  96.         private async Task SendResponse(string path, IOutputStream os)
  97.         {
  98.             var file = await Package.Current.InstalledLocation.GetFileAsync("Assets\\Default.png");
  99.             var content = await file.OpenStreamForReadAsync();
  100.  
  101.             using (Stream resp = os.AsStreamForWrite())
  102.             {
  103.  
  104.                 string header = String.Format("HTTP/1.1 200 OK\r\n" +
  105.                     "Content-Length: {0}\r\n" +
  106.                     "Content-Type:  image/png\r\n" +
  107.                     "Connection: close\r\n" +
  108.                     "\r\n",
  109.                     content.Length);
  110.                 byte[] headerArray = Encoding.UTF8.GetBytes(header);
  111.                 await resp.WriteAsync(headerArray, 0, headerArray.Length);
  112.                 await content.CopyToAsync(resp);
  113.             }
  114.         }
  115.         #endregion
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement