respect enviroment var no_proxy

This commit is contained in:
Harald Müller 2017-11-15 09:46:44 +01:00 committed by Yasuhiro Matsumoto
parent bd84bf7b04
commit fda8e5cb42

20
xmpp.go
View File

@ -68,6 +68,11 @@ func (c *Client) JID() string {
return c.jid return c.jid
} }
func containsIgnoreCase(s, substr string) bool {
s, substr = strings.ToUpper(s), strings.ToUpper(substr)
return strings.Contains(s, substr)
}
func connect(host, user, passwd string) (net.Conn, error) { func connect(host, user, passwd string) (net.Conn, error) {
addr := host addr := host
@ -81,16 +86,31 @@ func connect(host, user, passwd string) (net.Conn, error) {
if len(a) == 1 { if len(a) == 1 {
addr += ":5222" addr += ":5222"
} }
proxy := os.Getenv("HTTP_PROXY") proxy := os.Getenv("HTTP_PROXY")
if proxy == "" { if proxy == "" {
proxy = os.Getenv("http_proxy") proxy = os.Getenv("http_proxy")
} }
// test for no proxy
if proxy != "" {
noproxy := os.Getenv("no_proxy")
if noproxy != "" {
nplist := strings.Split(noproxy, ",")
for _, s := range nplist {
if containsIgnoreCase(addr, s) {
proxy = ""
break
}
}
}
}
if proxy != "" { if proxy != "" {
url, err := url.Parse(proxy) url, err := url.Parse(proxy)
if err == nil { if err == nil {
addr = url.Host addr = url.Host
} }
} }
c, err := net.Dial("tcp", addr) c, err := net.Dial("tcp", addr)
if err != nil { if err != nil {
return nil, err return nil, err