fix content

This commit is contained in:
mo 2020-08-06 15:06:33 +08:00
parent 90b349749c
commit 372b42cd09
10 changed files with 226 additions and 221 deletions

View File

@ -8,7 +8,7 @@ import (
"time"
"github.com/thinkgos/go-socks5"
"github.com/thinkgos/go-socks5/client_socks5"
"github.com/thinkgos/go-socks5/ccsocks5"
)
func handleErr(err error) {
@ -41,7 +41,7 @@ func main() {
go func() {
time.Sleep(time.Second)
c, err := client_socks5.NewClient("127.0.0.1:10809")
c, err := ccsocks5.NewClient("127.0.0.1:10809")
handleErr(err)
con, err := c.Dial("udp", lAddr.String())
handleErr(err)

View File

@ -8,7 +8,7 @@ import (
"time"
"github.com/thinkgos/go-socks5"
"github.com/thinkgos/go-socks5/client_socks5"
"github.com/thinkgos/go-socks5/ccsocks5"
)
func handleErr(err error) {
@ -39,7 +39,7 @@ func main() {
go func() {
time.Sleep(time.Second * 1)
c, err := client_socks5.NewClient("127.0.0.1:10808")
c, err := ccsocks5.NewClient("127.0.0.1:10808")
handleErr(err)
con, err := c.Dial("tcp", lAddr.String())
handleErr(err)

69
ccsocks5/Connect.go Normal file
View File

@ -0,0 +1,69 @@
package ccsocks5
import (
"io"
"os"
"time"
)
// Connect implement sock5 connect command
type Connect struct {
*Client
}
// SetReadBuffer sets the size of the operating system's receive buffer associated with the connection.
func (sf *Connect) SetReadBuffer(bytes int) error {
return sf.tcpConn.SetReadBuffer(bytes)
}
// SetWriteBuffer sets the size of the operating system's transmit buffer associated with the connection.
func (sf *Connect) SetWriteBuffer(bytes int) error {
return sf.tcpConn.SetWriteBuffer(bytes)
}
// SetKeepAlive sets whether the operating system should send
// keep-alive messages on the connection.
func (sf *Connect) SetKeepAlive(keepalive bool) error {
return sf.tcpConn.SetKeepAlive(keepalive)
}
// SetKeepAlivePeriod sets period between keep-alives.
func (sf *Connect) SetKeepAlivePeriod(d time.Duration) error {
return sf.tcpConn.SetKeepAlivePeriod(d)
}
// SetLinger sets the behavior of Close on a connection which still
// has data waiting to be sent or to be acknowledged.
func (sf *Connect) SetLinger(sec int) error {
return sf.tcpConn.SetLinger(sec)
}
// SetNoDelay controls whether the operating system should delay
// packet transmission in hopes of sending fewer packets (Nagle's
// algorithm). The default is true (no delay), meaning that data is
// sent as soon as possible after a Write.
func (sf *Connect) SetNoDelay(noDelay bool) error {
return sf.tcpConn.SetNoDelay(noDelay)
}
// ReadFrom implements the io.ReaderFrom ReadFrom method.
func (sf *Connect) ReadFrom(r io.Reader) (int64, error) {
return sf.tcpConn.ReadFrom(r)
}
// CloseRead shuts down the reading side of the TCP connection.
// Most callers should just use Close.
func (sf *Connect) CloseRead() error {
return sf.tcpConn.CloseRead()
}
// CloseWrite shuts down the writing side of the TCP connection.
// Most callers should just use Close.
func (sf *Connect) CloseWrite() error {
return sf.tcpConn.CloseWrite()
}
// File returns a copy of the underlying os.File.
func (sf *Connect) File() (f *os.File, err error) {
return sf.tcpConn.File()
}

63
ccsocks5/associate.go Normal file
View File

@ -0,0 +1,63 @@
package ccsocks5
import (
"net"
"os"
)
// Associate implement sock5 associate command
type Associate struct {
*Client
}
// SetReadBuffer sets the size of the operating system's receive buffer associated with the connection.
func (sf *Associate) SetReadBuffer(bytes int) error {
return sf.getUnderAssociate().SetReadBuffer(bytes)
}
// SetWriteBuffer sets the size of the operating system's transmit buffer associated with the connection.
func (sf *Associate) SetWriteBuffer(bytes int) error {
return sf.getUnderAssociate().SetWriteBuffer(bytes)
}
// ReadFrom implements the PacketConn ReadFrom method.
func (sf *Associate) ReadFrom(b []byte) (int, net.Addr, error) {
return sf.getUnderAssociate().ReadFrom(b)
}
// ReadFromUDP acts like ReadFrom but returns a UDPAddr.
func (sf *Associate) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) {
return sf.getUnderAssociate().ReadFromUDP(b)
}
// ReadMsgUDP reads a message from c, copying the payload into b and
// the associated out-of-band data into oob.
func (sf *Associate) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *net.UDPAddr, err error) {
return sf.getUnderAssociate().ReadMsgUDP(b, oob)
}
// WriteTo implements the PacketConn WriteTo method.
func (sf *Associate) WriteTo(b []byte, addr net.Addr) (int, error) {
return sf.getUnderAssociate().WriteTo(b, addr)
}
// WriteToUDP acts like WriteTo but takes a UDPAddr.
func (sf *Associate) WriteToUDP(b []byte, addr *net.UDPAddr) (int, error) {
return sf.getUnderAssociate().WriteToUDP(b, addr)
}
// WriteMsgUDP writes a message to addr via c if c isn't connected, or
// to c's remote address if c is connected (in which case addr must be
// nil)
func (sf *Associate) WriteMsgUDP(b, oob []byte, addr *net.UDPAddr) (n, oobn int, err error) {
return sf.getUnderAssociate().WriteMsgUDP(b, oob, addr)
}
// File returns a copy of the underlying os.File.
func (sf *Associate) File() (f *os.File, err error) {
return sf.getUnderAssociate().File()
}
func (sf *Associate) getUnderAssociate() *net.UDPConn {
return sf.Conn.(*underAssociate).UDPConn
}

View File

@ -1,4 +1,4 @@
package client_socks5
package ccsocks5
import (
"errors"
@ -11,26 +11,22 @@ import (
"github.com/thinkgos/go-socks5/statute"
)
// Client is socks5 client wrapper
// Client is socks5 client.
type Client struct {
proxyAddr string
auth *proxy.Auth
// On command UDP, let server control the tcp and udp connection relationship
tcpConn *net.TCPConn
underConn net.Conn
TCPDeadline time.Duration
KeepAlivePeriod time.Duration
UDPDeadline time.Duration
tcpConn *net.TCPConn
net.Conn
keepAlivePeriod time.Duration
bufferPool bufferpool.BufPool
}
// This is just create a client, you need to use Dial to create conn
// NewClient This is just create a client, you need to use Dial to create conn.
func NewClient(proxyAddr string, opts ...Option) (*Client, error) {
c := &Client{
proxyAddr: proxyAddr,
KeepAlivePeriod: time.Second,
TCPDeadline: time.Second,
UDPDeadline: time.Second,
keepAlivePeriod: time.Second * 30,
bufferPool: bufferpool.NewPool(32 * 1024),
}
for _, opt := range opts {
@ -39,42 +35,16 @@ func NewClient(proxyAddr string, opts ...Option) (*Client, error) {
return c, nil
}
func (sf *Client) Read(b []byte) (int, error) {
return sf.underConn.Read(b)
}
func (sf *Client) Write(b []byte) (int, error) {
return sf.underConn.Write(b)
}
// Close closes the connection.
func (sf *Client) Close() (err error) {
err = sf.tcpConn.Close()
if sf.underConn != nil {
err = sf.underConn.Close()
if sf.Conn != nil {
err = sf.Conn.Close()
}
return
}
func (sf *Client) LocalAddr() net.Addr {
return sf.underConn.LocalAddr()
}
func (sf *Client) RemoteAddr() net.Addr {
return sf.underConn.RemoteAddr()
}
func (sf *Client) SetDeadline(t time.Time) error {
return sf.underConn.SetDeadline(t)
}
func (sf *Client) SetReadDeadline(t time.Time) error {
return sf.underConn.SetReadDeadline(t)
}
func (sf *Client) SetWriteDeadline(t time.Time) error {
return sf.underConn.SetWriteDeadline(t)
}
// Dial connects to the address on the named network through proxy , with socks5 handshake.
func (sf *Client) Dial(network, addr string) (net.Conn, error) {
if network == "tcp" {
return sf.DialTCP(network, addr)
@ -85,10 +55,11 @@ func (sf *Client) Dial(network, addr string) (net.Conn, error) {
return nil, errors.New("not support network")
}
// DialTCP connects to the address on the named network through proxy , with socks5 handshake.
func (sf *Client) DialTCP(network, addr string) (net.Conn, error) {
conn := *sf // clone a client
_, err := net.ResolveTCPAddr(network, addr)
remoteAddress, err := net.ResolveTCPAddr(network, addr)
if err != nil {
return nil, err
}
@ -101,10 +72,14 @@ func (sf *Client) DialTCP(network, addr string) (net.Conn, error) {
conn.Close()
return nil, err
}
conn.underConn = conn.tcpConn
conn.Conn = &underConnect{
conn.tcpConn,
remoteAddress,
}
return &Connect{&conn}, nil
}
// DialUDP connects to the address on the named network through proxy , with socks5 handshake.
func (sf *Client) DialUDP(network string, laddr *net.UDPAddr, raddr string) (net.Conn, error) {
conn := *sf // clone a client
@ -140,7 +115,7 @@ func (sf *Client) DialUDP(network string, laddr *net.UDPAddr, raddr string) (net
conn.Close()
return nil, err
}
conn.underConn = &underAssociate{
conn.Conn = &underAssociate{
udpConn,
conn.bufferPool,
remoteAddress,
@ -218,8 +193,8 @@ func (sf *Client) dialProxyServer(network string) error {
}
sf.tcpConn = conn.(*net.TCPConn)
if sf.KeepAlivePeriod != 0 {
err = sf.tcpConn.SetKeepAlivePeriod(sf.KeepAlivePeriod)
if sf.keepAlivePeriod != 0 {
err = sf.tcpConn.SetKeepAlivePeriod(sf.keepAlivePeriod)
}
if err != nil {
sf.tcpConn.Close()

View File

@ -1,4 +1,4 @@
package client_socks5
package ccsocks5
import (
"time"
@ -8,34 +8,28 @@ import (
"github.com/thinkgos/go-socks5/bufferpool"
)
// Option user's option of the client
type Option func(c *Client)
// WithAuth with user's auth
// default is nil,no UserPass
func WithAuth(auth *proxy.Auth) Option {
return func(c *Client) {
c.auth = auth
}
}
// WithBufferPool with buffer pool
// default: 32k
func WithBufferPool(p bufferpool.BufPool) Option {
return func(c *Client) {
c.bufferPool = p
}
}
// WithKeepAlivePeriod set tcp keep alive period
func WithKeepAlivePeriod(t time.Duration) Option {
return func(c *Client) {
c.KeepAlivePeriod = t
}
}
func WithTCPDeadline(t time.Duration) Option {
return func(c *Client) {
c.TCPDeadline = t
}
}
func WithUDPDeadline(t time.Duration) Option {
return func(c *Client) {
c.UDPDeadline = t
c.keepAlivePeriod = t
}
}

61
ccsocks5/underconn.go Normal file
View File

@ -0,0 +1,61 @@
package ccsocks5
import (
"net"
"github.com/thinkgos/go-socks5/bufferpool"
"github.com/thinkgos/go-socks5/statute"
)
// underConnect under connect
type underConnect struct {
*net.TCPConn
remoteAddress net.Addr // real remote address, not the proxy address
}
// RemoteAddr returns the remote network address.
// The Addr returned is shared by all invocations of RemoteAddr,
// so do not modify it.
func (sf *underConnect) RemoteAddr() net.Addr {
return sf.remoteAddress
}
// underAssociate under associate
type underAssociate struct {
*net.UDPConn
bufferPool bufferpool.BufPool
remoteAddress net.Addr // real remote address, not the proxy address
}
// Read implements the Conn Read method.
func (sf *underAssociate) Read(b []byte) (int, error) {
b1 := sf.bufferPool.Get()
defer sf.bufferPool.Put(b1)
n, err := sf.UDPConn.Read(b1[:cap(b1)])
if err != nil {
return 0, err
}
datagram, err := statute.ParseDatagram(b1[:n])
if err != nil {
return 0, err
}
n = copy(b, datagram.Data)
return n, nil
}
// Write implements the Conn Write method.
func (sf *underAssociate) Write(b []byte) (int, error) {
datagram, err := statute.NewDatagram(sf.remoteAddress.String(), b)
if err != nil {
return 0, err
}
return sf.UDPConn.Write(datagram.Bytes())
}
// RemoteAddr returns the remote network address.
// The Addr returned is shared by all invocations of RemoteAddr,
// so do not modify it.
func (sf *underAssociate) RemoteAddr() net.Addr {
return sf.remoteAddress
}

View File

@ -1,51 +0,0 @@
package client_socks5
import (
"io"
"os"
"time"
)
type Connect struct {
*Client
}
func (sf *Connect) SetReadBuffer(bytes int) error {
return sf.tcpConn.SetReadBuffer(bytes)
}
func (sf *Connect) SetWriteBuffer(bytes int) error {
return sf.tcpConn.SetWriteBuffer(bytes)
}
func (sf *Connect) SetKeepAlive(keepalive bool) error {
return sf.tcpConn.SetKeepAlive(keepalive)
}
func (sf *Connect) SetKeepAlivePeriod(d time.Duration) error {
return sf.tcpConn.SetKeepAlivePeriod(d)
}
func (sf *Connect) SetLinger(sec int) error {
return sf.tcpConn.SetLinger(sec)
}
func (sf *Connect) SetNoDelay(noDelay bool) error {
return sf.tcpConn.SetNoDelay(noDelay)
}
func (sf *Connect) ReadFrom(r io.Reader) (int64, error) {
return sf.tcpConn.ReadFrom(r)
}
func (sf *Connect) CloseRead() error {
return sf.tcpConn.CloseRead()
}
func (sf *Connect) CloseWrite() error {
return sf.tcpConn.CloseWrite()
}
func (sf *Connect) File() (f *os.File, err error) {
return sf.tcpConn.File()
}

View File

@ -1,43 +0,0 @@
package client_socks5
import (
"net"
"os"
)
type Associate struct {
*Client
}
func (sf *Associate) getUDPConn() *net.UDPConn {
return sf.underConn.(*underAssociate).udpConn
}
func (sf *Associate) SetReadBuffer(bytes int) error {
return sf.getUDPConn().SetReadBuffer(bytes)
}
func (sf *Associate) SetWriteBuffer(bytes int) error {
return sf.getUDPConn().SetWriteBuffer(bytes)
}
func (sf *Associate) ReadFrom(b []byte) (int, net.Addr, error) {
return sf.getUDPConn().ReadFrom(b)
}
func (sf *Associate) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) {
return sf.getUDPConn().ReadFromUDP(b)
}
func (sf *Associate) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *net.UDPAddr, err error) {
return sf.getUDPConn().ReadMsgUDP(b, oob)
}
func (sf *Associate) WriteTo(b []byte, addr net.Addr) (int, error) {
return sf.getUDPConn().WriteTo(b, addr)
}
func (sf *Associate) WriteToUDP(b []byte, addr *net.UDPAddr) (int, error) {
return sf.getUDPConn().WriteToUDP(b, addr)
}
func (sf *Associate) WriteMsgUDP(b, oob []byte, addr *net.UDPAddr) (n, oobn int, err error) {
return sf.getUDPConn().WriteMsgUDP(b, oob, addr)
}
func (sf *Associate) File() (f *os.File, err error) {
return sf.getUDPConn().File()
}

View File

@ -1,63 +0,0 @@
package client_socks5
import (
"net"
"time"
"github.com/thinkgos/go-socks5/bufferpool"
"github.com/thinkgos/go-socks5/statute"
)
type underAssociate struct {
udpConn *net.UDPConn
bufferPool bufferpool.BufPool
remoteAddress net.Addr
}
func (sf *underAssociate) Read(b []byte) (int, error) {
b1 := sf.bufferPool.Get()
defer sf.bufferPool.Put(b1)
n, err := sf.udpConn.Read(b1[:cap(b1)])
if err != nil {
return 0, err
}
datagram, err := statute.ParseDatagram(b1[:n])
if err != nil {
return 0, err
}
n = copy(b, datagram.Data)
return n, nil
}
func (sf *underAssociate) Write(b []byte) (int, error) {
datagram, err := statute.NewDatagram(sf.remoteAddress.String(), b)
if err != nil {
return 0, err
}
return sf.udpConn.Write(datagram.Bytes())
}
func (sf *underAssociate) Close() error {
return sf.udpConn.Close()
}
func (sf *underAssociate) LocalAddr() net.Addr {
return sf.udpConn.LocalAddr()
}
func (sf *underAssociate) RemoteAddr() net.Addr {
return sf.remoteAddress
}
func (sf *underAssociate) SetDeadline(t time.Time) error {
return sf.udpConn.SetDeadline(t)
}
func (sf *underAssociate) SetReadDeadline(t time.Time) error {
return sf.udpConn.SetReadDeadline(t)
}
func (sf *underAssociate) SetWriteDeadline(t time.Time) error {
return sf.udpConn.SetWriteDeadline(t)
}