完成TPKT、X224、FastPath协议基本封装和测试

This commit is contained in:
2025-03-31 14:10:03 +08:00
parent 7c2bbde75b
commit 6720895668
9 changed files with 608 additions and 0 deletions

5
app/app.go Normal file
View File

@@ -0,0 +1,5 @@
package app
type App interface {
Start() error
}

38
app/client.go Normal file
View File

@@ -0,0 +1,38 @@
package app
import (
"fmt"
"log"
"net"
"rdp_channel/protocol"
"time"
)
type Client struct {
Host string
Port int
}
func NewClient(host string, port int) Client {
return Client{host, port}
}
func (client Client) Start() error {
addr := fmt.Sprintf("%s:%d", client.Host, client.Port)
conn, err := net.Dial("tcp", addr)
if err != nil {
return err
}
defer conn.Close()
log.Println("[Client] connected to " + addr)
tpkt := protocol.NewTPKT(conn)
//fast := protocol.NewFastPath(conn)
//x224 := protocol.NewX224(conn)
for {
time.Sleep(1 * time.Second)
err = tpkt.Write([]byte("This is a test message."))
}
}

11
app/client_test.go Normal file
View File

@@ -0,0 +1,11 @@
package app
import "testing"
func TestClient(t *testing.T) {
c := NewClient("127.0.0.1", 3388)
err := c.Start()
if err != nil {
t.Fatal(err)
}
}

54
app/server.go Normal file
View File

@@ -0,0 +1,54 @@
package app
import (
"fmt"
"log"
"net"
"rdp_channel/protocol"
)
type Server struct {
Host string
Port int
}
func NewServer(host string, port int) Server {
return Server{host, port}
}
func (server Server) Start() error {
addr := fmt.Sprintf("%s:%d", server.Host, server.Port)
listener, err := net.Listen("tcp", addr)
if err != nil {
return err
}
defer listener.Close()
log.Println("[SERVER] listening on " + addr)
for {
conn, err := listener.Accept()
if err != nil {
continue
}
go handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
defer conn.Close()
log.Println("[SERVER] new connection from " + conn.RemoteAddr().String())
tpkt := protocol.NewTPKT(conn)
//fast := protocol.NewFastPath(conn)
//x224 := protocol.NewX224(conn)
for {
payload, err := tpkt.Read()
if err != nil {
continue
}
log.Println("[SERVER] received payload: " + string(payload))
}
}

11
app/server_test.go Normal file
View File

@@ -0,0 +1,11 @@
package app
import "testing"
func TestServer(t *testing.T) {
s := NewServer("0.0.0.0", 3388)
err := s.Start()
if err != nil {
t.Fatal(err)
}
}