mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 10:45:47 +08:00
Don't crash when proxy is unavailable.
This commit is contained in:
parent
890030b3ef
commit
2326d55f73
@ -15,8 +15,11 @@
|
|||||||
package proxy
|
package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
"golang.org/x/net/proxy"
|
"golang.org/x/net/proxy"
|
||||||
@ -30,10 +33,34 @@ func InitHttpClient() {
|
|||||||
DefaultHttpClient = http.DefaultClient
|
DefaultHttpClient = http.DefaultClient
|
||||||
|
|
||||||
// use proxy
|
// use proxy
|
||||||
|
ProxyHttpClient = getProxyHttpClient()
|
||||||
|
}
|
||||||
|
|
||||||
|
func isAddressOpen(address string) bool {
|
||||||
|
timeout := time.Millisecond * 100
|
||||||
|
conn, err := net.DialTimeout("tcp", address, timeout)
|
||||||
|
if err != nil {
|
||||||
|
// cannot connect to address, proxy is not active
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if conn != nil {
|
||||||
|
defer conn.Close()
|
||||||
|
fmt.Printf("Socks5 proxy enabled: %s\n", address)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func getProxyHttpClient() *http.Client {
|
||||||
httpProxy := beego.AppConfig.String("httpProxy")
|
httpProxy := beego.AppConfig.String("httpProxy")
|
||||||
if httpProxy == "" {
|
if httpProxy == "" {
|
||||||
ProxyHttpClient = DefaultHttpClient
|
return &http.Client{}
|
||||||
return
|
}
|
||||||
|
|
||||||
|
if !isAddressOpen(httpProxy) {
|
||||||
|
return &http.Client{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://stackoverflow.com/questions/33585587/creating-a-go-socks5-client
|
// https://stackoverflow.com/questions/33585587/creating-a-go-socks5-client
|
||||||
@ -43,16 +70,9 @@ func InitHttpClient() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tr := &http.Transport{Dial: dialer.Dial}
|
tr := &http.Transport{Dial: dialer.Dial}
|
||||||
ProxyHttpClient = &http.Client{
|
return &http.Client{
|
||||||
Transport: tr,
|
Transport: tr,
|
||||||
}
|
}
|
||||||
|
|
||||||
//resp, err2 := ProxyHttpClient.Get("https://google.com")
|
|
||||||
//if err2 != nil {
|
|
||||||
// panic(err2)
|
|
||||||
//}
|
|
||||||
//defer resp.Body.Close()
|
|
||||||
//println("Response status: %s", resp.Status)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetHttpClient(url string) *http.Client {
|
func GetHttpClient(url string) *http.Client {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user