-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5623e88
commit 9125745
Showing
24 changed files
with
643 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
bin/ | ||
.idea/ | ||
coverage.out | ||
release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
Oops, something went wrong.