-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (66 loc) · 1.56 KB
/
main.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
package main
import (
"context"
"net"
"strconv"
echo "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/webview/webview"
)
var (
port int
portstr string
startup_succ bool = false
syncch chan int
e *echo.Echo
)
func init() {
var err error
port, err = GetFreePort()
if err == nil {
startup_succ = true
}
if startup_succ {
go run_server()
}
}
func run_server() {
e = echo.New()
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Root: "static",
HTML5: true,
Index: "CyberChef_v9.39.1.html",
Browse: true,
}))
portstr = strconv.FormatInt(int64(port), 10)
e.Start("127.0.0.1:" + portstr)
}
func main() {
w := webview.New(false)
defer w.Destroy()
w.SetTitle("CyberChef")
w.SetSize(1080, 768, webview.HintNone)
if startup_succ {
w.SetHtml(`<meta http-equiv="Refresh" content="0; url='http://127.0.0.1:` + portstr + `'" />`)
} else {
w.SetHtml(`<h1>Oh No!</h1>
Could not connect to Localhost on port <b>` + portstr + `</b><br>
Try relaunching the app.`)
}
w.Run()
e.Shutdown(context.Background())
}
// src: https://github.com/phayes/freeport/blob/master/freeport.go
// GetFreePort asks the kernel for a free open port that is ready to use.
func GetFreePort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0, err
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}