Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send smtp email through a proxy #27349

Closed
wants to merge 13 commits into from
Closed

Conversation

qdongxu
Copy link

@qdongxu qdongxu commented Sep 29, 2023

This PR addresses the issue #27348.
The notification mailing will leverage the environment variable, ALL_PROXY if it is exported.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Sep 29, 2023
@pull-request-size pull-request-size bot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Sep 29, 2023
@github-actions github-actions bot added the type/docs This PR mainly updates/creates documentation label Sep 29, 2023
@wxiaoguang
Copy link
Contributor

"modules/proxy" should be improved, instead of introducing a totally new mechanism.

@lunny
Copy link
Member

lunny commented Sep 29, 2023

Looks like it's not finished.

@silverwind
Copy link
Member

silverwind commented Sep 29, 2023

  1. Uppercase proxy vars are discouraged as per this, lowercase should be the only supported variant.
  2. I'm worried about all_proxy taking precedence over http_proxy and https_proxy once net/http: have DefaultTransport support all_proxy (SOCKS5) by default golang/go#31813 is implemented. Shouldn't it be called smtp_proxy?

@qdongxu
Copy link
Author

qdongxu commented Sep 30, 2023

Looks like it's not finished.

It's finished. the golang.org/x/net/proxy package support ALL_PROXY/all_proxy environment variable out of the box.

@qdongxu
Copy link
Author

qdongxu commented Sep 30, 2023

according to @wxiaoguang and @silverwind's comments, the strategy is to configure proxies in app.ini prior to the shell environment variable in order to behave clearly. the modules/proxy handles http_proxy only for git operations, but stmp can only leverage socks proxy. and it's a separate and irrelative decision to proxy git or mail. I'd like to add a [smtp_proxy] section, analogizing the [proxy] section.

@silverwind
Copy link
Member

A specific ini option sound much better then relying on all_proxy, which seems a too dangerous variable beause of it's possible influence on other protocols like http.

Apparently there is already a known bug in golang related to the variable: golang/go#16715

@wxiaoguang
Copy link
Contributor

but stmp can only leverage socks proxy.

That's not true. HTTP CONNECT proxy can also be used for any TCP protocol.

and it's a separate and irrelative decision to proxy git or mail.

From a system design view, the Gitea should have the consistent proxy behavior.

I'd like to add a [smtp_proxy] section, analogizing the [proxy] section.

Although at the moment I won't say "block" for it, but I really don't like this strange (inconsistent) behavior.

@pull-request-size pull-request-size bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Sep 30, 2023
@github-actions github-actions bot removed the type/docs This PR mainly updates/creates documentation label Sep 30, 2023
@wxiaoguang
Copy link
Contributor

Again, such design doesn't look good to me. Could you explain why the [proxy] couldn't be reused? From my understanding, it should work (by making some improvements) with SMTP.

image

@qdongxu
Copy link
Author

qdongxu commented Sep 30, 2023

  1. about @silverwind 's commnet, add [stmp_proxy]: enabled property. proxy will only take effect when it is set to true. either the [stmp_proxy]:proxy_url or the all_prxoy environment will be ignore when [stmp_proxy]: enabled is false.

  2. about @silverwind 's concern, all_proxy environment variables may impact other protocols, this will not happen as I checked the going library, package golang.org/x/net/http/httpproxy, which only reads http_proxy/https_proxy environment variables, while package golang.org/x/[email protected]/proxy, reads all_proxy only. there would be no conflict.

  3. about @wxiaoguang's comment http CONNECT can proxy for any TCP traffic, that's true after the connection is established, but the CONNECT here is a http method CONNECT instead of a TCP connect invocation. for the moment I evaluated golang proxy( socks5 proxy only ), golang httpproxy ( bind to http.Client) and https://github.com/elazarl/goproxy , I did not find the API to bind httpproxy for a non-http traffic, (while a socks proxy is for any tcp traffic, including a http traffic). maybe I am wrong but a relevant reference or example would be appreciated.

  4. about @wxiaoguang's comment, the Gitea should have consistent proxy behavior. I agree on it. but according to (4), the most consistent way is to make the properties in [proxy] and [smtp] section look similar and behave similarly.

  5. In [proxy] there's a ProxyHosts property, if host in ProxyHosts use proxyURL from config file , otherwise use http_proxy from the environment variable( and implies using the no_proxy environment variable ). This looks confusing, it's better to follow a unified http_proxy/no_proxy rule, but this can be addressed in another issue.

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Sep 30, 2023

I am pretty sure implementing the HTTP CONNECT proxy support is the right approach, and it's not difficult, like this: https://gist.github.com/jim3ma/3750675f141669ac4702bc9deaf31c6b .

HTTP CONNECT proxy is exactly (almost) the same as SOCKS5H proxy.

@pull-request-size pull-request-size bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 1, 2023
@qdongxu qdongxu changed the title send smtp email through socks5 proxy send smtp email through a proxy Oct 1, 2023
@qdongxu
Copy link
Author

qdongxu commented Oct 1, 2023

modules/proxy/httpsdialer.go is owned to https://gist.github.com/jim3ma/3750675f141669ac4702bc9deaf31c6b

updated to reuse [proxy]. and add a property SMTP_PROXY_ENABLED default as false, for backward compatibility.

Copy link
Contributor

@wxiaoguang wxiaoguang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Gitea's proxy system has many legacy problems.

I vote my approval here and will do some improvements in following PRs.

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Oct 1, 2023
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Oct 11, 2023
@silverwind
Copy link
Member

Still lint failures.

}
reqURL.Scheme = ""

req, err := http.NewRequest("CONNECT", reqURL.String(), nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems we could use httplib.NewRequest instead, which also brings the benefit of setting a common user-agent.

Copy link
Author

@qdongxu qdongxu Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for being late. As I check the httplib.NewRequest, It aims to wrap a normal HTTP request and it uses the HTTP proxy environment variable internally :

		TLSClientConfig: r.setting.TLSClientConfig,
		Proxy:           http.ProxyFromEnvironment,

while herein the proxy/httpsdialer.go just make a CONNECT to the proxy per se.

@silverwind silverwind self-requested a review October 16, 2023 17:47
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. labels Oct 16, 2023
…fication..

1. read configure from [smtp_proxy] at first.
2. if smtp_proxy:enabled is true but smtp_proxy:proxy_url is "", read
   all_proxy/ALL_PROXY, and NO_PROXY/no_proxy from enviroment variables.
3. if smtp_proxy:enabled is false ( default ), ignore both configration
   in app.ini and environment variables.
@qdongxu
Copy link
Author

qdongxu commented Nov 26, 2023

An alternative solution works : https://github.com/ankraft/smtpproxy
the issue and pr was closed thereby. thanks everyone reviewing the solution and code anyway.

@qdongxu qdongxu closed this Nov 26, 2023
@qdongxu qdongxu deleted the socks_proxy branch November 26, 2023 08:15
@go-gitea go-gitea locked as resolved and limited conversation to collaborators Feb 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/need 1 This PR needs approval from one additional maintainer to be merged. modifies/docs size/L Denotes a PR that changes 100-499 lines, ignoring generated files. type/docs This PR mainly updates/creates documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants