Skip to content

Commit

Permalink
cache client Jwt Token
Browse files Browse the repository at this point in the history
  • Loading branch information
yuweibo committed Jul 23, 2024
1 parent 9330f40 commit 59646a5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
webSocks5.exe
privateKey.txt
10 changes: 10 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"github.com/dgrijalva/jwt-go"
"github.com/patrickmn/go-cache"
"golang.org/x/net/websocket"
"io"
"net"
Expand Down Expand Up @@ -213,7 +214,15 @@ func byteSliceToIP(ip []byte) string {
return strings.Join(ipStrs, ".")
}

var jwtCache = cache.New(10*time.Hour, 10*time.Minute)

func genJwtToken(privateKeyFilePath string) (string, error) {

ctoken, found := jwtCache.Get(privateKeyFilePath)
if found {
return ctoken.(string), nil
}

// 私钥
privateKey, err := os.ReadFile(privateKeyFilePath)
if err != nil {
Expand All @@ -235,5 +244,6 @@ func genJwtToken(privateKeyFilePath string) (string, error) {
if err != nil {
return "", err
}
jwtCache.SetDefault(privateKeyFilePath, signedToken)
return signedToken, nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/labstack/gommon v0.4.2 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.22.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/tjfoc/gmsm v1.3.2 h1:7JVkAn5bvUJ7HtU08iW6UiD+UTmJTIToHCfeFzkcCxM=
github.com/tjfoc/gmsm v1.3.2/go.mod h1:HaUcFuY0auTiaHB9MHFGCPx5IaLhTUd2atbCFBQXn9w=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
Expand Down

0 comments on commit 59646a5

Please sign in to comment.