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

Feature/public visors advertising #743

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
d6f9fa9
Advertise public visors.
taras-skycoin Nov 18, 2020
0f2dfe8
Fix linter
taras-skycoin Nov 18, 2020
c1f2a73
Add explicit return.
taras-skycoin Nov 18, 2020
c3ecaf2
Remove public_address field;Fix linter.
taras-skycoin Nov 19, 2020
9b5cb14
Add ReadOnly address resolver client.
taras-skycoin Nov 19, 2020
1ca9025
Add boolean field wich indicates if visor is on public address.
taras-skycoin Nov 19, 2020
741a4b4
Pass STCPR port to service-discover port.
taras-skycoin Nov 19, 2020
2a1cc24
Merge remote-tracking branch 'upstream/develop' into advertise_public…
taras-skycoin Nov 19, 2020
17d8596
Fix sleep delays.
taras-skycoin Nov 19, 2020
d840013
Merge branch 'develop' of github.com:skycoin/skywire into advertise_p…
Darkren Feb 1, 2021
4426259
Start merging old trusted visors with the new public visor concept
Darkren Feb 2, 2021
9eba538
Add func to fetch default network interface name for OSX
Darkren Feb 2, 2021
ce12e5b
Include local visor IPs in the update request for visor service
Darkren Feb 2, 2021
00fc277
Clean up the code
Darkren Feb 2, 2021
6568a24
Fix import cycle, fix visor test
Darkren Feb 2, 2021
0ed9947
Merge branch 'develop' of github.com:skycoin/skywire into feature/pub…
Darkren Mar 16, 2021
c9ab872
Merge branch 'develop' of github.com:skycoin/skywire into feature/pub…
Darkren Mar 28, 2021
9d74cd9
Add debug logs
Darkren Mar 28, 2021
6ffe9d3
Update deps
Darkren Mar 28, 2021
fb05117
Add debug logs
Darkren Mar 28, 2021
a90a855
Make service updater stop retrying if visor is unreachable
Darkren Mar 30, 2021
4cc790f
Properly stop ticker on updater exit
Darkren Mar 30, 2021
083cf99
Remove debug logs
Darkren Mar 30, 2021
c25e632
Merge branch 'develop' into feature/public-visors-advertising
i-hate-nicknames Apr 27, 2021
0f249ce
Run make format
i-hate-nicknames Apr 27, 2021
dbc25f6
Use errors.Is instead of string matching
i-hate-nicknames Apr 27, 2021
6af273c
Add exponential backoff to service update entry
i-hate-nicknames Apr 27, 2021
d7b23ae
Refactor entry update loop
i-hate-nicknames Apr 27, 2021
0ebc644
Move POST service discovery to a separate func
i-hate-nicknames Apr 27, 2021
10b8760
Add DefaultNetworkInterfaceIPs netutil function
i-hate-nicknames Apr 27, 2021
a78aea4
Update config for pulic visors
i-hate-nicknames Apr 28, 2021
0055a0c
Add IsPublicIp function to netutil
i-hate-nicknames Apr 28, 2021
25b7ca0
Check is_public field for self advertising
i-hate-nicknames Apr 28, 2021
380ad39
Move public visor connection to autoconnector
i-hate-nicknames Apr 28, 2021
cc98efa
Keep track of the connected services
i-hate-nicknames Apr 28, 2021
a24fd9c
Add exponential backoff for fetching services
i-hate-nicknames Apr 28, 2021
31498e0
Fix lint errors
i-hate-nicknames Apr 29, 2021
443e8f5
Check if transport is up
i-hate-nicknames May 5, 2021
263a27a
Add codereview fixes: todos, error handling
i-hate-nicknames May 7, 2021
a90627e
Refactor URL construction
i-hate-nicknames May 11, 2021
3974489
Add app disc refactoring todo
i-hate-nicknames May 11, 2021
b293588
Take visor self-advertising out of snet package
i-hate-nicknames May 11, 2021
5616644
Use transport manager in autoconnector
i-hate-nicknames May 11, 2021
207d063
Merge branch 'develop' into feature/public-visors-advertising
i-hate-nicknames May 12, 2021
f86bfb0
Add public visor advertisement as init module
i-hate-nicknames May 12, 2021
0a54576
Use transport labels in connecting to public visors
i-hate-nicknames May 13, 2021
e3fd124
Merge branch 'develop' into feature/public-visors-advertising
i-hate-nicknames May 13, 2021
c1017cc
Use canonical names for variables
i-hate-nicknames May 17, 2021
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
Empty file added Char
Empty file.
14 changes: 9 additions & 5 deletions internal/vpn/os_client_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func DefaultNetworkGateway() (net.IP, error) {

var setupClientOnce sync.Once

func setupClientSysPrivileges() (suid int, err error) {
func setupClientSysPrivileges() (int, error) {
var err error
setupClientOnce.Do(func() {
var caps capability.Capabilities

Expand All @@ -64,15 +65,18 @@ func setupClientSysPrivileges() (suid int, err error) {

// set `CAP_NET_ADMIN` capability to needed caps sets.
caps.Set(capability.CAPS|capability.BOUNDS|capability.AMBIENT, capability.CAP_NET_ADMIN)
if e := caps.Apply(capability.CAPS | capability.BOUNDS | capability.AMBIENT); e != nil {
err = fmt.Errorf("failed to apply capabilties: %w", e)
err = caps.Apply(capability.CAPS | capability.BOUNDS | capability.AMBIENT)
if err != nil {
err = fmt.Errorf("failed to apply capabilties: %w", err)

return
}

// let child process keep caps sets from the parent, so we may do calls to
// system utilities with these caps.
if e := unix.Prctl(unix.PR_SET_KEEPCAPS, 1, 0, 0, 0); e != nil {
err = fmt.Errorf("failed to set PR_SET_KEEPCAPS: %w", e)
err = unix.Prctl(unix.PR_SET_KEEPCAPS, 1, 0, 0, 0)
if err != nil {
err = fmt.Errorf("failed to set PR_SET_KEEPCAPS: %w", err)
return
}
})
Expand Down
124 changes: 124 additions & 0 deletions pkg/servicedisc/autoconnect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package servicedisc

import (
"context"
"time"

"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/skycoin/src/util/logging"

"github.com/skycoin/skywire/internal/netutil"
"github.com/skycoin/skywire/pkg/snet/directtp/tptypes"
"github.com/skycoin/skywire/pkg/transport"
)

const (
// PublicServiceDelay defines a delay before adding transports to public services.
PublicServiceDelay = 10 * time.Second

fetchServicesDelay = 2 * time.Second
)

// ConnectFn provides a way to connect to remote service
type ConnectFn func(context.Context, cipher.PubKey) error

// Autoconnector continuously tries to connect to services
type Autoconnector interface {
Run(context.Context) error
}

type autoconnector struct {
client *HTTPClient
maxConns int
log *logging.Logger
tm *transport.Manager
}

// MakeConnector returns a new connector that will try to connect to at most maxConns
// services
func MakeConnector(conf Config, maxConns int, tm *transport.Manager, log *logging.Logger) Autoconnector {
connector := &autoconnector{}
connector.client = NewClient(log, conf)
connector.maxConns = maxConns
connector.log = log
connector.tm = tm
return connector
}

// Run implements Autoconnector interface
func (a *autoconnector) Run(ctx context.Context) error {
for {
time.Sleep(PublicServiceDelay)
a.log.Infof("Fetching public visors")
addresses, err := a.fetchPubAddresses(ctx)
if err != nil {
a.log.Errorf("Cannot fetch public services: %s", err)
}

tps := a.updateTransports()
absent := a.filterDuplicates(addresses, tps)
for _, pk := range absent {
a.log.WithField("pk", pk).Infoln("Adding transport to public visor")
logger := a.log.WithField("pk", pk).WithField("type", tptypes.STCPR)
if _, err := a.tm.SaveTransport(ctx, pk, tptypes.STCPR, transport.LabelAutomatic); err != nil {
logger.WithError(err).Warnln("Failed to add transport to public visor")
continue
}
logger.Infoln("Added transport to public visor")
}
}
}

// Remove all inactive automatic transports and return all active
// automatic transports
func (a *autoconnector) updateTransports() []*transport.ManagedTransport {
tps := a.tm.GetTransportsByLabel(transport.LabelAutomatic)
var tpsActive []*transport.ManagedTransport
for _, tr := range tps {
if !tr.IsUp() {
a.tm.DeleteTransport(tr.Entry.ID)
} else {
tpsActive = append(tpsActive, tr)
}
}
return tpsActive
}

func (a *autoconnector) fetchPubAddresses(ctx context.Context) ([]cipher.PubKey, error) {
retrier := netutil.NewRetrier(fetchServicesDelay, 0, 2)
var services []Service
fetch := func() (err error) {
// "return" services up from the closure
services, err = a.client.Services(ctx, a.maxConns)
if err != nil {
return err
}
return nil
}
if err := retrier.Do(fetch); err != nil {
return nil, err
}
var pks []cipher.PubKey
for _, service := range services {
pks = append(pks, service.Addr.PubKey())
}
return pks, nil
}

// return public keys from pks that are absent in given list of transports
func (a *autoconnector) filterDuplicates(pks []cipher.PubKey, trs []*transport.ManagedTransport) []cipher.PubKey {
var absent []cipher.PubKey
for _, pk := range pks {
found := false
for _, tr := range trs {
if tr.Entry.HasEdge(pk) {
found = true
break
}
}
if !found {
absent = append(absent, pk)
}
}
return absent
}
Loading