From 427bd88bbf529f5e9a5f2067eb6b400f064e0237 Mon Sep 17 00:00:00 2001 From: yashsinghcodes Date: Mon, 12 Aug 2024 19:52:49 +0530 Subject: [PATCH] added no_proxy --- backend/go-app/main.go | 70 +++++++++++++++++++++++++++------------ backend/go-app/walkoff.go | 30 +++-------------- 2 files changed, 52 insertions(+), 48 deletions(-) diff --git a/backend/go-app/main.go b/backend/go-app/main.go index 95a9dadf..f2407a37 100755 --- a/backend/go-app/main.go +++ b/backend/go-app/main.go @@ -397,6 +397,52 @@ func checkUsername(Username string) error { return nil } +func isNoProxy(rawURL string) bool { + noProxy := os.Getenv("NO_PROXY") + if noProxy == "" { + return false + } + + if noProxy == "*" { + return true + } + + noProxyList := strings.Split(noProxy, ",") + parsedURL, err := url.Parse(rawURL) + if err != nil { + return false + } + host := parsedURL.Hostname() + + for _,value := range noProxyList { + value = strings.TrimSpace(value) + + if host == value { + return true + } + if strings.HasPrefix(value, "*.") && strings.HasSuffix(host, value[2:]){ + return true + } + } + return false +} + +func checkGitProxy(cloneOptions *git.CloneOptions) *git.CloneOptions { + if os.Getenv("HTTP_PROXY") != "" && !isNoProxy(cloneOptions.URL){ + cloneOptions.ProxyOptions = gitProxy.ProxyOptions{ + URL: os.Getenv("HTTP_PROXY"), + } + } + + if os.Getenv("HTTPS_PROXY") != "" && !isNoProxy(cloneOptions.URL) { + cloneOptions.ProxyOptions = gitProxy.ProxyOptions{ + URL: os.Getenv("HTTPS_PROXY"), + } + } + + return cloneOptions +} + func createNewUser(username, password, role, apikey string, org shuffle.OrgMini) error { // Returns false if there is an issue // Use this for register @@ -4239,18 +4285,7 @@ func runInitEs(ctx context.Context) { } } - if os.Getenv("HTTP_PROXY") != "" { - cloneOptions.ProxyOptions = gitProxy.ProxyOptions{ - URL: os.Getenv("HTTP_PROXY"), - } - } - - if os.Getenv("HTTPS_PROXY") != "" { - cloneOptions.ProxyOptions = gitProxy.ProxyOptions{ - URL: os.Getenv("HTTPS_PROXY"), - } - } - + cloneOptions = checkGitProxy(cloneOptions) branch := os.Getenv("SHUFFLE_DOWNLOAD_AUTH_BRANCH") if len(branch) > 0 && branch != "master" && branch != "main" { @@ -4297,17 +4332,8 @@ func runInitEs(ctx context.Context) { URL: apis, } - if os.Getenv("HTTP_PROXY") != "" { - cloneOptions.ProxyOptions = gitProxy.ProxyOptions{ - URL: os.Getenv("HTTP_PROXY"), - } - } + cloneOptions = checkGitProxy(cloneOptions) - if os.Getenv("HTTPS_PROXY") != "" { - cloneOptions.ProxyOptions = gitProxy.ProxyOptions{ - URL: os.Getenv("HTTPS_PROXY"), - } - } _, err = git.Clone(storer, fs, cloneOptions) if err != nil { log.Printf("[ERROR] Failed loading repo %s into memory: %s", apis, err) diff --git a/backend/go-app/walkoff.go b/backend/go-app/walkoff.go index ac5dbe33..98bb5d09 100755 --- a/backend/go-app/walkoff.go +++ b/backend/go-app/walkoff.go @@ -34,7 +34,6 @@ import ( "github.com/go-git/go-billy/v5/memfs" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" - gitProxy "github.com/go-git/go-git/v5/plumbing/transport" "github.com/go-git/go-git/v5/storage/memory" http2 "gopkg.in/src-d/go-git.v4/plumbing/transport/http" @@ -2647,18 +2646,6 @@ func loadGithubWorkflows(url, username, password, userId, branch, orgId string) URL: url, } - if os.Getenv("HTTP_PROXY") != "" { - cloneOptions.ProxyOptions = gitProxy.ProxyOptions{ - URL: os.Getenv("HTTP_PROXY"), - } - } - - if os.Getenv("HTTPS_PROXY") != "" { - cloneOptions.ProxyOptions = gitProxy.ProxyOptions{ - URL: os.Getenv("HTTPS_PROXY"), - } - } - // FIXME: Better auth. if len(username) > 0 && len(password) > 0 { cloneOptions.Auth = &http2.BasicAuth{ @@ -2673,6 +2660,8 @@ func loadGithubWorkflows(url, username, password, userId, branch, orgId string) cloneOptions.ReferenceName = plumbing.ReferenceName(branch) } + cloneOptions = checkGitProxy(cloneOptions) + storer := memory.NewStorage() r, err := git.Clone(storer, fs, cloneOptions) if err != nil { @@ -3940,19 +3929,6 @@ func LoadSpecificApps(resp http.ResponseWriter, request *http.Request) { cloneOptions.ReferenceName = plumbing.ReferenceName(tmpBody.Branch) } - if os.Getenv("HTTP_PROXY") != "" { - cloneOptions.ProxyOptions = gitProxy.ProxyOptions{ - URL: os.Getenv("HTTP_PROXY"), - } - } - - if os.Getenv("HTTPS_PROXY") != "" { - cloneOptions.ProxyOptions = gitProxy.ProxyOptions{ - URL: os.Getenv("HTTPS_PROXY"), - } - } - - // FIXME: Better auth. if len(tmpBody.Field1) > 0 && len(tmpBody.Field2) > 0 { cloneOptions.Auth = &http2.BasicAuth{ @@ -3961,6 +3937,8 @@ func LoadSpecificApps(resp http.ResponseWriter, request *http.Request) { } } + cloneOptions = checkGitProxy(cloneOptions) + storer := memory.NewStorage() r, err := git.Clone(storer, fs, cloneOptions) if err != nil {