{"id":2041,"date":"2015-01-24T22:56:01","date_gmt":"2015-01-24T22:56:01","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=2041"},"modified":"2015-01-24T22:56:01","modified_gmt":"2015-01-24T22:56:01","slug":"simple-js-dns-proxy-golang-server-side","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2015\/01\/simple-js-dns-proxy-golang-server-side\/","title":{"rendered":"Simple JS->DNS Proxy with golang server side"},"content":{"rendered":"<p><a href=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/01\/jsdnsproxy.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/01\/jsdnsproxy.png\" alt=\"jsdnsproxy\" width=\"332\" height=\"199\" class=\"aligncenter size-full wp-image-2043\" srcset=\"https:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/01\/jsdnsproxy.png 332w, https:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/01\/jsdnsproxy-300x180.png 300w\" sizes=\"auto, (max-width: 332px) 100vw, 332px\" \/><\/a><\/p>\n<p>I wanted the ability to resolve DNS addresses in client side Javascript. Unfortunately you can&#8217;t do DNS lookups in browser. As a hack I wrote a quick DNS proxy in golang which allows the JS to perform GET requests to a golang server which will lookup and DNS entry and return IP addresses.<\/p>\n<p>The HTML and golang code is below, there&#8217;s also a tarball <a href=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/01\/dnsprox.tar.gz\">here<\/a>. You can run the go code with &#8220;go run&#8221;. If the html file is stored in the same directory you will be able to access it from localhost at http:\/\/localhost:8080\/web.html.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta charset=&quot;UTF-8&quot; \/&gt;\r\n\r\n&lt;script&gt;\r\n  \r\n  function gethostbyname(hostname) {\r\n    var xmlHttp = new XMLHttpRequest();\r\n    xmlHttp.open(&quot;GET&quot;, &quot;http:\/\/localhost:8080\/dns?hostname=&quot; + hostname, false);\r\n    xmlHttp.send(null);\r\n    return xmlHttp.responseText;\r\n  }\r\n\r\n  function press() {\r\n    var hostname = document.getElementById('hostname').value;\r\n    document.getElementById('comms').innerHTML += hostname + &quot;=&quot; + gethostbyname(hostname)  + &quot;&lt;br&gt;&quot;;\r\n  }\r\n\r\n&lt;\/script&gt;\r\n\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n  ServerIP: &lt;input id=&quot;hostname&quot; type=&quot;text&quot; \/&gt;&lt;br&gt;\r\n  &lt;input type=&quot;button&quot; id=&quot;lookup&quot; value=&quot;lookup&quot; onclick=&quot;press()&quot;&gt;&lt;\/input&gt;&lt;br&gt;&lt;br&gt;\r\n  &lt;div id='comms'&gt;&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\npackage main\r\n \r\nimport (\r\n    &quot;net\/http&quot;\r\n    &quot;net&quot;\r\n    &quot;fmt&quot;\r\n)\r\n\r\nfunc DNSProxyHandler(w http.ResponseWriter, r *http.Request) {\r\n \r\n  r.ParseForm()\r\n  hostname := r.Form.Get(&quot;hostname&quot;)\r\n\r\n  fmt.Printf(&quot;hostname: %s\\n&quot;,hostname)\r\n  \r\n  addrs, err := net.LookupIP(hostname)\r\n\r\n  var sendbytes string;\r\n  if err == nil {\r\n    sendbytes = addrs&#x5B;0].String()\r\n    fmt.Printf(&quot;sending: %s&quot;,sendbytes)\r\n  } else {\r\n    fmt.Printf(&quot;fail in dnsroxy\\n&quot;)\r\n  }\r\n\r\n  fmt.Fprintf(w, &quot;%s&quot;,sendbytes);\r\n\r\n}\r\n \r\nfunc main() {\r\n  http.HandleFunc(&quot;\/dns&quot; , DNSProxyHandler)\r\n  http.Handle(&quot;\/&quot;, http.FileServer(http.Dir(&quot;.&quot;)))\r\n  err := http.ListenAndServe(&quot;:8080&quot;, nil)\r\n  if err != nil {\r\n    panic(&quot;Error: &quot; + err.Error())\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I wanted the ability to resolve DNS addresses in client side Javascript. Unfortunately you can&#8217;t do DNS lookups in browser. As a hack I wrote a quick DNS proxy in golang which allows the JS to perform GET requests to a golang server which will lookup and DNS entry and return IP addresses. The HTML [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-2041","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-wV","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=2041"}],"version-history":[{"count":2,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2041\/revisions"}],"predecessor-version":[{"id":2045,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2041\/revisions\/2045"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=2041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=2041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=2041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}