Skip to content

Commit

Permalink
Bugfix: The client reads multi-TCP packets causes parsing to fail.
Browse files Browse the repository at this point in the history
  • Loading branch information
Leviathan1995 committed Jan 12, 2022
1 parent f13e695 commit 9c1aaed
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
12 changes: 6 additions & 6 deletions README.md
Expand Up @@ -13,11 +13,11 @@
* 通过 [release]() 下载对应架构的 spleen 包:
```shell
# wget 下载 (请自行替换最新版本)
> wget https://github.com/Leviathan1995/spleen/releases/download/v0.0.1/spleen_0.0.1_Linux_64-bit.tar.gz
> wget https://github.com/Leviathan1995/spleen/releases/download/v0.0.2/spleen_0.0.2_Linux_64-bit.tar.gz

# 解压
> tar -zxvf spleen_0.0.1_Linux_64-bit.tar.gz
> cd spleen_0.0.1_Linux_64-bit
> tar -zxvf spleen_0.0.2_Linux_64-bit.tar.gz
> cd spleen_0.0.2_Linux_64-bit

# 配置端口转发规则
> vim .server.json
Expand All @@ -44,11 +44,11 @@
* 通过 [release]() 下载对应架构的 spleen 包:
```shell
# wget 下载 (请自行替换最新版本)
> wget https://github.com/Leviathan1995/spleen/releases/download/v0.0.1/spleen_0.0.1_Linux_64-bit.tar.gz
> wget https://github.com/Leviathan1995/spleen/releases/download/v0.0.2/spleen_0.0.2_Linux_64-bit.tar.gz

# 解压
> tar -zxvf spleen_0.0.1_Linux_64-bit.tar.gz
> cd spleen_0.0.1_Linux_64-bit
> tar -zxvf spleen_0.0.2_Linux_64-bit.tar.gz
> cd spleen_0.0.2_Linux_64-bit

# 配置公网服务器地址
> vim .client.json
Expand Down
8 changes: 6 additions & 2 deletions client/util/client.go
Expand Up @@ -49,15 +49,19 @@ func (c *client) handleConn(srvConn *net.TCPConn) {
defer srvConn.Close()

/* Get the transfer port. */
portBuf := make([]byte, 32)
portBuf := make([]byte, 16)
nRead, err := srvConn.Read(portBuf)
if err != nil {
return
}
_ = <-connectionPool /* Remove a connection from pool. */

/* Try to direct connect to the destination sever. */
dstAddr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1"+":"+string(portBuf[:nRead]))
dstAddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1"+":"+string(portBuf[:nRead]))
if err != nil {
return
}

log.Printf("Try to connect %s:%d.\n", dstAddr.IP.String(), dstAddr.Port)
dstConn, err := net.DialTCP("tcp", nil, dstAddr)
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions server/util/server.go
Expand Up @@ -85,8 +85,10 @@ func (s *server) handleConn(cliConn *net.TCPConn, transferPort string) {
_ = intranetConn.SetLinger(0)

/* Send the mapping port to intranet server . */
log.Println("Send the mapping port to intranet server.")
err := s.TCPWrite(intranetConn, []byte(transferPort))
log.Printf("Send the mapping port %s to intranet server.\n", transferPort)
portBuf := make([]byte, 16)
copy(portBuf, transferPort)
err := s.TCPWrite(intranetConn, portBuf)
if err != nil {
return
}
Expand Down

0 comments on commit 9c1aaed

Please sign in to comment.