Skip to content

Commit

Permalink
Revert "智能代理"
Browse files Browse the repository at this point in the history
This reverts commit 00a8cb9.
  • Loading branch information
yuweibo committed Feb 14, 2025
1 parent 00a8cb9 commit 111a955
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 111 deletions.
6 changes: 1 addition & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,20 @@ var mode string
var wsServer string
var clientSocks5Port int
var clientJwtPrivateKeyFilePath string
var clientProxyDomainFilePath string
var forceWsProxy bool
var serverPort string

func main() {
flag.StringVar(&mode, "m", "s", "mode s(server) or c(client)")
flag.StringVar(&wsServer, "ws", "ws://localhost:1323/ws", "websocket Server")
flag.IntVar(&clientSocks5Port, "csp", 1080, "client Socks5 Port")
flag.StringVar(&clientJwtPrivateKeyFilePath, "cjp", "", "client Jwt PrivateKey File Path")
flag.StringVar(&clientProxyDomainFilePath, "dp", "", "client Proxy Domain File Path")
flag.BoolVar(&forceWsProxy, "fwp", true, "流量是否全部走webSocket")
flag.StringVar(&serverPort, "sp", "1323", "server Socks5 Port,default 1323")
flag.Parse()

if mode == "s" {
server.Listen(serverPort)
} else if mode == "c" {
client.Listen(client.Config{WsServerAddr: wsServer, Socks5Port: clientSocks5Port, JwtPrivateKeyFilePath: clientJwtPrivateKeyFilePath, ClientProxyDomainFilePath: clientProxyDomainFilePath, ForceWsProxy: forceWsProxy})
client.Listen(client.Config{WsServerAddr: wsServer, Socks5Port: clientSocks5Port, JwtPrivateKeyFilePath: clientJwtPrivateKeyFilePath})
} else {
fmt.Println("不支持该运行模式")
}
Expand Down
112 changes: 10 additions & 102 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package client
import (
"bufio"
"context"
"encoding/json"
"github.com/dgrijalva/jwt-go"
"github.com/labstack/gommon/log"
"github.com/patrickmn/go-cache"
Expand All @@ -13,18 +12,15 @@ import (
"net"
"os"
"strconv"
"strings"
"sync"
"time"
"webSocks5/pkg/protocol"
)

type Config struct {
WsServerAddr string
Socks5Port int
JwtPrivateKeyFilePath string
ClientProxyDomainFilePath string
ForceWsProxy bool
WsServerAddr string
Socks5Port int
JwtPrivateKeyFilePath string
}

var wsClientInitMu sync.Mutex
Expand All @@ -44,12 +40,6 @@ func Listen(config Config) {
log.Error(err)
return
}
domains, err := readProxyDomain(config)
if err != nil {
log.Error(err)
return
}
log.Info(domains)

listener, err := net.Listen("tcp", ":"+strconv.Itoa(config.Socks5Port))
if err != nil {
Expand All @@ -73,7 +63,7 @@ func Listen(config Config) {
//异步动态扩容ws连接
go prepareWsClient(wsAddr)
//解析socks协议传输数据
go rwSocksConn(conn, wsAddr, socksIdSeq, domains, config.ForceWsProxy)
go rwSocksConn(conn, wsAddr, socksIdSeq)
}
}

Expand Down Expand Up @@ -281,7 +271,7 @@ func closeWsConn(lock bool, key string, wsConn *websocket.Conn) {
}
}

func rwSocksConn(conn net.Conn, wsAddr string, socksIdSeq int, domains []string, forceWs bool) {
func rwSocksConn(conn net.Conn, wsAddr string, socksIdSeq int) {
socksId := socksIdPrefix + ":" + strconv.Itoa(socksIdSeq)
socksConnCache.SetDefault(socksId, conn)
defer closeSocksConn(nil, socksId, conn)
Expand Down Expand Up @@ -366,85 +356,17 @@ func rwSocksConn(conn net.Conn, wsAddr string, socksIdSeq int, domains []string,
}
dstPort := int(p1)<<8 + int(p2)
log.Debug("dstAddr:", dstAddr, "dstPort:", dstPort)

forceWsProxy := forceWsProxy(forceWs, domains, dstAddr, dstPort)
if forceWsProxy {
if wsProxy(conn, wsAddr, socksIdSeq, socksId, err, atyp, dstAddr, dstPort, reader) {
return
}
} else {
if directProxy(conn, wsAddr, socksIdSeq, dstAddr, dstPort, socksId, atyp, reader) {
return
}
}
log.Debug("socks connection closed")
}

func forceWsProxy(forceWs bool, domains []string, dstAddr string, dstPort int) bool {
if forceWs {
return true
}
include := false
for _, domain := range domains {
multiParts := strings.Contains(domain, ":")
if !multiParts {
if strings.Index(dstAddr, domain) > -1 {
include = true
break
}
} else {
parts := strings.SplitN(domain, ":", 2)
ip := parts[0]
port := parts[1]
if strings.Index(dstAddr, ip) > -1 && strconv.Itoa(dstPort) == port {
include = true
break
}
}
}
return include
}

func directProxy(conn net.Conn, wsAddr string, socksIdSeq int, dstAddr string, dstPort int, socksId string, atyp byte, reader *bufio.Reader) bool {
proxyConn, err := net.DialTimeout("tcp", dstAddr+":"+strconv.Itoa(dstPort), 5*time.Second)
if err != nil {
log.Error(err)
if wsProxy(conn, wsAddr, socksIdSeq, socksId, err, atyp, dstAddr, dstPort, reader) {
return true
}
return true
}
defer proxyConn.Close()
_, err = conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0})
if err != nil {
log.Error(err)
return true
}
ctx, cancel := context.WithCancel(context.Background())
go func() {
io.Copy(proxyConn, conn)
cancel()
}()
go func() {
io.Copy(conn, proxyConn)
cancel()
}()
<-ctx.Done()
return false
}

func wsProxy(conn net.Conn, wsAddr string, socksIdSeq int, socksId string, err error, atyp byte, dstAddr string, dstPort int, reader *bufio.Reader) bool {
wsCon, wsKey := chooseWsConn(0, wsAddr, socksIdSeq)
if wsCon == nil {
log.Warn("wsCon is nil")
return true
return
}
socksWsKeyCache.SetDefault(socksId, wsKey)
defer closeSocksConn(wsCon, socksId, conn)
err = websocket.JSON.Send(wsCon, protocol.WsProtocol{SocksId: socksId, Op: protocol.OPEN, DstAddrType: int(atyp), TargetAddr: dstAddr, TargetPort: dstPort})
if err != nil {
log.Error(err)
return true
return
}

ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -454,7 +376,9 @@ func wsProxy(conn net.Conn, wsAddr string, socksIdSeq int, socksId string, err e
}()

<-ctx.Done()
return false

log.Debug("socks connection closed")

}

func closeSocksConn(wsCon *websocket.Conn, socksId string, conn net.Conn) {
Expand Down Expand Up @@ -500,19 +424,3 @@ func genJwtToken(privateKeyFilePath string) (string, error) {
jwtCache.SetDefault(privateKeyFilePath, signedToken)
return signedToken, nil
}

func readProxyDomain(config Config) ([]string, error) {
if config.ClientProxyDomainFilePath != "" {
domain, err := os.ReadFile(config.ClientProxyDomainFilePath)
if err != nil {
return make([]string, 0), err
}
var domains []string
err = json.Unmarshal(domain, &domains)
if err != nil {
return make([]string, 0), err
}
return domains, nil
}
return make([]string, 0), nil
}
4 changes: 0 additions & 4 deletions proxyDomain.json

This file was deleted.

0 comments on commit 111a955

Please sign in to comment.