Skip to content

Commit

Permalink
support custom udp port range
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed Jul 27, 2024
1 parent 4ef5969 commit e13c7dc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -715,14 +715,15 @@ trzsz-ssh ( tssh ) 设计为 ssh 客户端的直接替代品,提供与 openssh
```
Host xxx
#!! UdpMode yes
#!! UdpPort 61000-62000
#!! TsshdPath ~/go/bin/tsshd
```

- `tssh` 在客户端扮演 `ssh` 的角色,`tsshd` 在服务端扮演 `sshd` 的角色。

- `tssh` 会先作为一个 ssh 客户端正常登录到服务器上,然后在服务器上启动一个新的 `tsshd` 进程。

- `tsshd` 进程会随机侦听一个 61000 到 62000 之间的 UDP 端口,并将其端口和密钥通过 ssh 通道发回给 `tssh` 进程。登录的 ssh 连接会被关闭,然后 `tssh` 进程通过 UDP 与 `tsshd` 进程通讯。
- `tsshd` 进程会随机侦听一个 61000 到 62000 之间的 UDP 端口(可通过 `UdpPort` 配置自定义),并将其端口和密钥通过 ssh 通道发回给`tssh`进程。登录的 ssh 连接会被关闭,然后`tssh`进程通过 UDP 与`tsshd` 进程通讯。

- `tsshd` 支持 `QUIC` 协议和 `KCP` 协议(默认是 `QUIC` 协议),可以命令行指定(如 `-oUdpMode=KCP`),或如下配置:

Expand Down
3 changes: 2 additions & 1 deletion README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -717,14 +717,15 @@ trzsz-ssh ( tssh ) is an ssh client designed as a drop-in replacement for the op
```
Host xxx
#!! UdpMode yes
#!! UdpPort 61000-62000
#!! TsshdPath ~/go/bin/tsshd
```
- The `tssh` plays the role of `ssh` on the client side, and the `tsshd` plays the role of `sshd` on the server side.
- The `tssh` will first login to the server normally as an ssh client, and then run a new `tsshd` process on the server.
- The `tsshd` process listens on a random udp port between 61000 and 62000, and sends its port number and a secret key back to the `tssh` process over the ssh channel. The ssh connection is then shut down, and the `tssh` process communicates with the `tsshd` process over udp.
- The `tsshd` process listens on a random udp port between 61000 and 62000 (can be customized by `UdpPort`), and sends its port number and a secret key back to the `tssh`process over the ssh channel. The ssh connection is then shut down, and the`tssh`process communicates with the`tsshd` process over udp.
- The `tsshd` supports `QUIC` protocol and `KCP` protocol (the default is `QUIC`), which can be specified on the command line (such as `-oUdpMode=KCP`), or configured as follows:
Expand Down
32 changes: 32 additions & 0 deletions tssh/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"sync"
"sync/atomic"
"time"
"unicode"

"github.com/google/shlex"
"github.com/trzsz/tsshd/tsshd"
Expand Down Expand Up @@ -851,6 +852,37 @@ func getTsshdCommand(args *sshArgs, udpMode int) string {
buf.WriteString(" --kcp")
}

if udpPort := getExOptionConfig(args, "UdpPort"); udpPort != "" {
ports := strings.FieldsFunc(udpPort, func(c rune) bool {
return unicode.IsSpace(c) || c == ',' || c == '-'
})
if len(ports) == 1 {
port, err := strconv.Atoi(ports[0])
if err != nil {
warning("UdpPort %s is invalid: %v", udpPort, err)
} else {
buf.WriteString(fmt.Sprintf(" --port %d", port))
}
} else if len(ports) == 2 {
for {
lowPort, err := strconv.Atoi(ports[0])
if err != nil {
warning("UdpPort %s is invalid: %v", udpPort, err)
break
}
highPort, err := strconv.Atoi(ports[1])
if err != nil {
warning("UdpPort %s is invalid: %v", udpPort, err)
break
}
buf.WriteString(fmt.Sprintf(" --port %d-%d", lowPort, highPort))
break // nolint:all
}
} else {
warning("UdpPort %s is invalid", udpPort)
}
}

return buf.String()
}

Expand Down

0 comments on commit e13c7dc

Please sign in to comment.