-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
AutoTLS with non-80 HTTP Port? #1577
Comments
Hello @AlbinoGeek, First of all, just for your knowedge the Now let's deep into your question, you can do it through After that, you have two options to handle the http to https redirection:
import "github.com/kataras/iris/v12/core/host" // import that package.
// to start a new server listening at :8080 and redirects
// to the secure address, then:
target, _ := url.Parse("https://127.0.0.1:8080")
go host.NewRedirection("127.0.0.1:8080", target, iris.StatusMovedPermanently).ListenAndServe()
app := iris.New()
// ...
app.Run(iris.AutoTLS(....), TLSNoRedirect)
2.1 middleware: func redirectToHTTPS(ctx iris.Context) {
if !ctx.Request().TLS != nil /* or ctx.Request().ProtoMajor != 2 */ {
ctx.Redirect(strings.Replace(ctx.FullRequestURI(),"http","https",1), iris.StatusMovedPermanently)
return
}
ctx.Next()
}
app.Use(redirectToHTTPS) 2.2 wrapper: app.WrapRouter(func(w http.ResponseWriter, r *http.Request, router http.HandlerFunc){
// checks here, if it's not https then redirect and return otherwise run the router with router(w,r )
}) You can always modify hosts through Please tell me if that helped you. |
@kataras In these examples it is unclear which is the HTTP port and which is the HTTPS port, as you only use one port within each of the examples. It is my understanding that they cannot be on the same port, but perhaps this is old knowledge. [ and yes, I am using SocketSharding, I just removed it for simplicity of reproducing the issue without adding complexity. ] |
@AlbinoGeek yes sorry, it was a copy-paste from an iris example, just change the port you are using for the HTTP server, here is the one: target, _ := url.Parse("https://127.0.0.1:8080") // PORT FOR HTTPS
go host.NewRedirection("127.0.0.1:OTHERPORT_FOR_HTTP", target, iris.StatusMovedPermanently).ListenAndServe() If you don't need HTTP to HTTPS redirection because other software does that then you can skip this part and just pass the |
@kataras I cannot use
|
@AlbinoGeek That's why I am asking the Iris version (in the .github issues templates), you don't use the Please always try replicate your issue using the $ cd your_project
$ go get -u github.com/kataras/iris/v12@master
$ go run . EDIT: That error came because he registered the |
It is worth noting that // NAT in front of me points 8443 -> 443 and 8080 -> 80
target, _ := url.Parse("https://127.0.0.1:8443")
go host.NewRedirection("127.0.0.1:8080", target, iris.StatusMovedPermanently).ListenAndServe()
app.Run(iris.AutoTLS(":8443", "my.domain.here", "[email protected]"), iris.WithSocketSharding) results in: (when not running with the permissions to bind on :80, because this code shouldn't require that)
All of this was tested with what was master, as of: After updating to master again, the results are still reproducable. |
OK that's a good start, let me see what's wrong here, will get back to you soon! |
And for sanity sake, to exclude the NAT from the issue, the following code works: var (
plainHost = pflag.String("host", "0.0.0.0", "HTTP Hostname")
plainPort = pflag.Int("port", 8080, "HTTP Port")
)
func main() {
pflag.Parse()
app := iris.Default()
app.Use(requestid.New())
app.Use(iris.Compression)
app.Get("/health", func(c iris.Context) {
c.JSON(map[string]interface{}{
"time": time.Now().Unix(),
})
})
app.Logger().SetLevel("debug")
app.Run(iris.Addr(fmt.Sprintf("%s:%d", *plainHost, *plainPort)))
} Means the site can be reached at |
@AlbinoGeek, did you try using letsencrypt with raw
The only code you should check: Lines 342 to 349 in da029d6
And autocert.Manager: https://github.com/golang/crypto/blob/123391ffb6de907695e1066dc40c1ff09322aeb6/acme/autocert/autocert.go#L368-L383 |
Totally fair, this makes it an upstream bug (for acme/autocert) -- because my particular setup requires a non-80 HTTP port, due to NAT. [And hey, the exhaustion of IPv4 addresses seems like reason enough to support this.] |
@AlbinoGeek sorry for confusion but I gave you the ]correct code in the first place, you messup with the parentheses... np, it's normal. All that happens because The correct: Example file: iris/_examples/http-server/listen-letsencrypt/main.go Lines 28 to 31 in c4843a4
|
The following code: target, _ := url.Parse("https://my.domain/")
go host.NewRedirection("0.0.0.0:8080", target, iris.StatusMovedPermanently).ListenAndServe()
app.Run(iris.AutoTLS("0.0.0.0:8443", "my.domain", "[email protected]", iris.TLSNoRedirect), iris.WithSocketSharding) Results in:
That's a partial fix at least, the redirect "worked", but ACME failed. Also worth noting, I can't create a bug upstream because By the way, this above code didn't bind on port |
@AlbinoGeek That's a letsencrypt issue and wouldn't work on raw
Yes, it didn't bind to :80 because you passed the
Yes it's not upstream thing, I've edited my comment like 10 seconds later, see above. |
OK now we solve that this is not an Iris problem and I can rest in peace, let's see how we can help you on that, do you prefer continueing that through a private room on our chat? |
Effectively that "solves" the original post ask, but still I know LetsEncrypt supports my domain/computer because nginx and certbot works, but AutoTLS does not. And yes, I'll sign on to the chat. |
Yes I had some issues with letsencrypt too, had to pass a real domain that directly binds to my remote computer, if you have a free domain that we can bind to one of my remote machines, we can run it together and see that it's working without erorrs. |
I'm on chat. I've been trying to do this using real domains, just don't want them showing up here (SEO and such, an unreleased product, etc.) And yes, I have some other domains we can test with as well. |
That's fine, I know that I was being there. That's why I asked for a private room :) I am in. |
For those that might find this issue later, here is the current solution: // Use your externally accessible HTTPS port here
target, _ := url.Parse("https://my.domain/")
// Use your internally accessible HTTP port here
go host.NewRedirection("0.0.0.0:8080", target, iris.StatusMovedPermanently).ListenAndServe()
// Use your internally accessible HTTPS port here
app.Run(iris.AutoTLS("0.0.0.0:8443", "my.domain", "[email protected]", iris.TLSNoRedirect))
// Note however, because of golang/crypto's implementation of certmanager,
// it is NOT currently possible to change the external port 80. I will PR this into an example shortly. |
Hello there!
This is a purely documentation / example question:
Why?
Because there is NAT in front of iris that routes
internal:8080
toexternal:80
Current Code
Looking through the code some I the two ways to create a Runner that conflict here:
iris/iris.go
Lines 658 to 667 in da029d6
iris/iris.go
Lines 584 to 590 in da029d6
They are simply too abstracted for me to know what I'd be changing without a thorough deep-dive into iris code.
The text was updated successfully, but these errors were encountered: