@@ -118,12 +118,13 @@ func startWebhookListener(forwardURL string, port int, skipVerify bool) error {
118118 // Create HTTP server
119119 mux := http .NewServeMux ()
120120 mux .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
121- handleWebhook (w , r , forwardURL , skipVerify , sessionID )
121+ handleWebhook (w , r , forwardURL , sessionID )
122122 })
123123
124124 server := & http.Server {
125- Addr : ":" + strconv .Itoa (port ),
126- Handler : mux ,
125+ Addr : ":" + strconv .Itoa (port ),
126+ Handler : mux ,
127+ ReadHeaderTimeout : 10 * time .Second ,
127128 }
128129
129130 // Handle graceful shutdown
@@ -158,7 +159,7 @@ func startWebhookListener(forwardURL string, port int, skipVerify bool) error {
158159}
159160
160161// handleWebhook processes incoming webhook requests and forwards them
161- func handleWebhook (w http.ResponseWriter , r * http.Request , forwardURL string , skipVerify bool , sessionID string ) {
162+ func handleWebhook (w http.ResponseWriter , r * http.Request , forwardURL , sessionID string ) {
162163 timestamp := time .Now ().Format ("15:04:05" )
163164
164165 // Create styles
@@ -174,7 +175,11 @@ func handleWebhook(w http.ResponseWriter, r *http.Request, forwardURL string, sk
174175 http .Error (w , "Failed to read request body" , http .StatusBadRequest )
175176 return
176177 }
177- defer r .Body .Close ()
178+ defer func () {
179+ if err := r .Body .Close (); err != nil {
180+ fmt .Printf ("[%s] %s Failed to close request body: %v\n " , timestamp , errorStyle .Render ("ERROR" ), err )
181+ }
182+ }()
178183
179184 // Try to parse as JSON to get event type if possible
180185 var webhook map [string ]interface {}
@@ -227,7 +232,11 @@ func handleWebhook(w http.ResponseWriter, r *http.Request, forwardURL string, sk
227232 http .Error (w , "Failed to forward request" , http .StatusBadGateway )
228233 return
229234 }
230- defer resp .Body .Close ()
235+ defer func () {
236+ if err := resp .Body .Close (); err != nil {
237+ fmt .Printf ("[%s] %s Failed to close response body: %v\n " , timestamp , errorStyle .Render ("ERROR" ), err )
238+ }
239+ }()
231240
232241 // Read response from target server
233242 respBody , err := io .ReadAll (resp .Body )
@@ -259,7 +268,9 @@ func handleWebhook(w http.ResponseWriter, r *http.Request, forwardURL string, sk
259268
260269 // Send response back to Vapi
261270 w .WriteHeader (resp .StatusCode )
262- w .Write (respBody )
271+ if _ , err := w .Write (respBody ); err != nil {
272+ fmt .Printf ("[%s] %s Failed to write response: %v\n " , timestamp , errorStyle .Render ("ERROR" ), err )
273+ }
263274
264275 // Print webhook details if it's a JSON payload
265276 if strings .Contains (r .Header .Get ("Content-Type" ), "application/json" ) && len (body ) > 0 {
@@ -273,9 +284,12 @@ func handleWebhook(w http.ResponseWriter, r *http.Request, forwardURL string, sk
273284
274285// generateSessionID creates a unique identifier for this listening session
275286func generateSessionID () string {
276- bytes := make ([]byte , 4 )
277- rand .Read (bytes )
278- return hex .EncodeToString (bytes )
287+ b := make ([]byte , 4 )
288+ if _ , err := rand .Read (b ); err != nil {
289+ // Fallback to timestamp-based ID if random fails
290+ return fmt .Sprintf ("%d" , time .Now ().Unix ())
291+ }
292+ return hex .EncodeToString (b )
279293}
280294
281295func init () {
@@ -287,5 +301,7 @@ func init() {
287301 listenCmd .Flags ().BoolVar (& skipVerify , "skip-verify" , false , "Skip TLS certificate verification when forwarding" )
288302
289303 // Mark required flags
290- listenCmd .MarkFlagRequired ("forward-to" )
304+ if err := listenCmd .MarkFlagRequired ("forward-to" ); err != nil {
305+ panic (fmt .Sprintf ("Failed to mark flag as required: %v" , err ))
306+ }
291307}
0 commit comments