update: support telnet server

This commit is contained in:
2024-09-07 18:33:48 +08:00
parent 68d03ced70
commit a5a7c38632
2 changed files with 91 additions and 29 deletions

View File

@@ -24,27 +24,27 @@ logmml:
# authType: local/omc
sshd:
listenAddr: 0.0.0.0
listenPort: 2222
listenPort: 32222
privateKey: ./.ssh/id_rsa
maxConnNum: 2
timeout: 1800
session: multiple
mmlHome: ./mmlhome
userName: manager
password: admin123
password: pass123
authType: local
omcUrl:
# authType: local/omc
telnetServer:
listenAddr: 0.0.0.0
listenPort: 2323
listenPort: 32323
maxConnNum: 2
timeout: 1800
session: multiple
mmlHome: ./mmlhome
userName: manager
password: admin123
password: pass123
authType: local
omcUrl:

View File

@@ -1,6 +1,7 @@
package main
import (
"bufio"
"fmt"
"io"
"net"
@@ -8,6 +9,7 @@ import (
"os/exec"
"strings"
"sync"
"time"
"be.ems/lib/dborm"
"be.ems/lib/global"
@@ -23,6 +25,13 @@ import (
var conf *config.YamlConfig
var (
telnetCC int
sshCC int
telnetMu sync.Mutex
sshMu sync.Mutex
)
func init() {
conf = config.GetYamlConfig()
log.InitLogger(conf.Logger.File, conf.Logger.Duration, conf.Logger.Count, "omc:sshsvc", config.GetLogLevel())
@@ -142,20 +151,6 @@ func handleAuth(authType, userName, password string) bool {
return false
}
// const (
// // 定义用户名和密码
// validUsername = "user"
// validPassword = "password"
// maxConnections = 5
// )
var (
telnetCC int
sshCC int
telnetMu sync.Mutex
sshMu sync.Mutex
)
func startTelnetServer(addr string) {
listener, err := net.Listen("tcp", addr)
if err != nil {
@@ -193,21 +188,88 @@ func handleTelnetConnection(conn net.Conn) {
telnetMu.Unlock()
}()
defer conn.Close()
io.WriteString(conn, "Welcome to the Telnet server!\n")
io.WriteString(conn, "Please enter username: ")
var username string
fmt.Fscanln(conn, &username)
reader := bufio.NewReader(conn)
writer := bufio.NewWriter(conn)
io.WriteString(conn, "Please enter password: ")
var password string
fmt.Fscanln(conn, &password)
// 发送欢迎信息
writer.WriteString("Welcome to the Telnet server!\n")
writer.Flush()
if handleAuth(conf.TelnetServer.AuthType, username, password) {
io.WriteString(conn, "Login successful!\n")
io.Copy(conn, conn) // Echo back whatever is received
// 请求用户名
writer.WriteString("Username: ")
writer.Flush()
user, _ := reader.ReadString('\n')
user = strings.TrimSpace(user)
// 关闭回显模式
writer.Write([]byte{255, 251, 1}) // IAC WILL ECHO
writer.Flush()
// 请求密码
writer.WriteString("Password: ")
writer.Flush()
// 读取密码并清除控制序列
var passBuilder strings.Builder
for {
b, err := reader.ReadByte()
if err != nil {
return
}
if b == '\n' || b == '\r' {
break
}
if b == 255 { // IAC
reader.ReadByte() // 忽略下一个字节
reader.ReadByte() // 忽略下一个字节
} else {
io.WriteString(conn, "Login failed!\n")
passBuilder.WriteByte(b)
}
}
pass := passBuilder.String()
// 恢复回显模式
writer.Write([]byte{255, 252, 1}) // IAC WONT ECHO
writer.Flush()
if handleAuth(conf.TelnetServer.AuthType, user, pass) {
writer.WriteString("\nAuthentication successful!\n")
writer.Flush()
handleCommands(user, reader, writer)
} else {
writer.WriteString("\nAuthentication failed!\n")
writer.Flush()
}
}
// 处理命令输入
func handleCommands(user string, reader *bufio.Reader, writer *bufio.Writer) {
header := fmt.Sprintf("[%s@omc]> ", user)
for {
command, err := reader.ReadString('\n')
if err != nil {
return
}
command = strings.TrimSpace(command)
// 处理其他命令
switch command {
case "hello":
writer.WriteString("Hello, world!\n")
case "time":
writer.WriteString(fmt.Sprintf("Current time: %s\n", time.Now().Format(time.RFC1123)))
case "exit", "quit":
writer.WriteString("Goodbye!\n")
writer.Flush()
return
case "":
case "\n":
case "\xff\xfe\x01":
default:
writer.WriteString("Unknown command\n")
}
writer.WriteString(header)
writer.Flush()
}
}