Skip to content

Commit

Permalink
Add GUI support (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Sep 6, 2022
1 parent 5623e88 commit 9125745
Show file tree
Hide file tree
Showing 24 changed files with 643 additions and 250 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ jobs:
- name: Test
run: |
make test
- name: Test
run: |
make test
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
bin/
.idea/
coverage.out
release
51 changes: 31 additions & 20 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,59 @@ project_name: transfer
builds:
- env:
- CGO_ENABLED=0
id: "cli"
binary: transfer
goarch:
- amd64
- arm64
- arm
goarm:
- 7
- 6
goos:
- windows
- linux
- darwin
ignore:
- goos: windows
goarch: arm
- goos: windows
goarch: arm64
- goos: darwin
goarch: arm
hooks:
post:
- upx "{{ .Path }}"
ldflags:
- -X github.com/linuxsuren/cobra-extension/version.version={{.Version}}
- -X github.com/linuxsuren/cobra-extension/version.commit={{.ShortCommit}}
- -X github.com/linuxsuren/cobra-extension/version.date={{.Date}}
- -w
- -s
- env:
- CGO_ENABLED=0
id: "gui"
binary: transfer-gui
main: ./ui/main.go
goos:
- windows
- linux
- darwin
ldflags:
- -X github.com/linuxsuren/cobra-extension/version.version={{.Version}}
- -X github.com/linuxsuren/cobra-extension/version.commit={{.ShortCommit}}
- -X github.com/linuxsuren/cobra-extension/version.date={{.Date}}
- -w
dist: release
archives:
- name_template: "{{ .Binary }}-{{ .Os }}-{{ .Arch }}{{ .Arm }}"
- name_template: "{{ .Binary }}-{{ .Os }}-{{ .Arch }}"
id: "cli"
builds:
- "cli"
replacements:
darwin: darwin
linux: linux
windows: windows
amd64: amd64
arm64: arm64
format_overrides:
- goos: windows
format: zip
files:
- README.md
- LICENSE
- name_template: "transfer-gui-{{ .Os }}-{{ .Arch }}"
id: "gui"
builds:
- "gui"
replacements:
darwin: darwin
linux: linux
amd64: amd64
files:
- README.md
- LICENSE
checksum:
name_template: 'checksums.txt'
snapshot:
Expand Down
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,12 @@ build-linux:

build-all: build-darwin build-linux build-win

build-gui:
CGO_ENABLE=0 GOOS=windows go build -ldflags -H=windowsgui -o bin/win/transfer-gui.exe ui/main.go
CGO_ENABLE=0 GOOS=windows go build -o bin/win/transfer-gui.exe ui/main.go

goreleaser:
goreleaser build --rm-dist --snapshot

test:
go test ./... -coverprofile coverage.out
70 changes: 70 additions & 0 deletions cmd/send.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cmd

import (
"context"
"fmt"
"github.com/linuxsuren/transfer/pkg"
"github.com/spf13/cobra"
"time"
)

func NewSendCmd() (cmd *cobra.Command) {
opt := &sendOption{}

cmd = &cobra.Command{
Use: "send",
Short: "Send data with UDP protocol",
PreRunE: opt.preRunE,
RunE: opt.runE,
}
flags := cmd.Flags()
flags.IntVarP(&opt.port, "port", "p", 3000, "The port to send")
return
}

type sendOption struct {
ip string
port int
}

func (o *sendOption) preRunE(cmd *cobra.Command, args []string) (err error) {
if len(args) >= 2 {
o.ip = args[1]
return
}

cmd.Println("no target ip provided, trying to find it")

ctx, cancel := context.WithCancel(cmd.Context())

waiter := make(chan string, 10)
pkg.FindWaiters(ctx, waiter)

o.ip = <-waiter
cancel()
return
}

func (o *sendOption) runE(cmd *cobra.Command, args []string) (err error) {
beginTime := time.Now()
if len(args) <= 0 {
cmd.PrintErrln("filename is required")
return
}

file := args[0]

sender := pkg.NewUDPSender(o.ip).WithPort(o.port)
msg := make(chan string, 10)

go func() {
for a := range msg {
cmd.Println(a)
}
}()

err = sender.Send(msg, file)
endTime := time.Now()
fmt.Printf("sent over with %f\n", endTime.Sub(beginTime).Seconds())
return
}
42 changes: 42 additions & 0 deletions cmd/wait.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cmd

import (
"github.com/linuxsuren/transfer/pkg"
"github.com/spf13/cobra"
)

type waitOption struct {
port int
listen string
}

func (o *waitOption) preRunE(cmd *cobra.Command, _ []string) (err error) {
err = pkg.Broadcast(cmd.Context())
return
}

func (o *waitOption) runE(cmd *cobra.Command, args []string) error {
waiter := pkg.NewUDPWaiter(o.port).ListenAddress(o.listen)
msg := make(chan string, 10)

go func() {
for a := range msg {
cmd.Println(a)
}
}()
return waiter.Start(msg)
}

func NewWaitCmd() (cmd *cobra.Command) {
opt := &waitOption{}
cmd = &cobra.Command{
Use: "wait",
Short: "Wait the data from a UDP protocol",
PreRunE: opt.preRunE,
RunE: opt.runE,
}
flags := cmd.Flags()
flags.IntVarP(&opt.port, "port", "p", 3000, "The port to listen")
flags.StringVarP(&opt.listen, "listen", "l", "0.0.0.0", "The address that want to listen")
return
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ require (
)

require (
github.com/asticode/go-astikit v0.29.1 // indirect
github.com/asticode/go-astilectron v0.29.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/maxence-charriere/go-app/v9 v9.6.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
github.com/asticode/go-astikit v0.29.1 h1:w27sLYXK84mDwArf/Vw1BiD5dfD5PBDB+iHoIcpYq0w=
github.com/asticode/go-astikit v0.29.1/go.mod h1:h4ly7idim1tNhaVkdVBeXQZEE3L0xblP7fCWbgwipF0=
github.com/asticode/go-astilectron v0.29.0 h1:oeceXo55BwcOWUMF/9FHfURBCORt7tgMwh7zeYyFGj0=
github.com/asticode/go-astilectron v0.29.0/go.mod h1:o7wZ7KDr3XH3xcEwcxfpWzNVf63JsMKtif/6IP4mpHk=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/maxence-charriere/go-app/v9 v9.6.4 h1:XYQZhr5AXiV+zw7eRQsLRNCicG7hdKYAB+BnNbpdRqw=
github.com/maxence-charriere/go-app/v9 v9.6.4/go.mod h1:UlniES44R5JoD4HsjMNrAqWXSzyw0smM0Ox+QwnO/IE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand All @@ -13,11 +21,13 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
38 changes: 2 additions & 36 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package main

import (
"fmt"
"strings"
"time"

cmd2 "github.com/linuxsuren/transfer/cmd"
"github.com/spf13/cobra"
)

Expand All @@ -13,41 +10,10 @@ func NewRoot() (cmd *cobra.Command) {
Use: "transfer",
}

cmd.AddCommand(newSendCmd(), newWaitCmd())
return
}

func retry(count int, callback func() error) (err error) {
if callback == nil {
return
}

for i := 0; i < count; i++ {
if err = callback(); err == nil {
break
}
// mainly do this on the darwin
time.Sleep(500 * time.Millisecond)
}
cmd.AddCommand(cmd2.NewSendCmd(), cmd2.NewWaitCmd())
return
}

func fillContainerWithNumber(num, size int) string {
return fillContainer(fmt.Sprintf("%d", num), size)
}

func fillContainer(txt string, size int) string {
length := len(txt)
buf := strings.Builder{}

prePendCount := size - length
for i := 0; i < prePendCount; i++ {
buf.WriteString(" ")
}
buf.WriteString(txt)
return buf.String()
}

func main() {
cmd := NewRoot()
err := cmd.Execute()
Expand Down
62 changes: 62 additions & 0 deletions pkg/broadcast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package pkg

import (
"context"
"net"
"time"
)

// Broadcast sends the broadcast message to all the potential ip addresses
func Broadcast(ctx context.Context) (err error) {
var ifaces []net.Interface
if ifaces, err = net.Interfaces(); err != nil {
return
}

var addrs []net.Addr
var allIPs []net.IP
for _, i := range ifaces {
if addrs, err = i.Addrs(); err != nil {
continue
}

for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
}

if ip == nil || ip.IsLinkLocalUnicast() || ip.IsLoopback() || ip.To4() == nil {
continue
}

allIPs = append(allIPs, ip)
}
}

for _, ip := range allIPs {
go func(ctx context.Context, ip net.IP) {
broadcast(ctx, ip)
}(ctx, ip)
}
return
}

func broadcast(ctx context.Context, ip net.IP) {
for {
select {
case <-ctx.Done():
return
case <-time.After(3 * time.Second):
ip.To4()[3] = 255
srcAddr := &net.UDPAddr{IP: net.IPv4zero, Port: 0}
dstAddr := &net.UDPAddr{IP: ip, Port: 9981}
conn, err := net.ListenUDP("udp", srcAddr)
if err != nil {
return
}
_, _ = conn.WriteToUDP([]byte("hello"), dstAddr)
}
}
}
Loading

0 comments on commit 9125745

Please sign in to comment.