forked from xxxuuu/fnos-qb-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
163 lines (146 loc) · 3.65 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
159
160
161
162
163
package main
import (
"bytes"
"context"
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/httputil"
"os"
"os/exec"
"regexp"
"strings"
"time"
"github.com/urfave/cli/v2"
)
func fetchQbPassword() (string, error) {
// exec command "ps aux | grep [q]bittorrent-nox"
cmd := exec.Command("bash", "-c", "ps aux | grep [q]bittorrent-nox")
output, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("exec command %s: %w", cmd.String(), err)
}
// parse output(likes --webui-password=xxx) to get password
re := regexp.MustCompile(`--webui-password=(\S+)`)
matches := re.FindStringSubmatch(string(output))
if len(matches) > 1 {
return matches[1], nil
}
return "", fmt.Errorf("no qbittorrent-nox process found")
}
func watchQbPassword(ch chan string) {
ticker := time.NewTicker(1 * time.Second)
for range ticker.C {
password, err := fetchQbPassword()
if err != nil {
fmt.Printf("fetch qbittorrent-nox password: %v\n", err)
continue
}
ch <- password
}
}
func proxyCmd(ctx *cli.Context) error {
uds := ctx.String("uds")
debug := ctx.Bool("debug")
port := ctx.Int("port")
expectedPassword := ctx.String("password")
password, err := fetchQbPassword()
if err != nil {
return fmt.Errorf("fetch qbittorrent-nox password: %w", err)
}
debugf := func(format string, args ...any) {
if debug {
fmt.Printf(format, args...)
}
}
fmt.Printf("proxy running on port %d\n", port)
ch := make(chan string)
go watchQbPassword(ch)
go func() {
for newPassword := range ch {
if newPassword == password {
continue
}
password = newPassword
fmt.Printf("new password: %s\n", password)
}
}()
proxy := httputil.ReverseProxy{
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return net.Dial("unix", uds)
},
},
Rewrite: func(r *httputil.ProxyRequest) {
debugf("request: %v\n", r.In.URL.Path)
r.Out.URL.Scheme = "http"
r.Out.URL.Host = fmt.Sprintf("file://%s", uds)
r.Out.Host = fmt.Sprintf("file://%s", uds)
body, _ := io.ReadAll(r.In.Body)
r.In.ParseForm()
if strings.Contains(r.In.URL.Path, "/api/v2/auth/login") {
outPassword := password
if expectedPassword != "" {
parts := strings.Split(string(body), "&")
debugf("parts: %v\n", parts)
for _, part := range parts {
if strings.HasPrefix(part, "password=") {
inputPassword := strings.TrimPrefix(part, "password=")
if inputPassword != expectedPassword {
outPassword = ""
break
}
}
}
}
body = []byte(fmt.Sprintf("username=admin&password=%s", outPassword))
r.Out.Header.Set("Content-Length", fmt.Sprintf("%d", len(body)))
r.Out.ContentLength = int64(len(body))
}
r.Out.Header.Del("Referer")
r.Out.Header.Del("Origin")
r.Out.Body = io.NopCloser(bytes.NewBuffer(body))
},
}
err = http.ListenAndServe(fmt.Sprintf(":%d", port), &proxy)
if err != nil {
return fmt.Errorf("listen and serve: %w", err)
}
return nil
}
func main() {
app := &cli.App{
Name: "fnos-qb-proxy",
Usage: "fnos-qb-proxy is a proxy for qBittorrent in fnOS",
Action: proxyCmd,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "password",
Aliases: []string{"p"},
Usage: "if not set, any password will be accepted",
Value: "",
},
&cli.StringFlag{
Name: "uds",
Usage: "qBittorrent unix domain socket(uds) path",
Value: "/home/admin/qbt.sock",
},
&cli.BoolFlag{
Name: "debug",
Aliases: []string{"d"},
Value: false,
},
&cli.IntFlag{
Name: "port",
Usage: "proxy running port",
Value: 8080,
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}