Skip to content

Commit

Permalink
Feature | Server Type (#245)
Browse files Browse the repository at this point in the history
* go mod vendor

* - add --auth flag for dmsg-discovery and dmsg-server as simple authentication for recognize official dmsg servers
- update dmsg server entry structure and add serverType value there, the value be `official` or `community`

* remove depguard from golagnci-lint as deprecated

* update CHANGELOG.md

* update README.md in integration

* update go-chi t v5.0.11

* use go1.21.5 toolchain

* upgrade setup-go@v4

* use golang 1.21.4 instead 1.21.5, because of golangci-lint issue

* add --official-servers flag for get list of official dmsg-servers keys, separated by comma

* update CHANGELOG.md
  • Loading branch information
mrpalide committed Dec 29, 2023
1 parent ee0160c commit 6351a24
Show file tree
Hide file tree
Showing 329 changed files with 17,973 additions and 18,525 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ jobs:
linux:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
- uses: actions/setup-go@v4
with:
go-version: 1.21.x
go-version: 1.21.4
- uses: actions/checkout@v3
- name: Install Requirements
run: |
Expand All @@ -23,7 +23,7 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.21.x
go-version: 1.21.4
- uses: actions/checkout@v3
- name: Install Requirements
run: |
Expand All @@ -40,7 +40,7 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.21.x
go-version: 1.21.4
- uses: actions/checkout@v3
- name: Install Requirements
run: |
Expand Down
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ linters:
- megacheck
- misspell
- nakedret
- depguard
enable-all: false
disable:
disable-all: true
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

updates may be generated with scripts/changelog.sh <PR#lowest> <PR#highest>

## 1.3.17

### Added
- add `--auth` flag as simple authentication logic for recognize official dmsg servers
- add `--official-server` flag for fetch dmsg servers list as official
- add `serverType` in dmsg server entries for showing type of it, as `official` or `community`

## 1.3.14

### Added
Expand Down
24 changes: 23 additions & 1 deletion cmd/dmsg-discovery/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands

import (
"context"
"errors"
"fmt"
"log"
"net"
Expand Down Expand Up @@ -45,12 +46,16 @@ var (
pk cipher.PubKey
sk cipher.SecKey
dmsgPort uint16
authPassphrase string
officialServers string
)

func init() {
sf.Init(RootCmd, "dmsg_disc", "")

RootCmd.Flags().StringVarP(&addr, "addr", "a", ":9090", "address to bind to")
RootCmd.Flags().StringVar(&authPassphrase, "auth", "", "auth passphrase as simple auth for official dmsg servers registration")
RootCmd.Flags().StringVar(&officialServers, "official-servers", "", "list of official dmsg servers keys separated by comma")
RootCmd.Flags().StringVar(&redisURL, "redis", store.DefaultURL, "connections string for a redis store")
RootCmd.Flags().StringVar(&whitelistKeys, "whitelist-keys", "", "list of whitelisted keys of network monitor used for deregistration")
RootCmd.Flags().DurationVar(&entryTimeout, "entry-timeout", store.DefaultTimeout, "discovery entry timeout")
Expand Down Expand Up @@ -111,7 +116,7 @@ var RootCmd = &cobra.Command{

// we enable metrics middleware if address is passed
enableMetrics := sf.MetricsAddr != ""
a := api.New(log, db, m, testMode, enableLoadTesting, enableMetrics, dmsgAddr)
a := api.New(log, db, m, testMode, enableLoadTesting, enableMetrics, dmsgAddr, authPassphrase)

var whitelistPKs []string
if whitelistKeys != "" {
Expand All @@ -128,6 +133,11 @@ var RootCmd = &cobra.Command{
api.WhitelistPKs.Set(v)
}

a.OfficialServers, err = fetchOfficialDmsgServers(officialServers)
if err != nil {
log.Info(err)
}

go a.RunBackgroundTasks(ctx, log)
log.WithField("addr", addr).Info("Serving discovery API...")
go func() {
Expand Down Expand Up @@ -277,3 +287,15 @@ func listenAndServe(addr string, handler http.Handler) error {
defer proxyListener.Close() // nolint:errcheck
return srv.Serve(proxyListener)
}

func fetchOfficialDmsgServers(officialServers string) (map[string]bool, error) {
dmsgServers := make(map[string]bool)
if officialServers != "" {
dmsgServersList := strings.Split(officialServers, ",")
for _, v := range dmsgServersList {
dmsgServers[v] = true
}
return dmsgServers, nil
}
return dmsgServers, errors.New("no official dmsg server list passed by --official-server flag")
}
7 changes: 5 additions & 2 deletions cmd/dmsg-server/commands/start/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ import (
)

var (
sf cmdutil.ServiceFlags
limitIP int
sf cmdutil.ServiceFlags
limitIP int
authPassphrase string
)

func init() {
sf.Init(RootCmd, "dmsg_srv", dmsgserver.DefaultConfigPath)
RootCmd.Flags().IntVar(&limitIP, "limit-ip", 15, "set limitation of IPs want connect to specific dmsg-server, default value is 15")
RootCmd.Flags().StringVar(&authPassphrase, "auth", "", "auth passphrase as simple auth for official dmsg servers registration")
}

// RootCmd contains commands for dmsg-server
Expand Down Expand Up @@ -93,6 +95,7 @@ var RootCmd = &cobra.Command{
MaxSessions: conf.MaxSessions,
UpdateInterval: conf.UpdateInterval,
LimitIP: limitIP,
AuthPassphrase: authPassphrase,
}
srv := dmsg.NewServer(conf.PubKey, conf.SecKey, disc.NewHTTP(conf.Discovery, &http.Client{}, log), &srvConf, m)
srv.SetLogger(log)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ module github.com/skycoin/dmsg

go 1.21

toolchain go1.21.3
toolchain go1.21.4

require (
github.com/ActiveState/termtest/conpty v0.5.0
github.com/VictoriaMetrics/metrics v1.18.1
github.com/confiant-inc/go-socks5 v0.0.0-20210816151940-c1124825b1d6
github.com/creack/pty v1.1.15
github.com/gin-gonic/gin v1.9.1
github.com/go-chi/chi/v5 v5.0.8-0.20220103230436-7dbe9a0bd10f
github.com/go-chi/chi/v5 v5.0.11
github.com/go-redis/redis/v8 v8.11.5
github.com/hashicorp/yamux v0.1.1
github.com/ivanpirog/coloredcobra v1.0.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
github.com/go-chi/chi/v5 v5.0.8-0.20220103230436-7dbe9a0bd10f h1:6kLofhLkWj7lgCc+mvcVLnwhTzQYgL/yW/Y0e/JYwjg=
github.com/go-chi/chi/v5 v5.0.8-0.20220103230436-7dbe9a0bd10f/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/chi/v5 v5.0.11 h1:BnpYbFZ3T3S1WMpD79r7R5ThWX40TaFB7L31Y8xqSwA=
github.com/go-chi/chi/v5 v5.0.11/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
Expand Down
4 changes: 2 additions & 2 deletions integration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
```
5. Start `dmsg-server`.
```bash
$ ./bin/dmsg-server ./integration/configs/dmsgserver1.json
$ ./bin/dmsg-server start ./integration/configs/dmsgserver1.json
```

## Put dmsg-server under load
Expand Down Expand Up @@ -48,4 +48,4 @@ For close all visors and delete generated configs, use these two commands:
```
pkill -9 -f 'skywire-visor -c ./config'
rm config*
```
```
16 changes: 14 additions & 2 deletions internal/dmsg-discovery/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/skycoin/dmsg/internal/discmetrics"
"github.com/skycoin/dmsg/internal/dmsg-discovery/store"
"github.com/skycoin/dmsg/pkg/disc"
"github.com/skycoin/dmsg/pkg/dmsg"
)

var log = logging.MustGetLogger("dmsg-discovery")
Expand All @@ -44,10 +45,12 @@ type API struct {
enableLoadTesting bool
dmsgAddr string
DmsgServers []string
authPassphrase string
OfficialServers map[string]bool
}

// New returns a new API object, which can be started as a server
func New(log logrus.FieldLogger, db store.Storer, m discmetrics.Metrics, testMode, enableLoadTesting, enableMetrics bool, dmsgAddr string) *API {
func New(log logrus.FieldLogger, db store.Storer, m discmetrics.Metrics, testMode, enableLoadTesting, enableMetrics bool, dmsgAddr, authPassphrase string) *API {
if log != nil {
log = logging.MustGetLogger("dmsg_disc")
}
Expand All @@ -67,6 +70,8 @@ func New(log logrus.FieldLogger, db store.Storer, m discmetrics.Metrics, testMod
reqsInFlightCountMiddleware: metricsutil.NewRequestsInFlightCountMiddleware(),
dmsgAddr: dmsgAddr,
DmsgServers: []string{},
authPassphrase: authPassphrase,
OfficialServers: make(map[string]bool),
}

r.Use(middleware.RequestID)
Expand Down Expand Up @@ -251,7 +256,6 @@ func (a *API) setEntry() func(w http.ResponseWriter, r *http.Request) {
if timeout := r.URL.Query().Get("timeout"); timeout == "true" {
entryTimeout = store.DefaultTimeout
}

entry := new(disc.Entry)
if err := json.NewDecoder(r.Body).Decode(entry); err != nil {
a.handleError(w, r, disc.ErrUnexpected)
Expand Down Expand Up @@ -287,6 +291,14 @@ func (a *API) setEntry() func(w http.ResponseWriter, r *http.Request) {
}
}

if entry.Server != nil {
if entry.Server.ServerType == a.authPassphrase || a.OfficialServers[entry.Static.Hex()] {
entry.Server.ServerType = dmsg.DefaultOfficialDmsgServerType
} else {
entry.Server.ServerType = dmsg.DefaultCommunityDmsgServerType
}
}

// Recover previous entry. If key not found we insert with sequence 0
// If there was a previous entry we check the new one is a valid iteration
oldEntry, err := a.db.Entry(r.Context(), entry.Static)
Expand Down
2 changes: 1 addition & 1 deletion internal/dmsg-discovery/api/entries_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func TestEntriesEndpoint(t *testing.T) {
tc.storerPreHook(t, dbMock, &tc.entry)
}

api := New(nil, dbMock, discmetrics.NewEmpty(), true, false, true, "")
api := New(nil, dbMock, discmetrics.NewEmpty(), true, false, true, "", "")
req, err := http.NewRequest(tc.method, tc.endpoint, bytes.NewBufferString(tc.httpBody))
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion internal/dmsg-discovery/api/error_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestErrorHandler(t *testing.T) {
tc := tc
t.Run(tc.err.Error(), func(t *testing.T) {
w := httptest.NewRecorder()
api := New(nil, store.NewMock(), discmetrics.NewEmpty(), true, false, true, "")
api := New(nil, store.NewMock(), discmetrics.NewEmpty(), true, false, true, "", "")
api.handleError(w, &http.Request{}, tc.err)

msg := new(disc.HTTPMessage)
Expand Down
2 changes: 1 addition & 1 deletion internal/dmsg-discovery/api/get_available_servers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestGetAvailableServers(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
db, entries := tc.databaseAndEntries(t)

api := New(nil, db, discmetrics.NewEmpty(), true, false, true, "")
api := New(nil, db, discmetrics.NewEmpty(), true, false, true, "", "")
req, err := http.NewRequest(tc.method, tc.endpoint, nil)
require.NoError(t, err)

Expand Down
3 changes: 3 additions & 0 deletions pkg/disc/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ type Server struct {

// AvailableSessions is the number of available sessions that the server can currently accept.
AvailableSessions int `json:"availableSessions"`

// ServerType of DMSG Server, be `official` of `community`
ServerType string `json:"serverType,omitempty"`
}

// String implements stringer
Expand Down
4 changes: 4 additions & 0 deletions pkg/dmsg/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ const (
DefaultMaxSessions = 100

DefaultDmsgHTTPPort = uint16(80)

DefaultOfficialDmsgServerType = "official"

DefaultCommunityDmsgServerType = "community"
)
10 changes: 7 additions & 3 deletions pkg/dmsg/entity_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (c *EntityCommon) delSession(ctx context.Context, pk cipher.PubKey) {

// updateServerEntry updates the dmsg server's entry within dmsg discovery.
// If 'addr' is an empty string, the Entry.addr field will not be updated in discovery.
func (c *EntityCommon) updateServerEntry(ctx context.Context, addr string, maxSessions int) (err error) {
func (c *EntityCommon) updateServerEntry(ctx context.Context, addr string, maxSessions int, authPassphrase string) (err error) {
if addr == "" {
panic("updateServerEntry cannot accept empty 'addr' input") // this should never happen
}
Expand All @@ -170,6 +170,10 @@ func (c *EntityCommon) updateServerEntry(ctx context.Context, addr string, maxSe
return errors.New("entry in discovery is not of a dmsg server")
}

if authPassphrase != "" {
entry.Server.ServerType = authPassphrase
}

sessionsDelta := entry.Server.AvailableSessions != availableSessions
addrDelta := entry.Server.Address != addr

Expand All @@ -192,7 +196,7 @@ func (c *EntityCommon) updateServerEntry(ctx context.Context, addr string, maxSe
return c.dc.PutEntry(ctx, c.sk, entry)
}

func (c *EntityCommon) updateServerEntryLoop(ctx context.Context, addr string, maxSessions int) {
func (c *EntityCommon) updateServerEntryLoop(ctx context.Context, addr string, maxSessions int, authPassphrase string) {
t := time.NewTimer(c.updateInterval)
defer t.Stop()

Expand All @@ -208,7 +212,7 @@ func (c *EntityCommon) updateServerEntryLoop(ctx context.Context, addr string, m
}

c.sessionsMx.Lock()
err := c.updateServerEntry(ctx, addr, maxSessions)
err := c.updateServerEntry(ctx, addr, maxSessions, authPassphrase)
c.sessionsMx.Unlock()

if err != nil {
Expand Down
12 changes: 8 additions & 4 deletions pkg/dmsg/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ServerConfig struct {
MaxSessions int
UpdateInterval time.Duration
LimitIP int
AuthPassphrase string
}

// DefaultServerConfig returns the default server config.
Expand Down Expand Up @@ -54,6 +55,8 @@ type Server struct {
limitIP int
ipCounter map[string]int
ipCounterLocker sync.RWMutex

authPassphrase string
}

// NewServer creates a new dmsg server entity.
Expand All @@ -74,13 +77,14 @@ func NewServer(pk cipher.PubKey, sk cipher.SecKey, dc disc.APIClient, conf *Serv
s.addrDone = make(chan struct{})
s.maxSessions = conf.MaxSessions
s.setSessionCallback = func(ctx context.Context) error {
return s.updateServerEntry(ctx, s.AdvertisedAddr(), s.maxSessions)
return s.updateServerEntry(ctx, s.AdvertisedAddr(), s.maxSessions, conf.AuthPassphrase)
}
s.delSessionCallback = func(ctx context.Context) error {
return s.updateServerEntry(ctx, s.AdvertisedAddr(), s.maxSessions)
return s.updateServerEntry(ctx, s.AdvertisedAddr(), s.maxSessions, conf.AuthPassphrase)
}
s.ipCounter = make(map[string]int)
s.limitIP = conf.LimitIP
s.authPassphrase = conf.AuthPassphrase
return s
}

Expand Down Expand Up @@ -186,13 +190,13 @@ func (s *Server) Serve(lis net.Listener, addr string) error {

func (s *Server) startUpdateEntryLoop(ctx context.Context) error {
err := netutil.NewDefaultRetrier(s.log).Do(ctx, func() error {
return s.updateServerEntry(ctx, s.AdvertisedAddr(), s.maxSessions)
return s.updateServerEntry(ctx, s.AdvertisedAddr(), s.maxSessions, s.authPassphrase)
})
if err != nil {
return err
}

go s.updateServerEntryLoop(ctx, s.AdvertisedAddr(), s.maxSessions)
go s.updateServerEntryLoop(ctx, s.AdvertisedAddr(), s.maxSessions, s.authPassphrase)
return nil
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion vendor/github.com/Azure/go-ansiterm/winterm/ansi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion vendor/github.com/Azure/go-ansiterm/winterm/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6351a24

Please sign in to comment.