-
Notifications
You must be signed in to change notification settings - Fork 7
/
net.go
148 lines (131 loc) · 3.56 KB
/
net.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
package compilers
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// Compile request object
type Request struct {
ScriptName string `json:name"`
Language string `json:"language"`
Script []byte `json:"script"` // source code file bytes
Includes map[string][]byte `json:"includes"` // filename => source code file bytes
Libraries string `json:"libraries"` // string of libName:LibAddr separated by comma
}
// Compile response object
type ResponseItem struct {
Objectname string `json:"objectname"`
Bytecode []byte `json:"bytecode"`
ABI string `json:"abi"` // json encoded
}
type Response struct {
Objects []ResponseItem `json:"objects"`
Error string `json:"error"`
}
// Proxy request object.
// A proxy request must contain a source.
// If the source is a literal (rather than filename),
// ProxyReq.Literal must be set to true and ProxyReq.Language must be provided
type ProxyReq struct {
Source string `json:"source"`
Literal bool `json:"literal"`
Language string `json:"language"`
Libraries string `json:"libraries"` // string of libName:LibAddr separated by comma
}
type ProxyRes struct {
Bytecode string `json:"bytecode"`
ABI string `json:"abi"` // json encoded abi struct
Error string `json:"error"`
}
// New Request object from script and map of include files
func NewRequest(script []byte, includes map[string][]byte, lang string, libs string) *Request {
if includes == nil {
includes = make(map[string][]byte)
}
req := &Request{
Script: script,
Includes: includes,
Language: lang,
Libraries: libs,
}
return req
}
// New response object from bytecode and an error
func NewResponse(objectname string, bytecode []byte, abi string, err error) *Response {
e := ""
if err != nil {
e = err.Error()
}
respItem := ResponseItem{
Objectname: objectname,
Bytecode: bytecode,
ABI: abi}
respItemArray := make([]ResponseItem, 1)
respItemArray[0] = respItem
return &Response{
Objects: respItemArray,
Error: e,
}
}
func NewProxyResponse(bytecode []byte, abi string, err error) *ProxyRes {
e := ""
if err != nil {
e = err.Error()
}
script := ""
if bytecode != nil {
script = hex.EncodeToString(bytecode)
}
return &ProxyRes{
Bytecode: script,
ABI: abi,
Error: e,
}
}
// send an http request and wait for the response
func requestResponse(req *Request) (*Response, error) {
lang := req.Language
URL := Languages[lang].URL
// logger.Debugf("Lang & URL for request =>\t%s:%s\n", URL, lang)
// make request
reqJ, err := json.Marshal(req)
if err != nil {
logger.Errorln("failed to marshal req obj", err)
return nil, err
}
httpreq, err := http.NewRequest("POST", URL, bytes.NewBuffer(reqJ))
if err != nil {
logger.Errorln("failed to compose request:", err)
return nil, err
}
httpreq.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(httpreq)
if err != nil {
logger.Errorln("failed to send HTTP request", err)
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode > 300 {
return nil, fmt.Errorf("HTTP error: %d", resp.StatusCode)
}
respJ := new(Response)
// read in response body
body, err := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(body, respJ)
if err != nil {
logger.Errorln("failed to unmarshal", err)
return nil, err
}
return respJ, nil
}
func printRequest(req *Request) {
fmt.Println("SCRIPT:", string(req.Script))
for k, v := range req.Includes {
fmt.Println("include:", k)
fmt.Println("SCRIPT:", string(v))
}
}