This commit is contained in:
Victor 2025-02-07 17:32:12 -08:00 committed by GitHub
commit 9ebdad5b83
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 57 additions and 12 deletions

9
build
View file

@ -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
@ -22,6 +23,7 @@ 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
@ -29,6 +31,10 @@ if [ -z $TABLES ] && [ -z $DEBUG ]; then
LDFLAGS="$LDFLAGS -s -w"
fi
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
@ -37,3 +43,4 @@ for CMD in yggstack ; do
upx --brute $CMD
fi
done
fi

11
cmd/yggstack/program.go Normal file
View file

@ -0,0 +1,11 @@
package main
import (
"os"
"github.com/yggdrasil-network/yggstack/src/driver"
)
func main() {
driver.Run(os.Args[1:])
}

28
pkg/yggstack/lib.go Normal file
View file

@ -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)
}

View file

@ -1,4 +1,4 @@
package main
package driver
import (
"context"
@ -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
@ -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)