@ -15,9 +15,11 @@
package sub
package sub
import (
import (
"context"
"fmt"
"fmt"
"os"
"os"
"strings"
"strings"
"time"
"github.com/rodaine/table"
"github.com/rodaine/table"
"github.com/spf13/cobra"
"github.com/spf13/cobra"
@ -27,24 +29,24 @@ import (
clientsdk "github.com/fatedier/frp/pkg/sdk/client"
clientsdk "github.com/fatedier/frp/pkg/sdk/client"
)
)
func init ( ) {
var adminAPITimeout = 30 * time . Second
rootCmd . AddCommand ( NewAdminCommand (
"reload" ,
"Hot-Reload frpc configuration" ,
ReloadHandler ,
) )
rootCmd . AddCommand ( NewAdminCommand (
func init ( ) {
"status" ,
commands := [ ] struct {
"Overview of all proxies status" ,
name string
StatusHandler ,
description string
) )
handler func ( * v1 . ClientCommonConfig ) error
} {
{ "reload" , "Hot-Reload frpc configuration" , ReloadHandler } ,
{ "status" , "Overview of all proxies status" , StatusHandler } ,
{ "stop" , "Stop the running frpc" , StopHandler } ,
}
rootCmd . AddCommand ( NewAdminCommand (
for _ , cmdConfig := range commands {
"stop" ,
cmd := NewAdminCommand ( cmdConfig . name , cmdConfig . description , cmdConfig . handler )
"Stop the running frpc" ,
cmd . Flags ( ) . DurationVar ( & adminAPITimeout , "api-timeout" , adminAPITimeout , "Timeout for admin API calls" )
StopHandler ,
rootCmd . AddCommand ( cmd )
) )
}
}
}
func NewAdminCommand ( name , short string , handler func ( * v1 . ClientCommonConfig ) error ) * cobra . Command {
func NewAdminCommand ( name , short string , handler func ( * v1 . ClientCommonConfig ) error ) * cobra . Command {
@ -73,7 +75,9 @@ func NewAdminCommand(name, short string, handler func(*v1.ClientCommonConfig) er
func ReloadHandler ( clientCfg * v1 . ClientCommonConfig ) error {
func ReloadHandler ( clientCfg * v1 . ClientCommonConfig ) error {
client := clientsdk . New ( clientCfg . WebServer . Addr , clientCfg . WebServer . Port )
client := clientsdk . New ( clientCfg . WebServer . Addr , clientCfg . WebServer . Port )
client . SetAuth ( clientCfg . WebServer . User , clientCfg . WebServer . Password )
client . SetAuth ( clientCfg . WebServer . User , clientCfg . WebServer . Password )
if err := client . Reload ( strictConfigMode ) ; err != nil {
ctx , cancel := context . WithTimeout ( context . Background ( ) , adminAPITimeout )
defer cancel ( )
if err := client . Reload ( ctx , strictConfigMode ) ; err != nil {
return err
return err
}
}
fmt . Println ( "reload success" )
fmt . Println ( "reload success" )
@ -83,7 +87,9 @@ func ReloadHandler(clientCfg *v1.ClientCommonConfig) error {
func StatusHandler ( clientCfg * v1 . ClientCommonConfig ) error {
func StatusHandler ( clientCfg * v1 . ClientCommonConfig ) error {
client := clientsdk . New ( clientCfg . WebServer . Addr , clientCfg . WebServer . Port )
client := clientsdk . New ( clientCfg . WebServer . Addr , clientCfg . WebServer . Port )
client . SetAuth ( clientCfg . WebServer . User , clientCfg . WebServer . Password )
client . SetAuth ( clientCfg . WebServer . User , clientCfg . WebServer . Password )
res , err := client . GetAllProxyStatus ( )
ctx , cancel := context . WithTimeout ( context . Background ( ) , adminAPITimeout )
defer cancel ( )
res , err := client . GetAllProxyStatus ( ctx )
if err != nil {
if err != nil {
return err
return err
}
}
@ -109,7 +115,9 @@ func StatusHandler(clientCfg *v1.ClientCommonConfig) error {
func StopHandler ( clientCfg * v1 . ClientCommonConfig ) error {
func StopHandler ( clientCfg * v1 . ClientCommonConfig ) error {
client := clientsdk . New ( clientCfg . WebServer . Addr , clientCfg . WebServer . Port )
client := clientsdk . New ( clientCfg . WebServer . Addr , clientCfg . WebServer . Port )
client . SetAuth ( clientCfg . WebServer . User , clientCfg . WebServer . Password )
client . SetAuth ( clientCfg . WebServer . User , clientCfg . WebServer . Password )
if err := client . Stop ( ) ; err != nil {
ctx , cancel := context . WithTimeout ( context . Background ( ) , adminAPITimeout )
defer cancel ( )
if err := client . Stop ( ctx ) ; err != nil {
return err
return err
}
}
fmt . Println ( "stop success" )
fmt . Println ( "stop success" )