From a57d8273a0f83a0617ce4ff563f0bcfbd563e287 Mon Sep 17 00:00:00 2001 From: janusec Date: Wed, 19 Jul 2023 22:49:55 +0800 Subject: [PATCH 1/2] add timeout seconds for transport --- gateway/gateway.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/gateway/gateway.go b/gateway/gateway.go index 5d4fc78..8d3f1eb 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -387,16 +387,19 @@ func ReverseHandlerFunc(w http.ResponseWriter, r *http.Request) { // var transport http.RoundTripper transport := &http.Transport{ TLSHandshakeTimeout: 30 * time.Second, + ResponseHeaderTimeout: 30 * time.Second, IdleConnTimeout: 30 * time.Second, - ExpectContinueTimeout: 5 * time.Second, + ExpectContinueTimeout: 10 * time.Second, + MaxIdleConns: 100, DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { - conn, err := net.Dial("tcp", targetDest) dest.CheckTime = nowTimeStamp + conn, err := net.Dial("tcp", targetDest) if err != nil { dest.Mutex.Lock() defer dest.Mutex.Unlock() dest.Online = false - utils.DebugPrintln("DialContext error", err) + timeout := time.Now().Unix() - nowTimeStamp + utils.DebugPrintln("DialContext error", err, timeout, "seconds") if data.NodeSetting.SMTP.SMTPEnabled { sendOfflineNotification(app, targetDest) } @@ -408,12 +411,12 @@ func ReverseHandlerFunc(w http.ResponseWriter, r *http.Request) { return conn, err }, DialTLS: func(network, addr string) (net.Conn, error) { - conn, err := net.Dial("tcp", targetDest) dest.CheckTime = nowTimeStamp + conn, err := net.Dial("tcp", targetDest) if err != nil { dest.Online = false - dest.CheckTime = nowTimeStamp - utils.DebugPrintln("DialContext error", err) + timeout := time.Now().Unix() - nowTimeStamp + utils.DebugPrintln("DialTLS error", err, timeout, "seconds") if data.NodeSetting.SMTP.SMTPEnabled { sendOfflineNotification(app, targetDest) } From 7f02e18bea105538704ce1144f88011d1cb30399 Mon Sep 17 00:00:00 2001 From: janusec Date: Sat, 12 Aug 2023 11:35:05 +0800 Subject: [PATCH 2/2] fix duplicate destinations --- backend/application.go | 10 ++++++++-- backend/init.go | 10 +++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/backend/application.go b/backend/application.go index a3641dd..a75aee7 100644 --- a/backend/application.go +++ b/backend/application.go @@ -285,18 +285,24 @@ func UpdateDestinations(app *models.Application, destinations []*models.Destinat } var err error if destination.ID == 0 { + // new destination.ID, err = data.DAL.InsertDestination(int64(destination.RouteType), destination.RequestRoute, destination.BackendRoute, destination.Destination, destination.PodsAPI, destination.PodPort, app.ID, destination.NodeID) if err != nil { utils.DebugPrintln("InsertDestination", err) + } else { + destination.Online = true + newDestinations = append(newDestinations, destination) } } else { + // update err = data.DAL.UpdateDestinationNode(int64(destination.RouteType), destination.RequestRoute, destination.BackendRoute, destination.Destination, destination.PodsAPI, destination.PodPort, app.ID, destination.NodeID, destination.ID) if err != nil { utils.DebugPrintln("UpdateDestinationNode", err) + } else { + destination.Online = true + newDestinations = append(newDestinations, destination) } } - destination.Online = true - newDestinations = append(newDestinations, destination) } app.Destinations = newDestinations diff --git a/backend/init.go b/backend/init.go index 8c010ab..168216e 100644 --- a/backend/init.go +++ b/backend/init.go @@ -159,7 +159,7 @@ func InitDatabase() { // v1.2.4 add constraint to access_stats if !dal.ExistConstraint("access_stats", "stat_id") { - _ = dal.ExecSQL(`ALTER TABLE "access_stats" ADD CONSTRAINT "stat_id" unique ("app_id","url_path","stat_date")`) + _ = dal.ExecSQL(`ALTER TABLE "access_stats" ADD CONSTRAINT "stat_id" UNIQUE("app_id","url_path","stat_date")`) //if err != nil { //utils.DebugPrintln("InitDatabase ALTER TABLE access_stats add constraint", err) //} @@ -183,6 +183,14 @@ func InitDatabase() { utils.DebugPrintln("InitDatabase ALTER TABLE applications add custom_headers", err) } } + + // v1.4.2fix2 ALTER TABLE "destinations" ADD CONSTRAINT "unidest" UNIQUE("app_id","request_route","route_type","destination"); + if !dal.ExistConstraint("destinations", "unidest") { + _ = dal.ExecSQL(`ALTER TABLE "destinations" ADD CONSTRAINT "unidest" UNIQUE("app_id","request_route","route_type","destination")`) + if err != nil { + utils.DebugPrintln("InitDatabase ALTER TABLE destinations add constraint", err) + } + } } // LoadAppConfiguration ...