Skip to content

Commit

Permalink
Refactor runCommand
Browse files Browse the repository at this point in the history
Reduces cyclomatic complexity.
  • Loading branch information
JeremyRand committed Jan 17, 2022
1 parent 61a7b42 commit c27d0c7
Showing 1 changed file with 61 additions and 53 deletions.
114 changes: 61 additions & 53 deletions ncprop279.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,75 +254,83 @@ func (s *Server) doResolve(queryID int, qname string, qtype uint16, parseOnion b
return result
}

func runCommand(words []string, cfg *Config, s *Server) {
if len(words) < 1 {
func runResolveCommand(args []string, cfg *Config, s *Server) {
if len(args) < 2 {
fmt.Fprintf(os.Stderr, "Not enough arguments to RESOLVE command.\n")
return
}

if words[0] == "RESOLVE" {
if len(words) < 3 {
fmt.Fprintf(os.Stderr, "Not enough arguments to RESOLVE command.\n")
return
}
queryIDStr := args[0]
queryID, err := strconv.Atoi(queryIDStr)
if err != nil {
fmt.Fprintf(os.Stderr, "Query ID '%s' was not an integer.\n", queryIDStr)
return
}

queryIDStr := words[1]
queryID, err := strconv.Atoi(queryIDStr)
if err != nil {
fmt.Fprintf(os.Stderr, "Query ID '%s' was not an integer.\n", queryIDStr)
return
}
name := args[1]
originalName := name
onlyOnion := cfg.OnlyOnion

name := words[2]
originalName := name
onlyOnion := cfg.OnlyOnion
if strings.HasSuffix(name, ".onion") {
name = strings.TrimSuffix(name, ".onion")
onlyOnion = true
}

if strings.HasSuffix(name, ".onion") {
name = strings.TrimSuffix(name, ".onion")
onlyOnion = true
}
streamID := ""
if len(args) >= 3 {
streamID = args[2]
}
if streamID == "" {
fmt.Fprintf(os.Stderr, "WARNING: Missing stream isolation ID from Prop279 client; stream isolation won't work properly. Maybe your Prop279 client is outdated?\n")
}

streamID := ""
if len(words) >= 4 {
streamID = words[3]
}
if streamID == "" {
fmt.Fprintf(os.Stderr, "WARNING: Missing stream isolation ID from Prop279 client; stream isolation won't work properly. Maybe your Prop279 client is outdated?\n")
}
result := StatusNxDomain

result := StatusNxDomain
if result == StatusNxDomain {
result = s.doResolve(queryID, "_tor."+name, dns.TypeTXT, true, streamID)
}

if !onlyOnion {
if result == StatusNxDomain {
result = s.doResolve(queryID, "_tor."+name, dns.TypeTXT, true, streamID)
}

if !onlyOnion {
if result == StatusNxDomain {
result = s.doResolve(queryID, name, dns.TypeA, false, streamID)
}
if result == StatusNxDomain {
result = s.doResolve(queryID, name, dns.TypeAAAA, false, streamID)
}
if result == StatusNxDomain {
result = s.doResolve(queryID, name, dns.TypeCNAME, false, streamID)
}
result = s.doResolve(queryID, name, dns.TypeA, false, streamID)
}
if result == StatusNxDomain {
fmt.Printf("RESOLVED %d %d \"%s is not registered\"\n", queryID, result, originalName)
result = s.doResolve(queryID, name, dns.TypeAAAA, false, streamID)
}
} else if words[0] == "CANCEL" {
if len(words) < 2 {
fmt.Fprintf(os.Stderr, "Not enough arguments to CANCEL command.\n")
return
if result == StatusNxDomain {
result = s.doResolve(queryID, name, dns.TypeCNAME, false, streamID)
}
}
if result == StatusNxDomain {
fmt.Printf("RESOLVED %d %d \"%s is not registered\"\n", queryID, result, originalName)
}
}

queryIDStr := words[1]
queryID, err := strconv.Atoi(queryIDStr)
if err != nil {
fmt.Fprintf(os.Stderr, "Query ID '%s' was not an integer.\n", queryIDStr)
return
}
func runCancelCommand(args []string, cfg *Config, s *Server) {
if len(args) < 1 {
fmt.Fprintf(os.Stderr, "Not enough arguments to CANCEL command.\n")
return
}

fmt.Printf("CANCELED %d\n", queryID)
queryIDStr := args[0]
queryID, err := strconv.Atoi(queryIDStr)
if err != nil {
fmt.Fprintf(os.Stderr, "Query ID '%s' was not an integer.\n", queryIDStr)
return
}

fmt.Printf("CANCELED %d\n", queryID)
}

func runCommand(words []string, cfg *Config, s *Server) {
if len(words) < 1 {
return
}

if words[0] == "RESOLVE" {
runResolveCommand(words[1:], cfg, s)
} else if words[0] == "CANCEL" {
runCancelCommand(words[1:], cfg, s)
}
}

Expand Down

0 comments on commit c27d0c7

Please sign in to comment.