-
Notifications
You must be signed in to change notification settings - Fork 0
/
web3.go
268 lines (245 loc) · 11.6 KB
/
web3.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
"encoding/base64"
"fmt"
cc "github.com/ivanpirog/coloredcobra"
"strings"
"github.com/bitfield/script"
"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
"log"
"net/http"
"os"
"strconv"
"sync"
"github.com/skycoin/skywire-utilities/pkg/cipher"
)
const name = "web3"
const shcmd = `/usr/bin/bash -c`
var (
// vars that contain generated html pages
mainhtml *string
// html snippets
htmlstart string = "<!doctype html><html lang=en><head></head><body style='background-color:black;color:white;'>\n<style type='text/css'>\npre {\n font-family:Courier New;\n font-size:10pt;\n}\n.af_line {\n color: gray;\n text-decoration: none;\n}\n.column {\n float: left;\n width: 30%;\n padding: 10px;\n}\n.row:after {\n content: '';\n display: table;\n clear: both;\n}\n</style>\n<pre>"
n0 string = "<a id='top' class='anchor' aria-hidden='true' href='#top'></a>"
n1 string = " <a href='/'>home</a>"
n13 string = "\n<br>\n"
navlinks string = n1 + n13
htmltoplink string = "<a href='#top'>top of page</a>\n"
htmlend string = "</pre></body></html>"
htmlstyle string = "<style>\npre {\n font-family:Courier New;\n font-size:10pt;\n}\n.af_line {\n color: gray;\n text-decoration: none;\n}\n.column {\n float: left;\n width: 30%;\n padding: 10px;\n}\n.row:after {\n content: '';\n display: table;\n clear: both;\n}\n</style>\n"
bodystyle string = "<body style='background-color:black;color:white;'>\n"
)
func main() {
Execute()
}
func init() {
// rootCmd.AddCommand(
// myCmd,
// )
rootCmd.CompletionOptions.DisableDefaultCmd = true
defaultport, err := strconv.Atoi(os.Getenv("WEBPORT"))
if err != nil {
defaultport = 8080
}
rootCmd.Flags().IntVarP(&webPort, "port", "p", defaultport, "port to serve on - env WEBPORT="+os.Getenv("WEBPORT"))
}
var rootCmd = &cobra.Command{
Use: name,
Short: "web application template",
Long: `
┬ ┬┌─┐┌┐ ┌┬┐┬ ┬┬─┐┌─┐┌─┐
│││├┤ ├┴┐ │ ├─┤├┬┘├┤ ├┤
└┴┘└─┘└─┘ ┴ ┴ ┴┴└─└─┘└─┘`,
Run: func(_ *cobra.Command, _ []string) {
fmt.Printf(scriptExec(fmt.Sprintf(`%s "lsof -ti tcp:%s | xargs kill -9"`, shcmd, strconv.Itoa(webPort))))
Server()
},
}
func Execute() {
cc.Init(&cc.Config{
RootCmd: rootCmd,
Headings: cc.HiBlue + cc.Bold, //+ cc.Underline,
Commands: cc.HiBlue + cc.Bold,
CmdShortDescr: cc.HiBlue,
Example: cc.HiBlue + cc.Italic,
ExecName: cc.HiBlue + cc.Bold,
Flags: cc.HiBlue + cc.Bold,
//FlagsDataType: cc.HiBlue,
FlagsDescr: cc.HiBlue,
NoExtraNewlines: true,
NoBottomNewline: true,
})
if err := rootCmd.Execute(); err != nil {
log.Fatal("Failed to execute command: ", err)
}
}
var (
webPort int
)
func mainhtmlfunc() {
l := "<!doctype html><html lang=en><head><title>Web3</title></head><body style='background-color:black;color:white;'>\n<style type='text/css'>\npre {\n font-family:Courier New;\n font-size:10pt;\n}\n.af_line {\n color: gray;\n text-decoration: none;\n}\n.column {\n float: left;\n width: 30%;\n padding: 10px;\n}\n.row:after {\n content: '';\n display: table;\n clear: both;\n}\n</style>\n<pre>"
l += navlinks
l += fmt.Sprintf("\n%s\n",
sex("source "+name+".sh ; _dayscalc "))
l += fmt.Sprintf("%s",
sex("source "+name+".sh ; _rainbowcal "))
l += "</body></html>"
mainhtml = &l
}
func sex(cmd string) string {
return scriptExecHTML(fmt.Sprintf(`%s "%s"`, shcmd, cmd))
}
func scriptExecHTML(cmd string) string {
res, err := script.Exec(cmd).String()
if err != nil {
res += fmt.Sprintf("<br><p style='color:red'>error during script.Exec:\n<br> %v\n<br></p>command:\n<br>\n%s\n<br>\n%s", err, cmd, res)
}
return res
}
func scriptExec(cmd string) string {
fmt.Printf("executing command: \n %s", cmd)
res, err := script.Exec(cmd).String()
if err != nil {
res = fmt.Sprintf("error during script.Exec:\n %v\nCommand:\n%s\nResult:\n%s\n", err, cmd, res)
}
return res
}
func Server() {
// fmt.Println("generating in-memory html")
// go mainhtmlfunc()
wg := new(sync.WaitGroup)
wg.Add(1)
r1 := gin.Default()
r1.GET("/", func(c *gin.Context) {
c.Writer.Header().Set("Server", "")
c.Writer.WriteHeader(http.StatusOK)
mainhtmlfunc()
c.Writer.Write([]byte(*mainhtml))
return
})
r1.GET("/:pkport/:file", func(c *gin.Context) {
var publicKey string
var dmsgPort string
file := c.Param("file") // Validate Skywire public key
result := strings.SplitN(c.Param("pkport"), ":", 2)
if len(result) == 2 {
publicKey = result[0]
dmsgPort = result[1]
} else {
errorMessage := fmt.Sprintf("Invalid Skywire <public-key>:<port>")
c.Writer.Header().Set("Server", "")
c.Writer.WriteHeader(http.StatusBadRequest)
c.Writer.Write([]byte(errorMessage))
return
}
pk := cipher.PubKey{}
err := pk.Set(publicKey)
if err != nil {
errorMessage := fmt.Sprintf("Invalid Skywire public key: %s", err.Error())
c.Writer.Header().Set("Server", "")
c.Writer.WriteHeader(http.StatusBadRequest)
c.Writer.Write([]byte(errorMessage))
return
}
// Handle valid public key
cmd := fmt.Sprintf(`bash -c 'dmsgget -n dmsg://%s:%s/%s'`, pk, dmsgPort, file)
res, err := script.Exec(cmd).String()
if err != nil {
errorMessage := fmt.Sprintf("error during script.Exec:\n%v\ncommand:\n%s\nresult:\n%s", err, cmd, res)
c.Writer.Header().Set("Server", "")
c.Writer.WriteHeader(http.StatusBadRequest)
c.Writer.Write([]byte(errorMessage))
return
}
c.Writer.Header().Set("Server", "")
c.Writer.WriteHeader(http.StatusOK)
c.Writer.Write([]byte(res))
})
faviconBase64 := `AAABAAEAICAAAAEAIACoEAAAFgAAACgAAAAgAAAAQAAAAAEAIAAAAAAAABAAACIuAAAiLgAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAG4zSAAvFh0Dj0Jg
D5NAZBuYQWccnlBmEYBMSwaSUVsAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA////
//////////////////AAAH/gAAAH4AAAA4AAQAGAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAQA
AgAAAIAAAACAAAABwAAAAfAAAAfwAAAP/gAAf/8AAH//AAB//4AB//+AA///wAP///AH///4H///
//////////////8=`
faviconBuffer, _ := base64.StdEncoding.DecodeString(faviconBase64)
r1.GET("/favicon.ico", func(c *gin.Context) {
_, _ = c.Writer.WriteString(string(faviconBuffer))
})
go func() {
fmt.Printf("listening on http://127.0.0.1:%d using gin router\n", webPort)
r1.Run(fmt.Sprintf(":%d", webPort))
wg.Done()
}()
wg.Wait()
}