Skip to content

Commit

Permalink
Merge tag '1.0.0' into develop
Browse files Browse the repository at this point in the history
no message
  • Loading branch information
mingcheng committed Jul 7, 2022
2 parents 3eb5662 + 7bdbf07 commit 1056861
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 24 deletions.
1 change: 1 addition & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ steps:
dockerfile: Dockerfile
tags:
- latest
- 1.0.0

volumes:
- name: docker-sock
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ backends:
timeout: 3
```
#### 环境变量
- `SELECT_TIME_INTERVAL` 自动切换代理的时间,单位为秒(默认300秒,五分钟)
- `CHECK_TIME_INTERVAL` 健康检查的轮询时间,单位为秒(默认一分钟)
- `DEBUG` 是否打开 debug 模式

### 部署

首先,以下是 docker-compose 相关的配置,建议使用 `network_mode: 'host'` 方式,防止 DOCK 的 iptables 造成网络联通错误
Expand Down
10 changes: 10 additions & 0 deletions backend.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/**
* File: backend.go
* Author: Ming Cheng<mingcheng@outlook.com>
*
* Created Date: Tuesday, June 21st 2022, 6:03:26 pm
* Last Modified: Thursday, July 7th 2022, 6:30:08 pm
*
* http://www.opensource.org/licenses/MIT
*/

package socks5lb

import (
Expand Down
37 changes: 29 additions & 8 deletions cmd/socks5lb/main.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
/**
* File: main.go
* Author: Ming Cheng<mingcheng@outlook.com>
*
* Created Date: Wednesday, June 22nd 2022, 12:39:47 pm
* Last Modified: Thursday, July 7th 2022, 6:29:42 pm
*
* http://www.opensource.org/licenses/MIT
*/

package main

import (
"flag"
"io/ioutil"
"syscall"

"github.com/judwhite/go-svc"
"github.com/mingcheng/socks5lb"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"io/ioutil"
"syscall"

"os"
)

const AppName = "socks5lb"

var config *socks5lb.Configure
var err error
var configFilePath string
var (
config *socks5lb.Configure
err error
cfgPath string
)

func init() {
log.SetOutput(os.Stdout)
log.SetLevel(log.TraceLevel)

flag.StringVar(&configFilePath, "c", "/etc/"+AppName+".yml", "configure file path")
isDebug := socks5lb.GetEnv("DEBUG", "")
if isDebug != "" {
log.SetLevel(log.TraceLevel)
} else {
log.SetLevel(log.InfoLevel)
}

flag.StringVar(&cfgPath, "c", "/etc/"+AppName+".yml", "configure file cfgPath")
}

// NewConfig returns a new Config instance
func NewConfig(path string) (config *socks5lb.Configure, err error) {
var (
data []byte
Expand All @@ -44,7 +64,8 @@ func NewConfig(path string) (config *socks5lb.Configure, err error) {
func main() {
flag.Parse()

if config, err = NewConfig(configFilePath); err != nil {
// read the config if err != nil
if config, err = NewConfig(cfgPath); err != nil {
log.Fatal(err)
}

Expand Down
14 changes: 14 additions & 0 deletions cmd/socks5lb/program.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/**
* File: program.go
* Author: Ming Cheng<mingcheng@outlook.com>
*
* Created Date: Wednesday, July 6th 2022, 2:14:35 pm
* Last Modified: Thursday, July 7th 2022, 6:29:55 pm
*
* http://www.opensource.org/licenses/MIT
*/

package main

import (
Expand All @@ -7,12 +17,14 @@ import (

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
}

// Init to initial the program
func (p *program) Init(env svc.Environment) (err error) {

log.Tracef("new initial backend pools")
Expand All @@ -31,6 +43,7 @@ func (p *program) Init(env svc.Environment) (err error) {
return
}

// Start when the program is start
func (p *program) Start() (err error) {
go func() {
err = p.Server.Start(p.Config.Socks5Listen, p.Config.TproxyListen)
Expand All @@ -39,6 +52,7 @@ func (p *program) Start() (err error) {
return
}

// Stop when the program is stop
func (p *program) Stop() (err error) {
return p.Server.Stop()
}
10 changes: 10 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/**
* File: config.go
* Author: Ming Cheng<mingcheng@outlook.com>
*
* Created Date: Tuesday, June 21st 2022, 6:03:38 pm
* Last Modified: Thursday, July 7th 2022, 6:30:15 pm
*
* http://www.opensource.org/licenses/MIT
*/

package socks5lb

type Configure struct {
Expand Down
10 changes: 10 additions & 0 deletions pool.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/**
* File: pool.go
* Author: Ming Cheng<mingcheng@outlook.com>
*
* Created Date: Tuesday, June 21st 2022, 6:03:26 pm
* Last Modified: Thursday, July 7th 2022, 6:47:39 pm
*
* http://www.opensource.org/licenses/MIT
*/

package socks5lb

import (
Expand Down
10 changes: 10 additions & 0 deletions redirect.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
//go:build !linux

/**
* File: redirect.go
* Author: Ming Cheng<mingcheng@outlook.com>
*
* Created Date: Wednesday, July 6th 2022, 11:46:51 am
* Last Modified: Thursday, July 7th 2022, 6:31:04 pm
*
* http://www.opensource.org/licenses/MIT
*/

package socks5lb

import (
Expand Down
70 changes: 65 additions & 5 deletions redirect_linux.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
//go:build linux

/**
* File: redirect_linux.go
* Author: Ming Cheng<mingcheng@outlook.com>
*
* Created Date: Wednesday, July 6th 2022, 11:47:00 am
* Last Modified: Thursday, July 7th 2022, 6:32:42 pm
*
* http://www.opensource.org/licenses/MIT
*/

package socks5lb

import (
"errors"
"fmt"
"github.com/LiamHaworth/go-tproxy"
log "github.com/sirupsen/logrus"
"github.com/txthinking/socks5"
"net"
"sync"
"syscall"
"time"
)

// getOriginalDstAddr to get the original address from the socket
func getOriginalDstAddr(conn *net.TCPConn) (addr net.Addr, c *net.TCPConn, err error) {
defer conn.Close()

Expand Down Expand Up @@ -40,12 +54,42 @@ func getOriginalDstAddr(conn *net.TCPConn) (addr net.Addr, c *net.TCPConn, err e

c, ok := cc.(*net.TCPConn)
if !ok {
err = errors.New("not a TCP connection")
err = errors.New("sorry, this is not a TCP connection")
}

return
}

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

func (s *Server) socks5Client() (client *socks5.Client, err error) {
clientLock.Lock()

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

log.Infof("markup current proxy connection: %v", client.Server)
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
}

return backend.socks5Client(0)
}

func (s *Server) ListenTProxy(addr string) (err error) {
tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
Expand All @@ -58,6 +102,22 @@ func (s *Server) ListenTProxy(addr string) (err error) {
log.Error(err)
return
}
defer s.tproxyListener.Close()

// connect to available socks5 proxy server
selectTimeInterval := SecFromEnv("SELECT_TIME_INTERVAL", 300)
log.Infof("auto select the socks5 proxy server every %v", selectTimeInterval)

timer := time.NewTicker(selectTimeInterval)
defer timer.Stop()
go func() {
for ; true; <-timer.C {
_, err := s.socks5Client()
if err != nil {
log.Error(err)
}
}
}()

for {
tproxyConn, err := s.tproxyListener.Accept()
Expand All @@ -69,11 +129,11 @@ func (s *Server) ListenTProxy(addr string) (err error) {
go func() {
defer tproxyConn.Close()

backend := s.Pool.Next()
if backend == nil {
log.Error("sorry, we don't have healthy backend, so close the connection")
if socks5Clients == nil {
log.Error("not found any suitable socks5 clients")
return
}
log.Tracef("using connected socks5 proxy client: %v", socks5Clients.Server)

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

log.Tracef("[red-tcp] %s -> %s", srcAddr, dstAddr)
socks5Conn, err := backend.Socks5Conn("tcp", dstAddr.String(), 0)
socks5Conn, err := socks5Clients.Dial("tcp", dstAddr.String())
if err != nil {
log.Error(err)
}
Expand Down
24 changes: 13 additions & 11 deletions server.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
/**
* File: server.go
* Author: Ming Cheng<mingcheng@outlook.com>
*
* Created Date: Wednesday, July 6th 2022, 5:39:05 pm
* Last Modified: Thursday, July 7th 2022, 6:31:24 pm
*
* http://www.opensource.org/licenses/MIT
*/

package socks5lb

import (
"io"
"net"
"strconv"
"time"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -32,18 +41,11 @@ func (s *Server) AddBackend() error {
}

func (s *Server) Start(socksListenAddr, tproxyListenAddr string) (err error) {
intervalStr := GetEnv("CHECK_TIME_INTERVAL", "1")
interval, err := strconv.ParseInt(intervalStr, 10, 64)
if err != nil {
log.Debugf("parse health check with error %v, reset default value 1", err)
interval = 1
}
healthCheckTime := time.Duration(interval) * time.Second
log.Tracef("check time interval is %v", healthCheckTime)
duration := SecFromEnv("CHECK_TIME_INTERVAL", 60)

s.healthCheckTimer = time.NewTicker(healthCheckTime)
s.healthCheckTimer = time.NewTicker(duration)
go func() {
log.Tracef("start health check, every %v", healthCheckTime)
log.Infof("auto check backend healthy, every %v", duration)
for ; true; <-s.healthCheckTimer.C {
s.Pool.Check()
}
Expand Down
12 changes: 12 additions & 0 deletions socks5.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
/**
* File: socks5.go
* Author: Ming Cheng<mingcheng@outlook.com>
*
* Created Date: Wednesday, July 6th 2022, 11:46:39 am
* Last Modified: Thursday, July 7th 2022, 6:31:37 pm
*
* http://www.opensource.org/licenses/MIT
*/

package socks5lb

import (
log "github.com/sirupsen/logrus"
"net"
)

// ListenSocks5 to listen on a specific address
func (s *Server) ListenSocks5(addr string) (err error) {
s.socks5Listener, err = net.Listen("tcp", addr)
if err != nil {
log.Error(err)
return
}
defer s.socks5Listener.Close()

for {
socks5Conn, err := s.socks5Listener.Accept()
Expand Down
Loading

0 comments on commit 1056861

Please sign in to comment.