Skip to content

Commit 4378d1f

Browse files
committed
Add automatic redirect to https if both http and https are configured
1 parent 8b53477 commit 4378d1f

File tree

7 files changed

+69
-24
lines changed

7 files changed

+69
-24
lines changed

Makefile

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export GO111MODULE=on
2+
export GOFLAGS= -mod=vendor
13

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

5254
.PHONY: .check-vendor
5355
.check-vendor:
54-
@dep ensure -no-vendor -dry-run
56+
@go mod verify
5557

5658
.PHONY: test
5759
test:
5860
@echo "==> Running tests (race)..."
59-
@go test -cover -race ./...
61+
@go test -cover -race -short -timeout 60m -v ./...
6062

6163
.PHONY: get-deps
6264
get-deps:
6365
@echo "==> Installing dependencies..."
64-
@dep ensure
66+
@go mod vendor
6567

6668
.PHONY: get-tools
6769
get-tools:
6870
@echo "==> Installing tools..."
69-
@go get -u github.com/golang/dep/cmd/dep
7071
@go get -u golang.org/x/lint/golint
7172
@go get -u github.com/golang/mock/gomock
7273

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

86+
.PHONY: release_fast
87+
release_fast: check clean build package
88+
8589
.PHONY: release
8690
release: check test clean build package
8791

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

9599
.PHONY: package
96100
package:
97-
mkdir ${OUTPUT_DIR}/dist
101+
mkdir -p ${OUTPUT_DIR}/dist
98102
cd ${OUTPUT_DIR}/pkg/; for osarch in *; do (cd $$osarch; tar zcvf ../../dist/tunnel_$$osarch.tar.gz ./*); done;
99103
cd ${OUTPUT_DIR}/dist; sha256sum * > ./SHA256SUMS
100104

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ $ tunneld -tlsCrt .tunneld/server.crt -tlsKey .tunneld/server.key
6868

6969
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.
7070

71+
If both http and https are configured an automatic redirect to the secure channel will be established using an `http.StatusMovedPermanently` (301)
72+
7173
### Run Server as a Service on Ubuntu using Systemd:
7274

7375
* 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).

cmd/tunnel/options.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ config.yaml:
4444
host: tls.my-tunnel-host.com
4545
4646
Author:
47-
Written by M. Matczuk ([email protected])
47+
Written by M. Matczuk ([email protected])
48+
Forked by H. Tribus ([email protected])
49+
4850
4951
Bugs:
5052
Submit bugs to https://github.com/hons82/go-http-tunnel/issues

cmd/tunneld/options.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ Example:
2222
tunneld -httpsAddr "" -sniAddr ":443" -rootCA client_root.crt -tlsCrt server.crt -tlsKey server.key
2323
2424
Author:
25-
Written by M. Matczuk ([email protected])
25+
Written by M. Matczuk ([email protected])
26+
Forked by H. Tribus ([email protected])
2627
2728
Bugs:
2829
Submit bugs to https://github.com/hons82/go-http-tunnel/issues

cmd/tunneld/tunneld.go

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import (
99
"crypto/x509"
1010
"fmt"
1111
"io/ioutil"
12+
"net"
1213
"net/http"
1314
"os"
1415
"strings"
1516

1617
"golang.org/x/net/http2"
1718

18-
"github.com/hons82/go-http-tunnel"
19+
tunnel "github.com/hons82/go-http-tunnel"
1920
"github.com/hons82/go-http-tunnel/id"
2021
"github.com/hons82/go-http-tunnel/log"
2122
)
@@ -68,13 +69,40 @@ func main() {
6869
// start HTTP
6970
if opts.httpAddr != "" {
7071
go func() {
71-
logger.Log(
72-
"level", 1,
73-
"action", "start http",
74-
"addr", opts.httpAddr,
75-
)
76-
77-
fatal("failed to start HTTP: %s", http.ListenAndServe(opts.httpAddr, server))
72+
if opts.httpsAddr != "" {
73+
logger.Log(
74+
"level", 1,
75+
"action", "start http redirect",
76+
"addr", opts.httpAddr,
77+
)
78+
79+
_, tlsPort, err := net.SplitHostPort(opts.httpsAddr)
80+
if err != nil {
81+
fatal("failed to get https port: %s", err)
82+
}
83+
fatal("failed to start HTTP: %s",
84+
http.ListenAndServe(opts.httpAddr, http.HandlerFunc(
85+
func(w http.ResponseWriter, r *http.Request) {
86+
host, _, err := net.SplitHostPort(r.Host)
87+
if err != nil {
88+
host = r.Host
89+
}
90+
u := r.URL
91+
u.Host = net.JoinHostPort(host, tlsPort)
92+
u.Scheme = "https"
93+
http.Redirect(w, r, u.String(), http.StatusMovedPermanently)
94+
},
95+
)),
96+
)
97+
} else {
98+
logger.Log(
99+
"level", 1,
100+
"action", "start http",
101+
"addr", opts.httpAddr,
102+
)
103+
104+
fatal("failed to start HTTP: %s", http.ListenAndServe(opts.httpAddr, server))
105+
}
78106
}()
79107
}
80108

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ require (
88
github.com/felixge/tcpkeepalive v0.0.0-20160804073959-5bb0b2dea91e
99
github.com/golang/mock v1.5.0
1010
github.com/inconshreveable/go-vhost v0.0.0-20160627193104-06d84117953b
11-
golang.org/x/net v0.0.0-20210326060303-6b1517762897
11+
github.com/kr/pretty v0.1.0 // indirect
12+
golang.org/x/net v0.0.0-20210421230115-4e50805a0758
13+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
1214
gopkg.in/yaml.v2 v2.4.0
1315
)

go.sum

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,33 @@ github.com/golang/mock v1.5.0 h1:jlYHihg//f7RRwuPfptm04yp4s7O6Kw8EZiVYIGcH0g=
88
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
99
github.com/inconshreveable/go-vhost v0.0.0-20160627193104-06d84117953b h1:IpLPmn6Re21F0MaV6Zsc5RdSE6KuoFpWmHiUSEs3PrE=
1010
github.com/inconshreveable/go-vhost v0.0.0-20160627193104-06d84117953b/go.mod h1:aA6DnFhALT3zH0y+A39we+zbrdMC2N0X/q21e6FI0LU=
11+
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
12+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
13+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
14+
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
15+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
1116
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
1217
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
1318
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
1419
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
1520
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
16-
golang.org/x/net v0.0.0-20210326060303-6b1517762897 h1:KrsHThm5nFk34YtATK1LsThyGhGbGe1olrte/HInHvs=
17-
golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k=
21+
golang.org/x/net v0.0.0-20210421230115-4e50805a0758 h1:aEpZnXcAmXkd6AvLb2OPt+EN1Zu/8Ne3pCqPjja5PXY=
22+
golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM=
1823
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
1924
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
2025
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2126
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
22-
golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
27+
golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2328
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
2429
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
25-
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
26-
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
30+
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
31+
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
2732
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
2833
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
2934
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
3035
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
31-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
3236
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
37+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
38+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3339
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
3440
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

0 commit comments

Comments
 (0)