Added checks for port 80 and 443

In my issue #1203, I noticed there was a problem when ports 80 and 443 were already in use. This caused the docker containers to be created but not running
This commit is contained in:
Kairav Mittal
2025-08-03 11:30:33 +05:30
parent 07b86521a5
commit c0c8edb9d1

View File

@@ -18,6 +18,7 @@ import (
"syscall"
"text/template"
"time"
"net"
"golang.org/x/term"
)
@@ -75,6 +76,17 @@ func main() {
fmt.Println("Lets get started!")
fmt.Println("")
for _, p := range []int{80, 443} {
if err := checkPortsAvailable(p); err != nil {
fmt.Fprintln(os.Stderr, err)
fmt.Printf("Please close any services on ports 80/443 in order to run the installation smoothly")
os.Exit(1)
}
}
reader := bufio.NewReader(os.Stdin)
inputContainer := readString(reader, "Would you like to run Pangolin as Docker or Podman containers?", "docker")
@@ -776,3 +788,21 @@ func run(name string, args ...string) error {
cmd.Stderr = os.Stderr
return cmd.Run()
}
func checkPortsAvailable(port int) error {
addr := fmt.Sprintf(":%d", port)
ln, err := net.Listen("tcp", addr)
if err != nil {
return fmt.Errorf(
"ERROR: port %d is occupied or cannot be bound: %w\n\n",
port, err,
)
}
if closeErr := ln.Close(); closeErr != nil {
fmt.Fprintf(os.Stderr,
"WARNING: failed to close test listener on port %d: %v\n",
port, closeErr,
)
}
return nil
}