Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 38 additions & 38 deletions cmd/commands/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package commands

import (
"crypto/tls"
"io"
"sync"

"fmt"
"io"
"net"
"net/http"
"sync"
"time"

"github.com/dgrijalva/jwt-go"
honeycomb "github.com/getspine/go-metrics-honeycomb"
"github.com/golang-jwt/jwt/v5"
"github.com/hashicorp/yamux"
"github.com/inconshreveable/go-vhost"
"github.com/pjvds/tunl/pkg/tunnel/certs"
Expand All @@ -26,37 +25,39 @@ import (
)

func createToken(signKey string, id string) (string, error) {
claims := jwt.StandardClaims{
Id: xid.New().String(),
now := time.Now().UTC()
claims := &jwt.RegisteredClaims{
Audience: jwt.ClaimStrings{"tunnel"},
ExpiresAt: jwt.NewNumericDate(now.Add(time.Duration(24) * time.Hour)),
ID: xid.New().String(),
IssuedAt: jwt.NewNumericDate(now),
Issuer: "tunl",
Subject: id,
Audience: "tunnels",
ExpiresAt: time.Now().Add(24 * time.Hour).UTC().Unix(),
IssuedAt: time.Now().UTC().Unix(),
}

token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString([]byte(signKey))
}

func verifyToken(signKey string, tokenString string) (*jwt.StandardClaims, error) {
token, err := jwt.ParseWithClaims(tokenString, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
func verifyToken(signKey string, tokenString string) (*jwt.RegisteredClaims, error) {
token, err := jwt.ParseWithClaims(tokenString, &jwt.RegisteredClaims{}, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return []byte(signKey), nil
})

if err != nil {
return nil, errors.Wrap(err, "invalid token")
}

claims, ok := token.Claims.(*jwt.StandardClaims)
claims, ok := token.Claims.(*jwt.RegisteredClaims)
if !ok || !token.Valid {
return nil, errors.New("invalid token")
}

if !claims.VerifyExpiresAt(time.Now().UTC().Unix(), false) {
expireAt, err := token.Claims.GetExpirationTime()
is_token_valid := err == nil && time.Now().Before(expireAt.Time)
if !is_token_valid {
return nil, errors.New("token expired")
}

Expand Down Expand Up @@ -325,30 +326,29 @@ var DaemonCommand = &cli.Command{
for {
conn, err := mux.NextError()
if err != nil {
switch err.(type){
case vhost.BadRequest:
logger.Debug("vhost accept error: bad request", zap.Error(err))
break

case vhost.NotFound:
logger.Error("vhost mux reached unknown host")
(&http.Response{
Status: "not found",
StatusCode: http.StatusNotFound,
}).Write(conn)
break

case vhost.Closed:
logger.Error("vhost mux reached closed host")
(&http.Response{
Status: "not found",
StatusCode: http.StatusGone,
}).Write(conn)
break
default:
logger.Debug("unknown mux error", zap.Error(err))
}

switch err.(type) {
case vhost.BadRequest:
logger.Debug("vhost accept error: bad request", zap.Error(err))
break

case vhost.NotFound:
logger.Error("vhost mux reached unknown host")
(&http.Response{
Status: "not found",
StatusCode: http.StatusNotFound,
}).Write(conn)
break

case vhost.Closed:
logger.Error("vhost mux reached closed host")
(&http.Response{
Status: "not found",
StatusCode: http.StatusGone,
}).Write(conn)
break
default:
logger.Debug("unknown mux error", zap.Error(err))
}
}

if conn != nil {
Expand Down
93 changes: 57 additions & 36 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,50 +1,71 @@
module github.com/pjvds/tunl

go 1.16
go 1.20

replace github.com/inconshreveable/go-vhost => github.com/pjvds/go-vhost v0.0.0-20201229150248-206eee94f4aa

replace github.com/armon/go-metrics => ./go-metrics

// replace github.com/armon/go-metrics => github.com/hashicorp/go-metrics v0.4.2-0.20221220172610-8cabd9eab1be

replace github.com/hashicorp/golang-lru => github.com/hashicorp/golang-lru/v2 v2.0.2

replace github.com/hashicorp/go-immutable-radix => github.com/hashicorp/go-immutable-radix/v2 v2.0.0

require (
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Microsoft/go-winio v0.4.16 // indirect
github.com/armon/go-metrics v0.3.6
github.com/atotto/clipboard v0.1.2 // indirect
github.com/containerd/containerd v1.4.3 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v20.10.2+incompatible
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/Masterminds/semver v1.5.0
github.com/armon/go-metrics v0.4.2-0.20221220172610-8cabd9eab1be
github.com/docker/docker v23.0.5+incompatible
github.com/getspine/go-metrics-honeycomb v0.0.0-20180125230210-7f1304f1bc83
github.com/gobwas/glob v0.2.3
github.com/gogo/protobuf v1.3.2 // indirect
github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d
github.com/golang-jwt/jwt/v5 v5.0.0
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/sessions v1.2.1
github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce
github.com/honeycombio/libhoney-go v1.15.1 // indirect
github.com/inconshreveable/go-vhost v0.0.0-20160627193104-06d84117953b
github.com/kr/pretty v0.2.1 // indirect
github.com/hashicorp/yamux v0.1.1
github.com/inconshreveable/go-vhost v1.0.0
github.com/mdp/qrterminal/v3 v3.0.0
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/pkg/errors v0.9.1
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
github.com/rs/xid v1.2.1
github.com/sirupsen/logrus v1.7.0 // indirect
github.com/urfave/cli/v2 v2.3.0
github.com/yelinaung/go-haikunator v0.0.0-20150320004105-1249cae259af
go.uber.org/zap v1.16.0
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/mod v0.4.0 // indirect
golang.org/x/net v0.0.0-20201021035429-f5854403a974
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
google.golang.org/grpc v1.34.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
gotest.tools/v3 v3.0.3 // indirect
honnef.co/go/tools v0.0.1-2020.1.4 // indirect
github.com/rs/xid v1.5.0
github.com/urfave/cli/v2 v2.25.1
github.com/yelinaung/go-haikunator v0.0.0-20221222235932-36bf4c441150
go.uber.org/zap v1.24.0
golang.org/x/net v0.6.0
)

replace github.com/inconshreveable/go-vhost => github.com/pjvds/go-vhost v0.0.0-20201229150248-206eee94f4aa
require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/atotto/clipboard v0.1.4
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
github.com/facebookgo/limitgroup v0.0.0-20150612190941-6abd8d71ec01 // indirect
github.com/facebookgo/muster v0.0.0-20150708232844-fd3d7953fd52 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/gorilla/securecookie v1.1.1 // indirect
github.com/hashicorp/go-immutable-radix/v2 v2.0.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.0 // indirect
github.com/honeycombio/libhoney-go v1.18.0 // indirect
github.com/klauspost/compress v1.16.5 // indirect
github.com/moby/term v0.0.0-20230430220526-1849d9c42740 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.6.0 // indirect
gopkg.in/alexcesaro/statsd.v2 v2.0.0 // indirect
gotest.tools/v3 v3.4.0 // indirect
rsc.io/qr v0.2.0 // indirect
)
Loading