From 1c00df24a4301ba4937e26bc5a23cf72489fe384 Mon Sep 17 00:00:00 2001 From: roboogg133 Date: Sat, 13 Sep 2025 23:11:48 -0300 Subject: [PATCH] Added ask for lan function --- cmd/packets/main.go | 79 ++++++++++++++++++++++++++++++++++++++++++-- go.mod | 3 +- go.sum | 6 ++-- internal/internal.go | 9 +++++ 4 files changed, 91 insertions(+), 6 deletions(-) diff --git a/cmd/packets/main.go b/cmd/packets/main.go index c14322c..651c0f3 100644 --- a/cmd/packets/main.go +++ b/cmd/packets/main.go @@ -7,22 +7,25 @@ import ( "fmt" "io" "log" + "net" "os" "packets/internal" "path/filepath" + "strconv" "strings" "time" "github.com/klauspost/compress/zstd" "github.com/pelletier/go-toml/v2" "github.com/spf13/cobra" + "golang.org/x/net/ipv4" _ "modernc.org/sqlite" ) // Consts const DefaultLinux_d = "/etc/packets" -const DefaultLANDeadline = 2 * time.Second +const LANDeadline = 2 * time.Second // Errors @@ -39,6 +42,11 @@ type ConfigTOML struct { } `toml:"Config"` } +type Peer struct { + IP net.IP + Port int +} + // init is doing some verifications func init() { @@ -183,7 +191,72 @@ func Install(file *os.File) error { return nil } -// COBRA CMDS +func AskLAN(filename string) ([]Peer, error) { + var peers []Peer + query := []byte("Q:" + filename) + pc, err := net.ListenPacket("udp", ":0") + if err != nil { + return []Peer{}, err + } + defer pc.Close() + + if pconn := ipv4.NewPacketConn(pc); pconn != nil { + _ = pconn.SetTTL(1) + } + + ifaces, _ := net.Interfaces() + for _, ifc := range ifaces { + if ifc.Flags&net.FlagUp == 0 || ifc.Flags&net.FlagLoopback != 0 { + continue + } + + addrs, _ := ifc.Addrs() + for _, a := range addrs { + ipnet, ok := a.(*net.IPNet) + if !ok || ipnet.IP.To4() == nil { + continue + } + + bcast := internal.BroadcastAddr(ipnet.IP.To4(), ipnet.Mask) + dst := &net.UDPAddr{IP: bcast, Port: 1333} + + _, err = pc.WriteTo(query, dst) + if err != nil { + fmt.Printf(":: (%s) can't send to %s: %s\n", ifc.Name, bcast, err.Error()) + } + } + } + _ = pc.SetDeadline(time.Now().Add(LANDeadline)) + buf := make([]byte, 1500) + + for { + n, addr, err := pc.ReadFrom(buf) + if err != nil { + break + } + msg := string(buf[:n]) + + if strings.HasPrefix(msg, "H:"+filename) { + parts := strings.Split(msg, ":") + port, _ := strconv.Atoi(parts[2]) + peers = append(peers, Peer{IP: addr.(*net.UDPAddr).IP, Port: port}) + } + } + return peers, nil +} + +// COBRA CMDS var rootCmd = &cobra.Command{Use: "packets"} -var installCmd = &cobra.Command{} +var installCmd = &cobra.Command{ + Use: "install {package} [packages...]", + Short: "Install a package", + Args: cobra.MinimumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + + }, +} + +func main() { + rootCmd.AddCommand(installCmd) +} diff --git a/go.mod b/go.mod index d0f9cd2..4320de3 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/pelletier/go-toml/v2 v2.2.4 github.com/spf13/cobra v1.10.1 github.com/yuin/gopher-lua v1.1.1 + golang.org/x/net v0.44.0 modernc.org/sqlite v1.38.2 ) @@ -19,7 +20,7 @@ require ( github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/spf13/pflag v1.0.10 // indirect golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect - golang.org/x/sys v0.34.0 // indirect + golang.org/x/sys v0.36.0 // indirect modernc.org/libc v1.66.3 // indirect modernc.org/mathutil v1.7.1 // indirect modernc.org/memory v1.11.0 // indirect diff --git a/go.sum b/go.sum index 587d32a..59f68be 100644 --- a/go.sum +++ b/go.sum @@ -29,11 +29,13 @@ golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/y golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8= golang.org/x/mod v0.25.0 h1:n7a+ZbQKQA/Ysbyb0/6IbB1H/X41mKgbhfv7AfG/44w= golang.org/x/mod v0.25.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww= +golang.org/x/net v0.44.0 h1:evd8IRDyfNBMBTTY5XRF1vaZlD+EmWx6x8PkhR04H/I= +golang.org/x/net v0.44.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY= golang.org/x/sync v0.15.0 h1:KWH3jNZsfyT6xfAfKiz6MRNmd46ByHDYaZ7KSkCtdW8= golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA= -golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k= +golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo= golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/internal/internal.go b/internal/internal.go index 6adf057..8651ef4 100644 --- a/internal/internal.go +++ b/internal/internal.go @@ -5,6 +5,7 @@ import ( "errors" "io" "log" + "net" "net/http" "os" "path/filepath" @@ -150,3 +151,11 @@ func GetConfigTOML() (*ConfigTOML, error) { return &config, nil } + +func BroadcastAddr(ip net.IP, mask net.IPMask) net.IP { + b := make(net.IP, len(ip)) + for i := range ip { + b[i] = ip[i] | ^mask[i] + } + return b +}