easy switching between different service types.

Working now with ssh as well.
This commit is contained in:
Erik Brakkee 2024-07-20 14:29:47 +02:00
parent 5055cebb7c
commit e90c3ed57a

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"bufio"
"cidebug/pkg/iowrappers" "cidebug/pkg/iowrappers"
"fmt" "fmt"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
@ -49,7 +50,7 @@ func setWinsize(f *os.File, w, h int) {
uintptr(unsafe.Pointer(&struct{ h, w, x, y uint16 }{uint16(h), uint16(w), 0, 0}))) uintptr(unsafe.Pointer(&struct{ h, w, x, y uint16 }{uint16(h), uint16(w), 0, 0})))
} }
func mainSsh() { func sshServer() *ssh.Server {
ssh.Handle(func(s ssh.Session) { ssh.Handle(func(s ssh.Session) {
cmd := exec.Command("bash") cmd := exec.Command("bash")
ptyReq, winCh, isPty := s.Pty() ptyReq, winCh, isPty := s.Pty()
@ -75,33 +76,48 @@ func mainSsh() {
} }
}) })
log.Println("starting ssh tcpserver on port 2222...") log.Println("starting ssh server")
server := ssh.Server{ server := ssh.Server{
Addr: ":2222", //Addr: ":2222",
PasswordHandler: passwordAuth, PasswordHandler: passwordAuth,
SubsystemHandlers: map[string]ssh.SubsystemHandler{ SubsystemHandlers: map[string]ssh.SubsystemHandler{
"sftp": SftpHandler, "sftp": SftpHandler,
}, },
} }
return &server
}
listener, err := net.Listen("tcp", ":2222") func echoServer(conn io.ReadWriter) {
if err != nil { io.Copy(conn, conn)
log.Fatal(err) }
func netCatServer(conn io.ReadWriter) {
stdio := bufio.NewReadWriter(
bufio.NewReaderSize(os.Stdin, 0),
bufio.NewWriterSize(os.Stdout, 0))
iowrappers.SynchronizeStreams(conn, stdio)
}
type AgentService interface {
Run(listener net.Listener)
}
type ListenerServer func() *ssh.Server
func (server ListenerServer) Run(listener net.Listener) {
server().Serve(listener)
}
type ConnectionServer func(conn io.ReadWriter)
func (server ConnectionServer) Run(listener net.Listener) {
for {
conn, err := listener.Accept()
if err != nil {
panic(err)
}
go echoServer(conn)
} }
server.Serve(listener)
//for {
// conn, err := listener.Accept()
// if err != nil {
// log.Println("Failed to accept connection:", err)
// continue
// }
// log.Println("Got connection")
// go server.HandleConn(conn)
//}
//log.Fatal(server.ListenAndServe())
} }
func main() { func main() {
@ -121,24 +137,10 @@ func main() {
log.Println("Connection established to rendez-vous server, waiting for debug sessions") log.Println("Connection established to rendez-vous server, waiting for debug sessions")
// Session is a listener var service AgentService
for { service = ListenerServer(sshServer)
conn, err := listener.Accept() //service = ConnectionServer(echoServer)
if err != nil { //service := ConnectionServer(netCatServer)
panic(err) service.Run(listener)
}
go handleConnection(conn)
}
} }
func handleConnection(conn io.ReadWriter) {
//stdio := bufio.NewReadWriter(
// bufio.NewReaderSize(os.Stdin, 0),
// bufio.NewWriterSize(os.Stdout, 0))
// echo server
io.Copy(conn, conn)
//iowrappers.SynchronizeStreams(conn, stdio)
}