Skip to content

Commit

Permalink
增加部分的注释和说明
Browse files Browse the repository at this point in the history
  • Loading branch information
mingcheng committed Jul 8, 2022
1 parent 1056861 commit 1eadd79
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 16 deletions.
3 changes: 3 additions & 0 deletions backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ func (b *Backend) httpProxyClient() (*http.Client, error) {
}, nil
}

// socks5Client to create http client with socks5 proxy
func (b *Backend) socks5Client(timeout int) (*socks5.Client, error) {
return socks5.NewClient(b.Addr, b.Socks5UserName, b.Socks5Password, timeout, timeout)
}

// Socks5Conn to create a connection by specific params
func (b *Backend) Socks5Conn(network, addr string, timeout int) (cc net.Conn, err error) {
client, err := b.socks5Client(timeout)
if err != nil {
Expand All @@ -93,6 +95,7 @@ func (b *Backend) Socks5Conn(network, addr string, timeout int) (cc net.Conn, er
return client.Dial(network, addr)
}

// NewBackend creates a new Backend instance
func NewBackend(addr string, config BackendCheckConfig) (backend *Backend) {
backend = &Backend{
Addr: addr,
Expand Down
14 changes: 11 additions & 3 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,30 @@ func (b *Pool) NextIndex() int {
return int(atomic.AddUint64(&b.current, uint64(1)) % uint64(len(b.backends)))
}

// Next returns the next index in the pool if there is one available
// Only supports round-robin operations by default
func (b *Pool) Next() *Backend {
// loop entire backends to find out an Alive backend
next := b.NextIndex()
l := len(b.backends) + next // start from next and move a full cycle
// start from next and move a full cycle
l := len(b.backends) + next
for i := next; i < l; i++ {
idx := i % len(b.backends) // take an index by modding
if b.backends[idx].Alive() { // if we have an alive backend, use it and store if its not the original one
// take an index by modding
idx := i % len(b.backends)
// if we have an alive backend, use it and store if its not the original one
if b.backends[idx].Alive() {
if i != next {
atomic.StoreUint64(&b.current, uint64(idx))
}

return b.backends[idx]
}
}

return nil
}

// Check if we have an alive backend
func (b *Pool) Check() {
for _, b := range b.backends {
err := b.Check()
Expand All @@ -56,6 +63,7 @@ func (b *Pool) Check() {
}
}

// NewPool instance for a new Pools instance
func NewPool() *Pool {
return &Pool{
backends: []*Backend{},
Expand Down
10 changes: 3 additions & 7 deletions redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@

package socks5lb

import (
"fmt"
)

func (s *Server) ListenTProxy(_ string) (err error) {
err = fmt.Errorf("sorry transparent proxy is ONLY supports Linux platform")
return
// ListenProxy is not implemented by default
func (s *Server) ListenTProxy(_ string) error {
return error.New("sorry transparent proxy is ONLY supports Linux platform")
}
15 changes: 9 additions & 6 deletions redirect_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
)

// getOriginalDstAddr to get the original address from the socket
// this function is referenced from
// https://github.com/ginuerzh/gost/blob/0247b941ac31344f0d7b3c547941a051188ba202/redirect.go#L72
func getOriginalDstAddr(conn *net.TCPConn) (addr net.Addr, c *net.TCPConn, err error) {
defer conn.Close()

Expand Down Expand Up @@ -65,6 +67,7 @@ var (
clientLock sync.Mutex
)

// socks5Client to connect to a proxy server
func (s *Server) socks5Client() (client *socks5.Client, err error) {
clientLock.Lock()

Expand All @@ -80,16 +83,16 @@ func (s *Server) socks5Client() (client *socks5.Client, err error) {
socks5Clients = client
}()

backend := s.Pool.Next()
if backend == nil {
log.Error("sorry, we don't have healthy backend, so close the connection")
socks5Clients = nil
return
// found a available backend
if backend := s.Pool.Next(); backend != nil {
return backend.socks5Client(0)
}

return backend.socks5Client(0)
err = errors.New("sorry, we don't have healthy backend, so close the connection")
return
}

// ListenTProxy is listening the local tcp port on the given address
func (s *Server) ListenTProxy(addr string) (err error) {
tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (s *Server) Stop() (e error) {
return
}

// Transport is used to connect to the server and client each other
func (s *Server) Transport(dst, src io.ReadWriter) (err error) {
// @see https://github.com/ginuerzh/gost/blob/0247b941ac31344f0d7b3c547941a051188ba202/server.go#L105
errs := make(chan error, 1)
Expand Down
1 change: 1 addition & 0 deletions socks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (s *Server) ListenSocks5(addr string) (err error) {
}
defer backendConn.Close()

// transport the socket connection directly to the backend
s.Transport(socks5Conn, backendConn)
}()
}
Expand Down

0 comments on commit 1eadd79

Please sign in to comment.