Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 使用go-sqlite3实现数据持久化 #363

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ COMMIT := `git rev-parse HEAD`
PLATFORM := linux
BUILD_DIR := build
VAR_SETTING := -X $(PACKAGE_NAME)/constant.Version=$(VERSION) -X $(PACKAGE_NAME)/constant.Commit=$(COMMIT)
GOBUILD = env CGO_ENABLED=0 $(GO_DIR)go build -tags "full" -trimpath -ldflags="-s -w -buildid= $(VAR_SETTING)" -o $(BUILD_DIR)
GOBUILD = $(GO_DIR)go build -tags "full" -trimpath -ldflags="-s -w -buildid= $(VAR_SETTING)" -o $(BUILD_DIR)

.PHONY: trojan-go release test
normal: clean trojan-go
Expand Down
3 changes: 2 additions & 1 deletion api/service/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func TestClientAPI(t *testing.T) {
if !valid {
t.Fail()
}
user.AddTraffic(1234, 5678)
user.AddSentTraffic(1234)
user.AddRecvTraffic(5678)
time.Sleep(time.Second)
conn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", port), grpc.WithInsecure())
common.Must(err)
Expand Down
40 changes: 20 additions & 20 deletions api/service/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,34 +102,34 @@ func (s *ServerAPI) SetUsers(stream TrojanServerService_SetUsersServer) error {
break
}
if req.Status.SpeedLimit != nil {
valid, user := s.auth.AuthUser(req.Status.User.Hash)
if !valid {
err = common.NewError("failed to auth new user").Base(err)
continue
}
if req.Status.SpeedLimit != nil {
user.SetSpeedLimit(int(req.Status.SpeedLimit.DownloadSpeed), int(req.Status.SpeedLimit.UploadSpeed))
err = s.auth.SetUserSpeedLimit(req.Status.User.Hash, int(req.Status.SpeedLimit.DownloadSpeed), int(req.Status.SpeedLimit.UploadSpeed))
if err != nil {
break
}
if req.Status.TrafficTotal != nil {
user.SetTraffic(req.Status.TrafficTotal.DownloadTraffic, req.Status.TrafficTotal.UploadTraffic)
}
if req.Status.TrafficTotal != nil {
err = s.auth.SetUserTraffic(req.Status.User.Hash, req.Status.TrafficTotal.DownloadTraffic, req.Status.TrafficTotal.UploadTraffic)
if err != nil {
break
}
user.SetIPLimit(int(req.Status.IpLimit))
}
err = s.auth.SetUserIPLimit(req.Status.User.Hash, int(req.Status.IpLimit))
case SetUsersRequest_Delete:
err = s.auth.DelUser(req.Status.User.Hash)
case SetUsersRequest_Modify:
valid, user := s.auth.AuthUser(req.Status.User.Hash)
if !valid {
err = common.NewError("invalid user " + req.Status.User.Hash)
} else {
if req.Status.SpeedLimit != nil {
user.SetSpeedLimit(int(req.Status.SpeedLimit.DownloadSpeed), int(req.Status.SpeedLimit.UploadSpeed))
if req.Status.SpeedLimit != nil {
err = s.auth.SetUserSpeedLimit(req.Status.User.Hash, int(req.Status.SpeedLimit.DownloadSpeed), int(req.Status.SpeedLimit.UploadSpeed))
if err != nil {
break
}
if req.Status.TrafficTotal != nil {
user.SetTraffic(req.Status.TrafficTotal.DownloadTraffic, req.Status.TrafficTotal.UploadTraffic)
}
if req.Status.TrafficTotal != nil {
err = s.auth.SetUserTraffic(req.Status.User.Hash, req.Status.TrafficTotal.DownloadTraffic, req.Status.TrafficTotal.UploadTraffic)
if err != nil {
break
}
user.SetIPLimit(int(req.Status.IpLimit))
}
err = s.auth.SetUserIPLimit(req.Status.User.Hash, int(req.Status.IpLimit))
}
if err != nil {
stream.Send(&SetUsersResponse{
Expand All @@ -156,7 +156,7 @@ func (s *ServerAPI) ListUsers(req *ListUsersRequest, stream TrojanServerService_
err := stream.Send(&ListUsersResponse{
Status: &UserStatus{
User: &User{
Hash: user.Hash(),
Hash: user.GetHash(),
},
TrafficTotal: &Traffic{
DownloadTraffic: downloadTraffic,
Expand Down
7 changes: 4 additions & 3 deletions api/service/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ func TestServerAPI(t *testing.T) {
fmt.Println(resp.Status.SpeedLimit)
}
stream1.CloseSend()
user.AddTraffic(1234, 5678)
user.AddSentTraffic(1234)
user.AddRecvTraffic(5678)
time.Sleep(time.Second * 1)
stream2, err := server.GetUsers(ctx)
common.Must(err)
Expand Down Expand Up @@ -127,7 +128,7 @@ func TestServerAPI(t *testing.T) {
return
default:
}
user.AddTraffic(200, 0)
user.AddSentTraffic(200)
}
}()
go func() {
Expand All @@ -137,7 +138,7 @@ func TestServerAPI(t *testing.T) {
return
default:
}
user.AddTraffic(0, 300)
user.AddRecvTraffic(300)
}
}()
time.Sleep(time.Second * 3)
Expand Down
4 changes: 4 additions & 0 deletions common/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func NewError(info string) *Error {
}
}

func NewErrorf(format string, a ...interface{}) *Error {
return NewError(fmt.Sprintf(format, a...))
}

func Must(err error) {
if err != nil {
fmt.Println(err)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ require (
google.golang.org/grpc v1.38.0
google.golang.org/protobuf v1.26.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
gorm.io/driver/sqlite v1.1.4
gorm.io/gorm v1.21.10
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0B/fFc00Y+Rasa88328GlI/XbtyysCtTHZS8h7IrBU=
github.com/jhump/protoreflect v1.8.2 h1:k2xE7wcUomeqwY0LDCYA16y4WWfyTcMx5mKhk0d4ua0=
github.com/jhump/protoreflect v1.8.2/go.mod h1:7GcYQDdMU/O/BBrl/cX6PNHpXh6cenjd8pneu5yW7Tg=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.2 h1:eVKgfIdy9b6zbWBMgFpfDPoAMifwSZagU9HmEU6zgiI=
github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
Expand Down Expand Up @@ -192,6 +197,8 @@ github.com/marten-seemann/qtls-go1-16 v0.1.3 h1:XEZ1xGorVy9u+lJq+WXNE+hiqRYLNvJG
github.com/marten-seemann/qtls-go1-16 v0.1.3/go.mod h1:gNpI2Ol+lRS3WwSOtIUUtRwZEQMXjYK+dQSBFbethAk=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-sqlite3 v1.14.5 h1:1IdxlwTNazvbKJQSxoJ5/9ECbEeaTTyeU7sEAZ5KKTQ=
github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/microcosm-cc/bluemonday v1.0.1/go.mod h1:hsXNsILzKxV+sX77C5b8FSuKF00vh2OMYv+xgHpAMF4=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
Expand Down Expand Up @@ -573,6 +580,11 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/sqlite v1.1.4 h1:PDzwYE+sI6De2+mxAneV9Xs11+ZyKV6oxD3wDGkaNvM=
gorm.io/driver/sqlite v1.1.4/go.mod h1:mJCeTFr7+crvS+TRnWc5Z3UvwxUN1BGBLMrf5LA9DYw=
gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.21.10 h1:kBGiBsaqOQ+8f6S2U6mvGFz6aWWyCeIiuaFcaBozp4M=
gorm.io/gorm v1.21.10/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
grpc.go4.org v0.0.0-20170609214715-11d0a25b4919/go.mod h1:77eQGdRu53HpSqPFJFmuJdjuHRquDANNeA4x7B8WQ9o=
h12.io/socks v1.0.2/go.mod h1:AIhxy1jOId/XCz9BO+EIgNL2rQiPTBNnOfnVnQ+3Eck=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
1 change: 1 addition & 0 deletions statistic/memory/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

type Config struct {
Passwords []string `json:"password" yaml:"password"`
Sqlite string `json:"sqlite" yaml:"sqlite"`
}

func init() {
Expand Down
Loading