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

Merge all skywire deployment repos [WIP] #1635

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
45 changes: 45 additions & 0 deletions cmd/address-resolver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Address Resolver

## API endpoints

### POST `/bind/stcpr`
Binds the visor with `STCPR` and saves the connection data.

### DELETE `bind/stcpr`
Deletes the bind of a visor(only a visor can delete it's own bind).

### GET `/resolve/{type}/{pk}`
Gets the bind info of a PK and it's binded transport type either `STCPR` or `SUDPH` if available.

### GET `/health`
Gets the health info of the service. e.g.
```
{
"build_info": {
"version": "v1.0.1-267-ge1617c5b",
"commit": "e1617c5b0121182cfd2b610dc518e4753e56440e",
"date": "2022-10-25T11:01:52Z"
},
"started_at": "2022-10-25T11:10:45.152629597Z"
}
```

### GET `/transports`
Gets the list of PK's binded as `STCPR` and `SUDPH`. e.g.
```
{
"sudph": [
"0218905f5d9079bab0b62985a05bd162623b193e948e17e7b719133f2c60b92093",
"0214456f6727b0dffacc3e4a9b331ff9bf7b7d97a9810c213772199f0f7ee59247"
],
"stcpr": [
"0218905f5d9079bab0b62985a05bd162623b193e948e17e7b719133f2c60b92093"
]
}
```

### DELETE `/deregister/{network}`
Deletes the binding of the PK's mentioned in the request. Can only be used by services that are whitelisted to use it.

### GET `/security/nonces/{pk}`
Gets the nonce for a particular PK. Used by the nonce store.
10 changes: 10 additions & 0 deletions cmd/address-resolver/address-resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Package main cmd/address-resolver/address-resolver.go
package main

import (
"github.com/skycoin/skywire/cmd/address-resolver/commands"
)

func main() {
commands.Execute()
}
252 changes: 252 additions & 0 deletions cmd/address-resolver/commands/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
// Package commands cmd/address-resolver/commands/root.go
package commands

import (
"context"
"fmt"
"log"
"log/syslog"
"os"
"strings"

cc "github.com/ivanpirog/coloredcobra"
logrussyslog "github.com/sirupsen/logrus/hooks/syslog"
"github.com/spf13/cobra"
"github.com/xtaci/kcp-go"

"github.com/skycoin/skywire/internal/armetrics"
"github.com/skycoin/skywire/pkg/address-resolver/api"
"github.com/skycoin/skywire/pkg/address-resolver/store"
"github.com/skycoin/skywire/pkg/buildinfo"
"github.com/skycoin/skywire/pkg/cipher"
"github.com/skycoin/skywire/pkg/cmdutil"
"github.com/skycoin/skywire/pkg/direct"
"github.com/skycoin/skywire/pkg/dmsg"
"github.com/skycoin/skywire/pkg/dmsghttp"
"github.com/skycoin/skywire/pkg/httpauth"
"github.com/skycoin/skywire/pkg/logging"
"github.com/skycoin/skywire/pkg/metricsutil"
"github.com/skycoin/skywire/pkg/storeconfig"
"github.com/skycoin/skywire/pkg/tcpproxy"
)

const (
redisPrefix = "address-resolver"
redisScheme = "redis://"
)

var (
addr string
metricsAddr string
redisURL string
redisPoolSize int
logEnabled bool
syslogAddr string
tag string
testing bool
dmsgDisc string
whitelistKeys string
testEnvironment bool
sk cipher.SecKey
dmsgPort uint16
)

func init() {
RootCmd.Flags().StringVarP(&addr, "addr", "a", ":9093", "address to bind to\033[0m")
RootCmd.Flags().StringVarP(&metricsAddr, "metrics", "m", "", "address to bind metrics API to\033[0m")
RootCmd.Flags().StringVar(&redisURL, "redis", "redis://localhost:6379", "connections string for a redis store\033[0m")
RootCmd.Flags().IntVar(&redisPoolSize, "redis-pool-size", 10, "redis connection pool size\033[0m")
RootCmd.Flags().BoolVarP(&logEnabled, "log", "l", true, "enable request logging\033[0m")
RootCmd.Flags().StringVar(&syslogAddr, "syslog", "", "syslog server address. E.g. localhost:514\033[0m")
RootCmd.Flags().StringVar(&tag, "tag", "address_resolver", "logging tag\033[0m")
RootCmd.Flags().BoolVarP(&testing, "testing", "t", false, "enable testing to start without redis\033[0m")
RootCmd.Flags().StringVar(&dmsgDisc, "dmsg-disc", "http://dmsgd.skywire.skycoin.com", "url of dmsg-discovery\033[0m")
RootCmd.Flags().StringVar(&whitelistKeys, "whitelist-keys", "", "list of whitelisted keys of network monitor used for deregistration\033[0m")
RootCmd.Flags().BoolVar(&testEnvironment, "test-environment", false, "distinguished between prod and test environment\033[0m")
RootCmd.Flags().Var(&sk, "sk", "dmsg secret key\r")
RootCmd.Flags().Uint16Var(&dmsgPort, "dmsgPort", dmsg.DefaultDmsgHTTPPort, "dmsg port value\r")
var helpflag bool
RootCmd.SetUsageTemplate(help)
RootCmd.PersistentFlags().BoolVarP(&helpflag, "help", "h", false, "help for "+RootCmd.Use)
RootCmd.SetHelpCommand(&cobra.Command{Hidden: true})
RootCmd.PersistentFlags().MarkHidden("help") //nolint
}

var RootCmd = &cobra.Command{
Use: "ar",
Short: "Address Resolver Server for skywire",
Long: `
┌─┐┌┬┐┌┬┐┬─┐┌─┐┌─┐┌─┐ ┬─┐┌─┐┌─┐┌─┐┬ ┬ ┬┌─┐┬─┐
├─┤ ││ ││├┬┘├┤ └─┐└─┐───├┬┘├┤ └─┐│ ││ └┐┌┘├┤ ├┬┘
┴ ┴─┴┘─┴┘┴└─└─┘└─┘└─┘ ┴└─└─┘└─┘└─┘┴─┘└┘ └─┘┴└─`,
SilenceErrors: true,
SilenceUsage: true,
DisableSuggestions: true,
DisableFlagsInUseLine: true,
Version: buildinfo.Version(),
Run: func(_ *cobra.Command, _ []string) {
if _, err := buildinfo.Get().WriteTo(os.Stdout); err != nil {
log.Printf("Failed to output build info: %v", err)
}

if !strings.HasPrefix(redisURL, redisScheme) {
redisURL = redisScheme + redisURL
}
storeConfig := storeconfig.Config{
Type: storeconfig.Redis,
URL: redisURL,
Password: storeconfig.RedisPassword(),
PoolSize: redisPoolSize,
}

if testing {
storeConfig.Type = storeconfig.Memory
}

var logger *logging.Logger
if logEnabled {
logger = logging.MustGetLogger(tag)
} else {
logger = nil
}

ctx, cancel := cmdutil.SignalContext(context.Background(), logger)
defer cancel()

transportStore, err := store.New(ctx, storeConfig, logger)
if err != nil {
logger.Fatal("Failed to initialize redis store: ", err)
}

// var whitelistPKs []string
// if whitelistKeys != "" {
// whitelistPKs = strings.Split(whitelistKeys, ",")
// } else {
// if testEnvironment {
// whitelistPKs = strings.Split(utilenv.TestNetworkMonitorPKs, ",")
// } else {
// whitelistPKs = strings.Split(utilenv.NetworkMonitorPKs, ",")
// }
// }
//
// for _, v := range whitelistPKs {
// api.WhitelistPKs.Set(v)
// }

nonceStore, err := httpauth.NewNonceStore(ctx, storeConfig, redisPrefix)
if err != nil {
logger.Fatal("Failed to initialize redis nonce store: ", err)
}

pk, err := sk.PubKey()
if err != nil {
logger.WithError(err).Warn("No SecKey found. Skipping serving on dmsghttp.")
}

if syslogAddr != "" {
hook, err := logrussyslog.NewSyslogHook("udp", syslogAddr, syslog.LOG_INFO, tag)
if err != nil && logger != nil {
logger.Fatalf("Unable to connect to syslog daemon on %v", syslogAddr)
}
logging.AddHook(hook)
}

metricsutil.ServeHTTPMetrics(logger, metricsAddr)

var m armetrics.Metrics
if metricsAddr == "" {
m = armetrics.NewEmpty()
} else {
m = armetrics.NewVictoriaMetrics()
}

var dmsgAddr string
if !pk.Null() {
dmsgAddr = fmt.Sprintf("%s:%d", pk.Hex(), dmsgPort)
}

enableMetrics := metricsAddr != ""
arAPI := api.New(logger, transportStore, nonceStore, enableMetrics, m, dmsgAddr)

udpListener, err := kcp.Listen(addr)
if err != nil {
log.Fatal("Failed to open UDP listener: ", err)
}

go arAPI.ListenUDP(udpListener)

if logger != nil {
logger.Infof("Listening on %s", addr)
}

go func() {
if err := tcpproxy.ListenAndServe(addr, arAPI); err != nil {
logger.Errorf("tcpproxy.ListenAndServe: %v", err)
cancel()
}
}()

if !pk.Null() {
servers := dmsghttp.GetServers(ctx, dmsgDisc, logger)

var keys cipher.PubKeys
keys = append(keys, pk)
dClient := direct.NewClient(direct.GetAllEntries(keys, servers), logger)
config := &dmsg.Config{
MinSessions: 0, // listen on all available servers
UpdateInterval: dmsg.DefaultUpdateInterval,
}

dmsgDC, closeDmsgDC, err := direct.StartDmsg(ctx, logger, pk, sk, dClient, config)
if err != nil {
logger.WithError(err).Fatal("failed to start direct dmsg client.")
}

defer closeDmsgDC()

go dmsghttp.UpdateServers(ctx, dClient, dmsgDisc, dmsgDC, logger)

go func() {
if err := dmsghttp.ListenAndServe(ctx, pk, sk, arAPI, dClient, dmsg.DefaultDmsgHTTPPort, config, dmsgDC, logger); err != nil {
logger.Errorf("dmsghttp.ListenAndServe: %v", err)
cancel()
}
}()
}

<-ctx.Done()

arAPI.Close()
},
}

// Execute executes root CLI command.
func Execute() {
cc.Init(&cc.Config{
RootCmd: RootCmd,
Headings: cc.HiBlue + cc.Bold, //+ cc.Underline,
Commands: cc.HiBlue + cc.Bold,
CmdShortDescr: cc.HiBlue,
Example: cc.HiBlue + cc.Italic,
ExecName: cc.HiBlue + cc.Bold,
Flags: cc.HiBlue + cc.Bold,
//FlagsDataType: cc.HiBlue,
FlagsDescr: cc.HiBlue,
NoExtraNewlines: true,
NoBottomNewline: true,
})
if err := RootCmd.Execute(); err != nil {
log.Fatal("Failed to execute command: ", err)

}
}

const help = "Usage:\r\n" +
" {{.UseLine}}{{if .HasAvailableSubCommands}}{{end}} {{if gt (len .Aliases) 0}}\r\n\r\n" +
"{{.NameAndAliases}}{{end}}{{if .HasAvailableSubCommands}}\r\n\r\n" +
"Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand)}}\r\n " +
"{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}\r\n\r\n" +
"Flags:\r\n" +
"{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}\r\n\r\n" +
"Global Flags:\r\n" +
"{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}\r\n\r\n"
6 changes: 3 additions & 3 deletions cmd/apps/skychat/skychat.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (

ipc "github.com/james-barrow/golang-ipc"

"github.com/skycoin/skywire-utilities/pkg/buildinfo"
"github.com/skycoin/skywire-utilities/pkg/cipher"
"github.com/skycoin/skywire-utilities/pkg/netutil"
"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/app/appnet"
"github.com/skycoin/skywire/pkg/app/appserver"
"github.com/skycoin/skywire/pkg/buildinfo"
"github.com/skycoin/skywire/pkg/cipher"
"github.com/skycoin/skywire/pkg/netutil"
"github.com/skycoin/skywire/pkg/routing"
"github.com/skycoin/skywire/pkg/visor/visorconfig"
)
Expand Down
6 changes: 3 additions & 3 deletions cmd/apps/skysocks-client/skysocks-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
"os"
"time"

"github.com/skycoin/skywire-utilities/pkg/buildinfo"
"github.com/skycoin/skywire-utilities/pkg/cipher"
"github.com/skycoin/skywire-utilities/pkg/netutil"
"github.com/skycoin/skywire/internal/skysocks"
"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/app/appnet"
"github.com/skycoin/skywire/pkg/app/appserver"
"github.com/skycoin/skywire/pkg/buildinfo"
"github.com/skycoin/skywire/pkg/cipher"
"github.com/skycoin/skywire/pkg/netutil"
"github.com/skycoin/skywire/pkg/routing"
"github.com/skycoin/skywire/pkg/visor/visorconfig"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/apps/skysocks/skysocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (

ipc "github.com/james-barrow/golang-ipc"

"github.com/skycoin/skywire-utilities/pkg/buildinfo"
"github.com/skycoin/skywire/internal/skysocks"
"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/app/appnet"
"github.com/skycoin/skywire/pkg/app/appserver"
"github.com/skycoin/skywire/pkg/buildinfo"
"github.com/skycoin/skywire/pkg/routing"
"github.com/skycoin/skywire/pkg/visor/visorconfig"
)
Expand Down
4 changes: 2 additions & 2 deletions cmd/apps/vpn-client/vpn-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (

ipc "github.com/james-barrow/golang-ipc"

"github.com/skycoin/skywire-utilities/pkg/buildinfo"
"github.com/skycoin/skywire-utilities/pkg/cipher"
"github.com/skycoin/skywire/internal/vpn"
"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/app/appevent"
"github.com/skycoin/skywire/pkg/app/appserver"
"github.com/skycoin/skywire/pkg/buildinfo"
"github.com/skycoin/skywire/pkg/cipher"
"github.com/skycoin/skywire/pkg/routing"
"github.com/skycoin/skywire/pkg/visor/visorconfig"
)
Expand Down
4 changes: 2 additions & 2 deletions cmd/apps/vpn-server/vpn-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"runtime"
"syscall"

"github.com/skycoin/skywire-utilities/pkg/buildinfo"
"github.com/skycoin/skywire-utilities/pkg/cipher"
"github.com/skycoin/skywire/internal/vpn"
"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/app/appnet"
"github.com/skycoin/skywire/pkg/app/appserver"
"github.com/skycoin/skywire/pkg/buildinfo"
"github.com/skycoin/skywire/pkg/cipher"
"github.com/skycoin/skywire/pkg/routing"
"github.com/skycoin/skywire/pkg/skyenv"
)
Expand Down
Loading