Skip to content
This repository has been archived by the owner on Oct 27, 2023. It is now read-only.

Commit

Permalink
Add autopeering over proxy
Browse files Browse the repository at this point in the history
To use autopeering over a proxy, set the environment variable ALL_PROXY. For
example, ALL_PROXY=socks5://127.0.0.1:9150 ./yggdrasil -autopeer
  • Loading branch information
zhoreeq committed Nov 1, 2020
1 parent 0a177db commit ccc4123
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
20 changes: 18 additions & 2 deletions src/autopeering/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package autopeering

import (
"context"
"math/rand"
"net"
"sort"
"strings"
"time"

"golang.org/x/net/proxy"
)

const defaultTimeout time.Duration = time.Duration(3) * time.Second
Expand Down Expand Up @@ -48,7 +50,10 @@ func testPeer(pstring string, results chan Peer) {
return
}

conn, err := net.DialTimeout(network, peer_addr[1], defaultTimeout)
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()

conn, err := proxy.Dial(ctx, network, peer_addr[1])
if err == nil {
t1 := time.Now()
conn.Close()
Expand Down Expand Up @@ -101,3 +106,14 @@ func RandomPick(peerList []string, num int) []string {

return res
}

// Return tcp peers only since socks proxy can only work with those
func GetTcpPeers() []string {
publicPeers := []string{}
for _, p := range PublicPeers {
if p[:3] == "tcp" {
publicPeers = append(publicPeers, p)
}
}
return publicPeers
}
37 changes: 33 additions & 4 deletions src/autopeering/module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package autopeering

import (
"fmt"
"net/url"
"os"
"time"

"github.com/gologme/log"
Expand All @@ -23,13 +26,29 @@ type AutoPeering struct {
log *log.Logger
checkPeerTimer *time.Timer
hadPeers time.Time
proxyURL *url.URL
publicPeers *[]string
}

func (ap *AutoPeering) Init(core *yggdrasil.Core, state *config.NodeState, popConfig *popura.PopuraConfig, log *log.Logger, options interface{}) error {
ap.core = core
ap.log = log

return nil
proxyEnv := os.Getenv("ALL_PROXY")
if proxyEnv == "" {
proxyEnv = os.Getenv("all_proxy")
}

if proxyEnv == "" {
ap.publicPeers = &PublicPeers
} else {
tcpPeers := GetTcpPeers()
ap.publicPeers = &tcpPeers
}

var err error
ap.proxyURL, err = url.Parse(proxyEnv)
return err
}

func (ap *AutoPeering) Start() error {
Expand Down Expand Up @@ -59,10 +78,12 @@ func (ap *AutoPeering) checkPeerLoop() {
ap.hadPeers = time.Now()
} else if time.Since(ap.hadPeers) > autopeerTimeout {
ap.hadPeers = time.Now()
peers := RandomPick(GetClosestPeers(PublicPeers, 10), 1)
peers := RandomPick(GetClosestPeers(*ap.publicPeers, 10), 1)
if len(peers) == 1 {
ap.log.Infoln("autopeering: adding new peer", peers[0])
if err := ap.core.AddPeer(peers[0], ""); err != nil {
peerUri := ap.getPeerUri(peers[0])

ap.log.Infoln("autopeering: adding new peer", peerUri)
if err := ap.core.AddPeer(peerUri, ""); err != nil {
ap.log.Infoln("autopeering: Failed to connect to peer:", err)
}
}
Expand All @@ -73,6 +94,14 @@ func (ap *AutoPeering) checkPeerLoop() {
})
}

// Return peer URI with respect to proxy environment settings
func (ap *AutoPeering) getPeerUri(uri string) string {
if !ap.proxyURL.IsAbs() {
return uri
}
return fmt.Sprintf("socks://%s/%s", ap.proxyURL.Host, uri[6:len(uri)])
}

func (ap *AutoPeering) UpdateConfig(yggConfig *config.NodeConfig, popConfig *popura.PopuraConfig) {}
func (ap *AutoPeering) SetupAdminHandlers(a *admin.AdminSocket) {}
func (ap *AutoPeering) IsStarted() bool { return false }

0 comments on commit ccc4123

Please sign in to comment.