I have a problem with redirect from http to https. In logs I see error:
http: TLS handshake error from 172.21.0.1:37412: tls: first record does not look like a TLS handshake
Used middleware: HTTPSRedirect.
I'm not sure that is bug. But I don't know how to fix it. Can you advice?
No redirect, incorrect response in browser and error in logs.
Redirect from http://localhost:8080/ to https://localhost:8080/.
Start server and open url http://localhost:8080/
package main
import (
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"net/http"
)
func indexHandler(c echo.Context) error {
return c.JSON(http.StatusOK, "OK")
}
func main() {
api := echo.New()
api.Pre(middleware.HTTPSRedirect())
api.Use(middleware.Logger())
api.Use(middleware.Recover())
api.GET("/", indexHandler)
api.Logger.Fatal(api.StartTLS(":8080", "/opt/cert/cert.pem", "/opt/cert/key.pem"))
}
echo v3.0.3 (commit 8d504c1b699c757b267255c53b3e5219f9974abc)
I have the same issue too. Unfortunately i can't do much to change my situation.
Try updating the version of OpenSSL & curl you are using.
Refer to: https://groups.google.com/forum/#!topic/vault-tool/P7iGkH0DfnI and https://groups.google.com/forum/#!topic/vault-tool/gczD8kvDRIc
I'm having the same issue, the http response is not redirected to the https one, making it unable to use http and https at the same time.
Any update on this bug?
So I have managed to solve this, it's not possible in go to bind the http and https interfaces to the same port, in the case of golang you have to run two servers with a go routine, on the ports 80 and 443, if you do it in different ports it will fail because echo will try to redirect the http connection to the https interface on the same port of the http, and it doesn't exist, example:
if you have your http on the port 8080 and the https on 1323, when going to 8080 it will redirect to https://localhost:8080, and it will simply fail, that's why you need to run the http on the 80 and the https on the 443.
Here is my code:
func main (){
e := echo.New()
e.Pre(middleware.HTTPSRedirect())
config.OpenDatabase() // Open database connection, only once in the lifetime of the app.
e.GET("/aws/healthy", func (c echo.Context) error {
return c.String(http.StatusOK, "Healthy!")
})
go func(c *echo.Echo){
e.Logger.Fatal(e.Start(":80"))
}(e)
e.Logger.Fatal(e.StartTLS(":443", "bundle.crt", "private.key"))
}
So when the user goes to localhost it will redirect to https://localhost, and it will work.
@vishr sorry to comment on this closed issue, but how can I supress this error: echo: http: TLS handshake error from 192.168.10.125:59282: tls: first record does not look like a TLS handshake. And I'm not being able to get this output on my log using: e.Logger.SetOutput(lf). is there any way to supress this error or log it into a file ?
I doubt that it is not being captured in the output you specify. Do you see other logs?
Yes, I'm setting e.Logger.SetOutput(lf) and
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{Output: lf }))
Both goes to my logfile, but this error: echo: http: TLS handshake error from 192.168.10.125:59282: tls: first record does not look like a TLS handshake which is not in json format goes to stdout. am I missing something ? There's a way to suppress it ?
Have you checked where your standard logs are going? Most likely you have a call to https://golang.org/pkg/log/#SetPrefix. Can you try log.Println("test") somewhere in the code and see where it lands?
Update:
Relevant issue https://github.com/labstack/echo/issues/680. What version of Echo are you using?
@vishr I'm using logrus like this log "github.com/sirupsen/logrus" , so I'm overriding the standard log.
But anyway I'm telling to logrus log into my logfile:log.SetOutput(lf). shouldn't echo log this error like the others ? I tested here fmt prints to stdout and not to my file. is Echo using fmt ?
Update:
I'm using version = "3.2.1"
in echo.go
Shouldn't this line do the trick ?
e.stdLogger = stdLog.New(e.Logger.Output(), e.Logger.Prefix()+": ", 0)
go func(c *echo.Echo){
e.Logger.Fatal(e.Start(":80"))
}(e)
@victorct Does this still work?
Most helpful comment
So I have managed to solve this, it's not possible in go to bind the http and https interfaces to the same port, in the case of golang you have to run two servers with a go routine, on the ports 80 and 443, if you do it in different ports it will fail because echo will try to redirect the http connection to the https interface on the same port of the http, and it doesn't exist, example:
if you have your http on the port 8080 and the https on 1323, when going to 8080 it will redirect to https://localhost:8080, and it will simply fail, that's why you need to run the http on the 80 and the https on the 443.
Here is my code:
So when the user goes to localhost it will redirect to https://localhost, and it will work.