restructuring

This commit is contained in:
Erik Brakkee 2024-07-30 19:59:13 +02:00
parent 367043e0c5
commit f382c02b41
30 changed files with 45 additions and 42 deletions

View File

@ -2,11 +2,11 @@ package main
import ( import (
"bufio" "bufio"
"converge/pkg/agent" "converge/pkg/agent/session"
"converge/pkg/agent/terminal"
"converge/pkg/comms" "converge/pkg/comms"
"converge/pkg/iowrappers" "converge/pkg/support/iowrappers"
"converge/pkg/terminal" "converge/pkg/support/websocketutil"
"converge/pkg/websocketutil"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"github.com/gliderlabs/ssh" "github.com/gliderlabs/ssh"
@ -37,8 +37,8 @@ func SftpHandler(sess ssh.Session) {
sess.LocalAddr().String(), sess.LocalAddr().String(),
"sftp", "sftp",
) )
agent.Login(sessionInfo, sess) session.Login(sessionInfo, sess)
defer agent.LogOut(sessionInfo.ClientId) defer session.LogOut(sessionInfo.ClientId)
debugStream := io.Discard debugStream := io.Discard
serverOptions := []sftp.ServerOption{ serverOptions := []sftp.ServerOption{
@ -73,9 +73,9 @@ func sshServer(hostKeyFile string, shellCommand string,
sessionInfo := comms.NewSessionInfo( sessionInfo := comms.NewSessionInfo(
s.LocalAddr().String(), "ssh", s.LocalAddr().String(), "ssh",
) )
agent.Login(sessionInfo, s) session.Login(sessionInfo, s)
iowrappers.SynchronizeStreams(process.Pipe(), s) iowrappers.SynchronizeStreams(process.Pipe(), s)
agent.LogOut(sessionInfo.ClientId) session.LogOut(sessionInfo.ClientId)
// will cause addition goroutines to remmain alive when the SSH // will cause addition goroutines to remmain alive when the SSH
// session is killed. For now acceptable since the agent is a short-lived // session is killed. For now acceptable since the agent is a short-lived
// process. Using Kill() here will create defunct processes and in normal // process. Using Kill() here will create defunct processes and in normal
@ -335,7 +335,7 @@ func main() {
"://"+urlObject.Host+"/static/wsproxy"+extension) "://"+urlObject.Host+"/static/wsproxy"+extension)
log.Println() log.Println()
agent.ConfigureAgent(commChannel, advanceWarningTime, agentExpriryTime, tickerInterval) session.ConfigureAgent(commChannel, advanceWarningTime, agentExpriryTime, tickerInterval)
listener := comms.NewAgentListener(commChannel.Session) listener := comms.NewAgentListener(commChannel.Session)

View File

@ -2,8 +2,8 @@ package main
import ( import (
"converge/pkg/comms" "converge/pkg/comms"
"converge/pkg/converge" "converge/pkg/server/converge"
"converge/pkg/websocketutil" "converge/pkg/support/websocketutil"
"fmt" "fmt"
"log" "log"
"math/rand" "math/rand"

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"converge/pkg/templates" templates2 "converge/pkg/server/templates"
"net/http" "net/http"
"os" "os"
"strings" "strings"
@ -30,13 +30,13 @@ func pageHandler(w http.ResponseWriter, r *http.Request) {
case "/": case "/":
fallthrough fallthrough
case "index.html": case "index.html":
templates.AboutTab().Render(r.Context(), w) templates2.AboutTab().Render(r.Context(), w)
case "usage.html": case "usage.html":
templates.UsageTab(secure, r.Host, username).Render(r.Context(), w) templates2.UsageTab(secure, r.Host, username).Render(r.Context(), w)
case "downloads.html": case "downloads.html":
templates.DownloadsTab().Render(r.Context(), w) templates2.DownloadsTab().Render(r.Context(), w)
case "sessions.html": case "sessions.html":
templates.SessionsTab().Render(r.Context(), w) templates2.SessionsTab().Render(r.Context(), w)
default: default:
http.NotFound(w, r) http.NotFound(w, r)
} }

View File

@ -1,8 +1,8 @@
package main package main
import ( import (
"converge/pkg/iowrappers" "converge/pkg/support/iowrappers"
"converge/pkg/websocketutil" "converge/pkg/support/websocketutil"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"

View File

@ -2,7 +2,7 @@ package main
import ( import (
"context" "context"
"converge/pkg/templates" templates2 "converge/pkg/server/templates"
"github.com/a-h/templ" "github.com/a-h/templ"
"log" "log"
"os" "os"
@ -30,14 +30,14 @@ func main() {
dir := "html/docs" dir := "html/docs"
fullindex := func() templ.Component { fullindex := func() templ.Component {
return templates.Index("s", "example.com", "converge") return templates2.Index("s", "example.com", "converge")
} }
usage := func() templ.Component { usage := func() templ.Component {
return templates.UsageTab("s", "example.com", "converge") return templates2.UsageTab("s", "example.com", "converge")
} }
render(dir, "fullindex.html", fullindex) render(dir, "fullindex.html", fullindex)
render(dir, "index.html", templates.AboutTab) render(dir, "index.html", templates2.AboutTab)
render(dir, "usage.html", usage) render(dir, "usage.html", usage)
render(dir, "downloads.html", templates.Downloads) render(dir, "downloads.html", templates2.Downloads)
} }

View File

@ -1,8 +1,8 @@
package main package main
import ( import (
"converge/pkg/iowrappers" "converge/pkg/support/iowrappers"
"converge/pkg/websocketutil" "converge/pkg/support/websocketutil"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"

View File

@ -1,8 +1,8 @@
package main package main
import ( import (
"converge/pkg/iowrappers" "converge/pkg/support/iowrappers"
"converge/pkg/websocketutil" "converge/pkg/support/websocketutil"
"fmt" "fmt"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"log" "log"

2
go.mod
View File

@ -4,6 +4,7 @@ go 1.21
require ( require (
github.com/ActiveState/termtest/conpty v0.5.0 github.com/ActiveState/termtest/conpty v0.5.0
github.com/a-h/templ v0.2.747
github.com/creack/pty v1.1.21 github.com/creack/pty v1.1.21
github.com/fsnotify/fsnotify v1.7.0 github.com/fsnotify/fsnotify v1.7.0
github.com/gliderlabs/ssh v0.3.7 github.com/gliderlabs/ssh v0.3.7
@ -16,7 +17,6 @@ require (
require ( require (
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/a-h/templ v0.2.747 // indirect
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect
github.com/kr/fs v0.1.0 // indirect github.com/kr/fs v0.1.0 // indirect
golang.org/x/sys v0.22.0 // indirect golang.org/x/sys v0.22.0 // indirect

5
go.sum
View File

@ -15,6 +15,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gliderlabs/ssh v0.3.7 h1:iV3Bqi942d9huXnzEF2Mt+CY9gLu8DNM4Obd+8bODRE= github.com/gliderlabs/ssh v0.3.7 h1:iV3Bqi942d9huXnzEF2Mt+CY9gLu8DNM4Obd+8bODRE=
github.com/gliderlabs/ssh v0.3.7/go.mod h1:zpHEXBstFnQYtGnB8k8kQLol82umzn/2/snG7alWVD8= github.com/gliderlabs/ssh v0.3.7/go.mod h1:zpHEXBstFnQYtGnB8k8kQLol82umzn/2/snG7alWVD8=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE= github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE=
@ -28,8 +30,9 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=

View File

@ -1,9 +1,9 @@
package agent package session
import ( import (
"bytes" "bytes"
"converge/pkg/async"
"converge/pkg/comms" "converge/pkg/comms"
"converge/pkg/support/async"
"fmt" "fmt"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/gliderlabs/ssh" "github.com/gliderlabs/ssh"

View File

@ -1,4 +1,4 @@
package agent package session
import ( import (
"crypto/rand" "crypto/rand"

View File

@ -1,7 +1,7 @@
package comms package comms
import ( import (
"converge/pkg/websocketutil" "converge/pkg/support/websocketutil"
"net" "net"
"strconv" "strconv"
) )

View File

@ -2,9 +2,9 @@ package converge
import ( import (
"converge/pkg/comms" "converge/pkg/comms"
"converge/pkg/concurrency"
"converge/pkg/iowrappers"
"converge/pkg/models" "converge/pkg/models"
"converge/pkg/support/concurrency"
iowrappers2 "converge/pkg/support/iowrappers"
"fmt" "fmt"
"io" "io"
"log" "log"
@ -25,7 +25,7 @@ var clientIdGenerator = concurrency.NewAtomicCounter()
type ClientConnection struct { type ClientConnection struct {
models.Client models.Client
agent net.Conn agent net.Conn
client iowrappers.ReadWriteAddrCloser client iowrappers2.ReadWriteAddrCloser
} }
func NewAgent(commChannel comms.CommChannel, publicId string, agentInfo comms.AgentInfo) *AgentConnection { func NewAgent(commChannel comms.CommChannel, publicId string, agentInfo comms.AgentInfo) *AgentConnection {
@ -39,7 +39,7 @@ func NewAgent(commChannel comms.CommChannel, publicId string, agentInfo comms.Ag
} }
} }
func NewClient(publicId string, clientConn iowrappers.ReadWriteAddrCloser, func NewClient(publicId string, clientConn iowrappers2.ReadWriteAddrCloser,
agentConn net.Conn) *ClientConnection { agentConn net.Conn) *ClientConnection {
return &ClientConnection{ return &ClientConnection{
Client: models.Client{ Client: models.Client{
@ -152,7 +152,7 @@ func (admin *Admin) addAgent(publicId string, agentInfo comms.AgentInfo, conn io
return agent, nil return agent, nil
} }
func (admin *Admin) addClient(publicId string, clientConn iowrappers.ReadWriteAddrCloser) (*ClientConnection, error) { func (admin *Admin) addClient(publicId string, clientConn iowrappers2.ReadWriteAddrCloser) (*ClientConnection, error) {
admin.mutex.Lock() admin.mutex.Lock()
defer admin.mutex.Unlock() defer admin.mutex.Unlock()
@ -274,7 +274,7 @@ func (admin *Admin) Register(publicId string, conn io.ReadWriteCloser,
return nil return nil
} }
func (admin *Admin) Connect(publicId string, conn iowrappers.ReadWriteAddrCloser) error { func (admin *Admin) Connect(publicId string, conn iowrappers2.ReadWriteAddrCloser) error {
defer conn.Close() defer conn.Close()
client, err := admin.addClient(publicId, conn) client, err := admin.addClient(publicId, conn)
if err != nil { if err != nil {
@ -285,6 +285,6 @@ func (admin *Admin) Connect(publicId string, conn iowrappers.ReadWriteAddrCloser
}() }()
log.Printf("Connecting client and agent: '%s'\n", publicId) log.Printf("Connecting client and agent: '%s'\n", publicId)
iowrappers.SynchronizeStreams(client.client, client.agent) iowrappers2.SynchronizeStreams(client.client, client.agent)
return nil return nil
} }

View File

@ -12,13 +12,13 @@ templ Usage(secure string, host string, username string) {
</p> </p>
<pre>{` <pre>{`
# linux # linux
`}curl http{secure}://{host}/static/agent > agent{` `}curl http{secure}://{host}/static/session > session{`
chmod 755 agent chmod 755 agent
`}./agent --id ID ws{secure}://{host}{` `}./agent --id ID ws{secure}://{host}{`
rm -f agent rm -f agent
# windows # windows
`}curl http{secure}://{host}/static/agent.exe > agent.exe{` `}curl http{secure}://{host}/static/session.exe > session.exe{`
`}agent --id ID ws{secure}://{host}{` `}agent --id ID ws{secure}://{host}{`
del agent.exe del agent.exe
`}</pre> `}</pre>