Skip to content

Commit

Permalink
Improve option parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
DeathsGun committed Apr 7, 2024
1 parent 4e7e63b commit 706bced
Show file tree
Hide file tree
Showing 16 changed files with 243 additions and 166 deletions.
3 changes: 2 additions & 1 deletion internal/commands/get.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"fmt"
"github.com/abiosoft/ishell/v2"
"github.com/deathsgun/wiesel/internal/loader"
)
Expand Down Expand Up @@ -57,7 +58,7 @@ func getFunc(c *ishell.Context) {
for _, arg := range c.Args {
for _, o := range options {
if o.Name == arg {
c.Println(o.Name, "=>", o.Value())
c.Println(o.Name, "=>", fmt.Sprintf("%#v", o.Value()))
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions internal/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func runFunc(c *ishell.Context) {

options := loader.Options[loader.CurrentModule]
for _, option := range options {
if !option.IsValid() {
console.Errorln(c, "One or more options failed to validate: "+option.Name)
return
}
option.SetRawValue(option.Value())
}

Expand Down
4 changes: 3 additions & 1 deletion internal/commands/set.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"fmt"
"github.com/abiosoft/ishell/v2"
"github.com/deathsgun/wiesel/internal/loader"
"github.com/deathsgun/wiesel/pkg/api/console"
Expand Down Expand Up @@ -50,8 +51,9 @@ func setFunc(c *ishell.Context) {
continue
}
o.SetValue(value)
c.Println("Set", option, "=>", value)
c.Println("Set", option, "=>", fmt.Sprintf("%#v", o.Value()))
return
}

console.Errorf(c, "Unknown option '%s'", option)
}
7 changes: 7 additions & 0 deletions internal/loader/plugin_nodebug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build !debug

package loader

const Debug = false

func InitModules() {}
87 changes: 18 additions & 69 deletions internal/loader/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"fmt"
"github.com/deathsgun/wiesel/pkg/api"
"github.com/deathsgun/wiesel/pkg/api/module"
"github.com/deathsgun/wiesel/pkg/api/netx"
"reflect"
"strconv"
"strings"
)

var CurrentModule string
Expand All @@ -16,17 +14,22 @@ var Options = make(map[string][]*module.Option)

func init() {
api.RegisterModule = RegisterModule
if Debug {
InitModules()
}
}

func RegisterModule(m module.Module) {
options, err := extractOptions(m)
if err != nil {
fmt.Println(err)
return
}
func RegisterModule(modules ...module.Module) {
for _, m := range modules {
options, err := extractOptions(m)
if err != nil {
fmt.Println(err)
return
}

Modules[m.Category().String()+"/"+m.Name()] = m
Options[m.Category().String()+"/"+m.Name()] = options
Modules[m.Category().String()+"/"+m.Name()] = m
Options[m.Category().String()+"/"+m.Name()] = options
}
}

func extractOptions(m module.Module) ([]*module.Option, error) {
Expand All @@ -51,69 +54,15 @@ func extractOptions(m module.Module) ([]*module.Option, error) {
option.Required, _ = strconv.ParseBool(requiredRaw)
}
if defaultRaw, ok := field.Tag.Lookup("default"); ok {
option.Default = convertType(value.Type(), defaultRaw)

err := module.ConvertType(defaultRaw, option.Instance)
if err != nil {
return nil, err
}
}

options = append(options, option)
}

return options, nil
}

func convertType(t reflect.Type, value string) any {
switch t.Kind() {
case reflect.String:
return value
case reflect.Int:
i, _ := strconv.Atoi(value)
return i
case reflect.Uint:
i, _ := strconv.ParseUint(value, 10, 64)
return i
case reflect.Int8:
i, _ := strconv.ParseInt(value, 10, 8)
return int8(i)
case reflect.Uint8:
i, _ := strconv.ParseUint(value, 10, 8)
return uint8(i)
case reflect.Int16:
i, _ := strconv.ParseInt(value, 10, 16)
return int16(i)
case reflect.Uint16:
i, _ := strconv.ParseUint(value, 10, 16)
return uint16(i)
case reflect.Int32:
i, _ := strconv.ParseInt(value, 10, 32)
return int32(i)
case reflect.Uint32:
i, _ := strconv.ParseUint(value, 10, 32)
return uint32(i)
case reflect.Int64:
i, _ := strconv.ParseInt(value, 10, 64)
return i
case reflect.Uint64:
i, _ := strconv.ParseUint(value, 10, 64)
return i
case reflect.Float32:
f, _ := strconv.ParseFloat(value, 32)
return float32(f)
case reflect.Float64:
f, _ := strconv.ParseFloat(value, 64)
return f
case reflect.Bool:
b, _ := strconv.ParseBool(value)
return b
default:
break
}
if t == reflect.TypeOf(&netx.HostList{}) {
return netx.ParseHosts(value)
}
if t == reflect.TypeOf(netx.HostList{}) {
return *netx.ParseHosts(value)
}
if t == reflect.TypeFor[[]string]() {
return strings.Split(value, ",")
}
return nil
}
17 changes: 13 additions & 4 deletions pkg/api/console/print.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package console

import (
"github.com/abiosoft/ishell/v2"
"github.com/fatih/color"
)

Expand Down Expand Up @@ -30,10 +31,6 @@ func Warningln(p Printer, s string) {
p.Println(s)
}

func Printf(p Printer, format string, args ...any) {
p.Print(color.New(color.Reset).Printf(format, args...))
}

func InfoComponent(p Printer, component, message string) {
p.Print(color.New(color.FgGreen).Add(color.Bold).Sprint("[+] "))
p.Printf("%s\t: ", component)
Expand All @@ -57,3 +54,15 @@ func ErrorComponentf(p Printer, component, format string, args ...any) {
p.Printf("%s\t: ", component)
p.Printf(format, args...)
}

func WarningComponent(c *ishell.Context, component, message string) {
c.Print(color.New(color.FgYellow).Add(color.Bold).Sprint("[!] "))
c.Printf("%s\t: ", component)
c.Println(message)
}

func WarningComponentf(c *ishell.Context, component, format string, args ...any) {
c.Print(color.New(color.FgYellow).Add(color.Bold).Sprint("[!] "))
c.Printf("%s\t: ", component)
c.Printf(format, args...)
}
25 changes: 22 additions & 3 deletions pkg/api/httpx/client.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
package httpx

import (
"crypto/tls"
"io"
"net/http"
"time"
)

//TODO: Global options for timeouts

var SecureClient = &http.Client{
Transport: &http.Transport{
TLSHandshakeTimeout: time.Second,
ResponseHeaderTimeout: time.Second,
},
}

var InsecureClient = &http.Client{
Transport: &http.Transport{
TLSHandshakeTimeout: time.Second,
ResponseHeaderTimeout: time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}

func Get(url string) (*http.Response, error) {
req, err := NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", RandomUserAgent())

return http.DefaultClient.Do(req)
return SecureClient.Do(req)
}

func Post(url string, body io.Reader) (*http.Response, error) {
Expand All @@ -21,7 +40,7 @@ func Post(url string, body io.Reader) (*http.Response, error) {
return nil, err
}

return http.DefaultClient.Do(req)
return SecureClient.Do(req)
}

func Head(url string) (*http.Response, error) {
Expand All @@ -32,5 +51,5 @@ func Head(url string) (*http.Response, error) {

req.Header.Set("User-Agent", RandomUserAgent())

return http.DefaultClient.Do(req)
return SecureClient.Do(req)
}
61 changes: 61 additions & 0 deletions pkg/api/module/converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package module

import (
"encoding"
"fmt"
"reflect"
"strconv"
"strings"
)

func ConvertType(value string, ref *reflect.Value) error {
if ref.Kind() == reflect.Ptr && ref.IsNil() {
ref.Set(reflect.New(ref.Type().Elem()))
}
unmarshaler, ok := ref.Interface().(encoding.TextUnmarshaler)
if ok {
return unmarshaler.UnmarshalText([]byte(value))
}

switch ref.Kind() {
case reflect.String:
ref.SetString(value)
return nil
case reflect.Bool:
v, err := strconv.ParseBool(value)
if err != nil {
return err
}
ref.SetBool(v)
return nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return err
}
ref.SetInt(v)
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
v, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return err
}
ref.SetUint(v)
return nil
case reflect.Float32, reflect.Float64:
v, err := strconv.ParseFloat(value, 64)
if err != nil {
return err
}
ref.SetFloat(v)
return nil
case reflect.Slice:
if ref.Type().Elem().Kind() != reflect.String {
return fmt.Errorf("unsupported type %s", ref.Type().Elem().Kind())
}
ref.Set(reflect.ValueOf(strings.Split(value, ",")))
return nil
default:
return fmt.Errorf("unsupported type %s", ref.Kind())
}
}
Loading

0 comments on commit 706bced

Please sign in to comment.