Skip to content

Commit

Permalink
Merge pull request #18 from SharzyL/master
Browse files Browse the repository at this point in the history
v6-only systemd services, XDG_CONFIG_HOME support
  • Loading branch information
ZenithalHourlyRate authored Jul 5, 2021
2 parents eae548d + aee056f commit c865c72
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 27 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ USAGE:
auth-thu [options] online [online_options]
VERSION:
2.1
2.1.1
AUTHORS:
Yuxiang Zhang <[email protected]>
Expand Down Expand Up @@ -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
Expand All @@ -74,8 +75,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`.

```
{
Expand Down Expand Up @@ -103,9 +104,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 `[email protected]` and store the config in `~/.auth-thu`.

For other authentication like IPv6, you can copy these service files and modify them correspondingly.
Or, to be more secure, you can choose `[email protected]` and store the config in home directory.

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).

Expand Down
64 changes: 44 additions & 20 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -228,13 +251,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
Expand Down Expand Up @@ -419,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`"},
Expand Down Expand Up @@ -479,6 +502,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,
},
Expand Down
1 change: 0 additions & 1 deletion docs/goauthing.service
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions docs/goauthing6.service
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions docs/[email protected]
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion docs/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c865c72

Please sign in to comment.