Skip to content

Commit

Permalink
Better url parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Kashkovsky committed Apr 15, 2022
1 parent e1a9e68 commit 4c20250
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion core/const.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package core

const ITArmyConfigURL string = "https://gist.githubusercontent.com/ddosukraine2022/f739250dba308a7a2215617b17114be9/raw/mhdos_targets_tcp.txt"
const ITArmyConfigURL string = "https://raw.githubusercontent.com/Kashkovsky/hostmonitor/main/itarmy_targets.txt"
4 changes: 2 additions & 2 deletions core/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ func (t *Tester) Test(url *url.URL) {
return
default:
t.out <- TestResult{
Id: url.Host,
Id: url.String(),
InProgress: true,
HttpStatus: "Testing...",
}
pass := t.tcp(url)
status, duration := t.http(url)
t.out <- TestResult{
Id: url.Host,
Id: url.String(),
url: *url,
Tcp: fmt.Sprintf("%d/10", pass),
HttpStatus: status,
Expand Down
33 changes: 33 additions & 0 deletions core/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package core

import (
"context"
"errors"
"io"
"net/http"
"net/url"
"strings"
"time"
)

Expand All @@ -30,3 +33,33 @@ func GetStringFromURL(url string) (string, error) {

return string(buf), nil
}

func ParceUrls(urls string) []*url.URL {
records := strings.Split(urls, "\n")
results := []*url.URL{}
for _, addr := range records {
res, err := parceUrl(addr)
if err != nil {
continue
}

results = append(results, res)
}

return results
}

func parceUrl(addr string) (*url.URL, error) {
if addr == "" {
return nil, errors.New("URL is an empty string")
}
u, err := url.Parse(addr)
if err != nil {
if !strings.Contains(addr, "://") {
return parceUrl("http://" + addr)
}
return nil, err
}

return u, nil
}
14 changes: 2 additions & 12 deletions core/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package core

import (
"log"
"net/url"
"strings"
"time"
)
Expand Down Expand Up @@ -68,18 +67,9 @@ func (w *Watcher) update() error {
}

func (w *Watcher) doWatch(f func(TestResult)) error {
records := strings.Split(w.rawUrls, "\n")
records := ParceUrls(w.rawUrls)
for _, addr := range records {
if addr == "" {
continue
}
u, err := url.Parse(addr)
if err != nil {
log.Default().Printf("Invalid url: %v, skipping...", addr)
continue
}

go w.tester.Test(u)
go w.tester.Test(addr)
go func() {
for {
select {
Expand Down

0 comments on commit 4c20250

Please sign in to comment.