Improve proxy code.

This commit is contained in:
Yang Luo 2021-09-06 01:13:13 +08:00
parent af2d7b32f9
commit 69ea8e4ab5
5 changed files with 12 additions and 22 deletions

View File

@ -14,14 +14,6 @@
package controllers
import (
"net/http"
"github.com/casbin/casnode/object"
)
var HttpClient *http.Client
type Response struct {
Status string `json:"status"`
Msg string `json:"msg"`
@ -29,10 +21,6 @@ type Response struct {
Data2 interface{} `json:"data2"`
}
func InitHttpClient() {
HttpClient = object.GetProxyHttpClient()
}
func (c *ApiController) ResponseOk(data ...interface{}) {
resp := Response{Status: "ok"}
switch len(data) {
@ -70,7 +58,7 @@ func (c *ApiController) RequireSignedIn() bool {
func (c *ApiController) RequireAdmin() bool {
user := c.GetSessionUser()
if user == nil || !object.CheckIsAdmin(user) {
if user == nil || !user.IsAdmin {
c.ResponseError("this operation requires admin privilege")
return true
}

View File

@ -19,7 +19,6 @@ import (
"github.com/astaxie/beego/plugins/cors"
_ "github.com/astaxie/beego/session/redis"
"github.com/casbin/casnode/casdoor"
"github.com/casbin/casnode/controllers"
"github.com/casbin/casnode/object"
"github.com/casbin/casnode/routers"
"github.com/casbin/casnode/service"
@ -28,9 +27,8 @@ import (
func main() {
object.InitAdapter()
object.InitHttpClient()
casdoor.InitCasdoorAdapter()
controllers.InitHttpClient()
object.HttpClient = controllers.HttpClient
service.InitDictionary()
util.InitSegmenter()
object.InitForumBasicInfo()

View File

@ -18,7 +18,6 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
@ -33,7 +32,6 @@ type BasicInfo struct {
var fileDate, version string
var onlineMemberNum, highestOnlineNum int
var HttpClient *http.Client
func InitForumBasicInfo() {
GetForumVersion()

View File

@ -35,9 +35,9 @@ func (n Node) SyncFromGoogleGroup() {
}
group := crawler.NewGoogleGroup(n.MailingList, n.GoogleGroupCookie)
conversations := group.GetAllConversations(*HttpClient)
conversations := group.GetAllConversations(*httpClient)
for _, conv := range conversations {
messages := conv.GetAllMessages(*HttpClient, true)
messages := conv.GetAllMessages(*httpClient, true)
if len(messages) < 1 {
fmt.Printf("Google Groups Crawler: Getting messages from Google Group: %s for node: %s failed, please check your cookie.\n", group.GroupName, n.Id)
break

View File

@ -24,6 +24,12 @@ import (
"golang.org/x/net/proxy"
)
var httpClient *http.Client
func InitHttpClient() {
httpClient = getProxyHttpClient()
}
func isAddressOpen(address string) bool {
timeout := time.Millisecond * 100
conn, err := net.DialTimeout("tcp", address, timeout)
@ -41,9 +47,9 @@ func isAddressOpen(address string) bool {
return false
}
func GetProxyHttpClient() *http.Client {
func getProxyHttpClient() *http.Client {
httpProxy := beego.AppConfig.String("httpProxy")
if len(httpProxy) == 0 {
if httpProxy == "" {
return &http.Client{}
}