main.IsURL   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
package main
2
3
import (
4
	"errors"
5
	"net"
6
	"net/url"
7
	"os"
8
	"os/user"
9
	"path/filepath"
10
	"strings"
11
12
	"github.com/mattn/go-isatty"
13
)
14
15
// FatalCheck panics if err is not nil.
16
func FatalCheck(err error) {
17
	if err != nil {
18
		Errorf("%v", err)
19
		panic(err)
20
	}
21
}
22
23
// FilterIPV4 returns parsed ipv4 string.
24
func FilterIPV4(ips []net.IP) []string {
25
	var ret = make([]string, 0)
26
	for _, ip := range ips {
27
		if ip.To4() != nil {
28
			ret = append(ret, ip.String())
29
		}
30
	}
31
	return ret
32
}
33
34
// MkdirIfNotExist creates `folder` directory if not available
35
func MkdirIfNotExist(folder string) error {
36
	if _, err := os.Stat(folder); err != nil {
37
		if err = os.MkdirAll(folder, 0700); err != nil {
38
			return err
39
		}
40
	}
41
	return nil
42
}
43
44
// ExistDir checks if `folder` is available
45
func ExistDir(folder string) bool {
46
	_, err := os.Stat(folder)
47
	return err == nil
48
}
49
50
// DisplayProgressBar shows a fancy progress bar
51
func DisplayProgressBar() bool {
52
	return isatty.IsTerminal(os.Stdout.Fd()) && displayProgress
53
}
54
55
// FolderOf makes sure you won't get LFI
56
func FolderOf(urlStr string) string {
57
	usr, err := user.Current()
58
	FatalCheck(err)
59
	homeDir := usr.HomeDir
60
	safePath := filepath.Join(homeDir, dataFolder)
61
62
	// Parse the URL to get the path
63
	parsedURL, err := url.Parse(urlStr)
64
	FatalCheck(err)
65
66
	// Check for directory traversal attempts in the raw path before cleaning
67
	if strings.Contains(parsedURL.Path, "..") {
68
		FatalCheck(errors.New("you may be a victim of directory traversal path attack"))
69
		return "" // Return is redundant because FatalCheck will panic
70
	}
71
72
	// Extract the last path from the URL, excluding parameters
73
	cleanPath := TaskFromURL(urlStr)
74
75
	fullQualifyPath, err := filepath.Abs(filepath.Join(homeDir, dataFolder, cleanPath))
76
	FatalCheck(err)
77
78
	if !strings.HasPrefix(fullQualifyPath, safePath) {
79
		FatalCheck(errors.New("path traversal attempt detected"))
80
	}
81
82
	return fullQualifyPath
83
}
84
85
// TaskFromURL runs when you want to download a single url
86
func TaskFromURL(urlStr string) string {
87
	// Extract the last path from the URL, excluding parameters.
88
	// eg: URL_ADDRESS.com/path/to/file?param=value -> file
89
	parsedURL, err := url.Parse(urlStr)
90
	FatalCheck(err)
91
	// Clean the path to remove any directory traversal attempts
92
	// This ensures we only get the filename without any path manipulation
93
	cleanPath := filepath.Clean(parsedURL.Path)
94
	return filepath.Base(strings.TrimRight(cleanPath, "/\\"))
95
}
96
97
// IsURL checks if `s` is actually a parsable URL.
98
func IsURL(s string) bool {
99
	u, err := url.Parse(s)
100
	if err != nil {
101
		return false
102
	}
103
	// Check for scheme and host to ensure it's a valid URL
104
	return u.Scheme != "" && u.Host != ""
105
}
106