Skip to content

Commit

Permalink
feat(internet): handle internet check error
Browse files Browse the repository at this point in the history
  • Loading branch information
fdkevin0 committed Dec 9, 2021
1 parent 5533a95 commit 6f68ac3
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/srun/internet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ package srun

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

// Internet 通过Http请求是否被302到 srun登陆地址 来判断是否能访问互联网
func (s PortalServer) Internet() bool {
redirected := false
connect := true
reqUrl, _ := url.ParseRequestURI(s.internetCheck)
gorequest.New().Get(reqUrl.String()).
_, _, errs := gorequest.New().Get(reqUrl.String()).
RedirectPolicy(func(req gorequest.Request, via []gorequest.Request) error {
redirected = req.URL.Hostname() == reqUrl.Hostname()
connect = req.URL.Hostname() != reqUrl.Hostname()
return http.ErrUseLastResponse
}).End()
return redirected
if errs != nil || len(errs) != 0 {
log.Printf("Internet check failed: %v", errs)
connect = false
}
return connect
}

0 comments on commit 6f68ac3

Please sign in to comment.