Skip to content

Commit

Permalink
Merge pull request #278 from Darkren/feature/vpn
Browse files Browse the repository at this point in the history
VPN apps
  • Loading branch information
Darkren authored Apr 30, 2020
2 parents 7288361 + c095e85 commit 2b52938
Show file tree
Hide file tree
Showing 155 changed files with 4,459 additions and 485 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ host-apps: ## Build app
${OPTS} go build ${BUILD_OPTS} -o ./apps/helloworld ./cmd/apps/helloworld
${OPTS} go build ${BUILD_OPTS} -o ./apps/skysocks ./cmd/apps/skysocks
${OPTS} go build ${BUILD_OPTS} -o ./apps/skysocks-client ./cmd/apps/skysocks-client
${OPTS} go build ${BUILD_OPTS} -o ./apps/vpn-server ./cmd/apps/vpn-server
${OPTS} go build ${BUILD_OPTS} -o ./apps/vpn-client ./cmd/apps/vpn-client

# Bin
bin: ## Build `skywire-visor`, `skywire-cli`, `hypervisor`
Expand All @@ -139,6 +141,8 @@ release: ## Build `skywire-visor`, `skywire-cli`, `hypervisor` and apps without
${OPTS} go build ${BUILD_OPTS} -o ./apps/helloworld ./cmd/apps/helloworld
${OPTS} go build ${BUILD_OPTS} -o ./apps/skysocks ./cmd/apps/skysocks
${OPTS} go build ${BUILD_OPTS} -o ./apps/skysocks-client ./cmd/apps/skysocks-client
${OPTS} go build ${BUILD_OPTS} -o ./apps/vpn-server ./cmd/apps/vpn-server
${OPTS} go build ${BUILD_OPTS} -o ./apps/vpn-client ./cmd/apps/vpn-client

github-release: ## Create a GitHub release
goreleaser --rm-dist
Expand Down
118 changes: 118 additions & 0 deletions cmd/apps/vpn-client/vpn-client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package main

import (
"context"
"flag"
"fmt"
"net"
"os"
"os/signal"
"syscall"
"time"

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

"github.com/SkycoinProject/skywire-mainnet/internal/vpn"
"github.com/SkycoinProject/skywire-mainnet/pkg/app"
"github.com/SkycoinProject/skywire-mainnet/pkg/app/appnet"
"github.com/SkycoinProject/skywire-mainnet/pkg/routing"
"github.com/SkycoinProject/skywire-mainnet/pkg/skyenv"
)

const (
appName = skyenv.VPNClientName
netType = appnet.TypeSkynet
vpnPort = routing.Port(skyenv.VPNServerPort)
)

const (
serverDialInitBO = 1 * time.Second
serverDialMaxBO = 10 * time.Second
)

var (
log = app.NewLogger(appName)
r = netutil.NewRetrier(log, serverDialInitBO, serverDialMaxBO, 0, 1)
)

var serverPKStr = flag.String("srv", "", "PubKey of the server to connect to")

func dialServer(appCl *app.Client, pk cipher.PubKey) (net.Conn, error) {
var conn net.Conn
err := r.Do(context.Background(), func() error {
var err error
conn, err = appCl.Dial(appnet.Addr{
Net: netType,
PubKey: pk,
Port: vpnPort,
})
return err
})
if err != nil {
return nil, err
}

return conn, nil
}

func main() {
flag.Parse()

if *serverPKStr == "" {
log.Fatalln("VPN server pub key is missing")
}

serverPK := cipher.PubKey{}
if err := serverPK.UnmarshalText([]byte(*serverPKStr)); err != nil {
log.WithError(err).Fatalln("Invalid VPN server pub key")
}

log.Infof("Connecting to VPN server %s", serverPK.String())

appCfg, err := app.ClientConfigFromEnv()
if err != nil {
log.WithError(err).Fatalln("Error getting app client config")
}

appClient, err := app.NewClient(logging.MustGetLogger(fmt.Sprintf("app_%s", appName)), appCfg)
if err != nil {
log.WithError(err).Fatalln("Error setting up VPN client")
}
defer func() {
appClient.Close()
}()

appConn, err := dialServer(appClient, serverPK)
if err != nil {
log.WithError(err).Fatalln("Error connecting to VPN server")
}
defer func() {
if err := appConn.Close(); err != nil {
log.WithError(err).Errorln("Error closing connection to the VPN server")
}
}()

log.Infof("Dialed %s", appConn.RemoteAddr())

vpnClient, err := vpn.NewClient(log, appConn)
if err != nil {
log.WithError(err).Fatalln("Error creating VPN client")
}

osSigs := make(chan os.Signal, 2)
sigs := []os.Signal{syscall.SIGTERM, syscall.SIGINT}
for _, sig := range sigs {
signal.Notify(osSigs, sig)
}

go func() {
<-osSigs
vpnClient.Close()
}()

if err := vpnClient.Serve(); err != nil {
log.WithError(err).Fatalln("Error serving VPN")
}
}
75 changes: 75 additions & 0 deletions cmd/apps/vpn-server/vpn-server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"fmt"
"os"
"os/signal"
"syscall"

"github.com/SkycoinProject/skycoin/src/util/logging"

"github.com/SkycoinProject/skywire-mainnet/internal/vpn"
"github.com/SkycoinProject/skywire-mainnet/pkg/app"
"github.com/SkycoinProject/skywire-mainnet/pkg/app/appnet"
"github.com/SkycoinProject/skywire-mainnet/pkg/routing"
"github.com/SkycoinProject/skywire-mainnet/pkg/skyenv"
)

const (
appName = skyenv.VPNServerName
netType = appnet.TypeSkynet
vpnPort = routing.Port(skyenv.VPNServerPort)
)

var (
log = app.NewLogger(appName)
)

func main() {
appCfg, err := app.ClientConfigFromEnv()
if err != nil {
log.WithError(err).Errorln("Error getting app client config")
return
}

appClient, err := app.NewClient(logging.MustGetLogger(fmt.Sprintf("app_%s", appName)), appCfg)
if err != nil {
log.WithError(err).Errorln("Error setting up VPN client")
return
}
defer func() {
appClient.Close()
}()

osSigs := make(chan os.Signal, 2)

sigs := []os.Signal{syscall.SIGTERM, syscall.SIGINT}
for _, sig := range sigs {
signal.Notify(osSigs, sig)
}

l, err := appClient.Listen(netType, vpnPort)
if err != nil {
log.WithError(err).Errorf("Error listening network %v on port %d", netType, vpnPort)
return
}

log.Infof("Got app listener, bound to %d", vpnPort)

srv, err := vpn.NewServer(log)
if err != nil {
log.WithError(err).Fatalln("Error creating VPN server")
}
defer func() {
if err := srv.Close(); err != nil {
log.WithError(err).Errorln("Error closing server")
}
}()
go func() {
if err := srv.Serve(l); err != nil {
log.WithError(err).Errorln("Error serving")
}
}()

<-osSigs
}
18 changes: 18 additions & 0 deletions cmd/skywire-cli/commands/visor/gen-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ func defaultConfig() *visor.Config {
defaultSkychatConfig(),
defaultSkysocksConfig(""),
defaultSkysocksClientConfig(),
defaultVPNServerConfig(),
defaultVPNClientConfig(),
}

conf.TrustedVisors = []cipher.PubKey{}
Expand Down Expand Up @@ -207,3 +209,19 @@ func defaultSkysocksClientConfig() visor.AppConfig {
Port: routing.Port(skyenv.SkysocksClientPort),
}
}

func defaultVPNServerConfig() visor.AppConfig {
return visor.AppConfig{
App: skyenv.VPNServerName,
AutoStart: true,
Port: routing.Port(skyenv.VPNServerPort),
}
}

func defaultVPNClientConfig() visor.AppConfig {
return visor.AppConfig{
App: skyenv.VPNClientName,
AutoStart: false,
Port: routing.Port(skyenv.VPNClientPort),
}
}
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,29 @@ module github.com/SkycoinProject/skywire-mainnet
go 1.13

require (
github.com/SkycoinProject/dmsg v0.2.0
github.com/SkycoinProject/dmsg v0.1.1-0.20200420091742-8c1a3d828a49
github.com/SkycoinProject/skycoin v0.27.0
github.com/SkycoinProject/yamux v0.0.0-20191213015001-a36efeefbf6a
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
github.com/go-chi/chi v4.0.2+incompatible
github.com/google/uuid v1.1.1
github.com/gorilla/securecookie v1.1.1
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
github.com/mholt/archiver/v3 v3.3.0
github.com/pkg/profile v1.3.0
github.com/prometheus/client_golang v1.3.0
github.com/prometheus/common v0.7.0
github.com/rakyll/statik v0.1.7
github.com/schollz/progressbar/v2 v2.15.0
github.com/sirupsen/logrus v1.4.2
github.com/sirupsen/logrus v1.5.0
github.com/skycoin/dmsg v0.0.0-20190805065636-70f4c32a994f // indirect
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8
github.com/spf13/cobra v0.0.5
github.com/stretchr/testify v1.4.0
go.etcd.io/bbolt v1.3.4
golang.org/x/crypto v0.0.0-20200427165652-729f1e841bcc // indirect
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b
golang.org/x/sys v0.0.0-20200428200454-593003d681fa // indirect
)

// Uncomment for tests with alternate branches of 'dmsg'
Expand Down
Loading

0 comments on commit 2b52938

Please sign in to comment.