socks

package module
v0.0.0-...-4f8127e Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2020 License: LGPL-3.0 Imports: 8 Imported by: 0

README

socks-go

A socks proxy protocol implemented by golang, support socks 4, 4a and 5.

Only CONNECT command support now.

usage example

server:

// listen 1080 and act as socks server
// support socks4, socks4a and socks5

package main

import (
    socks "github.com/a0s/socks-go"
    "log"
    "net"
    "time"
)

func main() {
    conn, err := net.Listen("tcp", ":1080")
    if err != nil {
        log.Fatal(err)
    }

    for {
        c, err := conn.Accept()
        if err != nil {
            log.Println(err)
            continue
        }

        log.Printf("connected from %s", c.RemoteAddr())

        d := net.Dialer{Timeout: 10 * time.Second}
        s := socks.Conn{Conn: c, Dial: d.Dial, Socks4Enabled: true, Socks5Enabled: true}
        go s.Serve()
    }
}

client:

// visit https://www.google.com through socks proxy server

package main

import (
    "bufio"
    "crypto/tls"
    socks "github.com/a0s/socks-go"
    "io"
    "log"
    "net"
    "net/http"
    "os"
)

func main() {
    // connect to socks server
    c, err := net.Dial("tcp", "localhost:1080")
    if err != nil {
        log.Fatal(err)
    }
    defer c.Close()

    sc := &socks.Client{Conn: c}

    // connect to remote server
    if err := sc.Connect("www.google.com", 443); err != nil {
        log.Fatal(err)
    }

    // tls
    conn := tls.Client(sc, &tls.Config{ServerName: "www.google.com"})
    if err := conn.Handshake(); err != nil {
        log.Fatal(err)
    }

    // send http request
    req, err := http.NewRequest("GET", "https://www.google.com/", nil)
    if err != nil {
        log.Fatal(err)
    }
    req.Write(conn)

    bio := bufio.NewReader(conn)

    // read response
    res, err := http.ReadResponse(bio, req)
    if err != nil {
        log.Fatal(err)
    }
    defer res.Body.Close()

    io.Copy(os.Stdout, res.Body)
}

Documentation

Overview

Package socks implement a socks4/4a/5 protocol server and client.

only support CONNECT command now

server example:

// listen 1080 and act as socks server
// support socks4, socks4a and socks5

package main

import (
    socks "github.com/a0s/socks-go"
    "log"
    "net"
    "time"
)

func main() {
    conn, err := net.Listen("tcp", ":1080")
    if err != nil {
        log.Fatal(err)
    }

    for {
        c, err := conn.Accept()
        if err != nil {
            log.Println(err)
            continue
        }

        log.Printf("connected from %s", c.RemoteAddr())

        d := net.Dialer{Timeout: 10 * time.Second}
        s := socks.Conn{Conn: c, Dial: d.Dial, Socks4Enabled: true, Socks5Enabled: true}
        go s.Serve()
    }
}

client example:

// visit https://www.google.com through socks proxy server

package main

import (
    "bufio"
    "crypto/tls"
    socks "github.com/a0s/socks-go"
    "io"
    "log"
    "net"
    "net/http"
    "os"
)

func main() {
    // connect to socks server
    c, err := net.Dial("tcp", "localhost:1080")
    if err != nil {
        log.Fatal(err)
    }
    defer c.Close()

    sc := &socks.Client{Conn: c}

    // connect to remote server
    if err := sc.Connect("www.google.com", 443); err != nil {
        log.Fatal(err)
    }

    // tls
    conn := tls.Client(sc, &tls.Config{ServerName: "www.google.com"})
    if err := conn.Handshake(); err != nil {
        log.Fatal(err)
    }

    // send http request
    req, err := http.NewRequest("GET", "https://www.google.com/", nil)
    if err != nil {
        log.Fatal(err)
    }
    req.Write(conn)

    bio := bufio.NewReader(conn)

    // read response
    res, err := http.ReadResponse(bio, req)
    if err != nil {
        log.Fatal(err)
    }
    defer res.Body.Close()

    io.Copy(os.Stdout, res.Body)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthService

type AuthService interface {
	// Authenticate auth the user
	// return true means ok, false means no access
	Authenticate(username, password string, addr net.Addr) bool
}

AuthService the service to authenticate the user for socks5

type Client

type Client struct {
	net.Conn
	// socks5 username
	Username string
	// socks5 password
	Password string
	// contains filtered or unexported fields
}

Client is a net.Conn with socks5 support

func (*Client) Close

func (sc *Client) Close() error

Close close the underlying connection

func (*Client) Connect

func (sc *Client) Connect(host string, port uint16) error

Connect handshakes with the socks server and request the server to connect to the target host and port

func (*Client) Dial

func (sc *Client) Dial(network, addr string) (net.Conn, error)

Dial dial to the addr from socks server, this is net.Dial style, can call sc.Connect instead

func (*Client) Read

func (sc *Client) Read(b []byte) (int, error)

Read read from the underlying connection

func (*Client) Write

func (sc *Client) Write(b []byte) (int, error)

Write write data to underlying connection

type Conn

type Conn struct {
	net.Conn
	// the function to dial to upstream server
	// when nil, use net.Dial
	Dial DialFunc
	// Auth the auth service to authenticate the user for socks5
	Auth AuthService
	// Enable socks4 protocol
	Socks4Enabled bool
	// Enable socks5 protocol
	Socks5Enabled bool
}

Conn present a socks connection

func (*Conn) Serve

func (s *Conn) Serve()

Serve serve the client

type DialFunc

type DialFunc func(network, addr string) (net.Conn, error)

DialFunc the function dial to remote

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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