feat: add authenticated user info and restructure handleConnection
- Display authenticated username in welcome page information box - Refactor handleConnection function for better structure and clarity
This commit is contained in:
@@ -2,9 +2,11 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
"time"
|
||||||
"tunnel_pls/internal/config"
|
"tunnel_pls/internal/config"
|
||||||
"tunnel_pls/internal/grpc/client"
|
"tunnel_pls/internal/grpc/client"
|
||||||
"tunnel_pls/session"
|
"tunnel_pls/session"
|
||||||
@@ -64,30 +66,32 @@ func (s *Server) Start() {
|
|||||||
|
|
||||||
func (s *Server) handleConnection(conn net.Conn) {
|
func (s *Server) handleConnection(conn net.Conn) {
|
||||||
sshConn, chans, forwardingReqs, err := ssh.NewServerConn(conn, s.config)
|
sshConn, chans, forwardingReqs, err := ssh.NewServerConn(conn, s.config)
|
||||||
defer func(sshConn *ssh.ServerConn) {
|
|
||||||
err = sshConn.Close()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("failed to close SSH server: %v", err)
|
|
||||||
}
|
|
||||||
}(sshConn)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to establish SSH connection: %v", err)
|
log.Printf("failed to establish SSH connection: %v", err)
|
||||||
err := conn.Close()
|
err = conn.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to close SSH connection: %v", err)
|
log.Printf("failed to close SSH connection: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx := context.Background()
|
|
||||||
log.Println("SSH connection established:", sshConn.User())
|
defer func(sshConn *ssh.ServerConn) {
|
||||||
|
err = sshConn.Close()
|
||||||
|
if err != nil && !errors.Is(err, net.ErrClosed) {
|
||||||
|
log.Printf("failed to close SSH server: %v", err)
|
||||||
|
}
|
||||||
|
}(sshConn)
|
||||||
|
|
||||||
user := "UNAUTHORIZED"
|
user := "UNAUTHORIZED"
|
||||||
if s.grpcClient != nil {
|
if s.grpcClient != nil {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||||
_, u, _ := s.grpcClient.AuthorizeConn(ctx, sshConn.User())
|
_, u, _ := s.grpcClient.AuthorizeConn(ctx, sshConn.User())
|
||||||
user = u
|
user = u
|
||||||
|
cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Println("SSH connection established:", sshConn.User())
|
||||||
sshSession := session.New(sshConn, forwardingReqs, chans, s.sessionRegistry, user)
|
sshSession := session.New(sshConn, forwardingReqs, chans, s.sessionRegistry, user)
|
||||||
err = sshSession.Start()
|
err = sshSession.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -672,22 +672,32 @@ func (m *model) View() string {
|
|||||||
MarginBottom(boxMargin).
|
MarginBottom(boxMargin).
|
||||||
Width(boxMaxWidth)
|
Width(boxMaxWidth)
|
||||||
|
|
||||||
urlDisplay := m.getTunnelURL()
|
authenticatedUser := m.interaction.lifecycle.GetUser()
|
||||||
if shouldUseCompactLayout(m.width, 80) && len(urlDisplay) > m.width-20 {
|
|
||||||
maxLen := m.width - 25
|
userInfoStyle := lipgloss.NewStyle().
|
||||||
if maxLen > 10 {
|
Foreground(lipgloss.Color("#FAFAFA")).
|
||||||
urlDisplay = truncateString(urlDisplay, maxLen)
|
Bold(true)
|
||||||
}
|
|
||||||
}
|
sectionHeaderStyle := lipgloss.NewStyle().
|
||||||
|
Foreground(lipgloss.Color("#888888")).
|
||||||
|
Bold(true)
|
||||||
|
|
||||||
|
addressStyle := lipgloss.NewStyle().
|
||||||
|
Foreground(lipgloss.Color("#FAFAFA"))
|
||||||
|
|
||||||
var infoContent string
|
var infoContent string
|
||||||
if shouldUseCompactLayout(m.width, 70) {
|
if shouldUseCompactLayout(m.width, 70) {
|
||||||
infoContent = fmt.Sprintf("🌐 %s", urlBoxStyle.Render(urlDisplay))
|
infoContent = fmt.Sprintf("👤 %s\n\n%s\n%s",
|
||||||
} else if isCompact {
|
userInfoStyle.Render(authenticatedUser),
|
||||||
infoContent = fmt.Sprintf("🌐 Forwarding to:\n\n %s", urlBoxStyle.Render(urlDisplay))
|
sectionHeaderStyle.Render("🌐 FORWARDING ADDRESS:"),
|
||||||
|
addressStyle.Render(fmt.Sprintf(" %s", urlBoxStyle.Render(m.getTunnelURL()))))
|
||||||
} else {
|
} else {
|
||||||
infoContent = fmt.Sprintf("🌐 F O R W A R D I N G T O:\n\n %s", urlBoxStyle.Render(urlDisplay))
|
infoContent = fmt.Sprintf("👤 Authenticated as: %s\n\n%s\n %s",
|
||||||
|
userInfoStyle.Render(authenticatedUser),
|
||||||
|
sectionHeaderStyle.Render("🌐 FORWARDING ADDRESS:"),
|
||||||
|
addressStyle.Render(urlBoxStyle.Render(m.getTunnelURL())))
|
||||||
}
|
}
|
||||||
|
|
||||||
b.WriteString(responsiveInfoBox.Render(infoContent))
|
b.WriteString(responsiveInfoBox.Render(infoContent))
|
||||||
b.WriteString("\n")
|
b.WriteString("\n")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user