mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 03:05:07 +03:00 
			
		
		
		
	
						commit
						e2d739f646
					
				
					 6 changed files with 130 additions and 24 deletions
				
			
		
							
								
								
									
										20
									
								
								build
									
										
									
									
									
								
							
							
						
						
									
										20
									
								
								build
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1,11 +1,25 @@
 | 
			
		|||
#!/bin/sh
 | 
			
		||||
while getopts ud option
 | 
			
		||||
do
 | 
			
		||||
  case "${option}"
 | 
			
		||||
  in
 | 
			
		||||
  u) UPX=true;;
 | 
			
		||||
  d) DEBUG=true;;
 | 
			
		||||
  esac
 | 
			
		||||
done
 | 
			
		||||
export GOPATH=$PWD
 | 
			
		||||
echo "Downloading..."
 | 
			
		||||
go get -d -v
 | 
			
		||||
go get -d -v yggdrasil
 | 
			
		||||
for file in *.go ; do
 | 
			
		||||
  echo "Building: $file"
 | 
			
		||||
  go build $@ $file
 | 
			
		||||
  #go build -ldflags="-s -w" -v $file
 | 
			
		||||
  #upx --brute ${file/.go/}
 | 
			
		||||
  #go build $@ $file
 | 
			
		||||
  if [ $DEBUG ]; then
 | 
			
		||||
    go build -tags debug -v $file
 | 
			
		||||
  else
 | 
			
		||||
    go build -ldflags="-s -w" -v $file
 | 
			
		||||
  fi
 | 
			
		||||
  if [ $UPX ]; then
 | 
			
		||||
    upx --brute ${file%.go}
 | 
			
		||||
  fi
 | 
			
		||||
done
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										97
									
								
								contrib/config/yggdrasilconf.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										97
									
								
								contrib/config/yggdrasilconf.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,97 @@
 | 
			
		|||
package main
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
This is a small utility that is designed to accompany the vyatta-yggdrasil
 | 
			
		||||
package. It takes a HJSON configuration file, makes changes to it based on
 | 
			
		||||
the command line arguments, and then spits out an updated file.
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"flag"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"strconv"
 | 
			
		||||
 | 
			
		||||
	"github.com/neilalexander/hjson-go"
 | 
			
		||||
	"golang.org/x/text/encoding/unicode"
 | 
			
		||||
 | 
			
		||||
	"yggdrasil/config"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type nodeConfig = config.NodeConfig
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	useconffile := flag.String("useconffile", "/etc/yggdrasil.conf", "update config at specified file path")
 | 
			
		||||
	flag.Parse()
 | 
			
		||||
	cfg := nodeConfig{}
 | 
			
		||||
	var config []byte
 | 
			
		||||
	var err error
 | 
			
		||||
	config, err = ioutil.ReadFile(*useconffile)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
	if bytes.Compare(config[0:2], []byte{0xFF, 0xFE}) == 0 ||
 | 
			
		||||
		bytes.Compare(config[0:2], []byte{0xFE, 0xFF}) == 0 {
 | 
			
		||||
		utf := unicode.UTF16(unicode.BigEndian, unicode.UseBOM)
 | 
			
		||||
		decoder := utf.NewDecoder()
 | 
			
		||||
		config, err = decoder.Bytes(config)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			panic(err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	var dat map[string]interface{}
 | 
			
		||||
	if err := hjson.Unmarshal(config, &dat); err != nil {
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
	confJson, err := json.Marshal(dat)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
	json.Unmarshal(confJson, &cfg)
 | 
			
		||||
	switch flag.Arg(0) {
 | 
			
		||||
	case "setMTU":
 | 
			
		||||
		cfg.IfMTU, err = strconv.Atoi(flag.Arg(1))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			cfg.IfMTU = 1280
 | 
			
		||||
		}
 | 
			
		||||
		if mtu, _ := strconv.Atoi(flag.Arg(1)); mtu < 1280 {
 | 
			
		||||
			cfg.IfMTU = 1280
 | 
			
		||||
		}
 | 
			
		||||
	case "setIfName":
 | 
			
		||||
		cfg.IfName = flag.Arg(1)
 | 
			
		||||
	case "setListen":
 | 
			
		||||
		cfg.Listen = flag.Arg(1)
 | 
			
		||||
	case "setAdminListen":
 | 
			
		||||
		cfg.AdminListen = flag.Arg(1)
 | 
			
		||||
	case "setIfTapMode":
 | 
			
		||||
		if flag.Arg(1) == "true" {
 | 
			
		||||
			cfg.IfTAPMode = true
 | 
			
		||||
		} else {
 | 
			
		||||
			cfg.IfTAPMode = false
 | 
			
		||||
		}
 | 
			
		||||
	case "addPeer":
 | 
			
		||||
		found := false
 | 
			
		||||
		for _, v := range cfg.Peers {
 | 
			
		||||
			if v == flag.Arg(1) {
 | 
			
		||||
				found = true
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		if !found {
 | 
			
		||||
			cfg.Peers = append(cfg.Peers, flag.Arg(1))
 | 
			
		||||
		}
 | 
			
		||||
	case "removePeer":
 | 
			
		||||
		for k, v := range cfg.Peers {
 | 
			
		||||
			if v == flag.Arg(1) {
 | 
			
		||||
				cfg.Peers = append(cfg.Peers[:k], cfg.Peers[k+1:]...)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	bs, err := hjson.Marshal(cfg)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Println(string(bs))
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -20,6 +20,20 @@ import "regexp"
 | 
			
		|||
import _ "net/http/pprof"
 | 
			
		||||
import "net/http"
 | 
			
		||||
import "runtime"
 | 
			
		||||
import "os"
 | 
			
		||||
 | 
			
		||||
// Start the profiler in debug builds, if the required environment variable is set.
 | 
			
		||||
func init() {
 | 
			
		||||
	envVarName := "PPROFLISTEN"
 | 
			
		||||
	hostPort := os.Getenv(envVarName)
 | 
			
		||||
	switch {
 | 
			
		||||
	case hostPort == "":
 | 
			
		||||
		fmt.Printf("DEBUG: %s not set, profiler not started.\n", envVarName)
 | 
			
		||||
	default:
 | 
			
		||||
		fmt.Printf("DEBUG: Starting pprof on %s\n", hostPort)
 | 
			
		||||
		go func() { fmt.Println(http.ListenAndServe(hostPort, nil)) }()
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Starts the function profiler. This is only supported when built with
 | 
			
		||||
// '-tags build'.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,14 +0,0 @@
 | 
			
		|||
// +build !debug
 | 
			
		||||
 | 
			
		||||
package yggdrasil
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"log"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Starts the function profiler. This is only supported when built with
 | 
			
		||||
// '-tags build'.
 | 
			
		||||
func StartProfiler(_ *log.Logger) error {
 | 
			
		||||
	return errors.New("Release builds do not support -pprof, build using '-tags debug'")
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -17,6 +17,7 @@ package yggdrasil
 | 
			
		|||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"math/rand"
 | 
			
		||||
	"net"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"sync/atomic"
 | 
			
		||||
| 
						 | 
				
			
			@ -112,6 +113,7 @@ func (iface *tcpInterface) call(saddr string, socksaddr *string) {
 | 
			
		|||
			defer func() {
 | 
			
		||||
				// Block new calls for a little while, to mitigate livelock scenarios
 | 
			
		||||
				time.Sleep(tcp_timeout)
 | 
			
		||||
				time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
 | 
			
		||||
				iface.mutex.Lock()
 | 
			
		||||
				delete(iface.calls, saddr)
 | 
			
		||||
				iface.mutex.Unlock()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -82,7 +82,6 @@ func doGenconf() string {
 | 
			
		|||
// The main function is responsible for configuring and starting Yggdrasil.
 | 
			
		||||
func main() {
 | 
			
		||||
	// Configure the command line parameters.
 | 
			
		||||
	pprof := flag.Bool("pprof", false, "Run pprof, see http://localhost:6060/debug/pprof/")
 | 
			
		||||
	genconf := flag.Bool("genconf", false, "print a new config to stdout")
 | 
			
		||||
	useconf := flag.Bool("useconf", false, "read config from stdin")
 | 
			
		||||
	useconffile := flag.String("useconffile", "", "read config from specified file path")
 | 
			
		||||
| 
						 | 
				
			
			@ -203,12 +202,6 @@ func main() {
 | 
			
		|||
	}
 | 
			
		||||
	// Create a new logger that logs output to stdout.
 | 
			
		||||
	logger := log.New(os.Stdout, "", log.Flags())
 | 
			
		||||
	// If the -pprof flag was provided then start the pprof service on port 6060.
 | 
			
		||||
	if *pprof {
 | 
			
		||||
		if err := yggdrasil.StartProfiler(logger); err != nil {
 | 
			
		||||
			logger.Println(err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	// Setup the Yggdrasil node itself. The node{} type includes a Core, so we
 | 
			
		||||
	// don't need to create this manually.
 | 
			
		||||
	n := node{}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue