Skip to content

Commit

Permalink
修复空指针的问题,避免程序奔溃
Browse files Browse the repository at this point in the history
  • Loading branch information
mingcheng committed Jul 15, 2022
1 parent 61bcd2a commit 084d332
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 39 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CGO_ENABLED=0
15 changes: 10 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
PROJECT=socks5lb
VERSION=`date +%Y%m%d`
COMMIT_HASH=`git rev-parse --short HEAD`
#https://gqlxj1987.github.io/2018/08/14/good-makefile-golang/
include .env

PROJECT=$(shell basename "$(PWD)")
VERSION=$(shell date +%Y%m%d)
COMMIT_HASH=$(shell git rev-parse --short HEAD)

SRC=./cmd/$(PROJECT)
BINARY=$(PROJECT)

GO_ENV=CGO_ENABLED=0
GO_FLAGS=-ldflags="-X main.version=$(VERSION) -X 'main.commit=$(COMMIT_HASH)' -X 'main.date=`date`'"
GO=go
GO=$(shell which go)

PACKAGES=`go list ./...`
GOFILES=`find . -name "*.go" -type f`

# Make is verbose in Linux. Make it silent.
MAKEFLAGS += --silent

all: build

build: cmd/$(PROJECT)
Expand Down
18 changes: 8 additions & 10 deletions cmd/socks5lb/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import "github.com/judwhite/go-svc"
// program to run a specific version of the local package socks5lb
type program struct {
Config *socks5lb.Configure
pool *socks5lb.Pool
Server socks5lb.Server
Server *socks5lb.Server
}

// Init to initial the program
Expand All @@ -33,23 +32,21 @@ func (p *program) Init(env svc.Environment) (err error) {
for _, v := range p.Config.Backends {
log.Tracef("add backend %s", v.Addr)
backend := socks5lb.NewBackend(v.Addr, *v.CheckConfig)
pool.Add(backend)
_ = pool.Add(backend)
}

p.Server = socks5lb.Server{
Pool: pool,
Config: p.Config.ServerConfig,
}
p.Server, err = socks5lb.NewServer(pool, p.Config.ServerConfig)

return
}

// Start when the program is start
func (p *program) Start() (err error) {
log.Infof("start the program")
go func() {
err = p.Server.Start()
if err != nil {
log.Panic(err)
if err = p.Server.Start(); err != nil {
log.Error(err)
p.Server.Stop()
}
}()

Expand All @@ -58,5 +55,6 @@ func (p *program) Start() (err error) {

// Stop when the program is stop
func (p *program) Stop() (err error) {
log.Infof("stop the program")
return p.Server.Stop()
}
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ type ServerConfig struct {
}

type Configure struct {
ServerConfig *ServerConfig `yaml:"server"`
Backends []Backend `yaml:"backends"`
ServerConfig ServerConfig `yaml:"server"`
Backends []Backend `yaml:"backends"`
}
5 changes: 5 additions & 0 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ import (
type Pool struct {
backends map[string]*Backend
current uint64
lock sync.Mutex
}

func (b *Pool) Add(backend *Backend) (err error) {
b.lock.Lock()
defer b.lock.Unlock()
if b.backends[backend.Addr] != nil {
return fmt.Errorf("%v is already exists, remove it first", backend.Addr)
}
Expand All @@ -32,6 +35,8 @@ func (b *Pool) Add(backend *Backend) (err error) {
}

func (b *Pool) Remove(addr string) (err error) {
b.lock.Lock()
defer b.lock.Unlock()
delete(b.backends, addr)
return
}
Expand Down
46 changes: 30 additions & 16 deletions redirect_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,38 @@ func getOriginalDstAddr(conn *net.TCPConn) (addr net.Addr, c *net.TCPConn, err e
}

var (
socks5Clients *socks5.Client
clientLock sync.Mutex
currentSocks5Client *socks5.Client
updateClientLock sync.Mutex
)

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

defer func() {
clientLock.Unlock()
if err != nil || client == nil {
if err != nil {
log.Error(err)
socks5Clients = nil
return
}

log.Infof("markup current proxy connection: %v", client.Server)
socks5Clients = client
if client != nil && currentSocks5Client != client {
if currentSocks5Client != nil && (currentSocks5Client.TCPConn != nil || currentSocks5Client.UDPConn != nil) {
log.Tracef("close pervious client before update the current socks5 client")
_ = currentSocks5Client.Close()
currentSocks5Client = nil
}

log.Infof("markup current socks5 proxy client %v", client.Server)
currentSocks5Client = client
}

// lock the update client
updateClientLock.Unlock()
}()

// found a available backend
if backend := s.Pool.Next(); backend != nil {
backend := s.Pool.Next()
if backend != nil {
return backend.socks5Client(0)
}

Expand Down Expand Up @@ -115,14 +125,18 @@ func (s *Server) ListenTProxy(addr string) (err error) {
defer timer.Stop()
go func() {
for ; true; <-timer.C {
_, err := s.socks5Client()
if err != nil {
log.Tracef("start to update current socks5 proxy client")
if _, err := s.updateSocks5Client(); err != nil {
log.Error(err)
}
}
}()

for {
if s.tproxyListener == nil {
return fmt.Errorf("transparent socket listenser is closed")
}

tproxyConn, err := s.tproxyListener.Accept()
if err != nil {
log.Error(err)
Expand All @@ -132,11 +146,11 @@ func (s *Server) ListenTProxy(addr string) (err error) {
go func() {
defer tproxyConn.Close()

if socks5Clients == nil {
if currentSocks5Client == nil {
log.Error("not found any suitable socks5 clients")
return
}
log.Tracef("using connected socks5 proxy client: %v", socks5Clients.Server)
log.Tracef("using connected socks5 proxy client: %v", currentSocks5Client.Server)

connect, ok := tproxyConn.(*tproxy.Conn)
if !ok {
Expand All @@ -153,7 +167,7 @@ func (s *Server) ListenTProxy(addr string) (err error) {
defer orgDstConn.Close()

log.Tracef("[red-tcp] %s -> %s", srcAddr, dstAddr)
socks5Conn, err := socks5Clients.Dial("tcp", dstAddr.String())
socks5Conn, err := currentSocks5Client.Dial("tcp", dstAddr.String())
if err != nil {
log.Error(err)
}
Expand Down
20 changes: 16 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ type Status struct {

type Server struct {
Pool *Pool
Status map[*Backend]Status
Config *ServerConfig

Config *ServerConfig
healthCheckTimer *time.Ticker

socks5Listener net.Listener
Expand Down Expand Up @@ -70,8 +69,14 @@ func (s *Server) Stop() (e error) {
log.Debug("shutting down the server")
s.healthCheckTimer.Stop()

go s.socks5Listener.Close()
go s.tproxyListener.Close()
if s.socks5Listener != nil {
go s.socks5Listener.Close()
}

if s.tproxyListener != nil {
go s.tproxyListener.Close()
}

return
}

Expand All @@ -98,3 +103,10 @@ func (s *Server) Transport(dst, src io.ReadWriter) (err error) {
log.Tracef("transport stream is finished")
return
}

func NewServer(pool *Pool, config ServerConfig) (*Server, error) {
return &Server{
Pool: pool,
Config: &config,
}, nil
}
5 changes: 3 additions & 2 deletions socks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ func (s *Server) ListenSocks5(addr string) (err error) {
defer s.socks5Listener.Close()

for {
socks5Conn, err := s.socks5Listener.Accept()
var socks5Conn net.Conn
socks5Conn, err = s.socks5Listener.Accept()
if err != nil {
log.Error(err)
continue
return
}

go func() {
Expand Down

0 comments on commit 084d332

Please sign in to comment.