Skip to content

Commit

Permalink
Refactored utils package
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Mar 14, 2023
1 parent 8aa81f2 commit 26ef27f
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 33 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.idea/
/.github/
/.vscode/
/bin/
/vendor/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store*
/.idea/
/.vscode/
/bin/
/vendor/
3 changes: 2 additions & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/sentinel-official/dvpn-node/api"
"github.com/sentinel-official/dvpn-node/context"
"github.com/sentinel-official/dvpn-node/libs/geoip"
"github.com/sentinel-official/dvpn-node/lite"
"github.com/sentinel-official/dvpn-node/node"
"github.com/sentinel-official/dvpn-node/services/v2ray"
Expand Down Expand Up @@ -136,7 +137,7 @@ func StartCmd() *cobra.Command {
}

log.Info("Fetching the GeoIP location info...")
location, err := utils.FetchGeoIPLocation()
location, err := geoip.Location()
if err != nil {
return err
}
Expand Down
21 changes: 11 additions & 10 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
tmlog "github.com/tendermint/tendermint/libs/log"
"gorm.io/gorm"

geoiptypes "github.com/sentinel-official/dvpn-node/libs/geoip/types"
"github.com/sentinel-official/dvpn-node/lite"
"github.com/sentinel-official/dvpn-node/types"
)
Expand All @@ -20,7 +21,7 @@ type Context struct {
config *types.Config
database *gorm.DB
handler http.Handler
location *types.GeoIPLocation
location *geoiptypes.GeoIPLocation
logger tmlog.Logger
service types.Service
}
Expand All @@ -29,14 +30,14 @@ func NewContext() *Context {
return &Context{}
}

func (c *Context) WithBandwidth(v *hubtypes.Bandwidth) *Context { c.bandwidth = v; return c }
func (c *Context) WithClient(v *lite.Client) *Context { c.client = v; return c }
func (c *Context) WithConfig(v *types.Config) *Context { c.config = v; return c }
func (c *Context) WithDatabase(v *gorm.DB) *Context { c.database = v; return c }
func (c *Context) WithHandler(v http.Handler) *Context { c.handler = v; return c }
func (c *Context) WithLocation(v *types.GeoIPLocation) *Context { c.location = v; return c }
func (c *Context) WithLogger(v tmlog.Logger) *Context { c.logger = v; return c }
func (c *Context) WithService(v types.Service) *Context { c.service = v; return c }
func (c *Context) WithBandwidth(v *hubtypes.Bandwidth) *Context { c.bandwidth = v; return c }
func (c *Context) WithClient(v *lite.Client) *Context { c.client = v; return c }
func (c *Context) WithConfig(v *types.Config) *Context { c.config = v; return c }
func (c *Context) WithDatabase(v *gorm.DB) *Context { c.database = v; return c }
func (c *Context) WithHandler(v http.Handler) *Context { c.handler = v; return c }
func (c *Context) WithLocation(v *geoiptypes.GeoIPLocation) *Context { c.location = v; return c }
func (c *Context) WithLogger(v tmlog.Logger) *Context { c.logger = v; return c }
func (c *Context) WithService(v types.Service) *Context { c.service = v; return c }

func (c *Context) Address() hubtypes.NodeAddress { return c.Operator().Bytes() }
func (c *Context) Bandwidth() *hubtypes.Bandwidth { return c.bandwidth }
Expand All @@ -47,7 +48,7 @@ func (c *Context) Handler() http.Handler { return c.handler }
func (c *Context) IntervalSetSessions() time.Duration { return c.Config().Node.IntervalSetSessions }
func (c *Context) IntervalUpdateStatus() time.Duration { return c.Config().Node.IntervalUpdateStatus }
func (c *Context) ListenOn() string { return c.Config().Node.ListenOn }
func (c *Context) Location() *types.GeoIPLocation { return c.location }
func (c *Context) Location() *geoiptypes.GeoIPLocation { return c.location }
func (c *Context) Log() tmlog.Logger { return c.logger }
func (c *Context) Moniker() string { return c.Config().Node.Moniker }
func (c *Context) Operator() sdk.AccAddress { return c.client.FromAddress() }
Expand Down
6 changes: 3 additions & 3 deletions utils/geoip.go → libs/geoip/geoip.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package utils
package geoip

import (
"encoding/json"
"net/http"
"time"

"github.com/sentinel-official/dvpn-node/types"
"github.com/sentinel-official/dvpn-node/libs/geoip/types"
)

func FetchGeoIPLocation() (*types.GeoIPLocation, error) {
func Location() (*types.GeoIPLocation, error) {
var (
client = &http.Client{Timeout: 15 * time.Second}
path = "http://ip-api.com/json"
Expand Down
File renamed without changes.
12 changes: 6 additions & 6 deletions lite/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
vpntypes "github.com/sentinel-official/hub/x/vpn/types"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"

"github.com/sentinel-official/dvpn-node/utils"
"github.com/sentinel-official/dvpn-node/types"
)

func (c *Client) queryAccount(remote string, accAddr sdk.AccAddress) (authtypes.AccountI, error) {
Expand All @@ -36,7 +36,7 @@ func (c *Client) queryAccount(remote string, accAddr sdk.AccAddress) (authtypes.
},
)
if err != nil {
return nil, utils.QueryError(err)
return nil, types.QueryError(err)
}

var result authtypes.AccountI
Expand Down Expand Up @@ -80,7 +80,7 @@ func (c *Client) queryNode(remote string, nodeAddr hubtypes.NodeAddress) (*nodet
nodetypes.NewQueryNodeRequest(nodeAddr),
)
if err != nil {
return nil, utils.QueryError(err)
return nil, types.QueryError(err)
}

return &res.Node, nil
Expand Down Expand Up @@ -119,7 +119,7 @@ func (c *Client) querySubscription(remote string, id uint64) (*subscriptiontypes
subscriptiontypes.NewQuerySubscriptionRequest(id),
)
if err != nil {
return nil, utils.QueryError(err)
return nil, types.QueryError(err)
}

return &res.Subscription, nil
Expand Down Expand Up @@ -158,7 +158,7 @@ func (c *Client) queryQuota(remote string, id uint64, accAddr sdk.AccAddress) (*
subscriptiontypes.NewQueryQuotaRequest(id, accAddr),
)
if err != nil {
return nil, utils.QueryError(err)
return nil, types.QueryError(err)
}

return &res.Quota, nil
Expand Down Expand Up @@ -197,7 +197,7 @@ func (c *Client) querySession(remote string, id uint64) (*sessiontypes.Session,
sessiontypes.NewQuerySessionRequest(id),
)
if err != nil {
return nil, utils.QueryError(err)
return nil, types.QueryError(err)
}

return &res.Session, nil
Expand Down
4 changes: 2 additions & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/spf13/viper"

"github.com/sentinel-official/dvpn-node/context"
httputils "github.com/sentinel-official/dvpn-node/utils/http"
"github.com/sentinel-official/dvpn-node/utils"
)

type Node struct {
Expand Down Expand Up @@ -57,7 +57,7 @@ func (n *Node) Start() error {
keyFile = path.Join(viper.GetString(flags.FlagHome), "tls.key")
)

return httputils.ListenAndServeTLS(
return utils.ListenAndServeTLS(
n.ListenOn(),
certFile,
keyFile,
Expand Down
4 changes: 2 additions & 2 deletions services/v2ray/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/pkg/errors"
"github.com/spf13/viper"

randutil "github.com/sentinel-official/dvpn-node/utils/rand"
"github.com/sentinel-official/dvpn-node/utils"
)

var (
Expand Down Expand Up @@ -42,7 +42,7 @@ func NewVMessConfig() *VMessConfig {
}

func (c *VMessConfig) WithDefaultValues() *VMessConfig {
c.ListenPort = randutil.RandomPort()
c.ListenPort = utils.RandomPort()
c.Transport = "grpc"

return c
Expand Down
4 changes: 2 additions & 2 deletions services/wireguard/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/pkg/errors"
"github.com/spf13/viper"

randutil "github.com/sentinel-official/dvpn-node/utils/rand"
"github.com/sentinel-official/dvpn-node/utils"
)

var (
Expand Down Expand Up @@ -68,7 +68,7 @@ func (c *Config) WithDefaultValues() *Config {
}

c.Interface = "wg0"
c.ListenPort = randutil.RandomPort()
c.ListenPort = utils.RandomPort()
c.PrivateKey = key.String()

return c
Expand Down
4 changes: 2 additions & 2 deletions types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
hubtypes "github.com/sentinel-official/hub/types"
"github.com/spf13/viper"

randutil "github.com/sentinel-official/dvpn-node/utils/rand"
"github.com/sentinel-official/dvpn-node/utils"
)

const (
Expand Down Expand Up @@ -346,7 +346,7 @@ func (c *NodeConfig) WithDefaultValues() *NodeConfig {
c.IntervalSetSessions = 10 * time.Second
c.IntervalUpdateSessions = MaxIntervalUpdateSessions
c.IntervalUpdateStatus = MaxIntervalUpdateStatus
c.ListenOn = fmt.Sprintf("0.0.0.0:%d", randutil.RandomPort())
c.ListenOn = fmt.Sprintf("0.0.0.0:%d", utils.RandomPort())
c.Type = "wireguard"

return c
Expand Down
2 changes: 1 addition & 1 deletion utils/error.go → types/error.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package types

import (
"google.golang.org/grpc/codes"
Expand Down
4 changes: 2 additions & 2 deletions types/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ func (s *Session) GetAddress() sdk.AccAddress {
return nil
}

address, err := sdk.AccAddressFromBech32(s.Address)
v, err := sdk.AccAddressFromBech32(s.Address)
if err != nil {
panic(err)
}

return address
return v
}
2 changes: 1 addition & 1 deletion utils/http/server.go → utils/http.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package http
package utils

import (
"crypto/rand"
Expand Down
2 changes: 1 addition & 1 deletion utils/rand/int.go → utils/rand.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rand
package utils

import (
"crypto/rand"
Expand Down

0 comments on commit 26ef27f

Please sign in to comment.