Skip to content

Commit

Permalink
refactor and support user/password authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
h12w committed May 23, 2020
1 parent 0ac3745 commit a6825e0
Show file tree
Hide file tree
Showing 11 changed files with 555 additions and 232 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ SOCKS is a SOCKS4, SOCKS4A and SOCKS5 proxy package for Go.

import "h12.io/socks"

### Create a SOCKS proxy dialing function
### 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

```go
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module h12.io/socks

go 1.9

require (
github.com/h12w/go-socks5 v0.0.0-20200522160539-76189e178364
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/h12w/go-socks5 v0.0.0-20200522160539-76189e178364 h1:5XxdakFhqd9dnXoAZy1Mb2R/DZ6D1e+0bGC/JhucGYI=
github.com/h12w/go-socks5 v0.0.0-20200522160539-76189e178364/go.mod h1:eDJQioIyy4Yn3MVivT7rv/39gAJTrA7lgmYr8EW950c=
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2 h1:JhzVVoYvbOACxoUmOs6V/G4D5nPVUW73rKvXxP4XUJc=
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE=
72 changes: 72 additions & 0 deletions net.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package socks

import (
"bytes"
"errors"
"fmt"
"net"
"strconv"
"time"
)

type requestBuilder struct {
bytes.Buffer
}

func (b *requestBuilder) add(data ...byte) {
_, _ = b.Write(data)
}

func (c *config) sendReceive(conn net.Conn, req []byte) (resp []byte, err error) {
if c.Timeout > 0 {
if err := conn.SetWriteDeadline(time.Now().Add(c.Timeout)); err != nil {
return nil, err
}
}
_, err = conn.Write(req)
if err != nil {
return
}
resp, err = c.readAll(conn)
return
}

func (c *config) readAll(conn net.Conn) (resp []byte, err error) {
resp = make([]byte, 1024)
if c.Timeout > 0 {
if err := conn.SetReadDeadline(time.Now().Add(c.Timeout)); err != nil {
return nil, err
}
}
n, err := conn.Read(resp)
resp = resp[:n]
return
}

func lookupIP(host string) (net.IP, error) {
ips, err := net.LookupIP(host)
if err != nil {
return nil, err
}
if len(ips) == 0 {
return nil, fmt.Errorf("cannot resolve host: %s", host)
}
ip := ips[0].To4()
if len(ip) != net.IPv4len {
return nil, errors.New("ipv6 is not supported by SOCKS4")
}
return ip, nil
}

func splitHostPort(addr string) (host string, port uint16, err error) {
host, portStr, err := net.SplitHostPort(addr)
if err != nil {
return "", 0, err
}
portInt, err := strconv.ParseUint(portStr, 10, 16)
if err != nil {
return "", 0, err
}
port = uint16(portInt)
return
}
61 changes: 61 additions & 0 deletions parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package socks

import (
"errors"
"fmt"
"net/url"
"time"
)

type (
config struct {
Proto int
Host string
Auth *auth
Timeout time.Duration
}
auth struct {
Username string
Password string
}
)

func parse(proxyURI string) (*config, error) {
uri, err := url.Parse(proxyURI)
if err != nil {
return nil, err
}
cfg := &config{}
switch uri.Scheme {
case "socks4":
cfg.Proto = SOCKS4
case "socks4a":
cfg.Proto = SOCKS4A
case "socks5":
cfg.Proto = SOCKS5
default:
return nil, fmt.Errorf("unknown SOCKS protocol %s", uri.Scheme)
}
cfg.Host = uri.Host
user := uri.User.Username()
password, _ := uri.User.Password()
if user != "" || password != "" {
if user == "" || password == "" || len(user) > 255 || len(password) > 255 {
return nil, errors.New("invalid user name or password")
}
cfg.Auth = &auth{
Username: user,
Password: password,
}
}
query := uri.Query()
timeout := query.Get("timeout")
if timeout != "" {
var err error
cfg.Timeout, err = time.ParseDuration(timeout)
if err != nil {
return nil, err
}
}
return cfg, nil
}
8 changes: 4 additions & 4 deletions socks_test.go → parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ func TestParse(t *testing.T) {
testcases := []struct {
name string
uri string
cfg Config
cfg config
}{
{
name: "full config",
uri: "socks5://u1:p1@127.0.0.1:8080?timeout=2s",
cfg: Config{
cfg: config{
Proto: SOCKS5,
Auth: Auth{
Auth: &auth{
Username: "u1",
Password: "p1",
},
Expand All @@ -29,7 +29,7 @@ func TestParse(t *testing.T) {
{
name: "simple socks5",
uri: "socks5://127.0.0.1:8080",
cfg: Config{
cfg: config{
Proto: SOCKS5,
Host: "127.0.0.1:8080",
},
Expand Down

0 comments on commit a6825e0

Please sign in to comment.