Skip to content

Commit

Permalink
Merge pull request #133
Browse files Browse the repository at this point in the history
Release v0.6.5
  • Loading branch information
bsrinivas8687 authored Mar 21, 2023
2 parents 440149f + d0520c6 commit 6530890
Show file tree
Hide file tree
Showing 16 changed files with 97 additions and 92 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ FROM alpine:3.17
COPY --from=build /go/bin/sentinelnode /usr/local/bin/process
COPY --from=build /root/hnsd/hnsd /usr/local/bin/hnsd

RUN apk add --no-cache ip6tables unbound-libs v2ray wireguard-tools
RUN apk add --no-cache unbound-libs v2ray wireguard-tools && \
rm -rf /etc/v2ray/ /usr/share/v2ray/

CMD ["process"]
15 changes: 7 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
.DEFAULT_GOAL := default
PACKAGES := $(shell go list ./...)
VERSION := $(shell git describe --tags | sed 's/^v//' | rev | cut -d - -f 2- | rev)
COMMIT := $(shell git log -1 --format='%H')

BUILD_TAGS := $(strip netgo)
TAGS := $(strip netgo)
LD_FLAGS := -s -w \
-X github.com/cosmos/cosmos-sdk/version.Name=sentinel \
-X github.com/cosmos/cosmos-sdk/version.AppName=sentinelnode \
-X github.com/cosmos/cosmos-sdk/version.Version=${VERSION} \
-X github.com/cosmos/cosmos-sdk/version.Commit=${COMMIT} \
-X github.com/cosmos/cosmos-sdk/version.BuildTags=${BUILD_TAGS}
-X github.com/cosmos/cosmos-sdk/version.BuildTags=${TAGS}

.PHONY: benchmark
benchmark:
@go test -mod=readonly -v -bench= ${PACKAGES}
@go test -bench -mod=readonly -v ./...

.PHONY: build
build:
go build -mod=readonly -tags="${BUILD_TAGS}" -ldflags="${LD_FLAGS}" \
go build -ldflags="${LD_FLAGS}" -mod=readonly -tags="${TAGS}" -trimpath \
-o ./bin/sentinelnode main.go

.PHONY: clean
clean:
rm -rf ./bin ./vendor

.PHONE: default
.PHONY: default
default: clean build

.PHONY: install
install:
go build -mod=readonly -tags="${BUILD_TAGS}" -ldflags="${LD_FLAGS}" \
go build -ldflags="${LD_FLAGS}" -mod=readonly -tags="${TAGS}" -trimpath \
-o "${GOPATH}/bin/sentinelnode" main.go

.PHONY: build-image
Expand All @@ -42,7 +41,7 @@ go-lint:

.PHONY: test
test:
@go test -mod=readonly -v -cover ${PACKAGES}
@go test -cover -mod=readonly -v ./...

.PHONY: tools
tools:
Expand Down
48 changes: 26 additions & 22 deletions api/session/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
return func(c *gin.Context) {
if ctx.Service().PeerCount() >= ctx.Config().QOS.MaxPeers {
err := fmt.Errorf("reached maximum peers limit; maximum %d", ctx.Config().QOS.MaxPeers)
err := fmt.Errorf("reached maximum peers limit %d", ctx.Config().QOS.MaxPeers)
c.JSON(http.StatusBadRequest, types.NewResponseError(1, err))
return
}
Expand All @@ -31,12 +31,12 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
&types.Session{},
).Where(
&types.Session{
ID: req.id,
ID: req.URI.ID,
},
).First(&item)

if item.ID != 0 {
err = fmt.Errorf("peer for session %d already exist", req.id)
err = fmt.Errorf("peer for session %d already exist", req.URI.ID)
c.JSON(http.StatusBadRequest, types.NewResponseError(3, err))
return
}
Expand All @@ -56,34 +56,34 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
return
}

account, err := ctx.Client().QueryAccount(req.accAddress)
account, err := ctx.Client().QueryAccount(req.AccAddress)
if err != nil {
c.JSON(http.StatusInternalServerError, types.NewResponseError(4, err))
return
}
if account == nil {
err = fmt.Errorf("account %s does not exist", req.accAddress)
err = fmt.Errorf("account %s does not exist", req.AccAddress)
c.JSON(http.StatusNotFound, types.NewResponseError(4, err))
return
}
if account.GetPubKey() == nil {
err = fmt.Errorf("public key for account %s does not exist", req.accAddress)
err = fmt.Errorf("public key for account %s does not exist", req.AccAddress)
c.JSON(http.StatusNotFound, types.NewResponseError(4, err))
return
}
if ok := account.GetPubKey().VerifySignature(sdk.Uint64ToBigEndian(req.id), req.signature); !ok {
err = fmt.Errorf("failed to verify the signature %s", req.signature)
if ok := account.GetPubKey().VerifySignature(sdk.Uint64ToBigEndian(req.URI.ID), req.Signature); !ok {
err = fmt.Errorf("failed to verify the signature %s", req.Signature)
c.JSON(http.StatusBadRequest, types.NewResponseError(4, err))
return
}

session, err := ctx.Client().QuerySession(req.id)
session, err := ctx.Client().QuerySession(req.URI.ID)
if err != nil {
c.JSON(http.StatusInternalServerError, types.NewResponseError(5, err))
return
}
if session == nil {
err = fmt.Errorf("session %d does not exist", req.id)
err = fmt.Errorf("session %d does not exist", req.URI.ID)
c.JSON(http.StatusNotFound, types.NewResponseError(5, err))
return
}
Expand Down Expand Up @@ -133,7 +133,7 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
}
}

quota, err := ctx.Client().QueryQuota(subscription.Id, req.accAddress)
quota, err := ctx.Client().QueryQuota(subscription.Id, req.AccAddress)
if err != nil {
c.JSON(http.StatusInternalServerError, types.NewResponseError(8, err))
return
Expand All @@ -160,23 +160,27 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
return
}

consumed := items[i].Download + items[i].Upload
quota.Consumed = quota.Consumed.Add(
hubtypes.NewBandwidthFromInt64(
consumed, 0,
).CeilTo(
hubtypes.Gigabyte.Quo(subscription.Price.Amount),
).Sum(),
)
consumed := sdk.NewInt(items[i].Download + items[i].Upload)
if subscription.Plan == 0 {
quota.Consumed = quota.Consumed.Add(
hubtypes.NewBandwidth(
consumed, sdk.ZeroInt(),
).CeilTo(
hubtypes.Gigabyte.Quo(subscription.Price.Amount),
).Sum(),
)
} else {
quota.Consumed = quota.Consumed.Add(consumed)
}
}

if quota.Consumed.GTE(quota.Allocated) {
err := fmt.Errorf("quota exceeded; allocated %s, consumed %s", quota.Allocated, quota.Consumed)
err = fmt.Errorf("quota exceeded; allocated %s, consumed %s", quota.Allocated, quota.Consumed)
c.JSON(http.StatusBadRequest, types.NewResponseError(10, err))
return
}

result, err := ctx.Service().AddPeer(req.key)
result, err := ctx.Service().AddPeer(req.Key)
if err != nil {
c.JSON(http.StatusInternalServerError, types.NewResponseError(11, err))
return
Expand All @@ -187,7 +191,7 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
&types.Session{},
).Create(
&types.Session{
ID: req.id,
ID: req.URI.ID,
Subscription: subscription.Id,
Key: req.Body.Key,
Address: req.URI.AccAddress,
Expand Down
16 changes: 6 additions & 10 deletions api/session/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import (
)

type RequestAddSession struct {
accAddress sdk.AccAddress
id uint64
key []byte
signature []byte
AccAddress sdk.AccAddress
Key []byte
Signature []byte

URI struct {
AccAddress string `uri:"acc_address"`
Expand All @@ -32,18 +31,15 @@ func NewRequestAddSession(c *gin.Context) (req *RequestAddSession, err error) {
return nil, err
}

req.accAddress, err = sdk.AccAddressFromBech32(req.URI.AccAddress)
req.AccAddress, err = sdk.AccAddressFromBech32(req.URI.AccAddress)
if err != nil {
return nil, err
}

req.id = req.URI.ID

req.key, err = base64.StdEncoding.DecodeString(req.Body.Key)
req.Key, err = base64.StdEncoding.DecodeString(req.Body.Key)
if err != nil {
return nil, err
}
req.signature, err = base64.StdEncoding.DecodeString(req.Body.Signature)
req.Signature, err = base64.StdEncoding.DecodeString(req.Body.Signature)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ require (
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
google.golang.org/genproto v0.0.0-20230320184635-7606e756e683 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1290,8 +1290,8 @@ google.golang.org/genproto v0.0.0-20201214200347-8c77b98c765d/go.mod h1:FWY/as6D
google.golang.org/genproto v0.0.0-20210108203827-ffc7fda8c3d7/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20210126160654-44e461bb6506/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20210226172003-ab064af71705/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 h1:DdoeryqhaXp1LtT/emMP1BRJPHHKFi5akj/nbx/zNTA=
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4/go.mod h1:NWraEVixdDnqcqQ30jipen1STv2r/n24Wb7twVTGR4s=
google.golang.org/genproto v0.0.0-20230320184635-7606e756e683 h1:khxVcsk/FhnzxMKOyD+TDGwjbEOpcPuIpmafPGFmhMA=
google.golang.org/genproto v0.0.0-20230320184635-7606e756e683/go.mod h1:NWraEVixdDnqcqQ30jipen1STv2r/n24Wb7twVTGR4s=
google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.19.1/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
Expand Down
4 changes: 2 additions & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ func (n *Node) Start() error {
}()

go func() {
if err := n.jobUpdateStatus(); err != nil {
if err := n.jobUpdateSessions(); err != nil {
panic(err)
}
}()

go func() {
if err := n.jobUpdateSessions(); err != nil {
if err := n.jobUpdateStatus(); err != nil {
panic(err)
}
}()
Expand Down
10 changes: 5 additions & 5 deletions services/v2ray/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ func configShow() *cobra.Command {
v := viper.New()
v.SetConfigFile(path)

cfg, err := v2raytypes.ReadInConfig(v)
config, err := v2raytypes.ReadInConfig(v)
if err != nil {
return err
}

fmt.Println(cfg.String())
fmt.Println(config.String())
return nil
},
}
Expand All @@ -103,18 +103,18 @@ func configSet() *cobra.Command {
v := viper.New()
v.SetConfigFile(path)

cfg, err := v2raytypes.ReadInConfig(v)
config, err := v2raytypes.ReadInConfig(v)
if err != nil {
return err
}

v.Set(args[0], args[1])

if err = v.Unmarshal(cfg); err != nil {
if err = v.Unmarshal(config); err != nil {
return err
}

return cfg.SaveToPath(path)
return config.SaveToPath(path)
},
}

Expand Down
1 change: 0 additions & 1 deletion services/v2ray/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"strings"
)

// nolint:lll
var (
configTemplate = strings.TrimSpace(`
{
Expand Down
5 changes: 4 additions & 1 deletion services/v2ray/v2ray.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ func (s *V2Ray) Init(home string) (err error) {
if err != nil {
return err
}
if err = s.config.Validate(); err != nil {
return err
}

t, err := template.New("wireguard_conf").Parse(configTemplate)
t, err := template.New("v2ray_json").Parse(configTemplate)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions services/wireguard/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ func configShow() *cobra.Command {
v := viper.New()
v.SetConfigFile(path)

cfg, err := wgtypes.ReadInConfig(v)
config, err := wgtypes.ReadInConfig(v)
if err != nil {
return err
}

fmt.Println(cfg.String())
fmt.Println(config.String())
return nil
},
}
Expand All @@ -103,18 +103,18 @@ func configSet() *cobra.Command {
v := viper.New()
v.SetConfigFile(path)

cfg, err := wgtypes.ReadInConfig(v)
config, err := wgtypes.ReadInConfig(v)
if err != nil {
return err
}

v.Set(args[0], args[1])

if err = v.Unmarshal(cfg); err != nil {
if err = v.Unmarshal(config); err != nil {
return err
}

return cfg.SaveToPath(path)
return config.SaveToPath(path)
},
}

Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions services/wireguard/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ func (c *Config) String() string {
}

func ReadInConfig(v *viper.Viper) (*Config, error) {
cfg := NewConfig().WithDefaultValues()
config := NewConfig().WithDefaultValues()
if err := v.ReadInConfig(); err != nil {
return nil, err
}
if err := v.Unmarshal(cfg); err != nil {
if err := v.Unmarshal(config); err != nil {
return nil, err
}

return cfg, nil
return config, nil
}
Loading

0 comments on commit 6530890

Please sign in to comment.