This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
121 lines (101 loc) · 2.65 KB
/
utils.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
package onion
import (
"fmt"
"net/url"
"strings"
)
var kuboGWHost = "ipfs.io"
type URLBuilder struct {
lassieIP string
l1ShimIP string
l1NginxIP string
bifrostIP string
}
func NewURLBuilder(lassieIP, l1ShimIP, l1NginxIP, bifrostIP string) *URLBuilder {
return &URLBuilder{
lassieIP: lassieIP,
l1ShimIP: l1ShimIP,
l1NginxIP: l1NginxIP,
bifrostIP: bifrostIP,
}
}
func (ub *URLBuilder) BuildURLsToTest(bifrostReqUrl string) URLsToTest {
return URLsToTest{
Path: parseRequestPath(bifrostReqUrl),
Lassie: ub.BuildLassieUrl(bifrostReqUrl),
L1Shim: ub.BuildL1ShimUrl(bifrostReqUrl),
L1Nginx: ub.BuildL1NginxUrl(bifrostReqUrl),
KuboGWUrl: ub.BuildKuboGWUrl(bifrostReqUrl),
BifrostURL: ub.BuildBifrostUrl(bifrostReqUrl),
}
}
func (b *URLBuilder) BuildLassieUrl(bifrostUrl string) string {
u := replaceIPInURL(bifrostUrl, b.lassieIP)
u = switchHTTPStoHTTP(u)
return u
}
func (b *URLBuilder) BuildL1ShimUrl(bifrostUrl string) string {
u := replaceIPInURL(bifrostUrl, b.l1ShimIP)
u = switchHTTPStoHTTP(u)
u = u + "&nocache=1"
return u
}
func (b *URLBuilder) BuildL1NginxUrl(bifrostUrl string) string {
u := replaceIPInURL(bifrostUrl, b.l1NginxIP)
u = switchHTTPtoHTTPS(u)
return u
}
func (b *URLBuilder) BuildKuboGWUrl(bifrostUrl string) string {
var result string
if idx := strings.Index(bifrostUrl, "?"); idx != -1 {
result = bifrostUrl[:idx]
} else {
panic("params not found in bifrost url")
}
result = replaceIPInURL(result, kuboGWHost)
return result
}
func (b *URLBuilder) BuildBifrostUrl(bifrostUrl string) string {
var result string
if idx := strings.Index(bifrostUrl, "?"); idx != -1 {
result = bifrostUrl[:idx]
} else {
panic("params not found in bifrost url")
}
result = replaceIPInURL(result, b.bifrostIP)
result = switchHTTPStoHTTP(result)
return result
}
func switchHTTPStoHTTP(u string) string {
return strings.Replace(u, "https://", "http://", -1)
}
func switchHTTPtoHTTPS(u string) string {
return strings.Replace(u, "http://", "https://", -1)
}
func replaceIPInURL(s, newIP string) string {
u, err := url.Parse(s)
if err != nil {
panic(fmt.Errorf("failed to parse url: %s", err))
}
u.Host = newIP
return u.String()
}
func parseRequestPath(bifrostUrl string) string {
u, err := url.Parse(bifrostUrl)
if err != nil {
panic(fmt.Errorf("failed to parse bifrost url: %s", err))
}
if len(u.Path) == 0 {
panic(fmt.Errorf("invalid bifrost url: %s; no path", bifrostUrl))
}
return u.Path
}
func ParseCidFromPath(path string) string {
split := strings.SplitN(path, "/ipfs/", 2)
if len(split) == 2 {
x := split[1]
split = strings.SplitN(x, "/", -1)
return split[0]
}
panic("invalid path")
}