Add local-port and public-tunnel reachability checks

This commit is contained in:
2026-10-30 22:42:40 +08:00
parent baff9449bd
commit 5f98c2834f
6 changed files with 411 additions and 8 deletions
+52
View File
@@ -50,6 +50,8 @@ func run(args []string) int {
return runConfig(configPath, rest)
case "route":
return runRoute(configPath, rest)
case "check":
return runCheck(configPath, rest)
case "import":
return runImport(configPath, rest)
case "up":
@@ -87,6 +89,7 @@ Commands:
route add <port|host:port> [--name NAME] [--proxy-protocol v1|v2]
route remove <name>
route list
check [name] Check local targets and the public TLS path
import --profile NAME --api URL
Take over an existing official opentunnel profile
up Provision if needed and run in the background
@@ -335,6 +338,55 @@ func runImport(configPath string, args []string) int {
return 0
}
func runCheck(configPath string, args []string) int {
if len(args) > 1 {
fmt.Fprintln(os.Stderr, "usage: tunnel check [route]")
return 2
}
cfg, err := client.Load(configPath)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
if len(cfg.Routes) == 0 {
fmt.Println("No routes.")
return 0
}
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
var results []client.RouteCheck
if len(args) == 1 {
results = []client.RouteCheck{client.CheckRoute(ctx, cfg, args[0])}
} else {
results = client.CheckAll(ctx, cfg)
}
failed := false
for _, result := range results {
local, public := "down", "down"
if result.LocalUp {
local = "up"
}
if result.PublicUp {
public = "up"
}
fmt.Printf("%-20s local=%-4s public=%-4s\n", result.Route, local, public)
if result.LocalError != "" {
fmt.Printf(" local: %s\n", result.LocalError)
}
if result.PublicError != "" {
fmt.Printf(" public: %s\n", result.PublicError)
}
if !result.LocalUp || !result.PublicUp {
failed = true
}
}
if failed {
return 1
}
return 0
}
func runUp(configPath string) int {
cfg, err := client.Load(configPath)
if err != nil {