Skip to content

Instantly share code, notes, and snippets.

@Und3rf10w
Last active September 30, 2022 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Und3rf10w/02d32a322ddff1b1e794f1b74cd5eb5b to your computer and use it in GitHub Desktop.
Save Und3rf10w/02d32a322ddff1b1e794f1b74cd5eb5b to your computer and use it in GitHub Desktop.
I have no idea how this works, but it does?
package main
import (
"fmt"
)
type methodArgumentsInterface interface {
determineStruct() interface{}
}
type httpMethodArguments struct {
host string
uri string
httpMethod string
}
type httpStruct struct {
method string
arguments httpMethodArguments
}
type smbMethodArguments struct {
host string
username string
password string
path string
}
type smbStruct struct {
method string
arguments smbMethodArguments
}
type GenericArgsStruct struct {
method string
arguments []string
}
func (m *GenericArgsStruct) determineStruct() interface{} {
switch m.method {
case "http":
// You should do a check here to ensure that len(m.arguments) matches whatever is required
retStruct := new(httpMethodArguments)
retStruct.host = m.arguments[0]
retStruct.uri = m.arguments[1]
retStruct.httpMethod = m.arguments[2]
// really you should just go do a function now instead of returning a pointer
return *retStruct
case "smb":
// You should do a check here to ensure that len(m.arguments) matches whatever is required
retStruct := new(smbMethodArguments)
retStruct.host = m.arguments[0]
retStruct.username = m.arguments[1]
retStruct.password = m.arguments[2]
retStruct.path = m.arguments[3]
// really you should just go do a function now instead of returning a pointer
return *retStruct
default:
return nil
}
return nil // silences warnings, but should never happen
}
func getArgumentsInterface() methodArgumentsInterface {
return &GenericArgsStruct{}
}
func getArgumentInstance(myInst *GenericArgsStruct) methodArgumentsInterface {
return myInst
}
func main() {
// let's pretend we received these from the user
// method := "http"
// providedArguments := []string{"example.com", "/page", "get"}
method := "smb"
providedArguments := []string{"127.0.0.1", "username", "Password123", "/secret"}
receipt := new(GenericArgsStruct)
receipt.method = method
receipt.arguments = providedArguments
foundstruct := receipt.determineStruct()
// you don't have to do the switch case if you just do whatever in determineStruct(), unless you need to access it again in this function
switch foundstruct.(type) {
case smbMethodArguments:
x := new(smbStruct)
x.method = receipt.method
x.arguments.host = foundstruct.(smbMethodArguments).host
x.arguments.username = foundstruct.(smbMethodArguments).username
x.arguments.password = foundstruct.(smbMethodArguments).password
x.arguments.path = foundstruct.(smbMethodArguments).path
// You should really just go do a function now
fmt.Println("Method: ", x.method)
fmt.Println("Arguments: ", x.arguments)
case httpMethodArguments:
x := new(httpStruct)
x.method = receipt.method
x.arguments.host = foundstruct.(httpMethodArguments).host
x.arguments.uri = foundstruct.(httpMethodArguments).uri
x.arguments.httpMethod = foundstruct.(httpMethodArguments).httpMethod
// You should really just go do a function now
fmt.Println("Method: ", x.method)
fmt.Println("Arguments: ", x.arguments)
}
// This "should work, but can't figure out how to keep the type within the switch case"
// fmt.Println("Method: ", x.method)
// fmt.Println("Arguments: ", x.arguments)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment