socks

package module
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2023 License: BSD-2-Clause Imports: 8 Imported by: 1

README

SOCKS

GoDoc

SOCKS is a SOCKS4, SOCKS4A and SOCKS5 proxy package for Go.

Quick Start

Get the package
go get -u "h12.io/socks"
Import the package
import "h12.io/socks"
Create a SOCKS proxy dialling function
dialSocksProxy := socks.Dial("socks5://127.0.0.1:1080?timeout=5s")
tr := &http.Transport{Dial: dialSocksProxy}
httpClient := &http.Client{Transport: tr}
User/password authentication
dialSocksProxy := socks.Dial("socks5://user:password@127.0.0.1:1080?timeout=5s")

Example

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))
}

Documentation

Overview

Package socks implements a SOCKS (SOCKS4, SOCKS4A and SOCKS5) proxy client.

A complete example using this package:

package main

import (
	"git.tcp.direct/kayos/socks"
	"fmt"
	"net/http"
	"io/ioutil"
)

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
}

Index

Constants

View Source
const (
	SOCKS4 = iota
	SOCKS4A
	SOCKS5
)

Constants to choose which version of SOCKS protocol to use.

Variables

View Source
var (
	ErrImproperProtocolResponse = errors.New("SOCKS server does not respond properly")
	ErrRejectedOrFailed         = errors.New("socks connection request rejected or failed")
	ErrIdentdFailed             = errors.New("socks connection request rejected because SOCKS server cannot connect to identd on the client")
	ErrIdentMismatch            = errors.New("socks connection request rejected because the client program and identd report different user-ids")
	ErrUnknownFailure           = errors.New("socks connection request failed, unknown error")
)

Functions

func Dial

func Dial(proxyURI string) func(string, string) (net.Conn, error)

Dial returns the dial function to be used in http.Transport object. Argument proxyURI should be in the format: "socks5://user:password@127.0.0.1:1080?timeout=5s". The protocol could be socks5, socks4 and socks4a.

func DialSocksProxy

func DialSocksProxy(socksType int, proxy string) func(string, string) (net.Conn, error)

DialSocksProxy returns the dial function to be used in http.Transport object. Argument socksType should be one of SOCKS4, SOCKS4A and SOCKS5. Argument proxy should be in this format "127.0.0.1:1080".

func DialWithConn

func DialWithConn(proxyURI string, conn net.Conn) func(string, string) (net.Conn, error)

DialWithConn returns the dial function to be used in http.Transport object. Argument proxyURI should be in the format: "socks5://user:password@127.0.0.1:1080?timeout=5s". The protocol could be socks5, socks4 and socks4a. DialWithConn will use the given connection to communicate with the proxy server.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL