Files

422 lines
14 KiB
Go

// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
// Portions Copyright 2023 The OpenAgent Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controllers
import (
"fmt"
"strconv"
"github.com/fasthttp/websocket"
"github.com/google/uuid"
"github.com/hanzoai/ai/conf"
"github.com/hanzoai/ai/log"
"github.com/hanzoai/ai/object"
"github.com/hanzoai/ai/util"
"github.com/hanzoai/ai/util/guacamole"
"github.com/valyala/fasthttp"
)
const (
TunnelClosed int = -1
Normal int = 0
ConnectionNotFound int = 800
NewTunnelError int = 801
ForcedDisconnect int = 802
NodeNotActive int = 803
ParametersError int = 804
NodeNotFound int = 805
ConnectionUpdateError int = 806
)
var UpGrader = websocket.FastHTTPUpgrader{
ReadBufferSize: 4096,
WriteBufferSize: 4096,
CheckOrigin: func(ctx *fasthttp.RequestCtx) bool {
return true
},
Subprotocols: []string{"guacamole"},
}
// AddNodeTunnel
// @Title AddNodeTunnel
// @Tag Connection API
// @Description add node tunnel session
// @Param nodeId query string true "The id of node"
// @Success 200 {object} Response
// @router /add-node-tunnel [get]
func (c *ApiController) AddNodeTunnel() {
nodeId := c.Input().Get("nodeId")
mode := c.Input().Get("mode")
user := c.GetSessionUser()
if user == nil {
c.ResponseError(c.T("auth:Please sign in first"))
return
}
// The connection inherits the node's owner, so naming another tenant's node
// here files a connection in their organization — which is refused at the
// tunnel, but should never have been created.
node, err := object.GetNode(nodeId)
if err != nil {
c.ResponseError(err.Error())
return
}
if node == nil || !reaches(user, node.Owner) {
c.ResponseError(c.T("general:The node does not exist"))
return
}
connection := &object.Connection{
Creator: user.Name,
ClientIp: c.getClientIp(),
UserAgent: c.getUserAgent(),
ClientIpDesc: util.GetDescFromIP(c.getClientIp()),
UserAgentDesc: util.GetDescFromUserAgent(c.getUserAgent()),
}
connection, err = object.CreateConnection(connection, nodeId, mode)
if err != nil {
c.ResponseError(err.Error())
return
}
c.ResponseOk(connection)
}
// GetNodeTunnel
// @Title GetNodeTunnel
// @Tag Connection API
// @Description get node tunnel session
// @Param width query string true "The width of the tunnel"
// @Param height query string true "The height of the tunnel"
// @Param dpi query string true "The dpi of the tunnel"
// @Param connectionId query string true "The id of the connectionId"
// @Param username query string true "The username for the tunnel"
// @Param password query string true "The password for the tunnel"
// @Success 200 {object} Response
// @router /get-node-tunnel [get]
func (c *ApiController) GetNodeTunnel() {
// A tunnel is an interactive session on a machine. Who is asking, and whether
// the connection they named is theirs, are decided HERE — before the socket is
// handed over — for two reasons that happen to point the same way.
//
// The session is not the request. The upgrade hijacks the connection and
// fasthttp runs the callback on it after this function has returned, so the
// request and every input on it are gone by the time the session starts: they
// have to be read now and carried in. And a refusal needs a request to refuse
// on. Once the socket is up there is no status left to send — only a websocket
// close a client may not be listening for.
caller, ok := c.RequireSignedInUser()
if !ok {
return
}
width := c.Input().Get("width")
height := c.Input().Get("height")
dpi := c.Input().Get("dpi")
connectionId := c.Input().Get("connectionId")
username := c.Input().Get("username")
password := c.Input().Get("password")
intWidth, err := strconv.Atoi(width)
if err != nil {
c.ResponseError(err.Error())
return
}
intHeight, err := strconv.Atoi(height)
if err != nil {
c.ResponseError(err.Error())
return
}
// The store answers a missing row with (nil, nil) — its convention everywhere —
// so a connection that is not there arrives as a nil value beside a nil error.
// A connection the caller's org does not reach answers the same way as one that
// does not exist: from where they stand the two are the same fact, and telling
// them apart would confirm another tenant's connections by name.
connection, err := object.GetConnection(connectionId)
if err != nil {
c.ResponseError(err.Error())
return
}
if connection == nil || !reaches(caller, connection.Owner) {
c.ResponseError(c.T("general:The connection does not exist"))
return
}
node, err := object.GetNode(connection.Node)
if err != nil {
c.ResponseError(err.Error())
return
}
if node == nil {
c.ResponseError(c.T("general:The node does not exist"))
return
}
if err := UpGrader.Upgrade(c.Fiber().RequestCtx(), func(ws *websocket.Conn) {
if node.RemoteUsername == "" {
node.RemoteUsername = username
node.RemotePassword = password
} else {
if node.RemotePassword == "" {
node.RemotePassword = password
}
}
configuration := guacamole.NewConfiguration()
propertyMap := configuration.LoadConfig()
setConfig(propertyMap, node, configuration)
configuration.SetParameter("width", width)
configuration.SetParameter("height", height)
configuration.SetParameter("dpi", dpi)
addr := conf.GetConfigString("guacamoleEndpoint")
if addr == "" {
guacamole.Disconnect(ws, NewTunnelError, "guacamoleEndpoint in app.conf should not be empty")
return
}
tunnel, err := guacamole.NewTunnel(addr, configuration)
if err != nil {
guacamole.Disconnect(ws, NewTunnelError, err.Error())
return
}
guacSession := &guacamole.Session{
Id: connectionId,
Protocol: connection.Protocol,
WebSocket: ws,
GuacdTunnel: tunnel,
}
guacSession.Observer = guacamole.NewObserver(connectionId)
guacamole.GlobalSessionManager.Add(guacSession)
connection.ConnectionId = tunnel.ConnectionID
connection.Width = intWidth
connection.Height = intHeight
connection.Status = object.Connecting
connection.Recording = configuration.GetParameter(guacamole.RecordingPath)
if connection.Recording == "" {
// No audit is required when no screen is recorded
connection.Reviewed = true
}
_, err = object.UpdateConnection(connectionId, connection)
if err != nil {
guacamole.Disconnect(ws, ConnectionUpdateError, err.Error())
return
}
guacamoleHandler := NewGuacamoleHandler(ws, tunnel)
guacamoleHandler.Start()
defer guacamoleHandler.Stop()
for {
_, message, err := ws.ReadMessage()
if err != nil {
log.Error(fmt.Sprintf("GetNodeTunnel():ws.ReadMessage() error: %s", err.Error()))
_ = tunnel.Close()
err2 := object.CloseConnection(connectionId, Normal, "Normal user exit")
if err2 != nil {
log.Error(fmt.Sprintf("GetNodeTunnel():object.CloseConnection() error: %s", err.Error()))
}
return
}
_, err = tunnel.WriteAndFlush(message)
if err != nil {
log.Error(fmt.Sprintf("GetNodeTunnel():tunnel.WriteAndFlush() error: %s", err.Error()))
err2 := object.CloseConnection(connectionId, Normal, "Normal user exit")
if err2 != nil {
log.Error(fmt.Sprintf("GetNodeTunnel():object.CloseConnection() (2nd) error: %s", err.Error()))
}
return
}
}
}); err != nil {
c.ResponseError(err.Error())
}
}
func (c *ApiController) TunnelMonitor() {
// A monitor mirrors somebody's screen, so it answers the same two questions
// GetNodeTunnel does, in the same place and for the same reasons: the request
// is gone once the socket is hijacked, and a refusal needs a request to refuse
// on.
caller, ok := c.RequireSignedInUser()
if !ok {
return
}
connectionId := c.Input().Get("connectionId")
connection, err := object.GetConnection(connectionId)
if err != nil {
c.ResponseError(err.Error())
return
}
if connection == nil || !reaches(caller, connection.Owner) {
c.ResponseError(c.T("general:The connection does not exist"))
return
}
if err := UpGrader.Upgrade(c.Fiber().RequestCtx(), func(ws *websocket.Conn) {
if connection.Status != object.Connected {
guacamole.Disconnect(ws, NodeNotActive, "Connection offline")
return
}
connectionId = connection.ConnectionId
configuration := guacamole.NewConfiguration()
configuration.ConnectionID = connectionId
configuration.SetParameter("width", strconv.Itoa(connection.Width))
configuration.SetParameter("height", strconv.Itoa(connection.Height))
configuration.SetParameter("dpi", "96")
configuration.SetReadOnlyMode()
addr := conf.GetConfigString("guacamoleEndpoint")
if addr == "" {
guacamole.Disconnect(ws, NewTunnelError, "guacamoleEndpoint in app.conf should not be empty")
return
}
tunnel, err := guacamole.NewTunnel(addr, configuration)
if err != nil {
// The client has been told; the session is over. This runs in the
// hijack goroutine, so a panic here would take the process with it
// rather than end this one monitor.
guacamole.Disconnect(ws, NewTunnelError, err.Error())
return
}
guacSession := &guacamole.Session{
Id: connectionId,
Protocol: connection.Protocol,
WebSocket: ws,
GuacdTunnel: tunnel,
}
forObsGuacSession := guacamole.GlobalSessionManager.Get(connectionId)
if forObsGuacSession == nil {
guacamole.Disconnect(ws, ConnectionNotFound, "Failed to obtain guacamole session")
return
}
guacSession.Id = uuid.NewString()
forObsGuacSession.Observer.Add(guacSession)
guacamoleHandler := NewGuacamoleHandler(ws, tunnel)
guacamoleHandler.Start()
defer guacamoleHandler.Stop()
for {
_, message, err := ws.ReadMessage()
if err != nil {
_ = tunnel.Close()
observerId := guacSession.Id
forObsGuacSession.Observer.Delete(observerId)
return
}
_, err = tunnel.WriteAndFlush(message)
if err != nil {
// The socket is hijacked and the session is ending, so there is no
// response left to write this on — the same reason the sibling loop
// in GetNodeTunnel logs here.
if err := object.CloseConnection(connectionId, Normal, "Normal user exit"); err != nil {
log.Error(fmt.Sprintf("TunnelMonitor():object.CloseConnection() error: %s", err.Error()))
}
return
}
}
}); err != nil {
c.ResponseError(err.Error())
}
}
func setConfig(propertyMap map[string]string, node *object.Node, configuration *guacamole.Configuration) {
switch node.RemoteProtocol {
case "SSH":
configuration.Protocol = "ssh"
case "RDP":
configuration.Protocol = "rdp"
case "Telnet":
configuration.Protocol = "telnet"
case "VNC":
configuration.Protocol = "vnc"
}
hostname := node.PublicIp
if hostname == "" {
hostname = node.PrivateIp
}
if hostname == "" {
hostname = node.Name
}
configuration.SetParameter("hostname", hostname)
configuration.SetParameter("port", strconv.Itoa(node.RemotePort))
configuration.SetParameter("username", node.RemoteUsername)
configuration.SetParameter("password", node.RemotePassword)
switch node.RemoteProtocol {
case "RDP":
configuration.SetParameter("security", "any")
configuration.SetParameter("ignore-cert", "true")
configuration.SetParameter("create-drive-path", "true")
configuration.SetParameter("resize-method", "display-update")
configuration.SetParameter(guacamole.EnableWallpaper, propertyMap[guacamole.EnableWallpaper])
configuration.SetParameter(guacamole.EnableTheming, propertyMap[guacamole.EnableTheming])
configuration.SetParameter(guacamole.EnableFontSmoothing, propertyMap[guacamole.EnableFontSmoothing])
configuration.SetParameter(guacamole.EnableFullWindowDrag, propertyMap[guacamole.EnableFullWindowDrag])
configuration.SetParameter(guacamole.EnableDesktopComposition, propertyMap[guacamole.EnableDesktopComposition])
configuration.SetParameter(guacamole.EnableMenuAnimations, propertyMap[guacamole.EnableMenuAnimations])
configuration.SetParameter(guacamole.DisableBitmapCaching, propertyMap[guacamole.DisableBitmapCaching])
configuration.SetParameter(guacamole.DisableOffscreenCaching, propertyMap[guacamole.DisableOffscreenCaching])
configuration.SetParameter(guacamole.ColorDepth, propertyMap[guacamole.ColorDepth])
configuration.SetParameter(guacamole.ForceLossless, propertyMap[guacamole.ForceLossless])
configuration.SetParameter(guacamole.PreConnectionId, propertyMap[guacamole.PreConnectionId])
configuration.SetParameter(guacamole.PreConnectionBlob, propertyMap[guacamole.PreConnectionBlob])
// if node.EnableRemoteApp {
// remoteApp := node.RemoteApps[0]
// configuration.SetParameter("remote-app", "||"+remoteApp.RemoteAppName)
// configuration.SetParameter("remote-app-dir", remoteApp.RemoteAppDir)
// configuration.SetParameter("remote-app-args", remoteApp.RemoteAppArgs)
// }
case "SSH":
configuration.SetParameter(guacamole.FontSize, propertyMap[guacamole.FontSize])
// configuration.SetParameter(guacamole.FontName, propertyMap[guacamole.FontName])
configuration.SetParameter(guacamole.ColorScheme, propertyMap[guacamole.ColorScheme])
configuration.SetParameter(guacamole.Backspace, propertyMap[guacamole.Backspace])
configuration.SetParameter(guacamole.TerminalType, propertyMap[guacamole.TerminalType])
case "Telnet":
configuration.SetParameter(guacamole.FontSize, propertyMap[guacamole.FontSize])
// configuration.SetParameter(guacamole.FontName, propertyMap[guacamole.FontName])
configuration.SetParameter(guacamole.ColorScheme, propertyMap[guacamole.ColorScheme])
configuration.SetParameter(guacamole.Backspace, propertyMap[guacamole.Backspace])
configuration.SetParameter(guacamole.TerminalType, propertyMap[guacamole.TerminalType])
default:
}
}