{"id":315,"date":"2019-09-09T14:20:29","date_gmt":"2019-09-09T21:20:29","guid":{"rendered":"http:\/\/blog.nillsf.com\/?p=315"},"modified":"2019-09-09T14:20:32","modified_gmt":"2019-09-09T21:20:32","slug":"setting-up-a-squid-proxy-with-authentication","status":"publish","type":"post","link":"https:\/\/nillsf.com\/index.php\/2019\/09\/09\/setting-up-a-squid-proxy-with-authentication\/","title":{"rendered":"Setting up a Squid proxy with authentication"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Do you know the feeling: you&#8217;re traveling out of the country, and suddenly you cannot access some websites? I recently hit this when I traveled to Europe, and some US websites I wanted to access threw me the GDPR block. I had once setup a proxy server to do some geo-timing tests, and decided to set one up again so I could access what I wanted to access.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Setting up Squid on Azure<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"http:\/\/www.squid-cache.org\/\">Squid <\/a>is an open source proxy and cache that is fairly simple to configure (but has a lot of configuration capabilities). For our simple proxy, we&#8217;ll set this one up on an Azure VM in West US 2. Be mindful of the Network Security Group you attach to that VM\/subnet: squid by default runs over port 3128, so you&#8217;ll need to open that for the solution to work.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"945\" height=\"1024\" src=\"\/wp-content\/uploads\/2019\/09\/2019-09-09-02_35_36-Create-a-virtual-machine-Microsoft-Azure-and-1-more-page-Microsoft-Edge-945x1024.jpg\" alt=\"\" class=\"wp-image-316\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/09\/2019-09-09-02_35_36-Create-a-virtual-machine-Microsoft-Azure-and-1-more-page-Microsoft-Edge-945x1024.jpg 945w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/09\/2019-09-09-02_35_36-Create-a-virtual-machine-Microsoft-Azure-and-1-more-page-Microsoft-Edge-277x300.jpg 277w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/09\/2019-09-09-02_35_36-Create-a-virtual-machine-Microsoft-Azure-and-1-more-page-Microsoft-Edge-768x832.jpg 768w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/09\/2019-09-09-02_35_36-Create-a-virtual-machine-Microsoft-Azure-and-1-more-page-Microsoft-Edge.jpg 1167w\" sizes=\"auto, (max-width: 945px) 100vw, 945px\" \/><figcaption>We&#8217;ll create a simple Ubuntu VM<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"803\" src=\"\/wp-content\/uploads\/2019\/09\/2019-09-09-02_35_45-Create-a-virtual-machine-Microsoft-Azure-and-1-more-page-Microsoft-Edge-1024x803.jpg\" alt=\"\" class=\"wp-image-317\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/09\/2019-09-09-02_35_45-Create-a-virtual-machine-Microsoft-Azure-and-1-more-page-Microsoft-Edge-1024x803.jpg 1024w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/09\/2019-09-09-02_35_45-Create-a-virtual-machine-Microsoft-Azure-and-1-more-page-Microsoft-Edge-300x235.jpg 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/09\/2019-09-09-02_35_45-Create-a-virtual-machine-Microsoft-Azure-and-1-more-page-Microsoft-Edge-768x602.jpg 768w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/09\/2019-09-09-02_35_45-Create-a-virtual-machine-Microsoft-Azure-and-1-more-page-Microsoft-Edge.jpg 1173w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Don&#8217;t forget to create a NSG that will allow port 3128 inbound.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Once we have that, we can connect to our VM, and install squid.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get update &amp;&amp; sudo apt-get install squid<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With that done, we&#8217;ll have to dive into the squid configuration. There&#8217;s a lot to squid configuration. The default configuration file located at <code>\/etc\/squid\/squid.conf<\/code> contains 7980 lines. We wont be editing 7980 lines for our basic proxy though. What we will be doing is the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Configure an ACL.<\/li><li>Allow access for that ACL.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For this, we&#8217;ll be editing the default config file &#8211; because this allows basic proxy functionality by default &#8211; and we&#8217;ll add our configuration to it. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For the first part (configuring the ACL) &#8211; look for an ACL section around line 980 in the <code>squid.conf<\/code> file, and add your own line to it. You don&#8217;t have to remove anything:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>acl nills-from-internet src 0.0.0.0\/0                                                                                   acl SSL_ports port 443                                                                                                  acl Safe_ports port 80          # http                                                                                  acl Safe_ports port 21          # ftp  \n(...)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For the second part, we&#8217;ll configure <code>http_access<\/code> to allow our ACL to use the proxy. Look for the following line around line 1188, and add your own <code>http_access<\/code> to it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS                                                        #                                                                                                                       http_access allow nills-from-internet<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we&#8217;ll restart our squid service:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo service squid restart<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And then we should be able to connect using our proxy service. Easy peasy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Let&#8217;s add a little bit of security<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">As I was writing this blog, I started thinking I don&#8217;t want to host a public proxy service. I just want to be able to read my web-pages without GDPR block. This can also be done &#8211; fairly easily &#8211; so why won&#8217;t we do this together as well?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First things first, we need to create a password file. We will use the <code>htpasswd<\/code> utility to create that. To get <code>htpasswd<\/code>, you need <code>apache2 <\/code>installed, which we&#8217;ll do first:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get install apache2 -y\nsudo htpasswd -c \/etc\/squid\/passwd nilfranadmin\n#enter and confirm your password now<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With that done, we&#8217;ll dive into the squid configuration file (remember, <code>\/etc\/squid\/squid.conf<\/code>) to configure our authentication:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First thing, look for the <code>auth_param<\/code> section around line 427, and add the following line in there:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>auth_param basic program \/usr\/lib\/squid\/basic_ncsa_auth \/etc\/squid\/passwd<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Afterwards, look for your ACL you created earlier, and then add the following line referencing the authentication:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>acl nills-from-internet src 0.0.0.0\/0                                                      acl auth_users proxy_auth REQUIRED <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And then finally, we&#8217;ll change our <code>http_access<\/code>, around line 1190:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http_access allow auth_users                                                               http_access allow nills-from-internet<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And after that&#8217;s done, let&#8217;s go ahead and restart our squid service, and see if we get an authentication request:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo service squid restart<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And what that done, all my apps suddenly needed proxy authentication information. Now, in all honesty and transparency, I&#8217;m not using SSL &#8211; which is not best practice for sending credentials. But as I&#8217;m using a fairly useless password, I can live with that to be able to browse the American internet.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"350\" src=\"\/wp-content\/uploads\/2019\/09\/image.png\" alt=\"\" class=\"wp-image-320\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/09\/image.png 666w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/09\/image-300x158.png 300w\" sizes=\"auto, (max-width: 666px) 100vw, 666px\" \/><figcaption>Edge asking for proxy information. Preview Edge, Chromium based that is. Just for the record.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"443\" height=\"449\" src=\"\/wp-content\/uploads\/2019\/09\/2019-09-09-14_13_00-Teams.jpg\" alt=\"\" class=\"wp-image-321\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/09\/2019-09-09-14_13_00-Teams.jpg 443w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/09\/2019-09-09-14_13_00-Teams-296x300.jpg 296w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/09\/2019-09-09-14_13_00-Teams-60x60.jpg 60w\" sizes=\"auto, (max-width: 443px) 100vw, 443px\" \/><figcaption>Even Teams asked for proxy information.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you are ever geo-blocked and need to browse the web quickly &#8211; please just go ahead and use a free or paid VPN\/proxy solution. They will be a lot easier to setup than what I just did (and probably be cheaper as well). However, if you like playing around and figuring out stuff, please follow along and setup your own Squid proxy server. Have fun!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you know the feeling: you&#8217;re traveling out of the country, and suddenly you cannot access some websites? I recently hit this when I traveled to Europe, and some US websites I wanted to access threw me the GDPR block. I had once setup a proxy server to do some geo-timing tests, and decided to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2,5],"tags":[28,26],"class_list":["post-315","post","type-post","status-publish","format-standard","hentry","category-azure","category-open-source","tag-side-project","tag-squid"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/comments?post=315"}],"version-history":[{"count":3,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/315\/revisions"}],"predecessor-version":[{"id":322,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/315\/revisions\/322"}],"wp:attachment":[{"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/media?parent=315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/categories?post=315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/tags?post=315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}