This is an automated email from the ASF dual-hosted git repository.

hsluoyz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/casbin-gateway.git


The following commit(s) were added to refs/heads/master by this push:
     new e24cd38  fix: prevent unsafe process termination on startup (#202)
e24cd38 is described below

commit e24cd387632a3018deff2363f741093a988a24ec
Author: Paperlz <[email protected]>
AuthorDate: Mon Aug 17 11:42:05 2026 +0800

    fix: prevent unsafe process termination on startup (#202)
---
 README.md       |  2 ++
 conf/app.conf   |  2 +-
 main.go         | 32 -------------------
 util/process.go | 95 ---------------------------------------------------------
 4 files changed, 3 insertions(+), 128 deletions(-)

diff --git a/README.md b/README.md
index b356bad..8e3747c 100644
--- a/README.md
+++ b/README.md
@@ -61,6 +61,8 @@ Casbin Gateway runs standalone out of the box: it signs users 
in against its own
 - **Docker Compose**: Use the provided `docker-compose.yml` for quick local 
setup
 - **Manual Installation**: Build and run from source
 
+The reverse-proxy gateway on ports 80 and 443 is disabled by default, so 
starting the management application does not take over those ports. Set 
`gatewayEnabled = true` in `conf/app.conf` when you are ready to use the WAF 
proxy.
+
 ### Necessary configuration
 
 #### Get the code
diff --git a/conf/app.conf b/conf/app.conf
index 96e7d2e..115f3a8 100644
--- a/conf/app.conf
+++ b/conf/app.conf
@@ -7,7 +7,7 @@ driverName = mysql
 dataSourceName = root:123@tcp(localhost:3306)/
 dbName = caswaf
 redisEndpoint =
-gatewayEnabled = true
+gatewayEnabled = false
 gatewayHttpPort = 80
 gatewayHttpsPort = 443
 httpProxy = ""
diff --git a/main.go b/main.go
index 16755a8..6e537aa 100644
--- a/main.go
+++ b/main.go
@@ -92,38 +92,6 @@ func main() {
 
        port := beego.AppConfig.DefaultInt("httpport", 17000)
 
-       // Stop old instances on all ports before starting new services
-       // Check gateway ports first since they bind first in service.Start()
-       gatewayEnabled, err := beego.AppConfig.Bool("gatewayEnabled")
-       if err != nil {
-               panic(err)
-       }
-
-       if gatewayEnabled {
-               gatewayHttpPort, err := beego.AppConfig.Int("gatewayHttpPort")
-               if err != nil {
-                       panic(err)
-               }
-               err = util.StopOldInstance(gatewayHttpPort)
-               if err != nil {
-                       panic(err)
-               }
-
-               gatewayHttpsPort, err := beego.AppConfig.Int("gatewayHttpsPort")
-               if err != nil {
-                       panic(err)
-               }
-               err = util.StopOldInstance(gatewayHttpsPort)
-               if err != nil {
-                       panic(err)
-               }
-       }
-
-       err = util.StopOldInstance(port)
-       if err != nil {
-               panic(err)
-       }
-
        service.Start()
 
        beego.Run(fmt.Sprintf(":%v", port))
diff --git a/util/process.go b/util/process.go
deleted file mode 100644
index 17af109..0000000
--- a/util/process.go
+++ /dev/null
@@ -1,95 +0,0 @@
-// Copyright 2023 The casbin 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 util
-
-import (
-       "fmt"
-       "os"
-       "os/exec"
-       "runtime"
-       "strconv"
-       "strings"
-)
-
-func getPidByPort(port int) (int, error) {
-       var cmd *exec.Cmd
-       switch runtime.GOOS {
-       case "windows":
-               cmd = exec.Command("cmd", "/c", "netstat -ano | findstr 
:"+strconv.Itoa(port))
-       case "darwin", "linux":
-               cmd = exec.Command("lsof", "-t", "-i", ":"+strconv.Itoa(port))
-       default:
-               return 0, fmt.Errorf("unsupported OS: %s", runtime.GOOS)
-       }
-
-       output, err := cmd.Output()
-       if err != nil {
-               if exitErr, ok := err.(*exec.ExitError); ok {
-                       if exitErr.ExitCode() == 1 {
-                               return 0, nil
-                       }
-               }
-               return 0, err
-       }
-
-       lines := strings.Split(string(output), "\n")
-       for _, line := range lines {
-               fields := strings.Fields(line)
-               if len(fields) > 0 {
-                       if runtime.GOOS == "windows" {
-                               if len(fields) >= 2 && fields[1] == 
"0.0.0.0:"+strconv.Itoa(port) {
-                                       pid, err := 
strconv.Atoi(fields[len(fields)-1])
-                                       if err != nil {
-                                               return 0, err
-                                       }
-
-                                       return pid, nil
-                               }
-                       } else {
-                               pid, err := strconv.Atoi(fields[0])
-                               if err != nil {
-                                       return 0, err
-                               }
-
-                               return pid, nil
-                       }
-               }
-       }
-
-       return 0, nil
-}
-
-func StopOldInstance(port int) error {
-       pid, err := getPidByPort(port)
-       if err != nil {
-               return err
-       }
-       if pid == 0 {
-               return nil
-       }
-
-       process, err := os.FindProcess(pid)
-       if err != nil {
-               return err
-       }
-
-       err = process.Kill()
-       if err != nil {
-               return err
-       }
-
-       fmt.Printf("The old instance with pid: %d has been stopped\n", pid)
-       return nil
-}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to