Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vCassius committed Nov 30, 2021
1 parent 930ebdb commit 7761d6d
Show file tree
Hide file tree
Showing 19 changed files with 636 additions and 5 deletions.
14 changes: 11 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@
*.dll
*.so
*.dylib

# goland
*.iml
*.ipr
*.iws
.idea/
# Test binary, built with `go test -c`
*.test

# logs
*.log
syslog/
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# project
TransGateLinux
TransGate
# Dependency directories (remove the comment below to include it)
# vendor/
44 changes: 42 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,42 @@
# transgate
tcp transgate TCP转发服务器
# TransGate
### 作者信息
Author: Vincent
E-Mail: [email protected]
QQ: 46603415
### 配置参数
#### 基本配置
`config.json`
```json
{
"IP":"0.0.0.0",
"Port":"7200",
"DesSrvIP":"192.168.10.249",
"DesSrvPort":"3389",
"MaxUser":4,
"RunModel":"bl"
}
```
```conf
"IP":"0.0.0.0", //转发服务器ip地址
"Port":"7200", //转发服务器端口
"DesSrvIP":"192.168.10.249", //目标服务器IP地址
"DesSrvPort":"3389", //目标服务器端口
"MaxUser":4 //最大连接数
"RunModel":"bl" //运行白名单模式写参数wl,运行黑名单模式写参数bl.
```
#### 白名单
`whitelist.json`
json数组,分隔符为,修改后必须重启网关生效
```json
{
"AcceptIPList":"127.0.0.1,192.168.10.255"
}
```
#### 黑名单
`blacklist.json`
json数组,分隔符为,修改后必须重启网关生效
```json
{
"BanIPList":"192.168.10.255,192.168.10.254,127.0.0.1"
}
```
3 changes: 3 additions & 0 deletions blacklist.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"BanIPList":"192.168.10.255,192.168.10.254,127.0.0.1"
}
8 changes: 8 additions & 0 deletions build-linux64.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/zsh
BUILD_TIME=$(date '+%Y/%m/%d %H:%M:%S')
BUILD_GO_VERSION=$(go version | awk '{print $3"@"$4}')
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-ldflags \
"-X 'TransGate/version.BuildTime=${BUILD_TIME}' \
-X TransGate/version.BuildGoVersion=${BUILD_GO_VERSION}" \
-o TransGateLinux main.go
8 changes: 8 additions & 0 deletions build-macos.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/zsh
BUILD_TIME=$(date '+%Y/%m/%d %H:%M:%S')
BUILD_GO_VERSION=$(go version | awk '{print $3"@"$4}')
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build \
-ldflags \
"-X 'TransGate/version.BuildTime=${BUILD_TIME}' \
-X TransGate/version.BuildGoVersion=${BUILD_GO_VERSION}" \
-o TransGate main.go
8 changes: 8 additions & 0 deletions build-win64.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/zsh
BUILD_TIME=$(date '+%Y/%m/%d %H:%M:%S')
BUILD_GO_VERSION=$(go version | awk '{print $3"@"$4}')
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build \
-ldflags \
"-X 'TransGate/version.BuildTime=${BUILD_TIME}' \
-X TransGate/version.BuildGoVersion=${BUILD_GO_VERSION}" \
-o TransGateWin64.exe main.go
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"IP":"0.0.0.0",
"Port":"7200",
"DesSrvIP":"rr-bp1ne645nbd0x7j8r4o.mysql.rds.aliyuncs.com",
"DesSrvPort":"3306",
"MaxUser":10,
"RunModel":"wl"
}
47 changes: 47 additions & 0 deletions global/global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @Author: Vincent
* @Author: [email protected]
* @Date: 2020/9/17 10:36 上午
* @Desc:
*/

package global

import (
"TransGate/logs"
"runtime"
)

// TransGateConf 全局变量来存储配置信息
type TransGateConf struct {
IP string
Port string
MaxUser int64 //允许连接的用户最大值
DesSrvIP string //目标服务器OP
DesSrvPort string //目标服务器端口
RunModel string //定义了运行模式,wl代表白名单,bl代表黑名单,白名单模式加载wl.json(只允许白名单访问),黑名单加载bl(除了黑名单以外的ip都可以访问)
}

type BanConf struct {
BanIPList string
}
type WLConf struct {
AcceptIPList string
}

type JsonStruct struct {
}

var IP string
var Port string
var MaxUser int64
var DesSrvIP string
var DesSrvPort string
var BanIPList string //设计用于禁止链接的ip列表
var BanIPArr []string
var AcceptIPList string
var AcceptIPArr []string
var Logger = logs.InitLogger("./syslog/TransGateSys.log", "debug")
var ConnectCount int64
var RunModel string //服务器运行模式
var CPUCoreMax = runtime.NumCPU() //全局变量记录cpu核心数
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module TransGate

go 1.14

require (
github.com/gogf/gf v1.13.4
github.com/natefinch/lumberjack v2.0.0+incompatible
go.uber.org/zap v1.16.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
)
80 changes: 80 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/clbanning/mxj v1.8.5-0.20200714211355-ff02cfb8ea28/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/gogf/gf v1.13.4 h1:+IWX/L/SNYjwp9C0tXABZ8vJfYaFKyx0cTAb2DE2a1Y=
github.com/gogf/gf v1.13.4/go.mod h1:dGX0/BElXDBYbdJGascqfrWScj8IMeOietDjVD6/5Fc=
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gqcn/structs v1.1.1/go.mod h1:/aBhTBSsKQ2Ec9pbnYdGphtdWXHFn4KrCL0fXM/Adok=
github.com/grokify/html-strip-tags-go v0.0.0-20190921062105-daaa06bf1aaf/go.mod h1:2Su6romC5/1VXOQMaWL2yb618ARB8iVo6/DR99A6d78=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee h1:0mgffUl7nfd+FpvXMVz4IDEaUSmT1ysygQC7qYo7sG4=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM=
go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5 h1:hKsoRgsbwY1NafxrwTs+k64bikrLBkAgPir1TNCj3Zs=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
42 changes: 42 additions & 0 deletions logic/kernel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @Author: Vincent
* @Author: [email protected]
* @Date: 2020/9/17 11:31 上午
* @Desc: 数据交换拷贝
*/

package logic

import (
"TransGate/global"
"io"
"net"
"sync/atomic"
)

func DataExHandle(proxy net.Conn, target net.Conn) {
defer func(proxy net.Conn) {
err := proxy.Close()
if err != nil {

}
}(proxy)
defer func(target net.Conn) {
err := target.Close()
if err != nil {

}
}(target)
ExitChan := make(chan bool, 1)
go func(proxy net.Conn, target net.Conn, ExChan chan bool) {
_, _ = io.Copy(proxy, target)
ExitChan <- true
}(proxy, target, ExitChan)
go func(proxy net.Conn, target net.Conn, ExChan chan bool) {
_, _ = io.Copy(target, proxy)
ExitChan <- true
}(proxy, target, ExitChan)

<-ExitChan
atomic.AddInt64(&global.ConnectCount, -1)
}
101 changes: 101 additions & 0 deletions logic/loadconf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @Author: Vincent
* @Author: [email protected]
* @Date: 2020/9/17 10:47 上午
* @Desc: 加载配置
*/

package logic

import (
"TransGate/global"
"encoding/json"
"io/ioutil"
"os"
"strings"
)

func Exists(path string) bool {
_, err := os.Stat(path) //os.Stat获取文件信息
if err != nil {
if os.IsNotExist(err) {
return false
}
return true
}
return true
}

func LoadAllConfig() {
chkGateConf := Exists("./config.json")
chkBanConf := Exists("./blacklist.json")
chkWLConf := Exists("./whitelist.json")
if chkGateConf && chkBanConf && chkWLConf {
LoadTransGateConf()
LoadBanConf()
LoadWLConf()
} else {
//NoticeInfo("load conf error!")
global.Logger.Error("load conf error!")
os.Exit(1)
}

}

// LoadTransGateConf 加载网关Json配置
func LoadTransGateConf() {
JsonParse := NewJsonStruct()
conf := global.TransGateConf{}
JsonParse.Load("./config.json", &conf)
global.IP = conf.IP
global.Port = conf.Port
global.MaxUser = conf.MaxUser
global.DesSrvIP = conf.DesSrvIP
global.DesSrvPort = conf.DesSrvPort
global.RunModel = conf.RunModel
}

// LoadBanConf 加载黑名单配置
func LoadBanConf() {
JsonParse := NewJsonStruct()
conf := BanConf{}
JsonParse.Load("./blacklist.json", &conf)
global.BanIPList = conf.BanIPList
global.BanIPArr = strings.Split(global.BanIPList, ",") //以,为分隔符,分割字符串导入到字符串数组,用于存储拒绝连接列表
}

// LoadWLConf 加载黑名单配置
func LoadWLConf() {
JsonParse := NewJsonStruct()
conf := WLConf{}
JsonParse.Load("./whitelist.json", &conf)
global.AcceptIPList = conf.AcceptIPList
global.AcceptIPArr = strings.Split(global.AcceptIPList, ",") //以,为分隔符,分割字符串导入到字符串数组,用于存储拒绝连接列表
}

type BanConf struct {
BanIPList string
}
type WLConf struct {
AcceptIPList string
}

func NewJsonStruct() *JsonStruct {
return &JsonStruct{}
}

type JsonStruct struct {
}

func (jst *JsonStruct) Load(filename string, v interface{}) {
//ReadFile函数会读取文件的全部内容,并将结果以[]byte类型返回
data, err := ioutil.ReadFile(filename)
if err != nil {
return
}
//读取的数据为json格式,需要进行解码
err = json.Unmarshal(data, v)
if err != nil {
return
}
}
Loading

0 comments on commit 7761d6d

Please sign in to comment.