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

Commit

Permalink
code upload
Browse files Browse the repository at this point in the history
  • Loading branch information
nickname76 committed Jul 31, 2020
1 parent c176d9f commit f319d28
Show file tree
Hide file tree
Showing 11 changed files with 1,898 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Binaries for programs and plugins
/cmd/tui/builds
*.exe
*.exe~
*.dll
Expand Down
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
# p2p-forwarder
Tool for farwarding ports. Made using libp2p.

[![Go Report Card](https://goreportcard.com/badge/github.com/nickname32/discordhook)](https://goreportcard.com/report/github.com/nickname32/p2p-forwarder)

Tool for farwarding ports. Made using [libp2p](https://github.com/libp2p/go-libp2p).

![screenshot](https://i.imgur.com/BMFXKNK.png)

## How it works

- A: opens desired ports ports inside P2P Forwarder
- A: shares it's id from P2P Forwarder with B
- B: connects to A's id inside P2P Forwarder
- B: connect to opened ports on A's machine using address like 127.0.89.N:PORT_ON_A's_MACHINE

P.S. every edit field handles Ctrl+C and Ctrl+V. To exit the program, press Ctrl+Q

## Project status

Project is on early beta stage. I recommend you to use it only for personal purposes like playing Minecraft with friends.

Current ui is using [clui](https://github.com/VladimirMarkelov/clui), but i'm working on more comfortable one, which will be using [gotk3](https://github.com/gotk3/gotk3) (bindings to gtk3)

Also i'm thinking about making cli.

### Feel free to contribute this project by opening an issue (it can be also a question) or creating pull requests

#### Star this project if you liked it or found it useful for you :3
27 changes: 27 additions & 0 deletions cmd/tui/build-and-zip-for-all-targets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh

rm -r ./builds

build() {
if [ "$1" = "windows" ]; then
./build.sh "$1" "$2" "./builds/P2P Forwarder.exe"
zip "./builds/P2P_Forwarder_$1_$2.zip" "./builds/P2P Forwarder.exe"
else
./build.sh "$1" "$2" "./builds/P2P Forwarder"
zip "./builds/P2P_Forwarder_$1_$2.zip" "./builds/P2P Forwarder"
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/P2P Forwarder.exe"
rm "./builds/P2P Forwarder"
12 changes: 12 additions & 0 deletions cmd/tui/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/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"
230 changes: 230 additions & 0 deletions cmd/tui/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
package main

import (
"strconv"
"strings"
"time"

"github.com/VladimirMarkelov/clui"
p2pforwarder "github.com/nickname32/p2p-forwarder"
)

func main() {
clui.InitLibrary()
defer clui.DeinitLibrary()

createView()

clui.MainLoop()
}

func createView() {
win := clui.AddWindow(0, 0, 0, 0, "P2P Forwarder")
win.SetPack(clui.Horizontal)
win.SetTitleButtons(clui.ButtonDefault)
win.SetMaximized(true)
win.SetSizable(false)
win.SetMovable(false)
win.SetBorder(clui.BorderNone)

frame := clui.CreateFrame(win, 0, 0, clui.BorderThin, clui.AutoSize)
frame.SetPack(clui.Vertical)

onErrFn, onInfoFn := createLog(clui.CreateFrame(win, 0, 0, clui.BorderThin, clui.AutoSize))
p2pforwarder.OnError(onErrFn)
p2pforwarder.OnInfo(onInfoFn)

label := clui.CreateLabel(frame, 64, 1, "Initialization...", clui.AutoSize)
clui.RefreshScreen()

fwr, cancel, err := p2pforwarder.NewForwarder()
if err != nil {
label.SetTitle("Error: " + err.Error())
return
}

win.OnClose(func(_ clui.Event) bool {
onInfoFn("Shutting down...")
cancel()
return true
})

label.Destroy()

createYourID(frame, fwr)
createConnections(frame, fwr)
createPortsControl(frame, fwr)
}

func createLog(parent clui.Control) (onErrFn func(error), onInfoFn func(string)) {
textView := clui.CreateTextView(parent, 0, 0, clui.AutoSize)
textView.SetMaxItems(500)
textView.SetAutoScroll(true)

logCh := make(chan string, 5)
go func() {
for {
msg := <-logCh
textView.AddText([]string{time.Now().Format("15:04:05.000") + " " + msg})
clui.RefreshScreen()
}
}()

return func(err error) {
logCh <- "Error - " + err.Error()
}, func(str string) {
logCh <- "Info - " + str
}
}

func createYourID(parent clui.Control, fwr *p2pforwarder.Forwarder) {
frame := clui.CreateFrame(parent, 0, 0, clui.BorderThick, clui.Fixed)

clui.CreateLabel(frame, 8, 1, "Your ID", clui.Fixed)

editField := clui.CreateEditField(frame, 56, fwr.ID(), clui.Fixed)
editField.OnChange(func(_ clui.Event) {
editField.SetTitle(fwr.ID())
})
}

func createConnections(parent clui.Control, fwr *p2pforwarder.Forwarder) {
clui.CreateLabel(clui.CreateFrame(parent, 0, 0, clui.BorderThin, clui.Fixed), 11, 1, "Connections", clui.Fixed)

frameA := clui.CreateFrame(parent, 0, 0, clui.BorderNone, clui.Fixed)
frameA.SetPack(clui.Horizontal)

buttonA := clui.CreateButton(frameA, 9, 4, "Connect", clui.Fixed)

frameB := clui.CreateFrame(frameA, 0, 0, clui.BorderNone, clui.Fixed)
frameB.SetPack(clui.Vertical)

editField := clui.CreateEditField(frameB, 56, "id here", clui.Fixed)

frameC := clui.CreateFrame(frameB, 0, 0, clui.BorderNone, clui.Fixed)
frameC.SetPack(clui.Horizontal)

label := clui.CreateLabel(frameB, 56, 1, "", clui.Fixed)

frameD := clui.CreateFrame(parent, 0, 0, clui.BorderNone, clui.Fixed)
frameA.SetPack(clui.Horizontal)
buttonB := clui.CreateButton(frameD, 9, 4, "Disconn", clui.Fixed)
listBox := clui.CreateListBox(frameD, 56, 4, clui.Fixed)

connsMap := map[string]func(){}

buttonA.OnClick(func(_ clui.Event) {
connInfo := strings.TrimSpace(editField.Title())

listenip, cancel, err := fwr.Forward(connInfo)
if err != nil {
label.SetTitle("Error: " + err.Error())
return
}

ok := listBox.AddItem(connInfo)
if !ok {
panic(ok)
}

connsMap[connInfo] = cancel

label.SetTitle("Connections are listened on " + listenip)
})
buttonB.OnClick(func(_ clui.Event) {
itemid := listBox.SelectedItem()
if itemid == -1 {
return
}

connInfo, ok := listBox.Item(itemid)
if !ok {
return
}

connsMap[connInfo]()

delete(connsMap, connInfo)

ok = listBox.RemoveItem(itemid)
if !ok {
panic(ok)
}
})
}

func createPortsControl(parent clui.Control, fwr *p2pforwarder.Forwarder) {
clui.CreateLabel(clui.CreateFrame(parent, 0, 0, clui.BorderThin, clui.Fixed), 13, 1, "Ports control", clui.Fixed)

frameA := clui.CreateFrame(parent, 0, 0, clui.BorderNone, clui.Fixed)
frameA.SetPack(clui.Horizontal)

buttonA := clui.CreateButton(frameA, 9, 4, "Open", clui.Fixed)

frameB := clui.CreateFrame(frameA, 0, 0, clui.BorderNone, clui.Fixed)
frameB.SetPack(clui.Vertical)

frameC := clui.CreateFrame(frameB, 0, 0, clui.BorderNone, clui.Fixed)
frameC.SetPack(clui.Horizontal)

editFieldA := clui.CreateEditField(frameC, 13, "tcp/udp here", clui.Fixed)
clui.CreateLabel(frameC, 1, 1, " ", clui.Fixed)
editFieldB := clui.CreateEditField(frameC, 16, "port here", clui.Fixed)

label := clui.CreateLabel(frameB, 56, 1, "", clui.Fixed)

frameD := clui.CreateFrame(parent, 0, 0, clui.BorderNone, clui.Fixed)
frameA.SetPack(clui.Horizontal)
buttonB := clui.CreateButton(frameD, 9, 4, "Close", clui.Fixed)
listBox := clui.CreateListBox(frameD, 56, 4, clui.Fixed)

portsMap := map[string]func(){}

buttonA.OnClick(func(_ clui.Event) {
portstr := strings.TrimSpace(editFieldB.Title())
networkType := strings.TrimSpace(editFieldA.Title())

port, err := strconv.ParseUint(portstr, 10, 16)
if err != nil {
label.SetTitle("Error: " + err.Error())
return
}

cancel, err := fwr.OpenPort(networkType, uint16(port))
if err != nil {
label.SetTitle("Error: " + err.Error())
return
}

portInfo := networkType + ":" + portstr

ok := listBox.AddItem(portInfo)
if !ok {
panic(ok)
}

portsMap[portInfo] = cancel

label.SetTitle("Port " + portInfo + " opened")
})
buttonB.OnClick(func(_ clui.Event) {
itemid := listBox.SelectedItem()
if itemid == -1 {
return
}

portInfo, ok := listBox.Item(itemid)
if !ok {
return
}

portsMap[portInfo]()

delete(portsMap, portInfo)

ok = listBox.RemoveItem(itemid)
if !ok {
panic(ok)
}
})
}
Loading

0 comments on commit f319d28

Please sign in to comment.