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

awl-tray: many improvements #3

Merged
merged 8 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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
20 changes: 12 additions & 8 deletions api/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,28 @@ import (
func (h *Handler) GetKnownPeers(c echo.Context) (err error) {
result := make([]entity.KnownPeersResponse, 0, len(h.conf.KnownPeers))

h.conf.RLock()
peers := make([]string, 0, len(h.conf.KnownPeers))
for peerID := range h.conf.KnownPeers {
peers = append(peers, peerID)
}
h.conf.RUnlock()
sort.Strings(peers)

for _, peerID := range peers {
peer := h.conf.KnownPeers[peerID]
h.conf.RLock()
knownPeer := h.conf.KnownPeers[peerID]
h.conf.RUnlock()

id := peer.PeerId()
id := knownPeer.PeerId()
kpr := entity.KnownPeersResponse{
PeerID: peerID,
Name: peer.DisplayName(),
Name: knownPeer.DisplayName(),
Version: h.p2p.PeerVersion(id),
IpAddr: peer.IPAddr,
IpAddr: knownPeer.IPAddr,
Connected: h.p2p.IsConnected(id),
Confirmed: peer.Confirmed,
LastSeen: peer.LastSeen,
Confirmed: knownPeer.Confirmed,
LastSeen: knownPeer.LastSeen,
Addresses: h.p2p.PeerAddresses(id),
NetworkStats: h.p2p.NetworkStatsForPeer(id),
}
Expand All @@ -64,12 +68,12 @@ func (h *Handler) GetKnownPeerSettings(c echo.Context) (err error) {
if err = c.Validate(req); err != nil {
return c.JSON(http.StatusBadRequest, ErrorMessage(err.Error()))
}
peer, exists := h.conf.GetPeer(req.PeerID)
knownPeer, exists := h.conf.GetPeer(req.PeerID)
if !exists {
return c.JSON(http.StatusNotFound, ErrorMessage("peer not found"))
}

return c.JSON(http.StatusOK, peer)
return c.JSON(http.StatusOK, knownPeer)
}

// @Tags Peers
Expand Down
26 changes: 21 additions & 5 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/anywherelan/awl/api"
"github.com/anywherelan/awl/awldns"
"github.com/anywherelan/awl/awlevent"
"github.com/anywherelan/awl/config"
"github.com/anywherelan/awl/p2p"
"github.com/anywherelan/awl/protocol"
Expand All @@ -21,6 +22,7 @@ import (
"github.com/anywherelan/ts-dns/net/dns"
"github.com/anywherelan/ts-dns/util/dnsname"
"github.com/ipfs/go-log/v2"
"github.com/libp2p/go-eventbus"
"github.com/libp2p/go-libp2p-core/host"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -61,7 +63,10 @@ type Application struct {
LogBuffer *ringbuffer.RingBuffer
logger *log.ZapEventLogger
Conf *config.Config
Eventbus awlevent.Bus

ctx context.Context
ctxCancel context.CancelFunc
p2pServer *p2p.P2p
host host.Host
vpnDevice *vpn.Device
Expand All @@ -80,7 +85,8 @@ func New() *Application {
}

func (a *Application) Init(ctx context.Context, tunDevice tun.Device) error {
p2pSrv := p2p.NewP2p(ctx, a.Conf)
a.ctx, a.ctxCancel = context.WithCancel(ctx)
p2pSrv := p2p.NewP2p(a.ctx, a.Conf)
host, err := p2pSrv.InitHost()
if err != nil {
return err
Expand Down Expand Up @@ -108,7 +114,7 @@ func (a *Application) Init(ctx context.Context, tunDevice tun.Device) error {
}

a.P2pService = service.NewP2p(p2pSrv, a.Conf)
a.AuthStatus = service.NewAuthStatus(a.P2pService, a.Conf)
a.AuthStatus = service.NewAuthStatus(a.P2pService, a.Conf, a.Eventbus)
a.Tunnel = service.NewTunnel(a.P2pService, vpnDevice, a.Conf)

host.SetStreamHandler(protocol.GetStatusMethod, a.AuthStatus.StatusStreamHandler)
Expand All @@ -134,10 +140,11 @@ func (a *Application) Init(ctx context.Context, tunDevice tun.Device) error {
}

func (a *Application) SetupLoggerAndConfig() *log.ZapEventLogger {
a.Eventbus = eventbus.NewBus()
// Config
conf, loadConfigErr := config.LoadConfig()
conf, loadConfigErr := config.LoadConfig(a.Eventbus)
if loadConfigErr != nil {
conf = config.NewConfig()
conf = config.NewConfig(a.Eventbus)
}

// Logger
Expand Down Expand Up @@ -192,7 +199,14 @@ func (a *Application) SetupLoggerAndConfig() *log.ZapEventLogger {
return a.logger
}

func (a *Application) Ctx() context.Context {
return a.ctx
}

func (a *Application) Close() {
if a.ctxCancel != nil {
a.ctxCancel()
}
if a.Api != nil {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
Expand Down Expand Up @@ -232,7 +246,9 @@ func (a *Application) initDNS() {
return
}
a.dnsResolver = awldns.NewResolver()
a.Conf.RegisterOnKnownPeersChanged(a.refreshDNSConfig)
awlevent.WrapSubscriptionToCallback(a.ctx, func(_ interface{}) {
a.refreshDNSConfig()
}, a.Eventbus, new(awlevent.KnownPeerChanged))
defer a.refreshDNSConfig()

tsLogger := log.Logger("ts/dnsconf")
Expand Down
3 changes: 2 additions & 1 deletion application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/anywherelan/awl/config"
"github.com/anywherelan/awl/entity"
"github.com/ipfs/go-log/v2"
"github.com/libp2p/go-eventbus"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
"golang.zx2c4.com/wireguard/tun/tuntest"
Expand Down Expand Up @@ -117,7 +118,7 @@ func newTestPeer(t testing.TB, disableLogging bool) testPeer {
tempDir := t.TempDir()
a.NoError(os.Setenv(config.AppDataDirEnvKey, tempDir))
if disableLogging {
tempConf := config.NewConfig()
tempConf := config.NewConfig(eventbus.NewBus())
tempConf.LoggerLevel = "fatal"
tempConf.Save()
}
Expand Down
43 changes: 43 additions & 0 deletions awlevent/awlevent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package awlevent

import (
"context"

"github.com/anywherelan/awl/protocol"
"github.com/libp2p/go-libp2p-core/event"
)

type Bus = event.Bus
type Emitter = event.Emitter

type KnownPeerChanged struct {
}

type ReceivedAuthRequest struct {
protocol.AuthPeer
PeerID string
}

func WrapSubscriptionToCallback(ctx context.Context, callback func(interface{}), bus Bus,
eventType interface{}, opts ...event.SubscriptionOpt) {
sub, err := bus.Subscribe(eventType, opts...)
if err != nil {
panic(err)
}

go func() {
defer sub.Close()

for {
select {
case ev, ok := <-sub.Out():
if !ok {
return
}
callback(ev)
case <-ctx.Done():
return
}
}
}()
}
3 changes: 2 additions & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/anywherelan/awl/application/pkg"
"github.com/anywherelan/awl/config"
"github.com/ipfs/go-log/v2"
"github.com/libp2p/go-eventbus"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -72,7 +73,7 @@ func (a *Application) init() {
addr = apiAddr
return nil
}
conf, err := config.LoadConfig()
conf, err := config.LoadConfig(eventbus.NewBus())
if err != nil {
return fmt.Errorf("could not load config, api_addr does not set, exit (%v)", err)
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/awl-tray/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ go 1.16
require (
github.com/Kodeworks/golang-image-ico v0.0.0-20141118225523-73f0f4cfade9
github.com/anywherelan/awl v0.0.0-00010101000000-000000000000
github.com/getlantern/systray v1.0.2
github.com/gen2brain/beeep v0.0.0-20210529141713-5586760f0cc1
github.com/getlantern/systray v1.1.0
github.com/godbus/dbus/v5 v5.0.4
github.com/ipfs/go-log/v2 v2.1.3
github.com/ncruces/zenity v0.7.7
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
)

replace (
github.com/anywherelan/awl => ../../
github.com/godbus/dbus/v5 => github.com/pymq/dbus/v5 v5.0.5-0.20210710104724-7ba66a7d9a5a
github.com/ipfs/go-log/v2 => github.com/anywherelan/go-log/v2 v2.0.3-0.20210308150645-ad120b957e42
)
Loading