@@ -3,8 +3,9 @@ package internalclient
33import (
44 "context"
55 "errors"
6- "fmt"
76 "net/http"
7+ "strconv"
8+ "strings"
89
910 "github.com/gorilla/websocket"
1011 "github.com/shellhub-io/shellhub/pkg/api/requests"
@@ -46,7 +47,7 @@ func (c *client) SessionCreate(ctx context.Context, session requests.SessionCrea
4647 R ().
4748 SetContext (ctx ).
4849 SetBody (session ).
49- Post ("/internal/sessions" )
50+ Post (c . Config . APIBaseURL + "/internal/sessions" )
5051
5152 return err
5253}
@@ -56,10 +57,11 @@ func (c *client) SessionAsAuthenticated(ctx context.Context, uid string) []error
5657 _ , err := c .http .
5758 R ().
5859 SetContext (ctx ).
60+ SetPathParam ("uid" , uid ).
5961 SetBody (& models.Status {
6062 Authenticated : true ,
6163 }).
62- Patch (fmt . Sprintf ( "/internal/sessions/%s" , uid ) )
64+ Patch (c . Config . APIBaseURL + "/internal/sessions/{ uid}" )
6365 if err != nil {
6466 errors = append (errors , err )
6567 }
@@ -72,7 +74,8 @@ func (c *client) FinishSession(ctx context.Context, uid string) []error {
7274 _ , err := c .http .
7375 R ().
7476 SetContext (ctx ).
75- Post (fmt .Sprintf ("/internal/sessions/%s/finish" , uid ))
77+ SetPathParam ("uid" , uid ).
78+ Post (c .Config .APIBaseURL + "/internal/sessions/{uid}/finish" )
7679 if err != nil {
7780 errors = append (errors , err )
7881 }
@@ -85,7 +88,8 @@ func (c *client) KeepAliveSession(ctx context.Context, uid string) []error {
8588 _ , err := c .http .
8689 R ().
8790 SetContext (ctx ).
88- Post (fmt .Sprintf ("/internal/sessions/%s/keepalive" , uid ))
91+ SetPathParam ("uid" , uid ).
92+ Post (c .Config .APIBaseURL + "/internal/sessions/{uid}/keepalive" )
8993 if err != nil {
9094 errors = append (errors , err )
9195 }
@@ -101,7 +105,7 @@ func (c *client) UpdateSession(ctx context.Context, uid string, model *models.Se
101105 "tenant" : uid ,
102106 }).
103107 SetBody (model ).
104- Patch ("/internal/sessions/{tenant}" )
108+ Patch (c . Config . APIBaseURL + "/internal/sessions/{tenant}" )
105109 if err != nil {
106110 return errors .Join (errors .New ("failed to update the session due error" ), err )
107111 }
@@ -114,14 +118,19 @@ func (c *client) UpdateSession(ctx context.Context, uid string, model *models.Se
114118}
115119
116120func (c * client ) EventSessionStream (ctx context.Context , uid string ) (* websocket.Conn , error ) {
117- connection , _ , err := websocket .
118- DefaultDialer .
119- DialContext (
120- ctx ,
121- fmt .Sprintf ("ws://api:8080/internal/sessions/%s/events" ,
122- uid ,
123- ),
124- nil )
121+ // Dial the enterprise events websocket. Convert configured enterprise HTTP scheme to ws(s).
122+ scheme := "ws"
123+ if strings .HasPrefix (c .Config .APIBaseURL , "https" ) {
124+ scheme = "wss"
125+ }
126+
127+ host := strings .TrimPrefix (strings .TrimPrefix (c .Config .APIBaseURL , "http://" ), "https://" )
128+
129+ connection , _ , err := websocket .DefaultDialer .DialContext (
130+ ctx ,
131+ scheme + "://" + host + "/internal/sessions/" + uid + "/events" ,
132+ nil ,
133+ )
125134 if err != nil {
126135 return nil , err
127136 }
@@ -135,9 +144,9 @@ func (c *client) SaveSession(ctx context.Context, uid string, seat int) error {
135144 SetContext (ctx ).
136145 SetPathParams (map [string ]string {
137146 "uid" : uid ,
138- "seat" : fmt . Sprintf ( "%d" , seat ),
147+ "seat" : strconv . Itoa ( seat ),
139148 }).
140- Post ("http://cloud:8080 /internal/sessions/{uid}/records/{seat}" )
149+ Post (c . Config . EnterpriseBaseURL + " /internal/sessions/{uid}/records/{seat}" )
141150 if err != nil {
142151 return errors .Join (errors .New ("failed to save the Asciinema file on Object Storage" ), err )
143152 }
@@ -150,7 +159,7 @@ func (c *client) SaveSession(ctx context.Context, uid string, seat int) error {
150159 // represent an error.
151160 logrus .WithFields (logrus.Fields {
152161 "uid" : uid ,
153- "seat" : fmt . Sprintf ( "%d" , seat ),
162+ "seat" : strconv . Itoa ( seat ),
154163 }).Debug ("save session not acceptable" )
155164
156165 return nil
0 commit comments