From fda8e5cb42c3ebc9a983b882146f203cd9858766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Harald=20M=C3=BCller?= Date: Wed, 15 Nov 2017 09:46:44 +0100 Subject: [PATCH] respect enviroment var no_proxy --- xmpp.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/xmpp.go b/xmpp.go index 6340b9c..64aed6f 100644 --- a/xmpp.go +++ b/xmpp.go @@ -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