-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor and support user/password authentication
- Loading branch information
Showing
11 changed files
with
555 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.