Skip to content

Commit

Permalink
feat: internet cmd and fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
fdkevin0 committed Mar 9, 2022
1 parent 6f68ac3 commit 78962b0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ihdu_cli
hdu_cli
.hdu_cli.yaml
.idea
.idea
/vendor
22 changes: 19 additions & 3 deletions cmd/net/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/hduhelp/hdu-cli/pkg/srun"
"github.com/hduhelp/hdu-cli/pkg/table"
"github.com/parnurzeal/gorequest"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
Expand All @@ -30,7 +31,14 @@ func init() {
cobra.CheckErr(viper.BindPFlag("net.endpoint", Cmd.PersistentFlags().Lookup("endpoint")))

Cmd.PersistentFlags().StringP("acid", "a", "", "ac_id of srun")
viper.SetDefault("net.acid", "0")
resp, _, errs := gorequest.New().Get(viper.GetString("net.endpoint")).End()
if errs != nil {
viper.SetDefault("net.acid", "0")
} else if acid := resp.Request.URL.Query().Get("ac_id"); acid != "" {
viper.SetDefault("net.acid", acid)
} else {
viper.SetDefault("net.acid", "0")
}
cobra.CheckErr(viper.BindPFlag("net.acid", Cmd.PersistentFlags().Lookup("acid")))

loginCmd.Flags().StringP("username", "u", "", "username of srun")
Expand All @@ -40,7 +48,7 @@ func init() {

logoutCmd.Flags().StringP("username", "u", "", "username of srun")

Cmd.AddCommand(infoCmd, loginCmd, logoutCmd)
Cmd.AddCommand(infoCmd, loginCmd, logoutCmd, internetCmd)
}

// infoCmd represents the info command
Expand Down Expand Up @@ -91,7 +99,6 @@ var loginCmd = &cobra.Command{
if _, err := portalServer.PortalLogout(); err != nil {
log.Println(err)
}
continue
}

//检测是否登录成功,如果登录过期则重新登录
Expand Down Expand Up @@ -136,3 +143,12 @@ var logoutCmd = &cobra.Command{
cobra.CheckErr(err)
},
}

// internetCmd represents the logout command
var internetCmd = &cobra.Command{
Use: "internet",
Short: "check if connect to the internet",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(portalServer.Internet())
},
}
21 changes: 9 additions & 12 deletions pkg/srun/internet.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
package srun

import (
"fmt"
"github.com/parnurzeal/gorequest"
"log"
"net/http"
"net/url"
)

// Internet 通过Http请求是否被302到 srun登陆地址 来判断是否能访问互联网
func (s PortalServer) Internet() bool {
connect := true
reqUrl, _ := url.ParseRequestURI(s.internetCheck)
_, _, errs := gorequest.New().Get(reqUrl.String()).
RedirectPolicy(func(req gorequest.Request, via []gorequest.Request) error {
connect = req.URL.Hostname() != reqUrl.Hostname()
return http.ErrUseLastResponse
}).End()
if errs != nil || len(errs) != 0 {
log.Printf("Internet check failed: %v", errs)
connect = false
resp, _, errs := gorequest.New().Get(reqUrl.String()).End()
if errs != nil {
fmt.Println(errs)
return false
}
return connect
if resp.Request.URL.Hostname() == reqUrl.Hostname() {
return true
}
return false
}
2 changes: 1 addition & 1 deletion pkg/srun/portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func New(endpoint, acID string) *PortalServer {
acID: acID,
jsonpCallback: "jQuery112403771213770126085_" + timestampStr,
timestampStr: timestampStr,
internetCheck: "http://www.baidu.com",
internetCheck: "https://www.baidu.com",
}
}

Expand Down

0 comments on commit 78962b0

Please sign in to comment.