-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
113 lines (99 loc) · 2.6 KB
/
app.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
package main
import (
"encoding/json"
"fmt"
"github.com/oniontree-org/go-oniontree"
"github.com/urfave/cli/v2"
"net/http"
"net/url"
)
const Version = "0.1"
type Application struct {
ot *oniontree.OnionTree
app *cli.App
}
func (a *Application) handleOnionTreeOpen() cli.BeforeFunc {
return func(c *cli.Context) error {
ot, err := oniontree.Open(c.String("C"))
if err != nil {
return fmt.Errorf("failed to open OnionTree repository: %s", err)
}
a.ot = ot
return nil
}
}
func (a *Application) handleSyncCommand() cli.ActionFunc {
newService := func(id string) *oniontree.Service {
s := oniontree.NewService(id)
s.Name = "Invidious"
s.Description = "Invidious is an alternative front-end to YouTube"
return s
}
return func(c *cli.Context) error {
id := c.Args().First()
if id == "" {
return fmt.Errorf("Missing a service ID")
}
client := &http.Client{
Timeout: c.Duration("timeout"),
}
req, err := http.NewRequest("GET", c.String("url"), nil)
if err != nil {
return fmt.Errorf("failed to create new request: %s", err)
}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to establish connection with the API: %s", err)
}
defer resp.Body.Close()
// Decode JSON payload
var arr []interface{}
decoder := json.NewDecoder(resp.Body)
if err := decoder.Decode(&arr); err != nil {
return fmt.Errorf("failed to decode response data: %s", err)
}
service, err := a.ot.GetService(id)
if err != nil {
if _, ok := err.(*oniontree.ErrIdNotExists); !ok {
return fmt.Errorf("failed to get existing service data: %s", err)
}
service = newService(id)
}
normalizeURL := func(u string) (string, error) {
nu, err := url.ParseRequestURI(u)
if err != nil {
return "", err
}
return fmt.Sprintf("%s://%s", nu.Scheme, nu.Host), nil
}
urls := []string{}
for _, obj := range arr {
instance := obj.([]interface{})[1].(map[string]interface{})
if instance["type"] != "onion" {
continue
}
u, err := normalizeURL(instance["uri"].(string))
if err != nil {
return fmt.Errorf("failed to normalize URL: %s", err)
}
urls = append(urls, u)
}
if c.Bool("replace") {
service.SetURLs(urls)
} else {
service.AddURLs(urls)
}
if err := a.ot.AddService(service); err != nil {
if _, ok := err.(*oniontree.ErrIdExists); !ok {
return fmt.Errorf("failed to add new service: %s", err)
}
if err := a.ot.UpdateService(service); err != nil {
return fmt.Errorf("failed to update service: %s", err)
}
}
return nil
}
}
func (a *Application) Run(args []string) error {
return a.app.Run(args)
}