Skip to content

Commit

Permalink
Merge pull request #45 from mrpalide/fix/fix-vpn-monitor-issue
Browse files Browse the repository at this point in the history
Fix VPN monitor issues
  • Loading branch information
mrpalide authored Jan 9, 2024
2 parents c66f356 + 21e3801 commit 44d439d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 114 deletions.
161 changes: 51 additions & 110 deletions cmd/vpn-lite-client/vpn-lite-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,99 +3,74 @@ package main

import (
"errors"
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"

cc "github.com/ivanpirog/coloredcobra"
"github.com/skycoin/skywire-utilities/pkg/buildinfo"
"github.com/skycoin/skywire-utilities/pkg/cipher"
"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/app/appevent"
"github.com/skycoin/skywire/pkg/app/appserver"
"github.com/spf13/cobra"

"github.com/skycoin/skywire-services/internal/vpn"
)

var (
serverPKStr string
serverPKStr = flag.String("srv", "", "PubKey of the server to connect to")
)

func init() {
RootCmd.Flags().StringVarP(&serverPKStr, "srv", "k", "", "PubKey of the server to connect to\033[0m")
var helpflag bool
RootCmd.SetUsageTemplate(help)
RootCmd.PersistentFlags().BoolVarP(&helpflag, "help", "h", false, "help for vpn-lite-client")
RootCmd.SetHelpCommand(&cobra.Command{Hidden: true})
RootCmd.PersistentFlags().MarkHidden("help") //nolint
}
func main() {
flag.Parse()

eventSub := appevent.NewSubscriber()

appCl := app.NewClient(eventSub)
defer appCl.Close()

if *serverPKStr == "" {
err := errors.New("VPN server pub key is missing")
print(fmt.Sprintf("%v\n", err))
setAppErr(appCl, err)
os.Exit(1)
}

serverPK := cipher.PubKey{}
if err := serverPK.UnmarshalText([]byte(*serverPKStr)); err != nil {
print(fmt.Sprintf("Invalid local SK: %v\n", err))
setAppErr(appCl, err)
os.Exit(1)
}

fmt.Printf("Connecting to VPN server %s\n", serverPK.String())

vpnLiteClientCfg := vpn.ClientConfig{
ServerPK: serverPK,
}
vpnLiteClient, err := vpn.NewLiteClient(vpnLiteClientCfg, appCl)
if err != nil {
print(fmt.Sprintf("Error creating VPN lite client: %v\n", err))
setAppErr(appCl, err)
}

osSigs := make(chan os.Signal, 2)
sigs := []os.Signal{syscall.SIGTERM, syscall.SIGINT}
for _, sig := range sigs {
signal.Notify(osSigs, sig)
}

go func() {
<-osSigs
vpnLiteClient.Close()
}()

defer setAppStatus(appCl, appserver.AppDetailedStatusStopped)

if err := vpnLiteClient.Serve(); err != nil {
print(fmt.Sprintf("Failed to serve VPN lite client: %v\n", err))
}

// RootCmd contains the root command
var RootCmd = &cobra.Command{
Use: "vpnlc",
Short: "Vpn lite client",
Long: `
┬ ┬┌─┐┌┐┌ ┬ ┬┌┬┐┌─┐ ┌─┐┬ ┬┌─┐┌┐┌┌┬┐
└┐┌┘├─┘│││───│ │ │ ├┤───│ │ │├┤ │││ │
└┘ ┴ ┘└┘ ┴─┘┴ ┴ └─┘ └─┘┴─┘┴└─┘┘└┘ ┴ `,
SilenceErrors: true,
SilenceUsage: true,
DisableSuggestions: true,
DisableFlagsInUseLine: true,
Version: buildinfo.Version(),
Run: func(_ *cobra.Command, _ []string) {

eventSub := appevent.NewSubscriber()

appCl := app.NewClient(eventSub)
defer appCl.Close()

if serverPKStr == "" {
err := errors.New("VPN server pub key is missing")
print(fmt.Sprintf("%v\n", err))
setAppErr(appCl, err)
os.Exit(1)
}

serverPK := cipher.PubKey{}
if err := serverPK.UnmarshalText([]byte(serverPKStr)); err != nil {
print(fmt.Sprintf("Invalid local SK: %v\n", err))
setAppErr(appCl, err)
os.Exit(1)
}

fmt.Printf("Connecting to VPN server %s\n", serverPK.String())

vpnLiteClientCfg := vpn.ClientConfig{
ServerPK: serverPK,
}
vpnLiteClient, err := vpn.NewLiteClient(vpnLiteClientCfg, appCl)
if err != nil {
print(fmt.Sprintf("Error creating VPN lite client: %v\n", err))
setAppErr(appCl, err)
}

osSigs := make(chan os.Signal, 2)
sigs := []os.Signal{syscall.SIGTERM, syscall.SIGINT}
for _, sig := range sigs {
signal.Notify(osSigs, sig)
}

go func() {
<-osSigs
vpnLiteClient.Close()
}()

defer setAppStatus(appCl, appserver.AppDetailedStatusStopped)

if err := vpnLiteClient.Serve(); err != nil {
print(fmt.Sprintf("Failed to serve VPN lite client: %v\n", err))
}

},
}

func setAppErr(appCl *app.Client, err error) {
Expand All @@ -109,37 +84,3 @@ func setAppStatus(appCl *app.Client, status appserver.AppDetailedStatus) {
print(fmt.Sprintf("Failed to set status %v: %v\n", status, err))
}
}

func main() {
Execute()
}

// Execute executes root CLI command.
func Execute() {
cc.Init(&cc.Config{
RootCmd: RootCmd,
Headings: cc.HiBlue + cc.Bold, //+ cc.Underline,
Commands: cc.HiBlue + cc.Bold,
CmdShortDescr: cc.HiBlue,
Example: cc.HiBlue + cc.Italic,
ExecName: cc.HiBlue + cc.Bold,
Flags: cc.HiBlue + cc.Bold,
//FlagsDataType: cc.HiBlue,
FlagsDescr: cc.HiBlue,
NoExtraNewlines: true,
NoBottomNewline: true,
})
if err := RootCmd.Execute(); err != nil {
log.Fatal("Failed to execute command: ", err)
}
}

const help = "Usage:\r\n" +
" {{.UseLine}}{{if .HasAvailableSubCommands}}{{end}} {{if gt (len .Aliases) 0}}\r\n\r\n" +
"{{.NameAndAliases}}{{end}}{{if .HasAvailableSubCommands}}\r\n\r\n" +
"Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand)}}\r\n " +
"{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}\r\n\r\n" +
"Flags:\r\n" +
"{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}\r\n\r\n" +
"Global Flags:\r\n" +
"{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}\r\n\r\n"
3 changes: 0 additions & 3 deletions cmd/vpn-monitor/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package commands

import (
"context"
"fmt"
"log"
"os"
"time"
Expand Down Expand Up @@ -60,8 +59,6 @@ var RootCmd = &cobra.Command{
mLogger := logging.NewMasterLogger()
conf := api.InitConfig(confPath, mLogger)

fmt.Println(conf.Version)

srvURLs := api.ServicesURLs{
SD: conf.Launcher.ServiceDisc,
UT: conf.UptimeTracker.Addr,
Expand Down
7 changes: 6 additions & 1 deletion pkg/vpn-monitor/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"io"
"math/rand"
"net/http"
"net/url"
"sync/atomic"
Expand Down Expand Up @@ -303,6 +304,10 @@ func (api *API) getVPNKeys() {
if len(vpns) == 0 {
api.logger.Warn("No vpns found... Trying again")
}
//randomize the order of the vpn entries
rand.Shuffle(len(vpns), func(i, j int) {
vpns[i], vpns[j] = vpns[j], vpns[i]
})
api.vpnKeys = []cipher.PubKey{}
for _, vpnEntry := range vpns {
api.vpnKeys = append(api.vpnKeys, vpnEntry.Addr.PubKey())
Expand Down Expand Up @@ -390,7 +395,7 @@ func InitConfig(confPath string, mLog *logging.MasterLogger) *visorconfig.V1 {
for _, app := range conf.Launcher.Apps {
for _, oldApp := range oldConf.Launcher.Apps {
if app.Name == oldApp.Name {
newConfLauncherApps = append(newConfLauncherApps, app)
newConfLauncherApps = append(newConfLauncherApps, oldApp)
}
}
}
Expand Down

0 comments on commit 44d439d

Please sign in to comment.