-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb1337.go
109 lines (87 loc) · 2.22 KB
/
web1337.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
package web1337
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"time"
"golang.org/x/net/proxy"
)
type Options struct {
ChainID string
WorkflowVersion uint
NodeURL string
ProxyURL string
}
type ChainInfo struct {
NodeURL string
WorkflowVersion uint
}
type Web1337 struct {
Chains map[string]ChainInfo
CurrentChain string
Proxy http.RoundTripper
}
func mapToJSON(data map[string]interface{}) string {
jsonData, err := json.Marshal(data)
if err != nil {
return ""
}
return string(jsonData)
}
func NewWeb1337(options Options) (*Web1337, error) {
web1337 := &Web1337{
Chains: make(map[string]ChainInfo),
}
if options.ProxyURL != "" {
var transport http.RoundTripper
if options.ProxyURL[:4] == "http" {
proxyURL, err := url.Parse(options.ProxyURL)
if err != nil {
return nil, err
}
transport = &http.Transport{Proxy: http.ProxyURL(proxyURL)}
} else if options.ProxyURL[:5] == "socks" {
dialer, err := proxy.SOCKS5("tcp", options.ProxyURL[7:], nil, proxy.Direct)
if err != nil {
return nil, err
}
transport = &http.Transport{Dial: dialer.Dial}
} else {
return nil, errors.New("unsupported proxy URL")
}
web1337.Proxy = transport
} else {
web1337.Proxy = http.DefaultTransport
}
web1337.CurrentChain = options.ChainID
web1337.Chains[options.ChainID] = ChainInfo{
NodeURL: options.NodeURL,
WorkflowVersion: options.WorkflowVersion,
}
return web1337, nil
}
func (sdk *Web1337) getRequest(url string) ([]byte, error) {
client := &http.Client{Transport: sdk.Proxy, Timeout: 10 * time.Second}
resp, err := client.Get(sdk.Chains[sdk.CurrentChain].NodeURL + url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
func (sdk *Web1337) postRequest(url string, payload interface{}) ([]byte, error) {
client := &http.Client{Transport: sdk.Proxy, Timeout: 10 * time.Second}
jsonPayload, err := json.Marshal(payload)
if err != nil {
return nil, err
}
resp, err := client.Post(sdk.Chains[sdk.CurrentChain].NodeURL+url, "application/json", bytes.NewBuffer(jsonPayload))
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}