socks Go package

repository·master·Indexed 19 days ago

https://github.com/h12w/socks

A Go package providing support for SOCKS4, SOCKS4A, and SOCKS5 proxy protocols. It allows developers to route TCP and HTTP traffic through a proxy by generating compatible dialing functions via socks.Dial and DialSocksProxy.

Tokens
1.4K
Snippets
8
Records
8
Agent score
18%

What's inside socks

  1. Use User/password authentication with SOCKS

    master

    To use a SOCKS proxy that requires user/password authentication, include the credentials in the connection string using the user:password@ format before the host address.

    dialSocksProxy := socks.Dial("socks5://user:password@127.0.0.1:1080?timeout=5s")
  2. Full example: Use socks to make HTTP requests

    master

    This example demonstrates how to initialize an http.Client using a SOCKS5 proxy via socks.Dial and perform a GET request.

    package main
    
    import (
    	"fmt"
    	"io/ioutil"
    	"log"
    	"net/http"
    
    	"h12.io/socks"
    )
    
    func main() {
    	dialSocksProxy := socks.Dial("socks5://127.0.0.1:1080?timeout=5s")
    	tr := &http.Transport{Dial: dialSocksProxy}
    	httpClient := &http.Client{Transport: tr}
    	resp, err := httpClient.Get("http://www.google.com")
    	if err != nil {
    		log.Fatal(err)
    	}
    	defer resp.Body.Close()
    	if resp.StatusCode != http.StatusOK {
    		log.Fatal(resp.StatusCode)
    	}
    	buf, err := ioutil.ReadAll(resp.Body)
    	if err != nil {
    		log.Fatal(err)
    	}
    	fmt.Println(string(buf))
    }
  3. Create a SOCKS proxy dialing function with socks.Dial

    master

    The socks.Dial function takes a SOCKS proxy connection string and returns a TCP dialing function. This returned function can be used to establish TCP connections through the proxy or assigned to the Dial field of an http.Transport to route HTTP traffic through the proxy.

    Supported protocols in the connection string include socks4, socks4a, and socks5.

    dialSocksProxy := socks.Dial("socks5://127.0.0.1:1080?timeout=5s")
    tr := &http.Transport{Dial: dialSocksProxy}
    httpClient := &http.Client{Transport: tr}
  4. Full example: Using socks with http.Client

    master

    This example demonstrates how to integrate the socks package into a standard Go http.Client to route requests through a SOCKS5 proxy.

    package main
    
    import (
    	"fmt"
    	"h12.io/socks"
    	"io/ioutil"
    	"net/http"
    )
    
    func main() {
    	dialSocksProxy := socks.Dial("socks5://127.0.0.1:1080?timeout=5s")
    	tr := &http.Transport{Dial: dialSocksProxy}
    	httpClient := &http.Client{Transport: tr}
    
    	bodyText, err := TestHttpsGet(httpClient, "https://h12.io/about")
    	if err != nil {
    		fmt.Println(err.Error())
    	}
    	fmt.Print(bodyText)
    }
    
    func TestHttpsGet(c *http.Client, url string) (bodyText string, err error) {
    	resp, err := c.Get(url)
    	if err != nil {
    		return
    	}
    	defer resp.Body.Close()
    
    	body, err := ioutil.ReadAll(resp.Body)
    	if err != nil {
    		return
    	}
    	bodyText = string(body)
    	return
    }
  5. Create a SOCKS proxy dialing function with DialSocksProxy()

    master

    Use DialSocksProxy(socksType int, proxy string) to generate a dialing function when you want to specify the protocol version and proxy address explicitly without using a URI string.

    Parameters:

    • socksType: An integer constant representing the protocol (SOCKS4, SOCKS4A, or SOCKS5).
    • proxy: The proxy address in the format host:port (e.g., "127.0.0.1:1080").
    // Example using SOCKS5
    dialSocksProxy := socks.DialSocksProxy(socks.SOCKS5, "127.0.0.1:1080")
  6. Create a SOCKS proxy dialing function with Dial()

    master

    Use Dial(proxyURI string) to generate a dialing function compatible with http.Transport.Dial. The proxyURI must be a URI string specifying the protocol, optional credentials, proxy address, and an optional timeout.

    Supported protocols in the URI prefix:

    • socks5://
    • socks4://
    • socks4a://

    Example URI format: socks5://user:password@127.0.0.1:1080?timeout=5s.

    dialSocksProxy := socks.Dial("socks5://127.0.0.1:1080?timeout=5s")
    tr := &http.Transport{Dial: dialSocksProxy}
    httpClient := &http.Client{Transport: tr}