From f308040da5f137afce6f00925ccc8232c462fc67 Mon Sep 17 00:00:00 2001 From: Jeroen Wijenbergh Date: Sat, 15 Feb 2025 22:11:34 +0100 Subject: CLI: Add debug and country-code flag --- cmd/cli/main.go | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) (limited to 'cmd/cli/main.go') diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 1edc463..263f55c 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -88,10 +88,15 @@ func stateCallback(state *client.Client, _ client.FSMStateID, newState client.FS if newState == client.StateAskProfile { sendProfile(state, data) } + + if newState == client.StateAskLocation { + fmt.Fprint(os.Stderr, "An invalid secure location is stored. This CLI doesn't support interactively choosing a location yet. Give a correct location with the -country-code flag") + os.Exit(1) + } } // Get a config for Institute Access or Secure Internet Server. -func getConfig(state *client.Client, url string, srvType srvtypes.Type) (*srvtypes.Configuration, error) { +func getConfig(state *client.Client, url string, srvType srvtypes.Type, cc string) (*srvtypes.Configuration, error) { if !strings.HasPrefix(url, "http") { url = "https://" + url } @@ -99,13 +104,23 @@ func getConfig(state *client.Client, url string, srvType srvtypes.Type) (*srvtyp defer ck.Cancel() //nolint:errcheck err := state.AddServer(ck, url, srvType, nil) if err != nil { - return nil, err + // TODO: This is quite hacky :^) + if !strings.Contains(err.Error(), "a secure internet server already exists.") { + return nil, err + } } + if cc != "" { + err = state.SetSecureLocation(url, cc) + if err != nil { + return nil, err + } + } + return state.GetConfig(ck, url, srvType, false, false) } // Get a config for a single server, Institute Access or Secure Internet. -func printConfig(url string, srvType srvtypes.Type) { +func printConfig(url string, cc string, srvType srvtypes.Type, debug bool) { var c *client.Client c, err := client.New( "org.eduvpn.app.linux", @@ -115,7 +130,7 @@ func printConfig(url string, srvType srvtypes.Type) { stateCallback(c, oldState, newState, data) return true }, - true, + debug, ) if err != nil { fmt.Printf("Register error: %v", err) @@ -135,13 +150,12 @@ func printConfig(url string, srvType srvtypes.Type) { defer c.Deregister() - cfg, err := getConfig(c, url, srvType) + cfg, err := getConfig(c, url, srvType, cc) if err != nil { fmt.Fprintf(os.Stderr, "failed getting a config: %v\n", err) return } - fmt.Println(cfg.Protocol) - fmt.Println("Obtained config:", cfg.VPNConfig) + fmt.Printf("Obtained config:\n%s\n", cfg.VPNConfig) } // The main function @@ -150,16 +164,18 @@ func main() { cu := flag.String("get-custom", "", "The url of a custom server to connect to") u := flag.String("get-institute", "", "The url of an institute to connect to") sec := flag.String("get-secure", "", "Gets secure internet servers") + cc := flag.String("country-code", "", "The country code to use in case of a secure internet server") + debug := flag.Bool("debug", false, "Whether or not to enable debugging") flag.Parse() // Connect to a VPN by getting an Institute Access config switch { case *cu != "": - printConfig(*cu, srvtypes.TypeCustom) + printConfig(*cu, "", srvtypes.TypeCustom, *debug) case *u != "": - printConfig(*u, srvtypes.TypeInstituteAccess) + printConfig(*u, "", srvtypes.TypeInstituteAccess, *debug) case *sec != "": - printConfig(*sec, srvtypes.TypeSecureInternet) + printConfig(*sec, *cc, srvtypes.TypeSecureInternet, *debug) default: flag.PrintDefaults() } -- cgit v1.2.3