Skip to content

Commit

Permalink
端口转发简单测试通过
Browse files Browse the repository at this point in the history
  • Loading branch information
1113655791@qq.com committed Apr 12, 2023
1 parent 005e25b commit 63d9a00
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 14 deletions.
6 changes: 3 additions & 3 deletions dial/proxy/proxy_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,13 @@ func DefaultConnectFunc(msg *Message) (i io.ReadWriteCloser, err error) {
}

func NewTCPClient(addr string, fn ...func(ctx context.Context, c *io.Client, e *Entity)) *io.Client {
e := New()
return dial.RedialPipe(addr, func(ctx context.Context, c *io.Client) {
c.SetPrintFunc(PrintWithASCII)
e := New()
for _, v := range fn {
v(ctx, c, e)
}
c.Swap(e)
c.SetPrintFunc(PrintWithASCII)
})
}

Expand All @@ -216,7 +216,7 @@ func NewSwapTCPServer(port int, fn ...func(s *io.Server)) error {
func WithClientDebug(b ...bool) func(ctx context.Context, c *io.Client, e *Entity) {
return func(ctx context.Context, c *io.Client, e *Entity) {
c.Debug(b...)
e.Debug()
e.Debug(b...)
}
}

Expand Down
17 changes: 13 additions & 4 deletions dial/proxy/proxy_server_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/injoyai/io"
"github.com/injoyai/io/dial"
"github.com/injoyai/logs"
"regexp"
"strings"
)

Expand All @@ -29,10 +30,19 @@ type PortForwardingServer struct {
// Listen 监听
func (this *PortForwardingServer) Listen(port int, sn, addr string) error {
s, err := dial.NewTCPServer(port, func(s *io.Server) {
s.Debug()
s.Tag.Set("sn", sn)
s.Tag.Set("addr", addr)
s.SetDealFunc(func(msg *io.IMessage) {
{
if _addr := regexp.MustCompile("(\\?|&)(_addr=)[0-9.:]+").FindString(msg.String()); len(_addr) > 7 {
s.Tag.Set("addr", _addr[7:])
}
if _sn := regexp.MustCompile("(\\?|&)(_sn=)[0-9a-zA-Z]+").FindString(msg.String()); len(_sn) > 5 {
s.Tag.Set("sn", _sn[5:])
}
}
sn = s.Tag.GetString("sn")
addr = s.Tag.GetString("addr")
pipe := this.GetClient(sn)
if pipe == nil {
msg.Client.CloseWithErr(fmt.Errorf("通道客户端未连接,关闭连接"))
Expand All @@ -53,10 +63,9 @@ func (this *PortForwardingServer) Listen(port int, sn, addr string) error {
}

// NewPortForwardingServer 端口转发服务端
func NewPortForwardingServer(port int) (*PortForwardingServer, error) {
pipeServer, err := dial.NewPipeServer(port)
func NewPortForwardingServer(port int, fn ...func(s *io.Server)) (*PortForwardingServer, error) {
pipeServer, err := dial.NewPipeServer(port, fn...)
ser := &PortForwardingServer{Server: pipeServer, listen: maps.NewSafe()}
pipeServer.Debug()
pipeServer.SetCloseFunc(func(msg *io.IMessage) {
//通道断开连接,则关闭所有代理连接
sn := msg.GetKey()
Expand Down
5 changes: 5 additions & 0 deletions io_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ func (this *Client) SetPrintWithASCII() {
this.SetPrintFunc(PrintWithASCII)
}

// SetPrintWithBase 设置打印ASCII,基础信息
func (this *Client) SetPrintWithBase() {
this.SetPrintFunc(PrintWithBase)
}

// SetReadWriteWithPkg 设置读写为默认分包方式
func (this *Client) SetReadWriteWithPkg() *Client {
this.IWriter.SetWriteWithPkg()
Expand Down
6 changes: 6 additions & 0 deletions io_i_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ func PrintWithASCII(msg Message, tag ...string) {
log.Print(PrintfWithASCII(msg, tag...))
}

func PrintWithBase(msg Message, tag ...string) {
if len(tag) > 0 && (tag[0] == TagErr || tag[0] == TagInfo) {
log.Print(PrintfWithASCII(msg, tag...))
}
}

func PrintfWithHEX(msg Message, tag ...string) string {
t := strings.Join(tag, "][")
if len(t) > 0 {
Expand Down
5 changes: 5 additions & 0 deletions io_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ func (this *Server) SetPrintWithASCII() *Server {
return this.SetPrintFunc(PrintWithASCII)
}

// SetPrintWithBase 设置打印方式ASCII,打印基础信息
func (this *Server) SetPrintWithBase() *Server {
return this.SetPrintFunc(PrintWithBase)
}

// SetTimeout 设置超时时间,还有time/3的时间误差
func (this *Server) SetTimeout(t time.Duration) *Server {
this.timeout = t
Expand Down
9 changes: 9 additions & 0 deletions testdata/main/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#-ldflags="-w -s"
#-ldflags="-H windowsgui"
#-ldflags="-X "

name="portProxyServer"

GOOS=linux GOARCH=amd64 go build -v -ldflags="-w -s" -o ./$name

sleep 5
14 changes: 9 additions & 5 deletions testdata/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ func main() {

func NewPortForwardingClient() {
serverAddr := ""
for len(serverAddr) == 0 {
fmt.Println("请输入服务地址(默认121.36.99.197:9000):")
fmt.Scanln(&serverAddr)
fmt.Println("请输入服务地址(默认121.36.99.197:9000):")
fmt.Scanln(&serverAddr)
if len(serverAddr) == 0 {
serverAddr = "121.36.99.197:9000"
}
sn := ""
fmt.Println("请输入SN(默认test):")
Expand All @@ -38,6 +39,7 @@ func NewPortForwardingClient() {
fmt.Println("请输入代理地址(默认代理全部):")
fmt.Scanln(&proxyAddr)
c := proxy.NewPortForwardingClient(serverAddr, sn, func(ctx context.Context, c *io.Client, e *proxy.Entity) {
c.SetPrintWithBase()
c.Debug()
if len(proxyAddr) > 0 {
e.SetWriteFunc(func(msg *proxy.Message) (*proxy.Message, error) {
Expand All @@ -53,11 +55,13 @@ func NewPortForwardingClient() {
func NewPortForwardingServer() error {

port := cfg.GetInt("port", 9000)
s, err := proxy.NewPortForwardingServer(port)
s, err := proxy.NewPortForwardingServer(port, func(s *io.Server) {
s.SetPrintWithBase()
s.Debug()
})
if err != nil {
return err
}
s.Debug()
for _, v := range cfg.GetStrings("listen") {
m := conv.NewMap(v)
logs.PrintErr(s.Listen(m.GetInt("port"), m.GetString("sn"), m.GetString("addr")))
Expand Down
4 changes: 2 additions & 2 deletions testdata/test_port_forwarding.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// NewPortForwardingClient 端口转发客户端
func NewPortForwardingClient(addr string) error {
return proxy.NewPortForwardingClient(addr, "sn", proxy.WithClientDebug()).Run()
return proxy.NewPortForwardingClient(addr, "sn", proxy.WithClientDebug(false)).Run()
}

// NewPortForwardingServer 端口转发服务端
Expand All @@ -16,7 +16,7 @@ func NewPortForwardingServer(port int) error {
if err != nil {
return err
}
s.Debug()
s.Debug(false)
logs.PrintErr(s.Listen(10000, "sn", "192.168.10.24:10001"))
return s.Run()
}

0 comments on commit 63d9a00

Please sign in to comment.