Updated argparse, added properly checks for MAC address

This commit is contained in:
Victor Golovanenko 2021-02-04 08:06:34 +03:00
parent 65cc8bfabc
commit 666ffe6517
No known key found for this signature in database
GPG Key ID: 49B2AA9695DBD79F
2 changed files with 39 additions and 21 deletions

View File

@ -1,4 +1,4 @@
import std / [strutils, strformat, json] import std / [strutils, strformat, json, sequtils]
const mac_separators_replace = {"-": ":", ".": ":"} const mac_separators_replace = {"-": ":", ".": ":"}
const mac_separators_remove = {":": "", "-": "", ".": ""} const mac_separators_remove = {":": "", "-": "", ".": ""}
@ -636,6 +636,19 @@ proc generateAll(mac: string, testing: bool = false): seq[PinCode] =
) )
return pins return pins
proc prepareMac(mac: string): string =
## Checks if MAC address is valid and prepares it. Takes a MAC address without separators.
## Returns prepared MAC address with a ":" separator if it's valid, otherwise an empty string.
var prepared_mac = mac.multiReplace(mac_separators_remove)
if prepared_mac.len != 12:
return ""
if not all(prepared_mac, proc(c: char): bool = c in HexDigits): # Checks if each char of prepared_mac is hexdigit
return ""
prepared_mac = prepared_mac.toUpperAscii()
for i in countdown(10, 2, 2):
prepared_mac.insert(":", i)
return prepared_mac
when is_main_module: when is_main_module:
import argparse import argparse
@ -648,25 +661,30 @@ when is_main_module:
arg("mac", help = "target MAC address to generate PIN code. Example: 11:22:33:44:55:66 or 11-22-33-44-55-66") arg("mac", help = "target MAC address to generate PIN code. Example: 11:22:33:44:55:66 or 11-22-33-44-55-66")
try: try:
let args = p.parse() let args = p.parse(commandLineParams())
if (args.mac != "") and (not args.help): let mac = prepareMac(args.mac)
let mac = args.mac.multiReplace(mac_separators_replace).toUpperAscii() if mac == "":
let pins = if args.gen_all: generateAll(mac, args.gen_testing) else: generateSuggested(mac) echo &"Error: \"{args.mac}\" isn't a valid MAC address"
if args.json: quit(1)
echo $(%*pins) let pins = if args.gen_all: generateAll(mac, args.gen_testing) else: generateSuggested(mac)
else: if args.json:
if pins.len != 0: echo $(%*pins)
if not args.gen_all: else:
echo &"Found {pins.len} PIN(s)" if pins.len != 0:
echo &"""{"PIN":<8} {"Name"}""" if not args.gen_all:
echo &"Found {pins.len} PIN(s)"
echo &"""{"PIN":<8} {"Name"}"""
for pin in pins: for pin in pins:
let pin_name = if pin.mode == ALGO_STATIC: "Static PIN -- " & let pin_name = if pin.mode == ALGO_STATIC: "Static PIN -- " &
pin.name else: pin.name pin.name else: pin.name
let pin_value = if pin.mode == ALGO_EMPTY: "<empty>" else: pin.pin let pin_value = if pin.mode == ALGO_EMPTY: "<empty>" else: pin.pin
echo &"{pin_value:<8} | {pin_name}" echo &"{pin_value:<8} | {pin_name}"
else: else:
echo "No PINs found -- try to get all PINs (-A)" echo "No PINs found -- try to get all PINs (-A)"
except ShortCircuit as e:
if e.flag == "argparse_help":
quit(p.help)
except UsageError: except UsageError:
echo "Error: invalid arguments. Use -h to get help" echo "Error: invalid arguments. Use -h to get help"
quit(QuitFailure) quit(QuitFailure)

View File

@ -1,6 +1,6 @@
# Package # Package
version = "0.3.0" version = "0.4.0"
author = "Victor Golovanenko (drygdryg)" author = "Victor Golovanenko (drygdryg)"
description = "Full-featured WPS PIN generator" description = "Full-featured WPS PIN generator"
license = "MIT" license = "MIT"
@ -12,4 +12,4 @@ bin = @["wpspin"]
# Dependencies # Dependencies
requires "nim >= 0.20.0" requires "nim >= 0.20.0"
requires "argparse >= 0.10.1" requires "argparse >= 2.0.0"