Skip to content
This repository has been archived by the owner on Jul 28, 2021. It is now read-only.

Commit

Permalink
Added Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
nickname76 committed Aug 30, 2020
1 parent 69d3ea3 commit c0d4925
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 92 deletions.
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
*.so
*.dylib

# Project builds
/cmd/tui-builds
/cmd/gui-builds
/cmd/cli-builds
# Binary builds
/cmd/builds

# Test binary, built with `go test -c`
*.test
Expand Down
77 changes: 77 additions & 0 deletions cmd/Makefile
Original file line number Diff line number Diff line change
@@ -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
4 changes: 0 additions & 4 deletions cmd/build-all.sh

This file was deleted.

39 changes: 0 additions & 39 deletions cmd/build-and-zip-all-targets.sh

This file was deleted.

12 changes: 0 additions & 12 deletions cmd/build.sh

This file was deleted.

1 change: 1 addition & 0 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ loop:
}
}
}

func shutdown() {
zap.L().Info("Shutdown...")

Expand Down
66 changes: 35 additions & 31 deletions forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (
noise "github.com/libp2p/go-libp2p-noise"
libp2pquic "github.com/libp2p/go-libp2p-quic-transport"
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"
)

Expand Down Expand Up @@ -74,14 +77,6 @@ func NewForwarder() (*Forwarder, context.CancelFunc, error) {
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{
host: h,

Expand Down Expand Up @@ -161,51 +156,60 @@ func createLibp2pHost(ctx context.Context, priv crypto.PrivKey) (host.Host, erro
var d *dht.IpfsDHT

h, err := libp2p.New(ctx,
// Use the keypair
libp2p.Identity(priv),
// Multiple listen addresses

libp2p.Transport(libp2pquic.NewTransport),
libp2p.Transport(tcp.NewTCPTransport),
libp2p.Transport(websocket.New),

libp2p.DefaultMuxers,

libp2p.Security(noise.ID, noise.New),
libp2p.Security(libp2ptls.ID, libp2ptls.New),

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),
// support any other default transports (TCP)
libp2p.DefaultTransports,

libp2p.DefaultMuxers,
"/ip4/0.0.0.0/tcp/0",
"/ip6/::/tcp/0",
),

// Let's prevent our peer from having too many
// connections by attaching a connection manager.
libp2p.ConnectionManager(connmgr.NewConnManager(
100, // Lowwater
400, // HighWater,
time.Minute, // GracePeriod
)),
libp2p.EnableNATService(),
// Attempt to open ports using uPNP for NATed hosts.

libp2p.NATPortMap(),
// Let this host use the DHT to find other hosts
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
}),

libp2p.EnableNATService(),

libp2p.EnableAutoRelay(),
libp2p.EnableRelay(relay.OptActive),
libp2p.DefaultStaticRelays(),

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 {
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)
if err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ require (
github.com/libp2p/go-libp2p-noise v0.1.1
github.com/libp2p/go-libp2p-quic-transport v0.8.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/nsf/termbox-go v0.0.0-20200418040025-38ba6e5628f1 // indirect
github.com/sparkymat/appdir v0.0.0-20190803090504-1c2ab64aee87
Expand Down
4 changes: 2 additions & 2 deletions protdial.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit c0d4925

Please sign in to comment.