-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
158 lines (131 loc) · 3.03 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"bufio"
"context"
"fmt"
"io"
"log"
"net"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"time"
"github.com/fatih/color"
)
type CommandExecuter interface {
ExecuteCommand(proxyAddr, command string, args []string) error
}
type DefaultExecuter struct{}
func (e DefaultExecuter) ExecuteCommand(proxyAddr, command string, args []string) error {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
defer cancel()
dialer, err := net.Dial("tcp", proxyAddr)
if err != nil {
return fmt.Errorf("failed to connect to proxy: %v", err)
}
defer dialer.Close()
fmt.Printf("Using proxy IP: %s\n", proxyAddr)
fmt.Println("..............................")
cmd := exec.CommandContext(ctx, command, args...)
cmd.Env = append(os.Environ(), "ALL_PROXY=socks5://"+proxyAddr)
stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
return fmt.Errorf("error creating stdout pipe: %v", err)
}
stderrPipe, err := cmd.StderrPipe()
if err != nil {
return fmt.Errorf("error creating stderr pipe: %v", err)
}
if err := cmd.Start(); err != nil {
return fmt.Errorf("error starting command: %v", err)
}
handleSignals(cmd, cancel)
displayPandaEmojis(ctx)
manageCommandOutput(stdoutPipe, false)
manageCommandOutput(stderrPipe, true)
return awaitCommandCompletion(cmd, ctx)
}
func displayPandaEmojis(ctx context.Context) {
pandaEmojis := []string{"⏳", "⌛"}
go func() {
for {
select {
case <-ctx.Done():
return
default:
for _, panda := range pandaEmojis {
fmt.Printf("\r%s ", panda)
time.Sleep(200 * time.Millisecond)
}
}
}
}()
}
func handleSignals(cmd *exec.Cmd, cancel context.CancelFunc) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM)
go func() {
<-signals
fmt.Println("\nInterrupt received, stopping...")
cancel()
terminateProcess(cmd)
}()
}
func manageCommandOutput(pipe io.Reader, isStderr bool) {
go func() {
scanner := bufio.NewScanner(pipe)
for scanner.Scan() {
line := scanner.Text()
clearLine()
if isStderr {
color.New(color.FgHiBlack).Println(line)
} else {
color.Green(line)
}
}
}()
}
func awaitCommandCompletion(cmd *exec.Cmd, ctx context.Context) error {
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
}()
select {
case <-ctx.Done():
terminateProcess(cmd)
return ctx.Err()
case err := <-done:
clearLine()
if err != nil {
return fmt.Errorf("command finished with error: %v", err)
}
}
clearLine()
return nil
}
func terminateProcess(cmd *exec.Cmd) {
if cmd.Process != nil {
cmd.Process.Kill()
}
}
func clearLine() {
fmt.Printf("\r%s\n", strings.Repeat(" ", 50))
}
func handleFatalError(message string, err error) {
if err != nil {
log.Fatalf("%s: %v", message, err)
}
}
func main() {
if len(os.Args) < 3 {
fmt.Println("Usage: go run main.go [proxy] [command] [command arguments]")
os.Exit(1)
}
executer := DefaultExecuter{}
err := executer.ExecuteCommand(os.Args[1], os.Args[2], os.Args[3:])
if err != nil {
fmt.Println(err)
}
}