1// Copyright 2012, Hailiang Wang. All rights reserved. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5/* 6Package socks implements a SOCKS (SOCKS4, SOCKS4A and SOCKS5) proxy client. 7 8A complete example using this package: 9 package main 10 11 import ( 12 "h12.io/socks" 13 "fmt" 14 "net/http" 15 "io/ioutil" 16 ) 17 18 func main() { 19 dialSocksProxy := socks.Dial("socks5://127.0.0.1:1080?timeout=5s") 20 tr := &http.Transport{Dial: dialSocksProxy} 21 httpClient := &http.Client{Transport: tr} 22 23 bodyText, err := TestHttpsGet(httpClient, "https://h12.io/about") 24 if err != nil { 25 fmt.Println(err.Error()) 26 } 27 fmt.Print(bodyText) 28 } 29 30 func TestHttpsGet(c *http.Client, url string) (bodyText string, err error) { 31 resp, err := c.Get(url) 32 if err != nil { return } 33 defer resp.Body.Close() 34 35 body, err := ioutil.ReadAll(resp.Body) 36 if err != nil { return } 37 bodyText = string(body) 38 return 39 } 40*/ 41package socks // import "h12.io/socks" 42 43import ( 44 "fmt" 45 "net" 46) 47 48// Constants to choose which version of SOCKS protocol to use. 49const ( 50 SOCKS4 = iota 51 SOCKS4A 52 SOCKS5 53) 54 55// Dial returns the dial function to be used in http.Transport object. 56// Argument proxyURI should be in the format: "socks5://user:password@127.0.0.1:1080?timeout=5s". 57// The protocol could be socks5, socks4 and socks4a. 58func Dial(proxyURI string) func(string, string) (net.Conn, error) { 59 cfg, err := parse(proxyURI) 60 if err != nil { 61 return dialError(err) 62 } 63 return cfg.dialFunc() 64} 65 66// DialSocksProxy returns the dial function to be used in http.Transport object. 67// Argument socksType should be one of SOCKS4, SOCKS4A and SOCKS5. 68// Argument proxy should be in this format "127.0.0.1:1080". 69func DialSocksProxy(socksType int, proxy string) func(string, string) (net.Conn, error) { 70 return (&config{Proto: socksType, Host: proxy}).dialFunc() 71} 72 73func (c *config) dialFunc() func(string, string) (net.Conn, error) { 74 switch c.Proto { 75 case SOCKS5: 76 return func(_, targetAddr string) (conn net.Conn, err error) { 77 return c.dialSocks5(targetAddr) 78 } 79 case SOCKS4, SOCKS4A: 80 return func(_, targetAddr string) (conn net.Conn, err error) { 81 return c.dialSocks4(targetAddr) 82 } 83 } 84 return dialError(fmt.Errorf("unknown SOCKS protocol %v", c.Proto)) 85} 86 87func dialError(err error) func(string, string) (net.Conn, error) { 88 return func(_, _ string) (net.Conn, error) { 89 return nil, err 90 } 91} 92