Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Parse;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Runtime.InteropServices.WindowsRuntime;
- using System.Text;
- using System.Threading.Tasks;
- using Windows.ApplicationModel;
- using Windows.Foundation;
- using Windows.Foundation.Collections;
- using Windows.Networking.Sockets;
- using Windows.Storage.Streams;
- using Windows.UI.Xaml;
- using Windows.UI.Xaml.Controls;
- using Windows.UI.Xaml.Controls.Primitives;
- using Windows.UI.Xaml.Data;
- using Windows.UI.Xaml.Input;
- using Windows.UI.Xaml.Media;
- using Windows.UI.Xaml.Navigation;
- using Windows.Web.Http;
- using Windows.Web.Http.Filters;
- // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
- namespace TestProxy
- {
- public struct INTERNET_PROXY_INFO
- {
- public uint dwAccessType;
- public string lpszProxy;
- public string lpszProxyBypass;
- }
- public partial class NativeMethods
- {
- [DllImport("urlmon.dll", CharSet = CharSet.Ansi)]
- public static extern uint UrlMkSetSessionOption(uint dwOption, [System.Runtime.InteropServices.InAttribute()] System.IntPtr lpBuffer, uint dwBufferLength, uint dwReserved);
- }
- /// <summary>
- /// An empty page that can be used on its own or navigated to within a Frame.
- /// </summary>
- public sealed partial class MainPage : Page
- {
- public MainPage()
- {
- this.InitializeComponent();
- }
- public uint INTERNET_OPEN_TYPE_PROXY = 3; // via named proxy
- public uint INTERNET_OPTION_PROXY = 38;
- private async void Button_Click(object sender, RoutedEventArgs e)
- {
- this.server = new StreamSocketListener();
- this.server.ConnectionReceived += ConnectionReceivedCallback;
- await this.server.BindServiceNameAsync("33321");
- var proxy = new INTERNET_PROXY_INFO();
- proxy.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
- proxy.lpszProxy = "http://127.0.0.1:33321";
- proxy.lpszProxyBypass = "";
- var size = Marshal.SizeOf<INTERNET_PROXY_INFO>(proxy);
- var ptr = Marshal.AllocHGlobal(Marshal.SizeOf<INTERNET_PROXY_INFO>(proxy));
- Marshal.StructureToPtr<INTERNET_PROXY_INFO>(proxy, ptr, false);
- var result = NativeMethods.UrlMkSetSessionOption(INTERNET_OPTION_PROXY, ptr, (uint)size, 0);
- Web.Navigate(new Uri("http://web.whatsapp.com/"));
- }
- #region Server
- private StreamSocketListener server;
- async void ConnectionReceivedCallback(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
- {
- DataReader reader = new DataReader(args.Socket.InputStream);
- reader.InputStreamOptions = InputStreamOptions.Partial;
- uint numStrBytes = await reader.LoadAsync(1024);
- string request = reader.ReadString(numStrBytes);
- using (IOutputStream output = args.Socket.OutputStream)
- {
- string requestMethod = request.Split('\n')[0];
- string[] requestParts = requestMethod.Split(' ');
- if (requestParts[0] == "GET")
- await SendResponse(requestParts[1], output);
- }
- }
- private async Task SendResponse(string path, IOutputStream os)
- {
- var file = await Package.Current.InstalledLocation.GetFileAsync("Assets\\Default.png");
- var content = await file.OpenStreamForReadAsync();
- using (Stream resp = os.AsStreamForWrite())
- {
- string header = String.Format("HTTP/1.1 200 OK\r\n" +
- "Content-Length: {0}\r\n" +
- "Content-Type: image/png\r\n" +
- "Connection: close\r\n" +
- "\r\n",
- content.Length);
- byte[] headerArray = Encoding.UTF8.GetBytes(header);
- await resp.WriteAsync(headerArray, 0, headerArray.Length);
- await content.CopyToAsync(resp);
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement