Skip to content

Commit 78962b0

Browse files
committed
feat: internet cmd and fix bugs
1 parent 6f68ac3 commit 78962b0

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
ihdu_cli
22
hdu_cli
33
.hdu_cli.yaml
4-
.idea
4+
.idea
5+
/vendor

cmd/net/cmd.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"github.com/hduhelp/hdu-cli/pkg/srun"
66
"github.com/hduhelp/hdu-cli/pkg/table"
7+
"github.com/parnurzeal/gorequest"
78
"github.com/spf13/cobra"
89
"github.com/spf13/viper"
910
"log"
@@ -30,7 +31,14 @@ func init() {
3031
cobra.CheckErr(viper.BindPFlag("net.endpoint", Cmd.PersistentFlags().Lookup("endpoint")))
3132

3233
Cmd.PersistentFlags().StringP("acid", "a", "", "ac_id of srun")
33-
viper.SetDefault("net.acid", "0")
34+
resp, _, errs := gorequest.New().Get(viper.GetString("net.endpoint")).End()
35+
if errs != nil {
36+
viper.SetDefault("net.acid", "0")
37+
} else if acid := resp.Request.URL.Query().Get("ac_id"); acid != "" {
38+
viper.SetDefault("net.acid", acid)
39+
} else {
40+
viper.SetDefault("net.acid", "0")
41+
}
3442
cobra.CheckErr(viper.BindPFlag("net.acid", Cmd.PersistentFlags().Lookup("acid")))
3543

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

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

43-
Cmd.AddCommand(infoCmd, loginCmd, logoutCmd)
51+
Cmd.AddCommand(infoCmd, loginCmd, logoutCmd, internetCmd)
4452
}
4553

4654
// infoCmd represents the info command
@@ -91,7 +99,6 @@ var loginCmd = &cobra.Command{
9199
if _, err := portalServer.PortalLogout(); err != nil {
92100
log.Println(err)
93101
}
94-
continue
95102
}
96103

97104
//检测是否登录成功,如果登录过期则重新登录
@@ -136,3 +143,12 @@ var logoutCmd = &cobra.Command{
136143
cobra.CheckErr(err)
137144
},
138145
}
146+
147+
// internetCmd represents the logout command
148+
var internetCmd = &cobra.Command{
149+
Use: "internet",
150+
Short: "check if connect to the internet",
151+
Run: func(cmd *cobra.Command, args []string) {
152+
fmt.Println(portalServer.Internet())
153+
},
154+
}

pkg/srun/internet.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
package srun
22

33
import (
4+
"fmt"
45
"github.com/parnurzeal/gorequest"
5-
"log"
6-
"net/http"
76
"net/url"
87
)
98

109
// Internet 通过Http请求是否被302到 srun登陆地址 来判断是否能访问互联网
1110
func (s PortalServer) Internet() bool {
12-
connect := true
1311
reqUrl, _ := url.ParseRequestURI(s.internetCheck)
14-
_, _, errs := gorequest.New().Get(reqUrl.String()).
15-
RedirectPolicy(func(req gorequest.Request, via []gorequest.Request) error {
16-
connect = req.URL.Hostname() != reqUrl.Hostname()
17-
return http.ErrUseLastResponse
18-
}).End()
19-
if errs != nil || len(errs) != 0 {
20-
log.Printf("Internet check failed: %v", errs)
21-
connect = false
12+
resp, _, errs := gorequest.New().Get(reqUrl.String()).End()
13+
if errs != nil {
14+
fmt.Println(errs)
15+
return false
2216
}
23-
return connect
17+
if resp.Request.URL.Hostname() == reqUrl.Hostname() {
18+
return true
19+
}
20+
return false
2421
}

pkg/srun/portal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func New(endpoint, acID string) *PortalServer {
1414
acID: acID,
1515
jsonpCallback: "jQuery112403771213770126085_" + timestampStr,
1616
timestampStr: timestampStr,
17-
internetCheck: "http://www.baidu.com",
17+
internetCheck: "https://www.baidu.com",
1818
}
1919
}
2020

0 commit comments

Comments
 (0)