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
}
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) {
addr := host
@ -81,16 +86,31 @@ func connect(host, user, passwd string) (net.Conn, error) {
if len(a) == 1 {
addr += ":5222"
}
proxy := os.Getenv("HTTP_PROXY")
if 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 != "" {
url, err := url.Parse(proxy)
if err == nil {
addr = url.Host
}
}
c, err := net.Dial("tcp", addr)
if err != nil {
return nil, err