Skip to content

Commit

Permalink
Add automatic redirect to https if both http and https are configured
Browse files Browse the repository at this point in the history
  • Loading branch information
hons82 committed May 3, 2021
1 parent 8b53477 commit 4378d1f
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 24 deletions.
18 changes: 11 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export GO111MODULE=on
export GOFLAGS= -mod=vendor

GO_FILES := $(shell \
find . '(' -path '*/.*' -o -path './vendor' ')' -prune \
Expand Down Expand Up @@ -51,22 +53,21 @@ check: .check-fmt .check-vet .check-lint .check-ineffassign .check-static .check

.PHONY: .check-vendor
.check-vendor:
@dep ensure -no-vendor -dry-run
@go mod verify

.PHONY: test
test:
@echo "==> Running tests (race)..."
@go test -cover -race ./...
@go test -cover -race -short -timeout 60m -v ./...

.PHONY: get-deps
get-deps:
@echo "==> Installing dependencies..."
@dep ensure
@go mod vendor

.PHONY: get-tools
get-tools:
@echo "==> Installing tools..."
@go get -u github.com/golang/dep/cmd/dep
@go get -u golang.org/x/lint/golint
@go get -u github.com/golang/mock/gomock

Expand All @@ -82,19 +83,22 @@ ARCH = "386 amd64 arm"
OSARCH = "!darwin/386 !darwin/arm !windows/arm"
GIT_COMMIT = $(shell git describe --always)

.PHONY: release_fast
release_fast: check clean build package

.PHONY: release
release: check test clean build package

.PHONY: build
build:
mkdir ${OUTPUT_DIR}
CGO_ENABLED=0 GOARM=5 gox -ldflags "-w -X main.version=$(GIT_COMMIT)" \
mkdir -p ${OUTPUT_DIR}
CGO_ENABLED=0 GOARM=5 gox -ldflags "-w -s -X main.version=$(GIT_COMMIT)" \
-os=${OS} -arch=${ARCH} -osarch=${OSARCH} -output "${OUTPUT_DIR}/pkg/{{.OS}}_{{.Arch}}/{{.Dir}}" \
./cmd/tunnel ./cmd/tunneld

.PHONY: package
package:
mkdir ${OUTPUT_DIR}/dist
mkdir -p ${OUTPUT_DIR}/dist
cd ${OUTPUT_DIR}/pkg/; for osarch in *; do (cd $$osarch; tar zcvf ../../dist/tunnel_$$osarch.tar.gz ./*); done;
cd ${OUTPUT_DIR}/dist; sha256sum * > ./SHA256SUMS

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ $ tunneld -tlsCrt .tunneld/server.crt -tlsKey .tunneld/server.key

This will run HTTP server on port `80` and HTTPS (HTTP/2) server on port `443`. If you want to use HTTPS it's recommended to get a properly signed certificate to avoid security warnings.

If both http and https are configured an automatic redirect to the secure channel will be established using an `http.StatusMovedPermanently` (301)

### Run Server as a Service on Ubuntu using Systemd:

* After completing the steps above successfully, create a new file for your service (you can name it whatever you want, just replace the name below with your chosen name).
Expand Down
4 changes: 3 additions & 1 deletion cmd/tunnel/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ config.yaml:
host: tls.my-tunnel-host.com
Author:
Written by M. Matczuk ([email protected])
Written by M. Matczuk ([email protected])
Forked by H. Tribus ([email protected])
Bugs:
Submit bugs to https://github.com/hons82/go-http-tunnel/issues
Expand Down
3 changes: 2 additions & 1 deletion cmd/tunneld/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Example:
tunneld -httpsAddr "" -sniAddr ":443" -rootCA client_root.crt -tlsCrt server.crt -tlsKey server.key
Author:
Written by M. Matczuk ([email protected])
Written by M. Matczuk ([email protected])
Forked by H. Tribus ([email protected])
Bugs:
Submit bugs to https://github.com/hons82/go-http-tunnel/issues
Expand Down
44 changes: 36 additions & 8 deletions cmd/tunneld/tunneld.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"strings"

"golang.org/x/net/http2"

"github.com/hons82/go-http-tunnel"
tunnel "github.com/hons82/go-http-tunnel"
"github.com/hons82/go-http-tunnel/id"
"github.com/hons82/go-http-tunnel/log"
)
Expand Down Expand Up @@ -68,13 +69,40 @@ func main() {
// start HTTP
if opts.httpAddr != "" {
go func() {
logger.Log(
"level", 1,
"action", "start http",
"addr", opts.httpAddr,
)

fatal("failed to start HTTP: %s", http.ListenAndServe(opts.httpAddr, server))
if opts.httpsAddr != "" {
logger.Log(
"level", 1,
"action", "start http redirect",
"addr", opts.httpAddr,
)

_, tlsPort, err := net.SplitHostPort(opts.httpsAddr)
if err != nil {
fatal("failed to get https port: %s", err)
}
fatal("failed to start HTTP: %s",
http.ListenAndServe(opts.httpAddr, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
host, _, err := net.SplitHostPort(r.Host)
if err != nil {
host = r.Host
}
u := r.URL
u.Host = net.JoinHostPort(host, tlsPort)
u.Scheme = "https"
http.Redirect(w, r, u.String(), http.StatusMovedPermanently)
},
)),
)
} else {
logger.Log(
"level", 1,
"action", "start http",
"addr", opts.httpAddr,
)

fatal("failed to start HTTP: %s", http.ListenAndServe(opts.httpAddr, server))
}
}()
}

Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ require (
github.com/felixge/tcpkeepalive v0.0.0-20160804073959-5bb0b2dea91e
github.com/golang/mock v1.5.0
github.com/inconshreveable/go-vhost v0.0.0-20160627193104-06d84117953b
golang.org/x/net v0.0.0-20210326060303-6b1517762897
github.com/kr/pretty v0.1.0 // indirect
golang.org/x/net v0.0.0-20210421230115-4e50805a0758
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v2 v2.4.0
)
18 changes: 12 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,33 @@ github.com/golang/mock v1.5.0 h1:jlYHihg//f7RRwuPfptm04yp4s7O6Kw8EZiVYIGcH0g=
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
github.com/inconshreveable/go-vhost v0.0.0-20160627193104-06d84117953b h1:IpLPmn6Re21F0MaV6Zsc5RdSE6KuoFpWmHiUSEs3PrE=
github.com/inconshreveable/go-vhost v0.0.0-20160627193104-06d84117953b/go.mod h1:aA6DnFhALT3zH0y+A39we+zbrdMC2N0X/q21e6FI0LU=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210326060303-6b1517762897 h1:KrsHThm5nFk34YtATK1LsThyGhGbGe1olrte/HInHvs=
golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k=
golang.org/x/net v0.0.0-20210421230115-4e50805a0758 h1:aEpZnXcAmXkd6AvLb2OPt+EN1Zu/8Ne3pCqPjja5PXY=
golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

0 comments on commit 4378d1f

Please sign in to comment.