From 62c07f6b366c9b7e55effcbdd82e912d2b6c96b0 Mon Sep 17 00:00:00 2001 From: SharzyL Date: Mon, 5 Jul 2021 20:25:35 +0800 Subject: [PATCH 1/4] add v6 only systemd services --- cli/main.go | 13 +++++++------ docs/goauthing.service | 1 - docs/goauthing6.service | 14 ++++++++++++++ docs/goauthing6@.service | 15 +++++++++++++++ docs/goauthing@.service | 1 - 5 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 docs/goauthing6.service create mode 100644 docs/goauthing6@.service diff --git a/cli/main.go b/cli/main.go index 84a7448..5d41fb0 100644 --- a/cli/main.go +++ b/cli/main.go @@ -228,13 +228,13 @@ func keepAliveLoop(c *cli.Context, campusOnly bool) (ret error) { } }() - v4Target := targetOutside - if campusOnly { - v4Target = targetInside - } for { - if ret = accessTarget(v4Target, false); ret != nil { - ret = fmt.Errorf("accessing %s failed (re-login might be required): %w", v4Target, ret) + target := targetOutside + if campusOnly || settings.V6 { + target = targetInside + } + if ret = accessTarget(target, settings.V6); ret != nil { + ret = fmt.Errorf("accessing %s failed (re-login might be required): %w", target, ret) break } // Consumes ~5MB per day @@ -479,6 +479,7 @@ func main() { Usage: "Keep your computer online", Flags: []cli.Flag{ &cli.BoolFlag{Name: "auth, a", Usage: "keep the Auth online only"}, + &cli.BoolFlag{Name: "ipv6, 6", Usage: "keep only ipv6 connection online"}, }, Action: cmdKeepalive, }, diff --git a/docs/goauthing.service b/docs/goauthing.service index d95adc2..194df3b 100644 --- a/docs/goauthing.service +++ b/docs/goauthing.service @@ -7,7 +7,6 @@ ExecStartPre=-/usr/local/bin/auth-thu -c /etc/goauthing.json -D deauth ExecStartPre=-/usr/local/bin/auth-thu -c /etc/goauthing.json -D auth ExecStartPre=-/usr/local/bin/auth-thu -c /etc/goauthing.json -D login ExecStart=/usr/local/bin/auth-thu -c /etc/goauthing.json -D online -StandardOutput=journal User=nobody Restart=always RestartSec=5 diff --git a/docs/goauthing6.service b/docs/goauthing6.service new file mode 100644 index 0000000..ddfeeac --- /dev/null +++ b/docs/goauthing6.service @@ -0,0 +1,14 @@ +[Unit] +Description=Authenticating utility for auth.tsinghua.edu.cn +StartLimitIntervalSec=0 + +[Service] +ExecStartPre=-/usr/local/bin/auth-thu -c /etc/goauthing.json -D deauth -6 +ExecStartPre=-/usr/local/bin/auth-thu -c /etc/goauthing.json -D auth -6 +ExecStart=/usr/local/bin/auth-thu -c /etc/goauthing.json -D online -6 +User=nobody +Restart=always +RestartSec=5 + +[Install] +WantedBy = multi-user.target diff --git a/docs/goauthing6@.service b/docs/goauthing6@.service new file mode 100644 index 0000000..a98a7d2 --- /dev/null +++ b/docs/goauthing6@.service @@ -0,0 +1,15 @@ +[Unit] +Description=Authenticating utility for auth.tsinghua.edu.cn +StartLimitIntervalSec=0 + +[Service] +# default config is in ~/.auth-thu +ExecStartPre=-/usr/local/bin/auth-thu -D deauth -6 +ExecStartPre=-/usr/local/bin/auth-thu -D auth -6 +ExecStart=/usr/local/bin/auth-thu -D online -6 +User=%i +Restart=always +RestartSec=5 + +[Install] +WantedBy = multi-user.target diff --git a/docs/goauthing@.service b/docs/goauthing@.service index b3ac6f6..c1bbafd 100644 --- a/docs/goauthing@.service +++ b/docs/goauthing@.service @@ -8,7 +8,6 @@ ExecStartPre=-/usr/local/bin/auth-thu -D deauth ExecStartPre=-/usr/local/bin/auth-thu -D auth ExecStartPre=-/usr/local/bin/auth-thu -D login ExecStart=/usr/local/bin/auth-thu -D online -StandardOutput=journal User=%i Restart=always RestartSec=5 From 49f5d52cad393209502acfead2aecdb5b0701386 Mon Sep 17 00:00:00 2001 From: SharzyL Date: Mon, 5 Jul 2021 21:06:43 +0800 Subject: [PATCH 2/4] add XDG_CONFIG_HOME support --- README.md | 4 ++-- cli/main.go | 49 ++++++++++++++++++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 623174d..54a5e3a 100644 --- a/README.md +++ b/README.md @@ -74,8 +74,8 @@ GLOBAL OPTIONS: --version, -v print the version ``` +The program looks for a config file in `$XDG_CONFIG_HOME/auth-thu`, `~/.config/auth-thu`, `~/.auth-thu` in order. Write a config file to store your username & password or other options in the following format. -The default location of config file is `~/.auth-thu`. ``` { @@ -103,7 +103,7 @@ Note that the program should have access to the configure file. For `goauthing.s setfacl -m u:nobody:r /etc/goauthing.json ``` -Or, to be more secure, you can choose `goauthing@.service` and store the config in `~/.auth-thu`. +Or, to be more secure, you can choose `goauthing@.service` and store the config in home directory. For other authentication like IPv6, you can copy these service files and modify them correspondingly. diff --git a/cli/main.go b/cli/main.go index 5d41fb0..83785a2 100644 --- a/cli/main.go +++ b/cli/main.go @@ -145,27 +145,50 @@ func setLoggerLevel(debug bool, daemon bool) { } } +func locateConfigFile(c *cli.Context) (cf string) { + cf = c.GlobalString("config-file") + if len(cf) != 0 { + return + } + + xdgConfigHome := os.Getenv("XDG_CONFIG_HOME") + homedir, _ := os.UserHomeDir() + if len(xdgConfigHome) == 0 { + xdgConfigHome = path.Join(homedir, ".config") + } + cf = path.Join(xdgConfigHome, "auth-thu") + _, err := os.Stat(cf) + if !os.IsNotExist(err) { + return + } + + cf = path.Join(homedir, ".auth-thu") + _, err = os.Stat(cf) + if !os.IsNotExist(err) { + return + } + + return "" +} + func parseSettings(c *cli.Context) (err error) { if c.Bool("help") { cli.ShowAppHelpAndExit(c, 0) } // Early debug flag setting (have debug messages when access config file) setLoggerLevel(c.GlobalBool("debug"), c.GlobalBool("daemonize")) - cf := c.GlobalString("config-file") - throwConfigFileError := true - if len(cf) == 0 { - // If run in daemon mode, config file is a must - throwConfigFileError = c.GlobalBool("daemonize") - homedir, _ := os.UserHomeDir() - cf = path.Join(homedir, ".auth-thu") - err = parseSettingsFile(cf) - } else { - err = parseSettingsFile(cf) + + cf := locateConfigFile(c) + if len(cf) == 0 && c.GlobalBool("daemonize") { + return fmt.Errorf("cannot find config file (it is necessary in daemon mode)") } - if throwConfigFileError && err != nil { - return err + if len(cf) != 0 { + err = parseSettingsFile(cf) + if err != nil { + return err + } + mergeCliSettings(c) } - mergeCliSettings(c) // Late debug flag setting setLoggerLevel(settings.Debug, settings.Daemon) return From e21e4f86f2f9b4650b0e57b2e8f8ad2c2531ac0f Mon Sep 17 00:00:00 2001 From: SharzyL Date: Mon, 5 Jul 2021 21:07:42 +0800 Subject: [PATCH 3/4] update doc of online -6 option --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 54a5e3a..6d96326 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ COMMANDS: online Keep your computer online OPTIONS: --auth, -a keep the Auth online only + --ipv6, -6 keep only ipv6 connection online GLOBAL OPTIONS: --username name, -u name your TUNET account name From aee056fdead1a7f3540fda4c43446014eb8e993c Mon Sep 17 00:00:00 2001 From: SharzyL Date: Mon, 5 Jul 2021 21:27:55 +0800 Subject: [PATCH 4/4] bump version, update doc --- README.md | 4 +--- cli/main.go | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6d96326..49403be 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ USAGE: auth-thu [options] online [online_options] VERSION: - 2.1 + 2.1.1 AUTHORS: Yuxiang Zhang @@ -106,8 +106,6 @@ setfacl -m u:nobody:r /etc/goauthing.json Or, to be more secure, you can choose `goauthing@.service` and store the config in home directory. -For other authentication like IPv6, you can copy these service files and modify them correspondingly. - It is suggested that one configures and runs it manually first with `debug` flag turned on, which ensures the correctness of one's config, then start it as system service. For `daemonize` flag, it forces the program to only log errors, hence debugging should be done earlier and manually. `daemonize` is automatically turned on for system service (ref to associated systemd unit files). ## Build diff --git a/cli/main.go b/cli/main.go index 83785a2..7132e94 100644 --- a/cli/main.go +++ b/cli/main.go @@ -442,7 +442,7 @@ func main() { auth-thu [options] logout auth-thu [options] online [online_options]`, Usage: "Authenticating utility for Tsinghua", - Version: "2.1", + Version: "2.1.1", HideHelp: true, Flags: []cli.Flag{ &cli.StringFlag{Name: "username, u", Usage: "your TUNET account `name`"},