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

Added an option to build a dynamic library #18

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions build
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ PKGVER=${PKGVER:-$(sh contrib/semver/version.sh --bare)}

LDFLAGS="-X $PKGSRC.buildName=$PKGNAME -X $PKGSRC.buildVersion=$PKGVER"
ARGS="-v"
DLL=false

while getopts "utc:l:dro:ps" option
while getopts "utc:l:dro:psk" option
do
case "$option"
in
Expand All @@ -22,18 +23,24 @@ do
o) ARGS="$ARGS -o $OPTARG";;
p) ARGS="$ARGS -buildmode=pie";;
s) ARGS="$ARGS -tags netgo,osusersgo,static" LDFLAGS="$LDFLAGS -extldflags '-static'" CGO_ENABLED=0;;
k) DLL=true;;
esac
done

if [ -z $TABLES ] && [ -z $DEBUG ]; then
LDFLAGS="$LDFLAGS -s -w"
fi

for CMD in yggstack ; do
echo "Building: $CMD"
go build $ARGS -ldflags="$LDFLAGS" -gcflags="$GCFLAGS" ./cmd/$CMD
if [ "$DLL" = true ]; then
echo "Building: shared library"
CGO_ENABLED=1 go build $ARGS -buildmode=c-shared -ldflags="$LDFLAGS" -gcflags="$GCFLAGS" -o yggstack ./pkg/yggstack
else
for CMD in yggstack ; do
echo "Building: $CMD"
go build $ARGS -ldflags="$LDFLAGS" -gcflags="$GCFLAGS" ./cmd/$CMD

if [ $UPX ]; then
upx --brute $CMD
fi
done
if [ $UPX ]; then
upx --brute $CMD
fi
done
fi
11 changes: 11 additions & 0 deletions cmd/yggstack/program.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"os"

"github.com/yggdrasil-network/yggstack/src/driver"
)

func main() {
driver.Run(os.Args[1:])
}
28 changes: 28 additions & 0 deletions pkg/yggstack/lib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build cgo
// +build cgo

package main

/*
#include <stdlib.h>
*/
import "C"
import (
"unsafe"

"github.com/yggdrasil-network/yggstack/src/driver"
)

func main() {}

//export YggstackMain
func YggstackMain(argc C.int, argv **C.char) {
length := int(argc)
args := make([]string, length)
argvPtr := unsafe.Pointer(argv)
for i := 0; i < length; i++ {
arg := *(**C.char)(unsafe.Add(argvPtr, uintptr(i)*unsafe.Sizeof((*C.char)(nil))))
args[i] = C.GoString(arg)
}
driver.Run(args)
}
7 changes: 3 additions & 4 deletions cmd/yggstack/main.go → src/driver/driver.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package driver

import (
"context"
Expand Down Expand Up @@ -47,8 +47,7 @@ type UDPSession struct {
remoteAddr net.Addr
}

// The main function is responsible for configuring and starting Yggdrasil.
func main() {
func Run(args []string) {
var localtcp types.TCPLocalMappings
var localudp types.UDPLocalMappings
var remotetcp types.TCPRemoteMappings
Expand All @@ -72,7 +71,7 @@ func main() {
flag.Var(&localudp, "local-udp", "UDP ports to forward to the remote Yggdrasil node, e.g. 22:[a:b:c:d]:2022, 127.0.0.1:[a:b:c:d]:22")
flag.Var(&remotetcp, "remote-tcp", "TCP ports to expose to the network, e.g. 22, 2022:22, 22:192.168.1.1:2022")
flag.Var(&remoteudp, "remote-udp", "UDP ports to expose to the network, e.g. 22, 2022:22, 22:192.168.1.1:2022")
flag.Parse()
flag.CommandLine.Parse(args)

// Catch interrupts from the operating system to exit gracefully.
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
Expand Down