[tomboy] [websync] libproxy support with authentication



commit 1e7d6fd4344e45267cd94d0002b456295233e327
Author: Alejandro J. Cura <alecu canonical com>
Date:   Thu Jun 10 17:27:05 2010 -0300

    [websync] libproxy support with authentication
    
    https://bugzilla.gnome.org/show_bug.cgi?id=621249

 .../WebSyncService/Api/AnonymousConnection.cs      |    2 +-
 Tomboy/Addins/WebSyncService/Api/OAuth.cs          |    2 +-
 .../Addins/WebSyncService/Api/ProxiedWebRequest.cs |   82 +++++++++++++++++++
 Tomboy/Addins/WebSyncService/LibProxy/LibProxy.cs  |   83 ++++++++++++++++++++
 Tomboy/Addins/WebSyncService/Makefile.am           |    1 +
 .../WebSyncService/WebSyncService-mac.csproj       |    2 +
 Tomboy/Addins/WebSyncService/WebSyncService.csproj |    2 +
 7 files changed, 172 insertions(+), 2 deletions(-)
---
diff --git a/Tomboy/Addins/WebSyncService/Api/AnonymousConnection.cs b/Tomboy/Addins/WebSyncService/Api/AnonymousConnection.cs
index 11a349d..7c377e2 100644
--- a/Tomboy/Addins/WebSyncService/Api/AnonymousConnection.cs
+++ b/Tomboy/Addins/WebSyncService/Api/AnonymousConnection.cs
@@ -70,7 +70,7 @@ namespace Tomboy.WebSync.Api
 			ServicePointManager.CertificatePolicy = new CertificateManager ();
 
 			try {
-				webRequest = System.Net.WebRequest.Create (uri) as HttpWebRequest;
+				webRequest = ProxiedWebRequest.Create (uri);
 
 				webRequest.Method = method;
 				webRequest.ServicePoint.Expect100Continue = false;
diff --git a/Tomboy/Addins/WebSyncService/Api/OAuth.cs b/Tomboy/Addins/WebSyncService/Api/OAuth.cs
index 5a039f4..e2fed66 100644
--- a/Tomboy/Addins/WebSyncService/Api/OAuth.cs
+++ b/Tomboy/Addins/WebSyncService/Api/OAuth.cs
@@ -234,7 +234,7 @@ namespace Tomboy.WebSync.Api
 			ServicePointManager.CertificatePolicy = new CertificateManager ();
 
 			// TODO: Set UserAgent, Timeout, KeepAlive, Proxy?
-			HttpWebRequest webRequest = System.Net.WebRequest.Create (url) as HttpWebRequest;
+			HttpWebRequest webRequest = ProxiedWebRequest.Create (url);
 			webRequest.Method = method.ToString ();
 			webRequest.ServicePoint.Expect100Continue = false;
 
diff --git a/Tomboy/Addins/WebSyncService/Api/ProxiedWebRequest.cs b/Tomboy/Addins/WebSyncService/Api/ProxiedWebRequest.cs
new file mode 100644
index 0000000..72cd9a2
--- /dev/null
+++ b/Tomboy/Addins/WebSyncService/Api/ProxiedWebRequest.cs
@@ -0,0 +1,82 @@
+
+using System;
+using System.Net;
+using LibProxy;
+
+namespace Tomboy.WebSync
+{
+	public static class ProxiedWebRequest
+	{		
+		private const string useProxyAuthentication =
+			"/system/http_proxy/use_authentication";
+		private const string proxyAuthenticationUser =
+			"/system/http_proxy/authentication_user";
+		private const string proxyAuthenticationPassword =
+			"/system/http_proxy/authentication_password";
+
+		public static bool useLibProxy = true;
+		public static HttpWebRequest Create (string uri)
+		{
+			HttpWebRequest webRequest = WebRequest.Create (uri) as HttpWebRequest;
+			if (useLibProxy) {
+				try {
+					ApplyProxy (webRequest, uri);
+				} catch (System.DllNotFoundException) {
+					Logger.Warn ("libproxy not installed");
+					useLibProxy = false;
+				}
+			}
+			
+			return webRequest;
+		}
+		
+		private static void ApplyProxy (HttpWebRequest webRequest, string uri)
+		{
+			ProxyFactory pf = new LibProxy.ProxyFactory ();
+			string[] proxies = pf.GetProxies (uri);
+			
+			foreach (string proxy in proxies) {
+				Uri proxyUri = new Uri (proxy);
+				string scheme = proxyUri.Scheme;
+				if (scheme == "direct") {
+					break;
+				} else if (scheme == "http" || scheme == "https") {
+					WebProxy webProxy = new WebProxy ();
+					
+					if (UseAuthentication ()) {
+						ICredentials credentials = 
+							new NetworkCredential (GetAuthUser (), GetAuthPass ());
+						webProxy.Credentials = credentials;
+					}
+					
+					webProxy.Address = proxyUri;
+					webRequest.Proxy = webProxy;
+					break;
+				}
+			}
+		}
+		
+		// this settings are taken from GConf/xml until libproxy supports
+		// returning the user/password to use for the proxy
+		// TODO: fix when libproxy release 0.5 is out
+		public static bool UseAuthentication ()
+		{
+			object useProxyAuth = Preferences.Get (useProxyAuthentication);
+			
+			if (useProxyAuth == null) {
+				return false;
+			}
+			return (bool) useProxyAuth;
+		}
+
+		public static string GetAuthUser ()
+		{
+			return Preferences.Get (proxyAuthenticationUser) as string;
+		}
+		
+		public static string GetAuthPass ()
+		{
+			return Preferences.Get (proxyAuthenticationPassword) as string;
+		}
+	}
+}
diff --git a/Tomboy/Addins/WebSyncService/LibProxy/LibProxy.cs b/Tomboy/Addins/WebSyncService/LibProxy/LibProxy.cs
new file mode 100644
index 0000000..1a46ef7
--- /dev/null
+++ b/Tomboy/Addins/WebSyncService/LibProxy/LibProxy.cs
@@ -0,0 +1,83 @@
+/***************************************************
+ *  This code comes from the libproxy C library
+ **************************************************/
+
+/*******************************************************************************
+ * libproxy - A library for proxy configuration
+ * Copyright (C) 2006 Nathaniel McCallum <nathaniel natemccallum com>
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+ ******************************************************************************/
+
+namespace LibProxy {
+        using System;
+        using System.Runtime.InteropServices;
+
+        public class ProxyFactory {
+                private HandleRef self;
+
+                [DllImport ("proxy")]
+                private static extern
+                        IntPtr px_proxy_factory_new();
+                
+                [DllImport ("proxy")]
+                private static extern 
+                        IntPtr px_proxy_factory_get_proxies(HandleRef self, string url);
+                
+                [DllImport ("proxy")]
+                private static extern
+                        void px_proxy_factory_free(HandleRef self);
+
+                public ProxyFactory()
+                {
+                        this.self = new HandleRef(this, px_proxy_factory_new());
+                }
+                
+                public string[] GetProxies(string url)
+                {
+                        int count = 0;
+                        
+                        // Get the results
+                        // TODO: If we call both this function and px_proxy_factory_free()
+                        // this crashes, figure out why...
+                        IntPtr array = px_proxy_factory_get_proxies(this.self, url);
+
+                        // Count the number of returned strings
+                        while (Marshal.ReadIntPtr(array, count * IntPtr.Size) != IntPtr.Zero) count++;
+                        
+                        // Allocate a correctly sized array
+                        string[] proxies = new string[count];
+                        
+                        // Fill the response array
+                        for (int i=0 ; i < count ; i++)
+                        {
+                                IntPtr p = Marshal.ReadIntPtr(array, i * IntPtr.Size);
+                                proxies[i] = Marshal.PtrToStringAnsi(p);
+                                Marshal.FreeCoTaskMem(p); // Free the string
+                        }
+                        
+                        // Free the array itself
+                        Marshal.FreeCoTaskMem(array);
+                        
+                        return proxies;
+                }
+                
+                ~ProxyFactory()
+                {
+                        // TODO: See note above...
+                        px_proxy_factory_free(this.self);
+                }
+        }
+}
diff --git a/Tomboy/Addins/WebSyncService/Makefile.am b/Tomboy/Addins/WebSyncService/Makefile.am
index e722830..75387ad 100644
--- a/Tomboy/Addins/WebSyncService/Makefile.am
+++ b/Tomboy/Addins/WebSyncService/Makefile.am
@@ -32,6 +32,7 @@ CSFILES = \
 	$(srcdir)/Api/Tests/*.cs            \
 	$(srcdir)/Hyena.Json/*.cs           \
 	$(srcdir)/Hyena.Json/Tests/*.cs     \
+	$(srcdir)/LibProxy/*.cs             \
 	$(srcdir)/OAuth/*.cs                \
 	$(srcdir)/OAuth/Mono.Rocks/*.cs
 RESOURCES = \
diff --git a/Tomboy/Addins/WebSyncService/WebSyncService-mac.csproj b/Tomboy/Addins/WebSyncService/WebSyncService-mac.csproj
index 6d60778..d429619 100644
--- a/Tomboy/Addins/WebSyncService/WebSyncService-mac.csproj
+++ b/Tomboy/Addins/WebSyncService/WebSyncService-mac.csproj
@@ -106,6 +106,8 @@
     <Compile Include="OAuth\Mono.Rocks\Check.cs" />
     <Compile Include="OAuth\Mono.Rocks\IEnumerable.cs" />
     <Compile Include="WebSyncPreferencesWidget.cs" />
+    <Compile Include="Api\ProxiedWebRequest.cs" />
+    <Compile Include="LibProxy\LibProxy.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="Makefile.am" />
diff --git a/Tomboy/Addins/WebSyncService/WebSyncService.csproj b/Tomboy/Addins/WebSyncService/WebSyncService.csproj
index be18769..a186d7c 100644
--- a/Tomboy/Addins/WebSyncService/WebSyncService.csproj
+++ b/Tomboy/Addins/WebSyncService/WebSyncService.csproj
@@ -121,6 +121,8 @@
     <Compile Include="WebSyncPreferencesWidget.cs" />
     <Compile Include="WebSyncServer.cs" />
     <Compile Include="WebSyncServiceAddin.cs" />
+    <Compile Include="Api\ProxiedWebRequest.cs" />
+    <Compile Include="LibProxy\LibProxy.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="Makefile.am" />



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]