mirror of
https://github.com/nickname76/p2p-forwarder.git
synced 2025-09-26 19:51:18 +08:00
Added Makefile
This commit is contained in:
6
.gitignore
vendored
6
.gitignore
vendored
@@ -5,10 +5,8 @@
|
|||||||
*.so
|
*.so
|
||||||
*.dylib
|
*.dylib
|
||||||
|
|
||||||
# Project builds
|
# Binary builds
|
||||||
/cmd/tui-builds
|
/cmd/builds
|
||||||
/cmd/gui-builds
|
|
||||||
/cmd/cli-builds
|
|
||||||
|
|
||||||
# Test binary, built with `go test -c`
|
# Test binary, built with `go test -c`
|
||||||
*.test
|
*.test
|
||||||
|
77
cmd/Makefile
Normal file
77
cmd/Makefile
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
GOCMD=go
|
||||||
|
|
||||||
|
TARGET_OS=$$($(GOCMD) env GOOS)
|
||||||
|
TARGET_ARCH=$$($(GOCMD) env GOARCH)
|
||||||
|
TARGET_UI=tui
|
||||||
|
BINARY_NAME=P2P-Forwarder
|
||||||
|
BUILDS_DIRECTORY=$(PWD)/builds
|
||||||
|
|
||||||
|
CFLAGS=-w -O3
|
||||||
|
export CGO_CFLAGS=$(CFLAGS)
|
||||||
|
export CGO_CPPFLAGS=$(CFLAGS)
|
||||||
|
export CGO_CXXFLAGS=$(CFLAGS)
|
||||||
|
export CGO_FFLAGS=$(CFLAGS)
|
||||||
|
export CGO_LDFLAGS=$(CFLAGS)
|
||||||
|
|
||||||
|
# build builds a binary
|
||||||
|
#
|
||||||
|
# arguments:
|
||||||
|
# 1 - "cli" or "tui"
|
||||||
|
# 2 - GOOS
|
||||||
|
# 3 - GOARCH
|
||||||
|
# 4 - out
|
||||||
|
define build
|
||||||
|
GOOS="$(2)" GOARCH="$(3)" $(GOCMD) build -gcflags="all=-trimpath='$(HOME)'" -asmflags="all=-trimpath='$(HOME)'" -ldflags="-s -w" -o "$(4)" github.com/nickname32/p2p-forwarder/cmd/$(1)
|
||||||
|
endef
|
||||||
|
|
||||||
|
# build builds a binary and zips it
|
||||||
|
#
|
||||||
|
# arguments:
|
||||||
|
# 1 - "cli" or "tui"
|
||||||
|
# 2 - GOOS
|
||||||
|
# 3 - GOARCH
|
||||||
|
define build-and-zip
|
||||||
|
$(call build,$(1),$(2),$(3),$(BUILDS_DIRECTORY)/$(BINARY_NAME)-$(1))
|
||||||
|
cd $(BUILDS_DIRECTORY); zip "./$(BINARY_NAME)-$(1)_$(2)_$(3).zip" "./$(BINARY_NAME)-$(1)"
|
||||||
|
endef
|
||||||
|
|
||||||
|
# build-and-zip-all-targets builds and zips binary for all targets
|
||||||
|
# 1 - "cli" or "tui"
|
||||||
|
define build-and-zip-all-targets
|
||||||
|
$(call build-and-zip,$(1),darwin,386)
|
||||||
|
$(call build-and-zip,$(1),darwin,amd64)
|
||||||
|
|
||||||
|
$(call build-and-zip,$(1),linux,386)
|
||||||
|
$(call build-and-zip,$(1),linux,amd64)
|
||||||
|
$(call build-and-zip,$(1),linux,arm)
|
||||||
|
$(call build-and-zip,$(1),linux,arm64)
|
||||||
|
|
||||||
|
-rm $(BUILDS_DIRECTORY)/$(BINARY_NAME)
|
||||||
|
|
||||||
|
$(call build,$(1),windows,386,$(BUILDS_DIRECTORY)/$(BINARY_NAME)-$(1).exe)
|
||||||
|
cd $(BUILDS_DIRECTORY); zip "./$(BINARY_NAME)-$(1)_windows_386.zip" "./$(BINARY_NAME)-$(1).exe"
|
||||||
|
$(call build,$(1),windows,amd64,$(BUILDS_DIRECTORY)/$(BINARY_NAME)-$(1).exe)
|
||||||
|
cd $(BUILDS_DIRECTORY); zip "./$(BINARY_NAME)-$(1)_windows_amd64.zip" "./$(BINARY_NAME)-$(1).exe"
|
||||||
|
|
||||||
|
-rm $(2)/$(BINARY_NAME)-$(1).exe
|
||||||
|
endef
|
||||||
|
|
||||||
|
all:
|
||||||
|
$(call build-and-zip-all-targets,tui)
|
||||||
|
$(call build-and-zip-all-targets,cli)
|
||||||
|
|
||||||
|
build:
|
||||||
|
$(call build,$(TARGET_UI),$(TARGET_OS),$(TARGET_ARCH),$(BUILDS_DIRECTORY)/$(BINARY_NAME)-$(TARGET_UI))
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(GOCMD) clean
|
||||||
|
|
||||||
|
run: run-tui
|
||||||
|
|
||||||
|
run-tui:
|
||||||
|
cd ./tui; $(GOCMD) run main.go $(ARGS)
|
||||||
|
run-cli:
|
||||||
|
cd ./tui; $(GOCMD) run main.go $(ARGS)
|
||||||
|
|
||||||
|
deps:
|
||||||
|
$(GOCMD) get
|
@@ -1,4 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
./build-and-zip-all-targets.sh ./tui ./tui-builds "P2P_Forwarder_tui"
|
|
||||||
./build-and-zip-all-targets.sh ./cli ./cli-builds "P2P_Forwarder_cli"
|
|
@@ -1,39 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
SOURCE_PATH=$(realpath "$1")
|
|
||||||
BUILDS_PATH=$(realpath "$2")
|
|
||||||
FILENAME=$3
|
|
||||||
|
|
||||||
MAIN_PATH=$PWD
|
|
||||||
|
|
||||||
rm -r "$BUILDS_PATH"
|
|
||||||
|
|
||||||
build() {
|
|
||||||
if [ "$1" = "windows" ]; then
|
|
||||||
cd "$SOURCE_PATH"
|
|
||||||
"$MAIN_PATH/build.sh" "$1" "$2" "$BUILDS_PATH/$FILENAME.exe"
|
|
||||||
cd "$BUILDS_PATH"
|
|
||||||
zip "./${FILENAME}_$1_$2.zip" "./$FILENAME.exe"
|
|
||||||
cd "$MAIN_PATH"
|
|
||||||
else
|
|
||||||
cd "$SOURCE_PATH"
|
|
||||||
"$MAIN_PATH/build.sh" "$1" "$2" "$BUILDS_PATH/$FILENAME"
|
|
||||||
cd "$BUILDS_PATH"
|
|
||||||
zip "./${FILENAME}_$1_$2.zip" "./$FILENAME"
|
|
||||||
cd "$MAIN_PATH"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
build windows 386
|
|
||||||
build windows amd64
|
|
||||||
|
|
||||||
build darwin 386
|
|
||||||
build darwin amd64
|
|
||||||
|
|
||||||
build linux 386
|
|
||||||
build linux amd64
|
|
||||||
build linux arm
|
|
||||||
build linux arm64
|
|
||||||
|
|
||||||
rm "$BUILDS_PATH/$FILENAME.exe"
|
|
||||||
rm "$BUILDS_PATH/$FILENAME"
|
|
12
cmd/build.sh
12
cmd/build.sh
@@ -1,12 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
export CFLAGS='-w -O3'
|
|
||||||
export CGO_CFLAGS="$CFLAGS"
|
|
||||||
export CGO_CPPFLAGS="$CFLAGS"
|
|
||||||
export CGO_CXXFLAGS="$CFLAGS"
|
|
||||||
export CGO_FFLAGS="$CFLAGS"
|
|
||||||
export CGO_LDFLAGS="$CFLAGS"
|
|
||||||
|
|
||||||
echo "Building for '$1':'$2' to '$3'"
|
|
||||||
|
|
||||||
GOOS="$1" GOARCH="$2" go build -o "$3" -gcflags="all=-trimpath=$HOME" -asmflags="all=-trimpath=$HOME" -ldflags="-s -w"
|
|
@@ -140,6 +140,7 @@ loop:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func shutdown() {
|
func shutdown() {
|
||||||
zap.L().Info("Shutdown...")
|
zap.L().Info("Shutdown...")
|
||||||
|
|
||||||
|
68
forwarder.go
68
forwarder.go
@@ -18,6 +18,9 @@ import (
|
|||||||
noise "github.com/libp2p/go-libp2p-noise"
|
noise "github.com/libp2p/go-libp2p-noise"
|
||||||
libp2pquic "github.com/libp2p/go-libp2p-quic-transport"
|
libp2pquic "github.com/libp2p/go-libp2p-quic-transport"
|
||||||
routing "github.com/libp2p/go-libp2p-routing"
|
routing "github.com/libp2p/go-libp2p-routing"
|
||||||
|
libp2ptls "github.com/libp2p/go-libp2p-tls"
|
||||||
|
"github.com/libp2p/go-tcp-transport"
|
||||||
|
websocket "github.com/libp2p/go-ws-transport"
|
||||||
"github.com/sparkymat/appdir"
|
"github.com/sparkymat/appdir"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -74,14 +77,6 @@ func NewForwarder() (*Forwarder, context.CancelFunc, error) {
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// This connects to public bootstrappers
|
|
||||||
for _, addr := range dht.DefaultBootstrapPeers {
|
|
||||||
pi, _ := peer.AddrInfoFromP2pAddr(addr)
|
|
||||||
// We ignore errors as some bootstrap peers may be down
|
|
||||||
// and that is fine.
|
|
||||||
h.Connect(ctx, *pi)
|
|
||||||
}
|
|
||||||
|
|
||||||
f := &Forwarder{
|
f := &Forwarder{
|
||||||
host: h,
|
host: h,
|
||||||
|
|
||||||
@@ -161,51 +156,60 @@ func createLibp2pHost(ctx context.Context, priv crypto.PrivKey) (host.Host, erro
|
|||||||
var d *dht.IpfsDHT
|
var d *dht.IpfsDHT
|
||||||
|
|
||||||
h, err := libp2p.New(ctx,
|
h, err := libp2p.New(ctx,
|
||||||
// Use the keypair
|
|
||||||
libp2p.Identity(priv),
|
libp2p.Identity(priv),
|
||||||
// Multiple listen addresses
|
|
||||||
libp2p.ListenAddrStrings(
|
|
||||||
"/ip4/0.0.0.0/udp/0/quic",
|
|
||||||
"/ip6/::/udp/0/quic",
|
|
||||||
),
|
|
||||||
libp2p.DefaultListenAddrs,
|
|
||||||
// support Noise connections
|
|
||||||
libp2p.Security(noise.ID, noise.New),
|
|
||||||
// support any other default transports (secio, tls)
|
|
||||||
libp2p.DefaultSecurity,
|
|
||||||
// support QUIC
|
|
||||||
libp2p.Transport(libp2pquic.NewTransport),
|
libp2p.Transport(libp2pquic.NewTransport),
|
||||||
// support any other default transports (TCP)
|
libp2p.Transport(tcp.NewTCPTransport),
|
||||||
libp2p.DefaultTransports,
|
libp2p.Transport(websocket.New),
|
||||||
|
|
||||||
libp2p.DefaultMuxers,
|
libp2p.DefaultMuxers,
|
||||||
|
|
||||||
// Let's prevent our peer from having too many
|
libp2p.Security(noise.ID, noise.New),
|
||||||
// connections by attaching a connection manager.
|
libp2p.Security(libp2ptls.ID, libp2ptls.New),
|
||||||
|
|
||||||
|
libp2p.ListenAddrStrings(
|
||||||
|
"/ip4/0.0.0.0/udp/0/quic",
|
||||||
|
"/ip6/::/udp/0/quic",
|
||||||
|
|
||||||
|
"/ip4/0.0.0.0/tcp/0",
|
||||||
|
"/ip6/::/tcp/0",
|
||||||
|
),
|
||||||
|
|
||||||
libp2p.ConnectionManager(connmgr.NewConnManager(
|
libp2p.ConnectionManager(connmgr.NewConnManager(
|
||||||
100, // Lowwater
|
100, // Lowwater
|
||||||
400, // HighWater,
|
400, // HighWater,
|
||||||
time.Minute, // GracePeriod
|
time.Minute, // GracePeriod
|
||||||
)),
|
)),
|
||||||
libp2p.EnableNATService(),
|
|
||||||
// Attempt to open ports using uPNP for NATed hosts.
|
|
||||||
libp2p.NATPortMap(),
|
libp2p.NATPortMap(),
|
||||||
// Let this host use the DHT to find other hosts
|
|
||||||
libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) {
|
libp2p.EnableNATService(),
|
||||||
var err error
|
|
||||||
d, err = dht.New(ctx, h, dht.BootstrapPeers(dht.GetDefaultBootstrapPeerAddrInfos()...))
|
|
||||||
return d, err
|
|
||||||
}),
|
|
||||||
libp2p.EnableAutoRelay(),
|
libp2p.EnableAutoRelay(),
|
||||||
libp2p.EnableRelay(relay.OptActive),
|
libp2p.EnableRelay(relay.OptActive),
|
||||||
libp2p.DefaultStaticRelays(),
|
libp2p.DefaultStaticRelays(),
|
||||||
|
|
||||||
libp2p.DefaultPeerstore,
|
libp2p.DefaultPeerstore,
|
||||||
|
|
||||||
|
libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) {
|
||||||
|
var err error
|
||||||
|
d, err = dht.New(ctx, h, dht.BootstrapPeers(dht.GetDefaultBootstrapPeerAddrInfos()...))
|
||||||
|
return d, err
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This connects to public bootstrappers
|
||||||
|
for _, addr := range dht.DefaultBootstrapPeers {
|
||||||
|
pi, err := peer.AddrInfoFromP2pAddr(addr)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
h.Connect(ctx, *pi)
|
||||||
|
}
|
||||||
|
|
||||||
err = d.Bootstrap(ctx)
|
err = d.Bootstrap(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
3
go.mod
3
go.mod
@@ -14,6 +14,9 @@ require (
|
|||||||
github.com/libp2p/go-libp2p-noise v0.1.1
|
github.com/libp2p/go-libp2p-noise v0.1.1
|
||||||
github.com/libp2p/go-libp2p-quic-transport v0.8.0
|
github.com/libp2p/go-libp2p-quic-transport v0.8.0
|
||||||
github.com/libp2p/go-libp2p-routing v0.1.0
|
github.com/libp2p/go-libp2p-routing v0.1.0
|
||||||
|
github.com/libp2p/go-libp2p-tls v0.1.3
|
||||||
|
github.com/libp2p/go-tcp-transport v0.2.1
|
||||||
|
github.com/libp2p/go-ws-transport v0.3.1
|
||||||
github.com/mattn/go-runewidth v0.0.9 // indirect
|
github.com/mattn/go-runewidth v0.0.9 // indirect
|
||||||
github.com/nsf/termbox-go v0.0.0-20200418040025-38ba6e5628f1 // indirect
|
github.com/nsf/termbox-go v0.0.0-20200418040025-38ba6e5628f1 // indirect
|
||||||
github.com/sparkymat/appdir v0.0.0-20190803090504-1c2ab64aee87
|
github.com/sparkymat/appdir v0.0.0-20190803090504-1c2ab64aee87
|
||||||
|
@@ -124,7 +124,7 @@ func (f *Forwarder) dialTCP(ctx context.Context, peerid peer.ID, protocolType by
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
addressstr := "tcp:" + listenip + ":" + strconv.Itoa(lport) + " -> " + strconv.FormatUint(uint64(port), 10)
|
addressstr := "tcp:" + listenip + ":" + strconv.Itoa(lport) + " -> " + "tcp:" + strconv.FormatUint(uint64(port), 10)
|
||||||
|
|
||||||
onInfoFn("Listening " + addressstr)
|
onInfoFn("Listening " + addressstr)
|
||||||
|
|
||||||
@@ -213,7 +213,7 @@ func (f *Forwarder) dialUDP(ctx context.Context, peerid peer.ID, protocolType by
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
addressstr := "udp:" + listenip + ":" + strconv.Itoa(lport) + " -> " + strconv.FormatUint(uint64(port), 10)
|
addressstr := "udp:" + listenip + ":" + strconv.Itoa(lport) + " -> " + "udp:" + strconv.FormatUint(uint64(port), 10)
|
||||||
|
|
||||||
onInfoFn("Listening " + addressstr)
|
onInfoFn("Listening " + addressstr)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user