forked from BruceWangNo1/remote-torrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtClient.go
151 lines (127 loc) · 3.23 KB
/
rtClient.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
package remote_torrent
import (
"net/http"
"net/url"
"strings"
"fmt"
"io/ioutil"
"time"
"os/exec"
"log"
"os"
)
func RTClient(args []string) {
username := strings.Split(args[0], ":")[0]
password := strings.Split(args[0], ":")[1]
ipAndPort := args[1]
magnet := args[2]
//finished := make(chan error, 2)
//
//c := make(chan os.Signal, 2)
//signal.Notify(c, os.Interrupt, syscall.SIGTERM)
//
//go func() {
// select {
// case <-c:
// if err := cmd.Process.Kill(); err != nil {
// log.Fatal("failed to kill: ", err)
// }
// os.Exit(1)
// return
// case err := <-finished:
// if err != nil {
// fmt.Println(err)
// return
// }
// }
//}()
// schedule torrent download
for {
schedulingResp, err := http.PostForm("http://" + ipAndPort + "/magnet", url.Values{"username": {username}, "password": {password}, "magnet": {magnet}})
if err != nil {
fmt.Println(err)
fmt.Println("retrying...")
continue
} else {
fmt.Println("Torrent download scheduling over http succeeds.")
}
if schedulingResp.StatusCode == http.StatusUnauthorized {
log.Fatal("Authentication failed.")
return
}
body, err := ioutil.ReadAll(schedulingResp.Body)
schedulingResp.Body.Close()
if err != nil {
fmt.Println(err)
fmt.Println("retrying...")
continue
}
fmt.Println(string(body))
break
}
// status check
ticker := time.NewTicker(time.Second * 3)
for range ticker.C {
statusCheckResp, statusCheckErr := http.PostForm("http://" + ipAndPort + "/status", url.Values{"username": {username}, "password": {password}})
if statusCheckErr != nil {
fmt.Println(statusCheckErr)
}
if statusCheckResp.StatusCode == http.StatusUnauthorized {
log.Fatal("Authentication failed.")
return
}
body, err := ioutil.ReadAll(statusCheckResp.Body)
if err != nil {
fmt.Println(err)
}
fmt.Printf("\rStatus check: %s.", string(body))
statusCheckResp.Body.Close()
if strings.Contains(string(body), "Torrent Download Finished.") {
fmt.Println("\nTorrent download completed 100% on server side.")
break
}
}
ticker.Stop()
close(ticker)
// download files from server's directory
for {
_, err := exec.LookPath("wget")
if err != nil {
log.Fatal("Please install wget on your local machine first.\n")
return
}
cmd := exec.Command("wget", "-r", "--post-data", "username=" + username + "&password=" + password, "http://" + ipAndPort + "/")
cmd.Stderr = os.Stdout
err = cmd.Run()
if err != nil {
log.Fatal("Error starting wget: %v", err)
}
//go func() {
// finished <- cmd.Wait()
//}()
fmt.Println("Torrent successfully retrieved to client.")
break
}
// remove torrent on server remotely
for {
removeTorrentResponse, removeTorrentErr := http.PostForm("http://" + ipAndPort + "/remove", url.Values{"username": {username}, "password": {password}})
if removeTorrentErr != nil {
fmt.Println(removeTorrentErr)
fmt.Println("retrying...")
continue
}
if removeTorrentResponse.StatusCode == http.StatusUnauthorized {
log.Fatal("Authentication failed.")
return
}
body, err := ioutil.ReadAll(removeTorrentResponse.Body)
removeTorrentResponse.Body.Close()
if err != nil {
fmt.Println(err)
continue
}
fmt.Println(string(body))
break
}
return
}