Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix various issues #1781

Merged
merged 17 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .goreleaser-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ builds:
goarch:
- amd64
- 386
- arm64
env:
- CGO_ENABLED=1
main: ./cmd/skywire/
Expand Down
19 changes: 5 additions & 14 deletions cmd/apps/skysocks-client/commands/skysocks-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,14 @@ var RootCmd = &cobra.Command{
setAppErr(appCl, err)
os.Exit(1)
}
var closeSignal bool
if runtime.GOOS == "windows" {
ipcClient, err := ipc.StartClient(visorconfig.SkysocksName, nil)
ipcClient, err := ipc.StartClient(visorconfig.SkysocksClientName, nil)
if err != nil {
setAppErr(appCl, err)
print(fmt.Sprintf("Error creating ipc server for skysocks: %v\n", err))
os.Exit(1)
}
go func() {
client.ListenIPC(ipcClient)
closeSignal = true
}()
go client.ListenIPC(ipcClient)
} else {
termCh := make(chan os.Signal, 1)
signal.Notify(termCh, os.Interrupt)
Expand All @@ -123,7 +119,6 @@ var RootCmd = &cobra.Command{
print(fmt.Sprintf("%v\n", err))
os.Exit(1)
}
closeSignal = true
}()
}

Expand All @@ -134,14 +129,10 @@ var RootCmd = &cobra.Command{
go httpProxy(httpCtx, httpAddr, addr)
}
defer httpCancel()
for {
if err := client.ListenAndServe(addr); err != nil {
print(fmt.Sprintf("Error serving proxy client: %v\n", err))
}
if closeSignal {
break
}
if err := client.ListenAndServe(addr); err != nil {
print(fmt.Sprintf("Error serving proxy client: %v\n", err))
}
setAppStatus(appCl, appserver.AppDetailedStatusStopped)
},
}

Expand Down
86 changes: 51 additions & 35 deletions cmd/skywire-cli/commands/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/skycoin/skywire/cmd/skywire-cli/internal"
"github.com/skycoin/skywire/pkg/app/appserver"
"github.com/skycoin/skywire/pkg/routing"
"github.com/skycoin/skywire/pkg/visor/visorconfig"
)

func init() {
Expand All @@ -39,6 +40,8 @@ func init() {
startCmd.Flags().StringVarP(&pk, "pk", "k", "", "server public key")
startCmd.Flags().StringVarP(&addr, "addr", "a", "", "address of proxy for use")
startCmd.Flags().StringVarP(&clientName, "name", "n", "", "name of skysocks client")
startCmd.Flags().IntVarP(&startingTimeout, "timeout", "t", 30, "timeout for starting proxy")
startCmd.Flags().StringVar(&httpAddr, "http", "", "address for http proxy")
stopCmd.Flags().BoolVar(&allClients, "all", false, "stop all skysocks client")
stopCmd.Flags().StringVar(&clientName, "name", "", "specific skysocks client that want stop")
}
Expand All @@ -53,8 +56,27 @@ var startCmd = &cobra.Command{
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("unable to create RPC client: %w", err))
}

if clientName != "" && pk != "" && addr != "" {
// add new app with -srv and -addr args, and if app was there just change -srv and -addr args and run it
// stop possible running proxy before start it again
if clientName != "" {
rpcClient.StopApp(clientName) //nolint
} else {
rpcClient.StopApp("skysocks-client") //nolint
}

tCtx := context.Background() //nolint
if startingTimeout != 0 {
tCtx, _ = context.WithTimeout(context.Background(), time.Duration(startingTimeout)*time.Second) //nolint
}
ctx, cancel := cmdutil.SignalContext(tCtx, &logrus.Logger{})
go func() {
<-ctx.Done()
cancel()
rpcClient.KillApp(clientName) //nolint
fmt.Print("\nStopped!")
os.Exit(1)
}()

if pk != "" {
err := pubkey.Set(pk)
if err != nil {
if len(args) > 0 {
Expand All @@ -66,10 +88,23 @@ var startCmd = &cobra.Command{
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Invalid or missing public key"))
}
}
arguments := map[string]any{}
arguments["app"] = "skysocks-client"

arguments := map[string]string{}
arguments["srv"] = pubkey.String()
arguments["addr"] = addr
arguments["--srv"] = pubkey.String()

if addr == "" {
addr = visorconfig.SkysocksClientAddr
}
arguments["--addr"] = addr

if httpAddr != "" {
arguments["--http"] = httpAddr
}

if clientName == "" {
clientName = "skysocks-client"
}

_, err = rpcClient.App(clientName)
if err == nil {
Expand All @@ -89,40 +124,14 @@ var startCmd = &cobra.Command{
}
internal.Catch(cmd.Flags(), rpcClient.StartApp(clientName))
internal.PrintOutput(cmd.Flags(), nil, "Starting.")
} else if clientName != "" && pk == "" && addr == "" {
internal.Catch(cmd.Flags(), rpcClient.StartApp(clientName))
internal.PrintOutput(cmd.Flags(), nil, "Starting.")
} else if pk != "" && clientName == "" && addr == "" {
err := pubkey.Set(pk)
if err != nil {
if len(args) > 0 {
err := pubkey.Set(args[0])
if err != nil {
internal.PrintFatalError(cmd.Flags(), err)
}
} else {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Invalid or missing public key"))
}
}
internal.Catch(cmd.Flags(), rpcClient.StartSkysocksClient(pubkey.String()))
internal.PrintOutput(cmd.Flags(), nil, "Starting.")
clientName = "skysocks-client"
// change defaul skysocks-proxy app -srv arg and run it
} else {
internal.Catch(cmd.Flags(), rpcClient.StartApp("skysocks-client"))
if clientName == "" {
clientName = "skysocks-client"
}
internal.Catch(cmd.Flags(), rpcClient.StartApp(clientName))
internal.PrintOutput(cmd.Flags(), nil, "Starting.")
clientName = "skysocks-client"
}

ctx, cancel := cmdutil.SignalContext(context.Background(), &logrus.Logger{})
defer cancel()
go func() {
<-ctx.Done()
cancel()
rpcClient.StopApp(clientName) //nolint
os.Exit(1)
}()

startProcess := true
for startProcess {
time.Sleep(time.Second * 1)
Expand All @@ -147,6 +156,13 @@ var startCmd = &cobra.Command{
}
internal.PrintOutput(cmd.Flags(), out, fmt.Sprintln("\nError! > "+state.DetailedStatus))
}
if state.Status == appserver.AppStatusStopped {
startProcess = false
out := output{
AppError: state.DetailedStatus,
}
internal.PrintOutput(cmd.Flags(), out, fmt.Sprintln("\nStopped!"+state.DetailedStatus))
}
}
}
}
Expand Down
45 changes: 22 additions & 23 deletions cmd/skywire-cli/commands/proxy/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,28 @@ import (
)

var (
version = buildinfo.Version()
binaryName = "skysocks-client"
stateName = "skysocks-client"
serviceType = servicedisc.ServiceTypeProxy
isUnFiltered bool
rawData bool
utURL string
sdURL string
cacheFileSD string
cacheFileUT string
cacheFilesAge int
ver string
country string
isStats bool
pubkey cipher.PubKey
pk string
allClients bool
noFilterOnline bool
clientName string
addr string

// startingTimeout int
// httpProxy string
version = buildinfo.Version()
binaryName = "skysocks-client"
stateName = "skysocks-client"
serviceType = servicedisc.ServiceTypeProxy
isUnFiltered bool
rawData bool
utURL string
sdURL string
cacheFileSD string
cacheFileUT string
cacheFilesAge int
ver string
country string
isStats bool
pubkey cipher.PubKey
pk string
allClients bool
noFilterOnline bool
clientName string
addr string
startingTimeout int
httpAddr string
)

// RootCmd contains commands that interact with the skywire-visor
Expand Down
5 changes: 3 additions & 2 deletions cmd/skywire-cli/commands/vpn/vvpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {
listCmd,
)
startCmd.Flags().StringVarP(&pk, "pk", "k", "", "server public key")
startCmd.Flags().IntVarP(&startingTimeout, "timeout", "t", 20, "starting timeout value in second")
startCmd.Flags().IntVarP(&startingTimeout, "timeout", "t", 30, "starting timeout value in second")
}

var startCmd = &cobra.Command{
Expand Down Expand Up @@ -67,7 +67,8 @@ var startCmd = &cobra.Command{
go func() {
<-ctx.Done()
cancel()
rpcClient.StopVPNClient("vpn-client") //nolint
rpcClient.KillApp("vpn-client") //nolint
fmt.Print("\nStopped!")
os.Exit(1)
}()
startProcess := true
Expand Down
60 changes: 60 additions & 0 deletions pkg/app/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,18 @@ func (l *AppLauncher) StopApp(name string) (*appserver.Proc, error) {
return proc, nil
}

// KillApp stop an app, by its name | We use it on stop apps during its running steps by cli.
func (l *AppLauncher) KillApp(name string) error {
log := l.log.WithField("func", "KillApp").WithField("app_name", name)

if err := l.killApp(name); err != nil {
log.WithError(err).Warn("Failed to kill app.")
return err
}

return nil
}

// RestartApp restarts a running app.
func (l *AppLauncher) RestartApp(name, binary string) error {
l.log.WithField("func", "RestartApp").WithField("app_name", name).
Expand Down Expand Up @@ -411,3 +423,51 @@ func (l *AppLauncher) killHangingProc(appName string, pid int) {
}
log.Info("Killed hanging child process that ran previously with this visor.")
}

func (l *AppLauncher) killApp(appName string) error {
log := l.log.WithField("func", "killHangingProcesses")

pidF, err := l.pidFile()
if err != nil {
return err
}
filename := pidF.Name()
log = log.WithField("pid_file", filename)

scan := bufio.NewScanner(pidF)
for scan.Scan() {
appInfo := strings.Split(scan.Text(), " ")
if len(appInfo) != 2 {
err := errors.New("line should be: [app name] [pid]")
log.WithError(err).Fatal("Failed parsing pid file.")
}

pid, err := strconv.Atoi(appInfo[1])
if err != nil {
log.WithError(err).Fatal("Failed parsing pid file.")
}
if appInfo[0] == appName {
l.killArbitraryProc(appInfo[0], pid)
}
}

return nil
}

func (l *AppLauncher) killArbitraryProc(appName string, pid int) {
log := l.log.WithField("app_name", appName).WithField("pid", pid)

p, err := os.FindProcess(pid)
if err != nil {
if runtime.GOOS != "windows" {
log.Info("Process not found.")
}
return
}

err = p.Signal(syscall.SIGKILL)
if err != nil {
return
}
log.Info("Killed process.")
}
Loading
Loading