Skip to content

Commit 3c5e5b5

Browse files
refactor: redesign with cobra and add autocomplete and version option (#39)
* refactor: redesign with cobra and add autocomplete and version option * chore: update github action version * feat: use viper for reading config from file * feat: use yaml format for config file * chore: change config format of dns to yaml * fix: change yaml to yml * chore: make exeption better
1 parent ebf36aa commit 3c5e5b5

22 files changed

Lines changed: 406 additions & 282 deletions

File tree

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- name: Set up Go
2121
uses: actions/setup-go@v4
2222
with:
23-
go-version: '>1.23.1'
23+
go-version: '>1.24.0'
2424

2525
- name: Test
2626
run: go test -v ./...

.github/workflows/golangci-lint.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ jobs:
2525
- name: Checkout repository
2626
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2727
- name: Install Go
28-
uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
28+
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
2929
with:
30-
go-version: 1.23.x
30+
go-version: 1.24.x
3131
- name: Lint
32-
uses: golangci/golangci-lint-action@971e284b6050e8a5849b72094c50ab08da042db8 # v6.1.1
32+
uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8.0.0
3333
with:
3434
args: --verbose
35-
version: v1.62.0
35+
version: v2.1.6

.golangci.yml

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,47 @@
1+
version: "2"
12
linters:
23
enable:
34
- depguard
4-
- goimports
55
- misspell
66
- revive
7-
8-
issues:
9-
exclude-rules:
10-
- path: _test.go
11-
linters:
12-
- errcheck
13-
14-
linters-settings:
15-
depguard:
16-
rules:
17-
no_exec_policy:
18-
files:
19-
- "!$test"
20-
deny:
21-
- pkg: "os/exec"
22-
desc: "Using os/exec to run sub processes it not allowed by policy"
23-
errcheck:
24-
exclude-functions:
25-
# Used in HTTP handlers, any error is handled by the server itself.
26-
- (net/http.ResponseWriter).Write
27-
revive:
7+
settings:
8+
depguard:
9+
rules:
10+
no_exec_policy:
11+
files:
12+
- '!$test'
13+
deny:
14+
- pkg: os/exec
15+
desc: Using os/exec to run sub processes it not allowed by policy
16+
errcheck:
17+
exclude-functions:
18+
- (net/http.ResponseWriter).Write
19+
revive:
20+
rules:
21+
- name: unused-parameter
22+
severity: warning
23+
disabled: true
24+
exclusions:
25+
generated: lax
26+
presets:
27+
- comments
28+
- common-false-positives
29+
- legacy
30+
- std-error-handling
2831
rules:
29-
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unused-parameter
30-
- name: unused-parameter
31-
severity: warning
32-
disabled: true
32+
- linters:
33+
- errcheck
34+
path: _test.go
35+
paths:
36+
- third_party$
37+
- builtin$
38+
- examples$
39+
formatters:
40+
enable:
41+
- goimports
42+
exclusions:
43+
generated: lax
44+
paths:
45+
- third_party$
46+
- builtin$
47+
- examples$

.goreleaser.yaml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ before:
66

77
builds:
88
- id: "403unlockercli-linux" # Unique ID for this build
9-
main: ./cmd/403unlockercli
9+
main: ./cmd
1010
binary: "403unlocker"
1111
env:
1212
- CGO_ENABLED=0
@@ -15,19 +15,23 @@ builds:
1515
goarch:
1616
- amd64
1717
- arm64
18+
ldflags:
19+
- -X 403unlocker-cli/cmd/cli.Version={{ .Version }}
1820

1921
- id: "403unlockercli-windows" # Unique ID for this build
20-
main: ./cmd/403unlockercli
22+
main: ./cmd
2123
binary: "403unlocker"
2224
env:
2325
- CGO_ENABLED=0
2426
goos:
2527
- windows
2628
goarch:
2729
- amd64
30+
ldflags:
31+
- -X 403unlocker-cli/cmd/cli.Version={{ .Version }}
2832

2933
- id: "403unlockercli-darwin" # Unique ID for this build
30-
main: ./cmd/403unlockercli
34+
main: ./cmd
3135
binary: "403unlocker"
3236
env:
3337
- CGO_ENABLED=0
@@ -36,6 +40,8 @@ builds:
3640
goarch:
3741
- amd64
3842
- arm64
43+
ldflags:
44+
- -X 403unlocker-cli/cmd/cli.Version={{ .Version }}
3945

4046
archives:
4147
- format: tar.gz
@@ -65,4 +71,4 @@ release:
6571
6672
---
6773
68-
Released by [GoReleaser](https://github.com/goreleaser/goreleaser).
74+
Released by [GoReleaser](https://github.com/goreleaser/goreleaser).

Makefile

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

22
OUTPUT = 403unlocker
3-
MAIN = cmd/403unlockercli/main.go
3+
MAIN = cmd/main.go
44
BIN_DIR = ~/.local/bin
55
CONFIG_DIR= ~/.config/403unlocker
6-
DNS_CONFIG_FILE_URL=https://raw.githubusercontent.com/403unlocker/403Unlocker-cli/refs/heads/main/config/dns.conf
7-
DOCKER_CONFIG_FILE_URL=https://raw.githubusercontent.com/403unlocker/403Unlocker-cli/refs/heads/main/config/dockerRegistry.conf
6+
DNS_CONFIG_FILE_URL=https://raw.githubusercontent.com/403unlocker/403Unlocker-cli/refs/heads/main/config/dns.yml
7+
DOCKER_CONFIG_FILE_URL=https://raw.githubusercontent.com/403unlocker/403Unlocker-cli/refs/heads/main/config/dockerRegistry.yml
88

99
.DEFAULT_GOAL := help
1010

@@ -23,23 +23,23 @@ lint:
2323

2424

2525
build:
26-
@go build -o $(OUTPUT) $(MAIN)
26+
@go build -ldflags "-X 403unlocker-cli/cmd/cli.Version=dev" -o $(OUTPUT) $(MAIN)
2727

2828

29-
test:
29+
test:
3030
@go test ./...
3131

3232

33-
clean:
33+
clean:
3434
@rm -f $(OUTPUT)
3535

3636

37-
install: build
37+
install: build
3838
@echo "Installing $(OUTPUT) to $(BIN_DIR)..."
3939
@install -m 755 $(OUTPUT) $(BIN_DIR)
40-
@echo "Downloading config files dns.conf to $(CONFIG_DIR)..."
40+
@echo "Downloading config files dns.yml to $(CONFIG_DIR)..."
4141
@wget $(DNS_CONFIG_FILE_URL) -q -P $(CONFIG_DIR)
42-
@echo "Downloading dockerRegistry.conf $(CONFIG_DIR)..."
42+
@echo "Downloading dockerRegistry.yml $(CONFIG_DIR)..."
4343
@wget $(DOCKER_CONFIG_FILE_URL) -q -P $(CONFIG_DIR)
4444

4545

cmd/403unlockercli/main.go

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

cmd/cli/bestdns.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package cli
2+
3+
import (
4+
"403unlocker-cli/internal/dns"
5+
"fmt"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var (
11+
timeoutBestDns int
12+
checkfirst bool
13+
)
14+
15+
var bestdnsCmd = &cobra.Command{
16+
Use: "bestdns",
17+
Short: "Finds the fastest DNS SNI-Proxy for downloading a specific URL",
18+
Long: `Finds the fastest DNS SNI-Proxy for downloading a specific URL
19+
20+
Examples:
21+
403unlocker bestdns --timeout 15 https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-16.8.0-ce.0.el7.x86_64.rpm/download.rpm`,
22+
Args: cobra.ExactArgs(1),
23+
RunE: func(cmd *cobra.Command, args []string) error {
24+
if !dns.URLValidator(args[0]) {
25+
fmt.Println("Error: Invalid URL")
26+
return cmd.Help()
27+
}
28+
return dns.CheckWithURL(args[0], checkfirst, timeoutBestDns)
29+
},
30+
Aliases: []string{"dns"},
31+
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
32+
return []string{}, cobra.ShellCompDirectiveNoFileComp
33+
},
34+
}
35+
36+
func init() {
37+
bestdnsCmd.PersistentFlags().IntVarP(&timeoutBestDns, "timeout", "t", 10, "Sets timeout")
38+
bestdnsCmd.PersistentFlags().BoolVarP(&checkfirst, "check", "c", false, "first check with GET to route '/' of domain ")
39+
40+
rootCmd.AddCommand(bestdnsCmd)
41+
}

cmd/cli/check.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cli
2+
3+
import (
4+
"403unlocker-cli/internal/check"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var checkCmd = &cobra.Command{
10+
Use: "check",
11+
Short: "Checks if the DNS SNI-Proxy can bypass 403 error for a specific domain",
12+
Long: `Checks if the DNS SNI-Proxy can bypass 403 error for a specific domain
13+
14+
Examples:
15+
403unlocker check https://pkg.go.dev`,
16+
Args: cobra.ExactArgs(1),
17+
RunE: func(cmd *cobra.Command, args []string) error {
18+
if !check.DomainValidator(args[0]) {
19+
cmd.Printf("Error: '%s' is not a valid domain format\n\n", args[0])
20+
return cmd.Help()
21+
}
22+
return check.CheckWithDNS(args[0])
23+
},
24+
Aliases: []string{"c"},
25+
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
26+
return []string{}, cobra.ShellCompDirectiveNoFileComp
27+
},
28+
}
29+
30+
func init() {
31+
rootCmd.AddCommand(checkCmd)
32+
}

cmd/cli/fastdocker.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cli
2+
3+
import (
4+
"403unlocker-cli/internal/docker"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var (
10+
timeoutFastDocker int
11+
)
12+
13+
var fastdockerCmd = &cobra.Command{
14+
Use: "fastdocker",
15+
Short: "Finds the fastest docker registries for a specific docker image",
16+
Long: `Examples:
17+
403unlocker fastdocker --timeout 15 gitlab/gitlab-ce:17.0.0-ce.0`,
18+
Args: cobra.ExactArgs(1),
19+
RunE: func(cmd *cobra.Command, args []string) error {
20+
if docker.DockerImageValidator(args[0]) {
21+
return docker.CheckWithDockerImage(args[0], timeoutFastDocker)
22+
} else {
23+
cmd.Printf("Error: '%s' is not a valid docker image format\n\n", args[0])
24+
return cmd.Help()
25+
}
26+
},
27+
Aliases: []string{"docker"},
28+
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
29+
return []string{}, cobra.ShellCompDirectiveNoFileComp
30+
},
31+
}
32+
33+
func init() {
34+
fastdockerCmd.PersistentFlags().IntVarP(&timeoutFastDocker, "timeout", "t", 10, "Sets timeout")
35+
36+
rootCmd.AddCommand(fastdockerCmd)
37+
38+
}

cmd/cli/root.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cli
2+
3+
import (
4+
"os"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var Version = "dev" // <- Injected by ldflags
10+
11+
var rootCmd = &cobra.Command{
12+
Use: "403unlocker",
13+
Short: "403Unlocker-CLI is a versatile command-line tool designed to bypass 403 restrictions effectively",
14+
Long: `403Unlocker-CLI is a versatile command-line tool designed to bypass 403 restrictions effectively`,
15+
Version: Version, // Enables --version flag
16+
}
17+
18+
func Execute() {
19+
err := rootCmd.Execute()
20+
if err != nil {
21+
os.Exit(1)
22+
}
23+
}
24+
25+
func init() {
26+
}

0 commit comments

Comments
 (0)