{"id":2113,"date":"2015-02-23T17:39:27","date_gmt":"2015-02-23T17:39:27","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=2113"},"modified":"2015-02-23T17:39:27","modified_gmt":"2015-02-23T17:39:27","slug":"connect-via-socks-server-golang","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2015\/02\/connect-via-socks-server-golang\/","title":{"rendered":"Connect via a SOCKS server in golang"},"content":{"rendered":"<p>The function below allows you to setup a SOCKS connection in golang. You should already have connected to the SOCKS server using net.Dial. The function will then send the required header to connect to the target address\/port provides in address:port format. For example:<\/p>\n<pre>\r\nconn, err = net.Dial(\"tcp\", \"socksproxy:8080\")\r\nerr = socks_connect(conn, \"hosttoconnectto:80\")\r\n<\/pre>\n<p>You can then communicate with the target server via the SOCKS proxy.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nfunc socks_connect(conn net.Conn, address string) (error) {\r\n\r\n        host, portstr, err := net.SplitHostPort(address)\r\n        port, err2 := strconv.Atoi(portstr)\r\n        if err2 != nil {\r\n          return errors.New(&quot;Unable to parse port&quot;)\r\n        }\r\n\r\n        version := &#x5B;]byte{0x04} \/\/ socks version 4\r\n        cmd := &#x5B;]byte{0x01}     \/\/ socks stream mode\r\n        buffer := bytes.NewBuffer(&#x5B;]byte{})\r\n        binary.Write(buffer, binary.BigEndian, version)\r\n        binary.Write(buffer, binary.BigEndian, cmd)\r\n        binary.Write(buffer, binary.BigEndian, uint16(port))\r\n        binary.Write(buffer, binary.BigEndian, &#x5B;]byte{0x00, 0x00, 0x00, 0x01})\r\n        binary.Write(buffer, binary.BigEndian, &#x5B;]byte{0x00})\r\n        binary.Write(buffer, binary.BigEndian, &#x5B;]byte(host))\r\n        binary.Write(buffer, binary.BigEndian, &#x5B;]byte{0x00})\r\n        binary.Write(conn, binary.BigEndian, buffer.Bytes())\r\n\r\n        data := make(&#x5B;]byte, 8) \/\/ socks responses are 8 bytes\r\n        count, err := conn.Read(data)\r\n\r\n        if err != nil {\r\n          return errors.New(&quot;Unable to connect to socks server.&quot;)\r\n        }\r\n        if count == 0 {\r\n          return errors.New(&quot;Unable to connect to socks server.&quot;)\r\n        }\r\n        if data&#x5B;1] == 0x5a { \/\/ success\r\n                return nil\r\n        }\r\n\r\n        return errors.New(&quot;Unable to connect to socks server.&quot;)\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The function below allows you to setup a SOCKS connection in golang. You should already have connected to the SOCKS server using net.Dial. The function will then send the required header to connect to the target address\/port provides in address:port format. For example: conn, err = net.Dial(&#8220;tcp&#8221;, &#8220;socksproxy:8080&#8221;) err = socks_connect(conn, &#8220;hosttoconnectto:80&#8221;) You can then [&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-2113","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-y5","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2113","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=2113"}],"version-history":[{"count":1,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2113\/revisions"}],"predecessor-version":[{"id":2114,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2113\/revisions\/2114"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=2113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=2113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=2113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}