mirror of
https://github.com/fosrl/olm.git
synced 2026-02-08 05:56:41 +00:00
Connecting disconnecting working
This commit is contained in:
18
api/api.go
18
api/api.go
@@ -255,6 +255,15 @@ func (s *API) handleConnect(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if we are already connected, reject new connection requests
|
||||||
|
s.statusMu.RLock()
|
||||||
|
alreadyConnected := s.isConnected
|
||||||
|
s.statusMu.RUnlock()
|
||||||
|
if alreadyConnected {
|
||||||
|
http.Error(w, "Already connected to a server. Disconnect first before connecting again.", http.StatusConflict)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var req ConnectionRequest
|
var req ConnectionRequest
|
||||||
decoder := json.NewDecoder(r.Body)
|
decoder := json.NewDecoder(r.Body)
|
||||||
if err := decoder.Decode(&req); err != nil {
|
if err := decoder.Decode(&req); err != nil {
|
||||||
@@ -374,6 +383,15 @@ func (s *API) handleDisconnect(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if we are already disconnected, reject new disconnect requests
|
||||||
|
s.statusMu.RLock()
|
||||||
|
alreadyDisconnected := !s.isConnected
|
||||||
|
s.statusMu.RUnlock()
|
||||||
|
if alreadyDisconnected {
|
||||||
|
http.Error(w, "Not currently connected to a server.", http.StatusConflict)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
logger.Info("Received disconnect request via API")
|
logger.Info("Received disconnect request via API")
|
||||||
|
|
||||||
// Send disconnect signal
|
// Send disconnect signal
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ var (
|
|||||||
apiServer *api.API
|
apiServer *api.API
|
||||||
olmClient *websocket.Client
|
olmClient *websocket.Client
|
||||||
tunnelCancel context.CancelFunc
|
tunnelCancel context.CancelFunc
|
||||||
|
tunnelRunning bool
|
||||||
)
|
)
|
||||||
|
|
||||||
func Run(ctx context.Context, config Config) {
|
func Run(ctx context.Context, config Config) {
|
||||||
@@ -132,6 +133,7 @@ func Run(ctx context.Context, config Config) {
|
|||||||
// Start the tunnel process with the new credentials
|
// Start the tunnel process with the new credentials
|
||||||
if id != "" && secret != "" && endpoint != "" {
|
if id != "" && secret != "" && endpoint != "" {
|
||||||
logger.Info("Starting tunnel with new credentials")
|
logger.Info("Starting tunnel with new credentials")
|
||||||
|
tunnelRunning = true
|
||||||
go TunnelProcess(ctx, config, id, secret, endpoint)
|
go TunnelProcess(ctx, config, id, secret, endpoint)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,7 +147,7 @@ func Run(ctx context.Context, config Config) {
|
|||||||
|
|
||||||
default:
|
default:
|
||||||
// If we have credentials and no tunnel is running, start it
|
// If we have credentials and no tunnel is running, start it
|
||||||
if id != "" && secret != "" && endpoint != "" && olmClient == nil {
|
if id != "" && secret != "" && endpoint != "" && !tunnelRunning {
|
||||||
logger.Info("Starting tunnel process with initial credentials")
|
logger.Info("Starting tunnel process with initial credentials")
|
||||||
go TunnelProcess(ctx, config, id, secret, endpoint)
|
go TunnelProcess(ctx, config, id, secret, endpoint)
|
||||||
} else if id == "" || secret == "" || endpoint == "" {
|
} else if id == "" || secret == "" || endpoint == "" {
|
||||||
@@ -829,6 +831,7 @@ func StopTunnel() {
|
|||||||
|
|
||||||
// Reset the connected state
|
// Reset the connected state
|
||||||
connected = false
|
connected = false
|
||||||
|
tunnelRunning = false
|
||||||
|
|
||||||
// Update API server status
|
// Update API server status
|
||||||
apiServer.SetConnectionStatus(false)
|
apiServer.SetConnectionStatus(false)
|
||||||
|
|||||||
Reference in New Issue
Block a user