Skip to content

Commit e0bc127

Browse files
authored
Improved code readibility, used go style guide and folder structure. (#2)
Go is designed to use cameCase and PascalCase for private and public functions, using snake_case here makes non sense, Add build info and rename unchain.go to main.go
1 parent 41ed5ea commit e0bc127

File tree

4 files changed

+96
-97
lines changed

4 files changed

+96
-97
lines changed

README.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,22 @@
99
A tool to find redirection chains in multiple URLs
1010
</p>
1111

12-
13-
1412
## Introduction
13+
UnChain automates process of finding and following `30X` redirects by extracting "Location" header of HTTP responses.
1514

16-
<p>
17-
UnChain automates process of finding and following `30X` redirects by extracting "Location" header of HTTP responses.
18-
</p>
15+
### Building
16+
To build UnChain simple run:
17+
```
18+
go build -o unchain ./cmd/main.go
19+
```
1920

2021
## Usage
2122

23+
```
2224
usage: unchain [-h|--help] -u|--url "<value>"
2325
24-
25-
26-
Arguments:
26+
arguments:
2727
2828
-h --help Print help information
2929
-u --url File containing urls or a single url
30-
31-
32-
30+
```

build.nix

Lines changed: 0 additions & 1 deletion
This file was deleted.

cmd/main.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"os"
7+
"strings"
8+
9+
"github.com/akamensky/argparse"
10+
"github.com/common-nighthawk/go-figure"
11+
"github.com/fatih/color"
12+
tw "github.com/olekukonko/tablewriter"
13+
cf "github.com/redcode-labs/Coldfire"
14+
)
15+
16+
func findRedirects(url string) {
17+
if !strings.Contains(url, "https://") && !strings.Contains(url, "http:") {
18+
url = "https://" + url
19+
}
20+
var tableData [][]string
21+
nextUrl := url
22+
for i := 0; i < 50; i++ {
23+
redir := &http.Client{CheckRedirect: func(req *http.Request, via []*http.Request) error {
24+
return http.ErrUseLastResponse
25+
}}
26+
res, err := redir.Get(nextUrl)
27+
if err != nil {
28+
cf.PrintError(err.Error())
29+
}
30+
nextUrl = res.Header.Get("Location")
31+
sc := cf.IntToStr(res.StatusCode)
32+
urlColor := cf.Red
33+
u := nextUrl
34+
if sc == "200" {
35+
urlColor = cf.Green
36+
u = url
37+
}
38+
id := cf.IntToStr(i)
39+
tableData = append(tableData, []string{id, urlColor(u), sc})
40+
if sc == "200" {
41+
break
42+
}
43+
}
44+
table := tw.NewWriter(os.Stdout)
45+
table.SetHeader([]string{"ID", "URL", "STATUS CODE"})
46+
table.SetAutoWrapText(false)
47+
table.SetCenterSeparator("*")
48+
table.SetAlignment(tw.ALIGN_CENTER)
49+
table.SetRowSeparator("-")
50+
for v := range tableData {
51+
table.Append(tableData[v])
52+
}
53+
if len(tableData) != 0 {
54+
fmt.Println("")
55+
cf.PrintInfo("URL => " + url)
56+
table.Render()
57+
fmt.Println("")
58+
}
59+
}
60+
61+
func printBanner() {
62+
banner := figure.NewFigure("UnChain", "", true)
63+
color.Set(color.FgMagenta)
64+
fmt.Println("")
65+
banner.Print()
66+
color.Unset()
67+
fmt.Println("")
68+
fmt.Println("\tCreated by: redcodelabs.io " + cf.Red("<*>"))
69+
fmt.Println("")
70+
}
71+
72+
func main() {
73+
printBanner()
74+
parser := argparse.NewParser("unchain", "")
75+
var URLS = parser.String("u", "url", &argparse.Options{Required: true, Help: "File containing urls or a single url"})
76+
err := parser.Parse(os.Args)
77+
cf.ExitOnError(err)
78+
var urls []string
79+
if cf.Exists(*URLS) {
80+
urls = cf.FileToSlice(*URLS)
81+
} else {
82+
urls = []string{*URLS}
83+
}
84+
for _, u := range urls {
85+
findRedirects(u)
86+
}
87+
}

unchain.go

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)