From 9001a42e8a7cf31de184fb2209fce16dfe7515aa Mon Sep 17 00:00:00 2001 From: Saahil Bhavsar Date: Fri, 7 Jun 2024 23:31:47 +0530 Subject: [PATCH] Fixes config issues Signed-off-by: Saahil Bhavsar --- autoproxy.config | 2 +- cli/cmd/config.go | 10 ++- cli/cmd/login.go | 4 +- cli/cmd/logout.go | 4 +- cli/cmd/root.go | 19 +++++ cli/cmd/schedule.go | 4 +- daemon/commands/login.go | 85 ++++-------------- daemon/commands/schedule.go | 166 +++++++++++++++++++++++------------- daemon/main_unix.go | 37 ++++---- daemon/main_windows.go | 37 ++++---- pkg/keystore/keystore.go | 53 ------------ pkg/utils.go | 12 +-- 12 files changed, 208 insertions(+), 225 deletions(-) delete mode 100644 pkg/keystore/keystore.go diff --git a/autoproxy.config b/autoproxy.config index 36d32c7..d293383 100644 --- a/autoproxy.config +++ b/autoproxy.config @@ -1,6 +1,6 @@ { "base_url": "http://www.gstatic.com/generate_204", - "login_base_url": "https://gateway.iitj.ac.in:1003/" + "login_base_url": "https://gateway.iitj.ac.in:1003/", "username": "", "password": "", "token": "", diff --git a/cli/cmd/config.go b/cli/cmd/config.go index 56bf5c1..55f28ba 100644 --- a/cli/cmd/config.go +++ b/cli/cmd/config.go @@ -5,9 +5,8 @@ import ( "syscall" "github.com/spf13/cobra" + "github.com/spf13/viper" "golang.org/x/term" - - "github.com/SaahilNotSahil/iitj-autoproxy/pkg/keystore" ) func init() { @@ -30,7 +29,10 @@ var configCmd = &cobra.Command{ cobra.CheckErr(err) password := string(bytePassword) - keystore.Set("username", username) - keystore.Set("password", password) + viper.Set("username", username) + viper.Set("password", password) + viper.Set("token", "") + + cobra.CheckErr(viper.WriteConfig()) }, } diff --git a/cli/cmd/login.go b/cli/cmd/login.go index e54119b..5405858 100644 --- a/cli/cmd/login.go +++ b/cli/cmd/login.go @@ -9,7 +9,9 @@ import ( ) func init() { - loginCmd.Flags().BoolVarP(&isDummy, "dummy", "d", false, "") + loginCmd.Flags().BoolVarP( + &isDummy, "dummy", "d", false, "Run a dummy version of the command", + ) rootCmd.AddCommand(loginCmd) } diff --git a/cli/cmd/logout.go b/cli/cmd/logout.go index a66c1b4..4dbc6b3 100644 --- a/cli/cmd/logout.go +++ b/cli/cmd/logout.go @@ -9,7 +9,9 @@ import ( ) func init() { - logoutCmd.Flags().BoolVarP(&isDummy, "dummy", "d", false, "") + logoutCmd.Flags().BoolVarP( + &isDummy, "dummy", "d", false, "Run a dummy version of the command", + ) rootCmd.AddCommand(logoutCmd) } diff --git a/cli/cmd/root.go b/cli/cmd/root.go index 445e3ce..ac2325e 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -1,11 +1,30 @@ package cmd import ( + "os" + "github.com/spf13/cobra" + "github.com/spf13/viper" ) var isDummy bool +func initConfig() { + home, err := os.UserHomeDir() + cobra.CheckErr(err) + + viper.AddConfigPath(home) + + viper.SetConfigName(".autoproxyconfig") + viper.SetConfigType("json") + + cobra.CheckErr(viper.ReadInConfig()) +} + +func init() { + cobra.OnInitialize(initConfig) +} + var rootCmd = &cobra.Command{ Use: "autoproxy", Short: "Use IITJ internet hassle-free", diff --git a/cli/cmd/schedule.go b/cli/cmd/schedule.go index eb2bfa9..ba9e696 100644 --- a/cli/cmd/schedule.go +++ b/cli/cmd/schedule.go @@ -9,7 +9,9 @@ import ( ) func init() { - scheduleCmd.Flags().BoolVarP(&isDummy, "dummy", "d", false, "") + scheduleCmd.Flags().BoolVarP( + &isDummy, "dummy", "d", false, "Run a dummy version of the command", + ) rootCmd.AddCommand(scheduleCmd) } diff --git a/daemon/commands/login.go b/daemon/commands/login.go index 010ed8c..7dad9cd 100644 --- a/daemon/commands/login.go +++ b/daemon/commands/login.go @@ -7,17 +7,16 @@ import ( "github.com/SaahilNotSahil/iitj-autoproxy/pkg" "github.com/SaahilNotSahil/iitj-autoproxy/pkg/daemon" - "github.com/SaahilNotSahil/iitj-autoproxy/pkg/keystore" ) func LoginCmd() { err := viper.ReadInConfig() if err != nil { - pkg.Logger.Println(err) - log.Println(err) + pkg.Logger.Println("Error reading config file") + log.Println("Error reading config file") err = daemon.SendMessageToCLI( - "Error reading config file. Please make sure the file exists and is valid", + "Error reading config file", ) if err != nil { pkg.Logger.Println(err) @@ -29,16 +28,16 @@ func LoginCmd() { return } - var username string - var password string + username := viper.GetString("username") + password := viper.GetString("password") + baseURL := viper.GetString("base_url") - username, err = keystore.Get("username") - if err != nil { - pkg.Logger.Println(err) - log.Println(err) + if username == "" || password == "" { + pkg.Logger.Println("Please configure the application using the config command") + log.Println("Please configure the application using the config command") err = daemon.SendMessageToCLI( - err.Error(), + "Please configure the application using the config command", ) if err != nil { pkg.Logger.Println(err) @@ -50,25 +49,7 @@ func LoginCmd() { return } - password, err = keystore.Get("password") - if err != nil { - pkg.Logger.Println(err) - log.Println(err) - - err = daemon.SendMessageToCLI( - err.Error(), - ) - if err != nil { - pkg.Logger.Println(err) - log.Println(err) - - return - } - - return - } - - err = pkg.Login(viper.GetString("base_url"), username, password) + err = pkg.Login(baseURL, username, password) if err != nil { pkg.Logger.Println(err) log.Println(err) @@ -93,29 +74,11 @@ func LoginCmd() { func LoginDummyCmd() { err := viper.ReadInConfig() if err != nil { - pkg.Logger.Println(err) - log.Println(err) - - err = daemon.SendMessageToCLI( - "Error reading config file. Please make sure the file exists and is valid", - ) - if err != nil { - pkg.Logger.Println(err) - log.Println(err) - - return - } - - return - } - - _, err = keystore.Get("username") - if err != nil { - pkg.Logger.Println(err) - log.Println(err) + pkg.Logger.Println("Error reading config file") + log.Println("Error reading config file") err = daemon.SendMessageToCLI( - err.Error(), + "Error reading config file", ) if err != nil { pkg.Logger.Println(err) @@ -127,23 +90,9 @@ func LoginDummyCmd() { return } - _, err = keystore.Get("password") - if err != nil { - pkg.Logger.Println(err) - log.Println(err) - - err = daemon.SendMessageToCLI( - err.Error(), - ) - if err != nil { - pkg.Logger.Println(err) - log.Println(err) - - return - } - - return - } + _ = viper.GetString("username") + _ = viper.GetString("password") + _ = viper.GetString("base_url") err = daemon.SendMessageToCLI("Dummy login successful") if err != nil { diff --git a/daemon/commands/schedule.go b/daemon/commands/schedule.go index 13777ae..04194ef 100644 --- a/daemon/commands/schedule.go +++ b/daemon/commands/schedule.go @@ -8,7 +8,6 @@ import ( "github.com/SaahilNotSahil/iitj-autoproxy/pkg" "github.com/SaahilNotSahil/iitj-autoproxy/pkg/daemon" ds "github.com/SaahilNotSahil/iitj-autoproxy/pkg/dummy/scheduler" - "github.com/SaahilNotSahil/iitj-autoproxy/pkg/keystore" "github.com/SaahilNotSahil/iitj-autoproxy/pkg/scheduler" ) @@ -20,11 +19,11 @@ var ( func ScheduleCmd() { err := viper.ReadInConfig() if err != nil { - pkg.Logger.Println(err) - log.Println(err) + pkg.Logger.Println("Error reading config file") + log.Println("Error reading config file") err = daemon.SendMessageToCLI( - "Error reading config file. Please make sure the file exists and is valid", + "Error reading config file", ) if err != nil { pkg.Logger.Println(err) @@ -34,16 +33,16 @@ func ScheduleCmd() { return } - var username string - var password string + username := viper.GetString("username") + password := viper.GetString("password") + baseURL := viper.GetString("base_url") - username, err = keystore.Get("username") - if err != nil { - pkg.Logger.Println(err) - log.Println(err) + if username == "" || password == "" { + pkg.Logger.Println("Please configure the application using the config command") + log.Println("Please configure the application using the config command") err = daemon.SendMessageToCLI( - err.Error(), + "Please configure the application using the config command", ) if err != nil { pkg.Logger.Println(err) @@ -53,26 +52,8 @@ func ScheduleCmd() { return } - password, err = keystore.Get("password") - if err != nil { - pkg.Logger.Println(err) - log.Println(err) - - err = daemon.SendMessageToCLI( - err.Error(), - ) - if err != nil { - pkg.Logger.Println(err) - log.Println(err) - } - - return - } - - base_url := viper.GetString("base_url") - scheduler_running_state := scheduler.RunLoginScheduler( - base_url, username, password, kill, + baseURL, username, password, kill, ) if !scheduler_running_state { pkg.Logger.Println("Scheduler already running") @@ -89,71 +70,62 @@ func ScheduleCmd() { viper.Set("scheduler_running_state", scheduler_running_state) - err = daemon.SendMessageToCLI("Scheduler started") + err = viper.WriteConfig() if err != nil { pkg.Logger.Println(err) log.Println(err) - } - } -} -func ScheduleDummyCmd() { - err := viper.ReadInConfig() - if err != nil { - pkg.Logger.Println(err) - log.Println(err) + err = daemon.SendMessageToCLI(err.Error()) + if err != nil { + pkg.Logger.Println(err) + log.Println(err) + } + } - err = daemon.SendMessageToCLI( - "Error reading config file. Please make sure the file exists and is valid", - ) + err = daemon.SendMessageToCLI("Scheduler started") if err != nil { pkg.Logger.Println(err) log.Println(err) - - return } - - return } +} - _, err = keystore.Get("username") +func ScheduleDummyCmd() { + err := viper.ReadInConfig() if err != nil { - pkg.Logger.Println(err) - log.Println(err) + pkg.Logger.Println("Error reading config file") + log.Println("Error reading config file") err = daemon.SendMessageToCLI( - err.Error(), + "Error reading config file", ) if err != nil { pkg.Logger.Println(err) log.Println(err) - - return } return } - _, err = keystore.Get("password") - if err != nil { - pkg.Logger.Println(err) - log.Println(err) + username := viper.GetString("username") + password := viper.GetString("password") + _ = viper.GetString("base_url") + + if username == "" || password == "" { + pkg.Logger.Println("Please configure the application using the config command") + log.Println("Please configure the application using the config command") err = daemon.SendMessageToCLI( - err.Error(), + "Please configure the application using the config command", ) if err != nil { pkg.Logger.Println(err) log.Println(err) - - return } return } - _ = viper.GetString("base_url") - scheduler_running_state := ds.RunLoginScheduler(killDummy) if !scheduler_running_state { pkg.Logger.Println("Dummy scheduler already running") @@ -170,6 +142,18 @@ func ScheduleDummyCmd() { viper.Set("dummy_scheduler_running_state", scheduler_running_state) + err = viper.WriteConfig() + if err != nil { + pkg.Logger.Println(err) + log.Println(err) + + err = daemon.SendMessageToCLI(err.Error()) + if err != nil { + pkg.Logger.Println(err) + log.Println(err) + } + } + err = daemon.SendMessageToCLI("Dummy scheduler started") if err != nil { pkg.Logger.Println(err) @@ -179,6 +163,24 @@ func ScheduleDummyCmd() { } func KillScheduler() { + err := viper.ReadInConfig() + if err != nil { + pkg.Logger.Println("Error reading config file") + log.Println("Error reading config file") + + err = daemon.SendMessageToCLI( + "Error reading config file", + ) + if err != nil { + pkg.Logger.Println(err) + log.Println(err) + + return + } + + return + } + if scheduler.SchedulerRunning { pkg.Logger.Println("Stopping the scheduler...") log.Println("Stopping the scheduler...") @@ -193,11 +195,41 @@ func KillScheduler() { viper.Set("scheduler_running_state", false) + err = viper.WriteConfig() + if err != nil { + pkg.Logger.Println(err) + log.Println(err) + + err = daemon.SendMessageToCLI(err.Error()) + if err != nil { + pkg.Logger.Println(err) + log.Println(err) + } + } + kill <- true } } func KillDummyScheduler() { + err := viper.ReadInConfig() + if err != nil { + pkg.Logger.Println("Error reading config file") + log.Println("Error reading config file") + + err = daemon.SendMessageToCLI( + "Error reading config file", + ) + if err != nil { + pkg.Logger.Println(err) + log.Println(err) + + return + } + + return + } + if ds.SchedulerRunning { pkg.Logger.Println("Stopping the dummy scheduler...") log.Println("Stopping the dummy scheduler...") @@ -212,6 +244,18 @@ func KillDummyScheduler() { viper.Set("dummy_scheduler_running_state", false) + err = viper.WriteConfig() + if err != nil { + pkg.Logger.Println(err) + log.Println(err) + + err = daemon.SendMessageToCLI(err.Error()) + if err != nil { + pkg.Logger.Println(err) + log.Println(err) + } + } + killDummy <- true } } diff --git a/daemon/main_unix.go b/daemon/main_unix.go index c6f02c2..932b0d8 100644 --- a/daemon/main_unix.go +++ b/daemon/main_unix.go @@ -9,6 +9,7 @@ import ( "log" "os" "os/signal" + "path" "runtime" "syscall" @@ -92,29 +93,30 @@ func initConfig() { baseConfigPath = "/etc/iitj-autoproxy/autoproxy.config" } - configName := ".autoproxy.config" + configName := ".autoproxyconfig" - viper.AddConfigPath(home) + targetConfig := path.Join(home, configName) - viper.SetConfigType("json") + viper.AddConfigPath(home) viper.SetConfigName(configName) + viper.SetConfigType("json") - err = viper.ReadInConfig() + _, err = os.Stat(targetConfig) if err != nil { pkg.Logger.Println(err) log.Println(err) - _, err = copy(baseConfigPath, home+"/"+configName) + _, err = copy(baseConfigPath, targetConfig) if err != nil { pkg.Logger.Println(err) log.Fatal(err) } + } - err = viper.ReadInConfig() - if err != nil { - pkg.Logger.Println(err) - log.Fatal(err) - } + err = viper.ReadInConfig() + if err != nil { + pkg.Logger.Println(err) + log.Fatal(err) } } @@ -135,7 +137,7 @@ func cleanup() { func copy(src, dst string) (int64, error) { sourceFileStat, err := os.Stat(src) if err != nil { - return 0, err + return 0, fmt.Errorf("failed to stat source file %s: %w", src, err) } if !sourceFileStat.Mode().IsRegular() { @@ -144,15 +146,20 @@ func copy(src, dst string) (int64, error) { source, err := os.Open(src) if err != nil { - return 0, err + return 0, fmt.Errorf("failed to open source file %s: %w", src, err) } defer source.Close() - destination, err := os.Create(dst) + destination, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, sourceFileStat.Mode()) if err != nil { - return 0, err + return 0, fmt.Errorf("failed to create destination file %s: %w", dst, err) } defer destination.Close() - return io.Copy(destination, source) + bytesCopied, err := io.Copy(destination, source) + if err != nil { + return 0, fmt.Errorf("failed to copy data from %s to %s: %w", src, dst, err) + } + + return bytesCopied, nil } diff --git a/daemon/main_windows.go b/daemon/main_windows.go index df83753..daa0a98 100644 --- a/daemon/main_windows.go +++ b/daemon/main_windows.go @@ -9,6 +9,7 @@ import ( "log" "os" "os/signal" + "path" "syscall" "github.com/spf13/viper" @@ -87,29 +88,30 @@ func initConfig() { baseConfigPath := "C:/ProgramData/IITJ Autoproxy/autoproxy.config" - configName := ".autoproxy.config" + configName := ".autoproxyconfig" - viper.AddConfigPath(home) + targetConfig := path.Join(home, configName) - viper.SetConfigType("json") + viper.AddConfigPath(home) viper.SetConfigName(configName) + viper.SetConfigType("json") - err = viper.ReadInConfig() + _, err = os.Stat(targetConfig) if err != nil { pkg.Logger.Println(err) log.Println(err) - _, err = copy(baseConfigPath, home+"/"+configName) + _, err = copy(baseConfigPath, targetConfig) if err != nil { pkg.Logger.Println(err) log.Fatal(err) } + } - err = viper.ReadInConfig() - if err != nil { - pkg.Logger.Println(err) - log.Fatal(err) - } + err = viper.ReadInConfig() + if err != nil { + pkg.Logger.Println(err) + log.Fatal(err) } } @@ -130,7 +132,7 @@ func cleanup() { func copy(src, dst string) (int64, error) { sourceFileStat, err := os.Stat(src) if err != nil { - return 0, err + return 0, fmt.Errorf("failed to stat source file %s: %w", src, err) } if !sourceFileStat.Mode().IsRegular() { @@ -139,15 +141,20 @@ func copy(src, dst string) (int64, error) { source, err := os.Open(src) if err != nil { - return 0, err + return 0, fmt.Errorf("failed to open source file %s: %w", src, err) } defer source.Close() - destination, err := os.Create(dst) + destination, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, sourceFileStat.Mode()) if err != nil { - return 0, err + return 0, fmt.Errorf("failed to create destination file %s: %w", dst, err) } defer destination.Close() - return io.Copy(destination, source) + bytesCopied, err := io.Copy(destination, source) + if err != nil { + return 0, fmt.Errorf("failed to copy data from %s to %s: %w", src, dst, err) + } + + return bytesCopied, nil } diff --git a/pkg/keystore/keystore.go b/pkg/keystore/keystore.go deleted file mode 100644 index 82df589..0000000 --- a/pkg/keystore/keystore.go +++ /dev/null @@ -1,53 +0,0 @@ -package keystore - -import ( - "errors" - "log" - "os" - - "github.com/spf13/viper" - - "github.com/SaahilNotSahil/iitj-autoproxy/pkg" -) - -func init() { - home, err := os.UserHomeDir() - if err != nil { - pkg.Logger.Println(err) - log.Fatal(err) - } - - configName := ".autoproxy.config" - - viper.AddConfigPath(home) - - viper.SetConfigType("json") - viper.SetConfigName(configName) - - err = viper.ReadInConfig() - if err != nil { - pkg.Logger.Println(err) - log.Fatal(err) - } -} - -func Get(key string) (string, error) { - item := viper.GetString(key) - if item != "" { - return "", errors.New("Please set the value for " + key) - } - - return item, nil -} - -func Set(key string, value string) { - viper.Set(key, value) -} - -func Remove(key string) { - viper.Set(key, "") -} - -func Keys() []string { - return viper.AllKeys() -} diff --git a/pkg/utils.go b/pkg/utils.go index d02f3cd..41d91a5 100644 --- a/pkg/utils.go +++ b/pkg/utils.go @@ -19,7 +19,9 @@ func Login(url string, username string, password string) error { return err } - loginPageURL := viper.GetString("login_base_url") + "fgtauth?" + token + loginBaseURL := viper.GetString("login_base_url") + + loginPageURL := loginBaseURL + "fgtauth?" + token res, err := http.Get(loginPageURL) if err != nil { @@ -40,7 +42,7 @@ func Login(url string, username string, password string) error { magic = strs[0] } - referer := viper.GetString("login_base_url") + "login?" + token + referer := loginBaseURL + "login?" + token data := u.Values{} data.Add("4Tredir", referer) @@ -53,7 +55,7 @@ func Login(url string, username string, password string) error { req, err := http.NewRequest( "POST", - viper.GetString("login_base_url"), + loginBaseURL, strings.NewReader(data.Encode()), ) if err != nil { @@ -94,14 +96,14 @@ func Login(url string, username string, password string) error { viper.Set("token", token) - return nil + return viper.WriteConfig() } func Logout(token string) error { url := viper.GetString("login_base_url") + "logout?" + token _, err := http.Get(url) - + return err }