Skip to content

Commit bed3099

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

File tree

9 files changed

+65
-44
lines changed

9 files changed

+65
-44
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,22 @@ ARCH = "386 amd64 arm"
8282
OSARCH = "!darwin/386 !darwin/arm !windows/arm"
8383
GIT_COMMIT = $(shell git describe --always)
8484

85+
.PHONY: release_fast
86+
release_fast: check clean build package
87+
8588
.PHONY: release
8689
release: check test clean build package
8790

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

9598
.PHONY: package
9699
package:
97-
mkdir ${OUTPUT_DIR}/dist
100+
mkdir -p ${OUTPUT_DIR}/dist
98101
cd ${OUTPUT_DIR}/pkg/; for osarch in *; do (cd $$osarch; tar zcvf ../../dist/tunnel_$$osarch.tar.gz ./*); done;
99102
cd ${OUTPUT_DIR}/dist; sha256sum * > ./SHA256SUMS
100103

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

benchmark/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Detailed results of load spike test.
3131

3232
This test compares performance on twenty minutes constant pressure runs. tunnel shows ability to trade latency for throughput. It runs fine at 300 req/sec but at higher request rates we observe poor latency and some message drops. Koding tunnel has acceptable performance at 300 req/sec, however, with increased load it just breaks.
3333

34-
Both implementations have a connection (or memory) leak when dealing with too high loads. This results in process (or machine) crash as machine runs out of memory. It's 100% reproducible, when process crashes it has few hundred thousands go routines waiting on select in a connection and memory full of connection buffers.
34+
Both implementations have a connection (or memory) leak when dealing with too high loads. This results in process (or machine) crash as machine runs out of memory. It's 100% reproducible, when process crashes it has few hundred thousands go routines waiting on select in a connection and memory full of connection buffers.
3535

3636
![](constload.png)
3737

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/tunnel/tunnel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"gopkg.in/yaml.v2"
1919

2020
"github.com/cenkalti/backoff"
21-
tunnel "github.com/hons82/go-http-tunnel"
21+
"github.com/hons82/go-http-tunnel"
2222
"github.com/hons82/go-http-tunnel/id"
2323
"github.com/hons82/go-http-tunnel/log"
2424
"github.com/hons82/go-http-tunnel/proto"

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: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"crypto/x509"
1010
"fmt"
1111
"io/ioutil"
12+
"net"
1213
"net/http"
1314
"os"
1415
"strings"
@@ -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: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
module github.com/hons82/go-http-tunnel
22

3-
go 1.16
3+
go 1.13
44

55
require (
66
github.com/calmh/luhn v2.0.0+incompatible
7-
github.com/cenkalti/backoff v2.2.1+incompatible
7+
github.com/cenkalti/backoff v2.1.1+incompatible
88
github.com/felixge/tcpkeepalive v0.0.0-20160804073959-5bb0b2dea91e
9-
github.com/golang/mock v1.5.0
9+
github.com/golang/mock v1.2.0
1010
github.com/inconshreveable/go-vhost v0.0.0-20160627193104-06d84117953b
11-
golang.org/x/net v0.0.0-20210326060303-6b1517762897
12-
gopkg.in/yaml.v2 v2.4.0
11+
golang.org/x/net v0.0.0-20171123081856-c7086645de24
12+
golang.org/x/text v0.1.1-0.20171102192421-88f656faf3f3 // indirect
13+
gopkg.in/yaml.v2 v2.2.2
1314
)

go.sum

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,18 @@
11
github.com/calmh/luhn v2.0.0+incompatible h1:xHkbAc8FBgMiGUaKsiYcwtf8xhSXVtRKA2NhY7hFCAc=
22
github.com/calmh/luhn v2.0.0+incompatible/go.mod h1:70IGmMi0GKRs073gl/oH5/yiJnTt61h35YQhvo/k3Cc=
3-
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
4-
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
3+
github.com/cenkalti/backoff v2.1.1+incompatible h1:tKJnvO2kl0zmb/jA5UKAt4VoEVw1qxKWjE/Bpp46npY=
4+
github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
55
github.com/felixge/tcpkeepalive v0.0.0-20160804073959-5bb0b2dea91e h1:mVIjvOd7NckIwf9J4hLB2YWXBYjhREF4vBeZXZ8mrWM=
66
github.com/felixge/tcpkeepalive v0.0.0-20160804073959-5bb0b2dea91e/go.mod h1:z0yk3Pix6k848RFizhkU4uY36ts5pB1t3toBwudGbBo=
7-
github.com/golang/mock v1.5.0 h1:jlYHihg//f7RRwuPfptm04yp4s7O6Kw8EZiVYIGcH0g=
8-
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
7+
github.com/golang/mock v1.2.0 h1:28o5sBqPkBsMGnC6b4MvE2TzSr5/AT4c/1fLqVGIwlk=
8+
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
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-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
12-
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
13-
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
14-
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
15-
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=
18-
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
19-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
20-
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
21-
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=
23-
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
24-
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=
27-
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
28-
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
29-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
30-
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
11+
golang.org/x/net v0.0.0-20171123081856-c7086645de24 h1:z0cmn+BVQSCN8exp26jnHqHXHIvTlqIYhjHln4k/UAU=
12+
golang.org/x/net v0.0.0-20171123081856-c7086645de24/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
13+
golang.org/x/text v0.1.1-0.20171102192421-88f656faf3f3 h1:OxMYHd6bm+jH+TI7NBCb/CaYk6pMJnBC8GIzIi68Hk4=
14+
golang.org/x/text v0.1.1-0.20171102192421-88f656faf3f3/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
3115
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
3216
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
33-
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
34-
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
17+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
18+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 commit comments

Comments
 (0)