Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] support auto login and logout at night . #35

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
TestCommand,
VersionCommand,
UpdateCommand,
NightCommand,
},
Action: func(ctx *cli.Context) error {
if ctx.NArg() != 0 {
Expand Down
69 changes: 69 additions & 0 deletions pkg/cmd/night.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package cmd

import (
"errors"
"github.com/neucn/ipgw/pkg/console"
"github.com/neucn/ipgw/pkg/handler"
"github.com/urfave/cli/v2"
"time"
)

var (
NightCommand = &cli.Command{
Name: "night",
Usage: "auto login and logout at night",
Action: func(ctx *cli.Context) error {
var hasLogin bool = false
const retryNum int = 5

const freeStartHour = 0
const loginDelayMinutes = 5

const freeEndHour = 6
const logoutAdvanceMinutes = 5

console.InfoL("Please check if the time on the current computer is correct!!!")

for {
console.Info("\rThe current datetime is:", time.Now())
//fmt.Println("\ntest")

if (time.Now().Hour() >= freeStartHour && time.Now().Minute() >= loginDelayMinutes) && (time.Now().Hour() < freeEndHour && time.Now().Minute() < 60-logoutAdvanceMinutes) {
for i := 0; !hasLogin && i < retryNum; i++ {
err := loginUseDefaultAccount(ctx)
if err == nil {
hasLogin = true
console.InfoL("\nLogin successfully at", time.Now().String())
}
}

}

if time.Now().Hour() >= freeEndHour-1 && time.Now().Minute() > 60-logoutAdvanceMinutes {
for i := 0; hasLogin && i < retryNum; i++ {
h := handler.NewIpgwHandler()
connected, loggedIn := h.IsConnectedAndLoggedIn()
if !connected {
return errors.New("not in campus network")
}
if !loggedIn {
console.InfoL("not logged in yet")
}
info := h.GetInfo()
if err := h.Logout(); err != nil {
console.InfoL("fail to logout account '%s':\n\t%v", info.Username, err)
} else {
hasLogin = false
console.InfoL("\nLogout successfully at", time.Now().String())
}

}

}
time.Sleep(1 * time.Second)

}
return nil
},
}
)