-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor, add magicpacket validation and tests
- Loading branch information
Showing
8 changed files
with
228 additions
and
106 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
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,51 @@ | ||
package listen | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"syscall" | ||
|
||
"github.com/jedrw/gowake/pkg/magicpacket" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var port int | ||
var ip string | ||
|
||
var ListenCmd = &cobra.Command{ | ||
Use: "listen", | ||
Short: "Listen for a magic packet", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ip, _ := cmd.Flags().GetString("ip") | ||
port, _ := cmd.Flags().GetInt("port") | ||
cont, _ := cmd.Flags().GetBool("continuous") | ||
fmt.Printf("Listening for magic packets on %s:%d\n", ip, port) | ||
for { | ||
remote, mac, err := magicpacket.Listen(ip, port) | ||
if err != nil { | ||
var errno syscall.Errno | ||
if errors.As(err, &errno) { | ||
if errno == syscall.EACCES { | ||
return fmt.Errorf("%w: please run as elevated user", err) | ||
} | ||
} else { | ||
return err | ||
} | ||
} | ||
|
||
fmt.Printf("%s from %s\n", mac, remote.String()) | ||
if !cont { | ||
break | ||
} | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
var continuous bool | ||
ListenCmd.Flags().IntVarP(&port, "port", "p", 9, "Port to listen for magic packets on") | ||
ListenCmd.Flags().StringVarP(&ip, "ip", "i", "0.0.0.0", "Address to listen for magic packets on") | ||
ListenCmd.Flags().BoolVarP(&continuous, "continuous", "c", false, "Listen continuously for magic packets") | ||
} |
This file was deleted.
Oops, something went wrong.
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,31 +1,62 @@ | ||
package magicpacket | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"net" | ||
) | ||
|
||
var ( | ||
ErrNotValidEUI48MacAddress = errors.New("not a valid EUI-48 MAC address") | ||
ErrMalformedMagicPacket = errors.New("malformed magic packet") | ||
) | ||
|
||
type MagicPacket [102]byte | ||
|
||
func New(mac string) (MagicPacket, error) { | ||
// Parse mac address | ||
hwAddr, err := net.ParseMAC(mac) | ||
if err != nil { | ||
return MagicPacket{}, err | ||
} | ||
|
||
if len(hwAddr) != 6 { | ||
return MagicPacket{}, fmt.Errorf("invalid EUI-48 MAC address") | ||
return MagicPacket{}, fmt.Errorf("%w, %s", ErrNotValidEUI48MacAddress, hwAddr) | ||
} | ||
|
||
// Build magicpacket | ||
magicPacket := MagicPacket{255, 255, 255, 255, 255, 255} | ||
|
||
offset := 6 | ||
for i := 0; i < 16; i++ { | ||
copy(magicPacket[offset:], hwAddr[:]) | ||
offset += 6 | ||
} | ||
|
||
return magicPacket, err | ||
return magicPacket, nil | ||
} | ||
|
||
func (mp MagicPacket) Bytes() []byte { | ||
return mp[:] | ||
} | ||
|
||
func (mp MagicPacket) Mac() string { | ||
return net.HardwareAddr.String(mp[96:]) | ||
} | ||
|
||
func (mp MagicPacket) Validate() error { | ||
macLength := 6 | ||
offset := 6 | ||
for i := 0; i < 16; i++ { | ||
if !bytes.Equal(mp[offset:offset+macLength], mp[96:]) { | ||
return ErrMalformedMagicPacket | ||
} | ||
|
||
offset += 6 | ||
} | ||
|
||
_, err := net.ParseMAC(mp.Mac()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.